/** * Reads and unmarshalls an XML representation into a DIDL content model. * * @param xml The XML representation. * @return A DIDL content model. * @throws Exception */ public DIDLContent parse(String xml) throws Exception { if (xml == null || xml.length() == 0) { throw new RuntimeException("Null or empty XML"); } DIDLContent content = new DIDLContent(); createRootHandler(content, this); log.fine("Parsing DIDL XML content"); parse(new InputSource(new StringReader(xml))); return content; }
@Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { super.startElement(uri, localName, qName, attributes); if (!DIDLContent.NAMESPACE_URI.equals(uri)) return; if (localName.equals("container")) { Container container = createContainer(attributes); getInstance().addContainer(container); createContainerHandler(container, this); } else if (localName.equals("item")) { Item item = createItem(attributes); getInstance().addItem(item); createItemHandler(item, this); } else if (localName.equals("desc")) { DescMeta desc = createDescMeta(attributes); getInstance().addDescMetadata(desc); createDescMetaHandler(desc, this); } }
public void replaceGenericContainerAndItems() { setItems(replaceGenericItems(getItems())); setContainers(replaceGenericContainers(getContainers())); }
/** * Adds {@link Item} or {@link Container} typed instances, ignores everything else. */ public DIDLContent addObject(Object object) { if(object instanceof Item) { addItem((Item)object); } else if(object instanceof Container) { addContainer((Container)object); } return this; }
protected void generateRoot(DIDLContent content, Document descriptor, boolean nestedItems) { Element rootElement = descriptor.createElementNS(DIDLContent.NAMESPACE_URI, "DIDL-Lite"); descriptor.appendChild(rootElement); // rootElement.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:didl", DIDLContent.NAMESPACE_URI); rootElement.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:upnp", DIDLObject.Property.UPNP.NAMESPACE.URI); rootElement.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:dc", DIDLObject.Property.DC.NAMESPACE.URI); rootElement.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:sec", DIDLObject.Property.SEC.NAMESPACE.URI); for (Container container : content.getContainers()) { if (container == null) continue; generateContainer(container, descriptor, rootElement, nestedItems); } for (Item item : content.getItems()) { if (item == null) continue; generateItem(item, descriptor, rootElement); } for (DescMeta descMeta : content.getDescMetadata()) { if (descMeta == null) continue; generateDescMetadata(descMeta, descriptor, rootElement); } }
private DIDLContent createContentDidl(Container cont, long firstResult, long maxResults) { DIDLContent didl = new DIDLContent(); int cnt = 0; for (Container c : cont.getContainers()) { if (firstResult > 0) { firstResult--; continue; } if (maxResults == 0 || cnt < maxResults) { didl.addContainer(c); cnt++; } } for (Item i : cont.getItems()) { if (firstResult > 0) { firstResult--; continue; } if (maxResults == 0 || cnt < maxResults) { didl.addItem(i); cnt++; } } return didl; }
DIDLContent didl = new DIDLContent(); DIDLParser dip = new DIDLParser(); if (me instanceof Movie) { didl.addItem(Metadata.getUpnpMovie((Movie) me, true)); didl.addItem(Metadata.getUpnpTvShowEpisode(((TvShowEpisode) me).getTvShow(), (TvShowEpisode) me, true)); url = didl.getItems().get(0).getResources().get(0).getValue();
for (Container childContainer : didl.getContainers()) { DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(childContainer) { @Override for (Item childItem : didl.getItems()) { DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(childItem) { @Override
DIDLContent didlContent = new DIDLContent(); didlContent.addItem(item); DIDLParser parser = new DIDLParser(); String metadata = "";
throws Exception final DIDLContent content = new DIDLContent(); int numberReturned = 0; int totalMatches = 0; content.addObject(toObject()); break; .results() .stream() .forEach(_c(child -> content.addObject(child.as(DIDLAdapter).toObject()))); numberReturned = (int)content.getCount(); break;
/******************************************************************************************************************* * * Converts the owner object to a {@link DIDLContent}. * * @param browseFlag whether metadata for a single object or enumeration of children should be returned * @param from in case of multiple results, the first item to return * @param maxResults in case of multiple results, how many items to return * @return the holder of {@code DIDLContent} * ******************************************************************************************************************/ @Nonnull public default ContentHolder toContent (final @Nonnull BrowseFlag browseFlag, final @Nonnegative int from, final @Nonnegative int maxResults) throws Exception { final DIDLContent content = new DIDLContent(); content.addObject(toObject()); return new ContentHolder(content, 1, 1); }
public DIDLContent addContainer(Container container) { getContainers().add(container); return this; }
public DIDLContent addItem(Item item) { getItems().add(item); return this; }
DIDLContent didlContent = new DIDLContent(); didlContent.addContainer( (Container) didlObject ); didlContent.addItem( (Item) didlObject );
for (Container childContainer : didl.getContainers()) { DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(childContainer) { @Override for (Item childItem : didl.getItems()) { DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(childItem) { @Override
/** * Adds {@link Item} or {@link Container} typed instances, ignores everything else. */ public DIDLContent addObject(Object object) { if(object instanceof Item) { addItem((Item)object); } else if(object instanceof Container) { addContainer((Container)object); } return this; }
DIDLContent content = new DIDLContent(); Res res = new Res(); Movie movie = new Movie(id, parentID, title, creator, res); content.addItem(movie); String didlString = ""; try {
protected void generateRoot(DIDLContent content, Document descriptor, boolean nestedItems) { Element rootElement = descriptor.createElementNS(DIDLContent.NAMESPACE_URI, "DIDL-Lite"); descriptor.appendChild(rootElement); // rootElement.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:didl", DIDLContent.NAMESPACE_URI); rootElement.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:upnp", DIDLObject.Property.UPNP.NAMESPACE.URI); rootElement.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:dc", DIDLObject.Property.DC.NAMESPACE.URI); rootElement.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:sec", DIDLObject.Property.SEC.NAMESPACE.URI); for (Container container : content.getContainers()) { if (container == null) continue; generateContainer(container, descriptor, rootElement, nestedItems); } for (Item item : content.getItems()) { if (item == null) continue; generateItem(item, descriptor, rootElement); } for (DescMeta descMeta : content.getDescMetadata()) { if (descMeta == null) continue; generateDescMetadata(descMeta, descriptor, rootElement); } }
@Test public void must_properly_generate_DIDL_content() throws Exception { // given final MediaFolder mediaFolder = mock(MediaFolder.class); when(mediaFolder.getPath()).thenReturn(Paths.get("/folder")); final PhotoItem photoItem = new PhotoItem(mediaFolder, "20150524-0034", "description"); final PhotoItemDIDLAdapter underTest = new PhotoItemDIDLAdapter(photoItem); // when final DIDLContent content = new DIDLContent(); content.addObject(underTest.toObject()); // then final DIDLParser parser = new DIDLParser(); final String xml = xmlPrettyPrinted(parser.generate(content)); final Path actualResult = Paths.get("target", "test-results", "didl.xml"); final Path expectedResult = Paths.get("target", "test-classes", "expected-results", "didl.xml"); Files.createDirectories(actualResult.getParent()); Files.write(actualResult, xml.getBytes(StandardCharsets.UTF_8)); assertSameContents(expectedResult.toFile(), actualResult.toFile()); } }
public Container getFirstContainer() { return getContainers().get(0); }