📄 didlparser.java
字号:
} if (container.getClazz() == null) { throw new RuntimeException("Missing 'upnp:class' element for container: " + container.getId()); } Element containerElement = appendNewElement(descriptor, parent, "container"); if (container.getId() == null) throw new NullPointerException("Missing id on container: " + container); containerElement.setAttribute("id", container.getId()); if (container.getParentID() == null) throw new NullPointerException("Missing parent id on container: " + container); containerElement.setAttribute("parentID", container.getParentID()); if (container.getChildCount() != null) { containerElement.setAttribute("childCount", Integer.toString(container.getChildCount())); } containerElement.setAttribute("restricted", Boolean.toString(container.isRestricted())); containerElement.setAttribute("searchable", Boolean.toString(container.isSearchable())); appendNewElementIfNotNull( descriptor, containerElement, "dc:title", container.getTitle(), DIDLObject.Property.DC.NAMESPACE.URI ); appendNewElementIfNotNull( descriptor, containerElement, "dc:creator", container.getCreator(), DIDLObject.Property.DC.NAMESPACE.URI ); appendNewElementIfNotNull( descriptor, containerElement, "upnp:writeStatus", container.getWriteStatus(), DIDLObject.Property.UPNP.NAMESPACE.URI ); appendClass(descriptor, containerElement, container.getClazz(), "upnp:class", false); for (DIDLObject.Class searchClass : container.getSearchClasses()) { appendClass(descriptor, containerElement, searchClass, "upnp:searchClass", true); } for (DIDLObject.Class createClass : container.getCreateClasses()) { appendClass(descriptor, containerElement, createClass, "upnp:createClass", true); } appendProperties(descriptor, containerElement, container, "upnp", DIDLObject.Property.UPNP.NAMESPACE.class, DIDLObject.Property.UPNP.NAMESPACE.URI); appendProperties(descriptor, containerElement, container, "dc", DIDLObject.Property.DC.NAMESPACE.class, DIDLObject.Property.DC.NAMESPACE.URI); if (nestedItems) { for (Item item : container.getItems()) { if (item == null) continue; generateItem(item, descriptor, containerElement); } } for (Res resource : container.getResources()) { if (resource == null) continue; generateResource(resource, descriptor, containerElement); } for (DescMeta descMeta : container.getDescMetadata()) { if (descMeta == null) continue; generateDescMetadata(descMeta, descriptor, containerElement); } } protected void generateItem(Item item, Document descriptor, Element parent) { if (item.getTitle() == null) { throw new RuntimeException("Missing 'dc:title' element for item: " + item.getId()); } if (item.getClazz() == null) { throw new RuntimeException("Missing 'upnp:class' element for item: " + item.getId()); } Element itemElement = appendNewElement(descriptor, parent, "item"); if (item.getId() == null) throw new NullPointerException("Missing id on item: " + item); itemElement.setAttribute("id", item.getId()); if (item.getParentID() == null) throw new NullPointerException("Missing parent id on item: " + item); itemElement.setAttribute("parentID", item.getParentID()); if (item.getRefID() != null) itemElement.setAttribute("refID", item.getRefID()); itemElement.setAttribute("restricted", Boolean.toString(item.isRestricted())); appendNewElementIfNotNull( descriptor, itemElement, "dc:title", item.getTitle(), DIDLObject.Property.DC.NAMESPACE.URI ); appendNewElementIfNotNull( descriptor, itemElement, "dc:creator", item.getCreator(), DIDLObject.Property.DC.NAMESPACE.URI ); appendNewElementIfNotNull( descriptor, itemElement, "upnp:writeStatus", item.getWriteStatus(), DIDLObject.Property.UPNP.NAMESPACE.URI ); appendClass(descriptor, itemElement, item.getClazz(), "upnp:class", false); appendProperties(descriptor, itemElement, item, "upnp", DIDLObject.Property.UPNP.NAMESPACE.class, DIDLObject.Property.UPNP.NAMESPACE.URI); appendProperties(descriptor, itemElement, item, "dc", DIDLObject.Property.DC.NAMESPACE.class, DIDLObject.Property.DC.NAMESPACE.URI); for (Res resource : item.getResources()) { if (resource == null) continue; generateResource(resource, descriptor, itemElement); } for (DescMeta descMeta : item.getDescMetadata()) { if (descMeta == null) continue; generateDescMetadata(descMeta, descriptor, itemElement); } } protected void generateResource(Res resource, Document descriptor, Element parent) { if (resource.getValue() == null) { throw new RuntimeException("Missing resource URI value" + resource); } if (resource.getProtocolInfo() == null) { throw new RuntimeException("Missing resource protocol info: " + resource); } Element resourceElement = appendNewElement(descriptor, parent, "res", resource.getValue()); resourceElement.setAttribute("protocolInfo", resource.getProtocolInfo().toString()); if (resource.getImportUri() != null) resourceElement.setAttribute("importUri", resource.getImportUri().toString()); if (resource.getSize() != null) resourceElement.setAttribute("size", resource.getSize().toString()); if (resource.getDuration() != null) resourceElement.setAttribute("duration", resource.getDuration()); if (resource.getBitrate() != null) resourceElement.setAttribute("bitrate", resource.getBitrate().toString()); if (resource.getSampleFrequency() != null) resourceElement.setAttribute("sampleFrequency", resource.getSampleFrequency().toString()); if (resource.getBitsPerSample() != null) resourceElement.setAttribute("bitsPerSample", resource.getBitsPerSample().toString()); if (resource.getNrAudioChannels() != null) resourceElement.setAttribute("nrAudioChannels", resource.getNrAudioChannels().toString()); if (resource.getColorDepth() != null) resourceElement.setAttribute("colorDepth", resource.getColorDepth().toString()); if (resource.getProtection() != null) resourceElement.setAttribute("protection", resource.getProtection()); if (resource.getResolution() != null) resourceElement.setAttribute("resolution", resource.getResolution()); } protected void generateDescMetadata(DescMeta descMeta, Document descriptor, Element parent) { if (descMeta.getId() == null) { throw new RuntimeException("Missing id of description metadata: " + descMeta); } if (descMeta.getNameSpace() == null) { throw new RuntimeException("Missing namespace of description metadata: " + descMeta); } Element descElement = appendNewElement(descriptor, parent, "desc"); descElement.setAttribute("id", descMeta.getId()); descElement.setAttribute("nameSpace", descMeta.getNameSpace().toString()); if (descMeta.getType() != null) descElement.setAttribute("type", descMeta.getType()); populateDescMetadata(descElement, descMeta); } /** * Expects an <code>org.w3c.Document</code> as metadata, copies nodes of the document into the DIDL content. * <p> * This method will ignore the content and log a warning if it's of the wrong type. If you override * {@link #createDescMetaHandler(org.teleal.cling.support.model.DescMeta, org.teleal.common.xml.SAXParser.Handler)}, * you most likely also want to override this method. * </p> * * @param descElement The DIDL content {@code <desc>} element wrapping the final metadata. * @param descMeta The metadata with a <code>org.w3c.Document</code> payload. */ protected void populateDescMetadata(Element descElement, DescMeta descMeta) { if (descMeta.getMetadata() instanceof Document) { Document doc = (Document) descMeta.getMetadata(); NodeList nl = doc.getDocumentElement().getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { Node n = nl.item(i); if (n.getNodeType() != Node.ELEMENT_NODE) continue; Node clone = descElement.getOwnerDocument().importNode(n, true); descElement.appendChild(clone); } } else { log.warning("Unknown desc metadata content, please override populateDescMetadata(): " + descMeta.getMetadata()); } } protected void appendProperties(Document descriptor, Element parent, DIDLObject object, String prefix, Class<? extends DIDLObject.Property.NAMESPACE> namespace, String namespaceURI) { for (DIDLObject.Property<Object> property : object.getPropertiesByNamespace(namespace)) { Element el = descriptor.createElementNS(namespaceURI, prefix + ":" + property.getDescriptorName()); parent.appendChild(el); property.setOnElement(el); } } protected void appendClass(Document descriptor, Element parent, DIDLObject.Class clazz, String element, boolean appendDerivation) { Element classElement = appendNewElementIfNotNull( descriptor, parent, element, clazz.getValue(), DIDLObject.Property.UPNP.NAMESPACE.URI ); if (clazz.getFriendlyName() != null && clazz.getFriendlyName().length() > 0) classElement.setAttribute("name", clazz.getFriendlyName()); if (appendDerivation) classElement.setAttribute("includeDerived", Boolean.toString(clazz.isIncludeDerived())); } /** * Sends the given string to the log with <code>Level.FINE</code>, if that log level is enabled. * * @param s The string to send to the log. */ public void debugXML(String s) { if (log.isLoggable(Level.FINE)) { log.fine("-------------------------------------------------------------------------------------"); log.fine("\n" + s); log.fine("-------------------------------------------------------------------------------------"); } } /* ############################################################################################# */ public abstract class DIDLObjectHandler<I extends DIDLObject> extends Handler<I> { protected DIDLObjectHandler(I instance, Handler parent) { super(instance, parent); } @Override public void endElement(String uri, String localName, String qName) throws SAXException { super.endElement(uri, localName, qName); if (DIDLObject.Property.DC.NAMESPACE.URI.equals(uri)) { if ("title".equals(localName)) { getInstance().setTitle(getCharacters()); } else if ("creator".equals(localName)) { getInstance().setCreator(getCharacters()); } else if ("description".equals(localName)) { getInstance().addProperty(new DIDLObject.Property.DC.DESCRIPTION(getCharacters())); } else if ("publisher".equals(localName)) { getInstance().addProperty(new DIDLObject.Property.DC.PUBLISHER(new Person(getCharacters()))); } else if ("contributor".equals(localName)) { getInstance().addProperty(new DIDLObject.Property.DC.CONTRIBUTOR(new Person(getCharacters()))); } else if ("date".equals(localName)) { getInstance().addProperty(new DIDLObject.Property.DC.DATE(getCharacters())); } else if ("language".equals(localName)) { getInstance().addProperty(new DIDLObject.Property.DC.LANGUAGE(getCharacters())); } else if ("rights".equals(localName)) { getInstance().addProperty(new DIDLObject.Property.DC.RIGHTS(getCharacters())); } else if ("relation".equals(localName)) { getInstance().addProperty(new DIDLObject.Property.DC.RELATION(URI.create(getCharacters()))); } } else if (DIDLObject.Property.UPNP.NAMESPACE.URI.equals(uri)) { if ("writeStatus".equals(localName)) { try { getInstance().setWriteStatus( WriteStatus.valueOf(getCharacters()) ); } catch (Exception ex) { log.info("Ignoring invalid writeStatus value: " + getCharacters()); } } else if ("class".equals(localName)) { getInstance().setClazz( new DIDLObject.Class( getCharacters(), getAttributes().getValue("name") ) ); } else if ("artist".equals(localName)) { getInstance().addProperty( new DIDLObject.Property.UPNP.ARTIST( new PersonWithRole(getCharacters(), getAttributes().getValue("role")) ) ); } else if ("actor".equals(localName)) { getInstance().addProperty( new DIDLObject.Property.UPNP.ACTOR( new PersonWithRole(getCharacters(), getAttributes().getValue("role")) ) ); } else if ("author".equals(localName)) { getInstance().addProperty( new DIDLObject.Property.UPNP.AUTHOR( new PersonWithRole(getCharacters(), getAttributes().getValue("role")) ) ); } else if ("producer".equals(localName)) { getInstance().addProperty( new DIDLObject.Property.UPNP.PRODUCER(new Person(getCharacters())) ); } else if ("director".equals(localName)) { getInstance().addProperty( new DIDLObject.Property.UPNP.DIRECTOR(new Person(getCharacters())) ); } else if ("longDescription".equals(localName)) { getInstance().addProperty( new DIDLObject.Property.UPNP.LONG_DESCRIPTION(getCharacters()) ); } else if ("storageUsed".equals(localName)) { getInstance().addProperty( new DIDLObject.Property.UPNP.STORAGE_USED(Long.valueOf(getCharacters())) ); } else if ("storageTotal".equals(localName)) { getInstance().addProperty( new DIDLObject.Property.UPNP.STORAGE_TOTAL(Long.valueOf(getCharacters())) ); } else if ("storageFree".equals(localName)) { getInstance().addProperty( new DIDLObject.Property.UPNP.STORAGE_FREE(Long.valueOf(getCharacters())) ); } else if ("storageMaxPartition".equals(localName)) { getInstance().addProperty( new DIDLObject.Property.UPNP.STORAGE_MAX_PARTITION(Long.valueOf(getCharacters()))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -