📄 didlparser.java
字号:
); } else if ("storageMedium".equals(localName)) { getInstance().addProperty( new DIDLObject.Property.UPNP.STORAGE_MEDIUM(StorageMedium.valueOrVendorSpecificOf(getCharacters())) ); } else if ("genre".equals(localName)) { getInstance().addProperty( new DIDLObject.Property.UPNP.GENRE(getCharacters()) ); } else if ("album".equals(localName)) { getInstance().addProperty( new DIDLObject.Property.UPNP.ALBUM(getCharacters()) ); } else if ("playlist".equals(localName)) { getInstance().addProperty( new DIDLObject.Property.UPNP.PLAYLIST(getCharacters()) ); } else if ("region".equals(localName)) { getInstance().addProperty( new DIDLObject.Property.UPNP.REGION(getCharacters()) ); } else if ("rating".equals(localName)) { getInstance().addProperty( new DIDLObject.Property.UPNP.RATING(getCharacters()) ); } else if ("toc".equals(localName)) { getInstance().addProperty( new DIDLObject.Property.UPNP.TOC(getCharacters()) ); } else if ("albumArtURI".equals(localName)) { DIDLObject.Property albumArtURI = new DIDLObject.Property.UPNP.ALBUM_ART_URI(URI.create(getCharacters())); Attributes albumArtURIAttributes = getAttributes(); for (int i = 0; i < albumArtURIAttributes.getLength(); i++) { if ("profileID".equals(albumArtURIAttributes.getLocalName(i))) { albumArtURI.addAttribute( new DIDLObject.Property.DLNA.PROFILE_ID( new DIDLAttribute( DIDLObject.Property.DLNA.NAMESPACE.URI, "dlna", albumArtURIAttributes.getValue(i)) )); } } getInstance().addProperty(albumArtURI); } else if ("artistDiscographyURI".equals(localName)) { getInstance().addProperty( new DIDLObject.Property.UPNP.ARTIST_DISCO_URI(URI.create(getCharacters())) ); } else if ("lyricsURI".equals(localName)) { getInstance().addProperty( new DIDLObject.Property.UPNP.LYRICS_URI(URI.create(getCharacters())) ); } else if ("icon".equals(localName)) { getInstance().addProperty( new DIDLObject.Property.UPNP.ICON(URI.create(getCharacters())) ); } else if ("radioCallSign".equals(localName)) { getInstance().addProperty( new DIDLObject.Property.UPNP.RADIO_CALL_SIGN(getCharacters()) ); } else if ("radioStationID".equals(localName)) { getInstance().addProperty( new DIDLObject.Property.UPNP.RADIO_STATION_ID(getCharacters()) ); } else if ("radioBand".equals(localName)) { getInstance().addProperty( new DIDLObject.Property.UPNP.RADIO_BAND(getCharacters()) ); } else if ("channelNr".equals(localName)) { getInstance().addProperty( new DIDLObject.Property.UPNP.CHANNEL_NR(Integer.valueOf(getCharacters())) ); } else if ("channelName".equals(localName)) { getInstance().addProperty( new DIDLObject.Property.UPNP.CHANNEL_NAME(getCharacters()) ); } else if ("scheduledStartTime".equals(localName)) { getInstance().addProperty( new DIDLObject.Property.UPNP.SCHEDULED_START_TIME(getCharacters()) ); } else if ("scheduledEndTime".equals(localName)) { getInstance().addProperty( new DIDLObject.Property.UPNP.SCHEDULED_END_TIME(getCharacters()) ); } else if ("DVDRegionCode".equals(localName)) { getInstance().addProperty( new DIDLObject.Property.UPNP.DVD_REGION_CODE(Integer.valueOf(getCharacters())) ); } else if ("originalTrackNumber".equals(localName)) { getInstance().addProperty( new DIDLObject.Property.UPNP.ORIGINAL_TRACK_NUMBER(Integer.valueOf(getCharacters())) ); } else if ("userAnnotation".equals(localName)) { getInstance().addProperty( new DIDLObject.Property.UPNP.USER_ANNOTATION(getCharacters()) ); } } } } public class RootHandler extends Handler<DIDLContent> { RootHandler(DIDLContent instance, SAXParser parser) { super(instance, parser); } @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); } } @Override protected boolean isLastElement(String uri, String localName, String qName) { if (DIDLContent.NAMESPACE_URI.equals(uri) && "DIDL-Lite".equals(localName)) { // Now transform all the generically typed Container and Item instances into // more specific Album, MusicTrack, etc. instances getInstance().replaceGenericContainerAndItems(); return true; } return false; } } public class ContainerHandler extends DIDLObjectHandler<Container> { public ContainerHandler(Container instance, Handler parent) { super(instance, parent); } @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("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); } else if (localName.equals("res")) { Res res = createResource(attributes); if (res != null) { getInstance().addResource(res); createResHandler(res, this); } } // We do NOT support recursive container embedded in container! The schema allows it // but the spec doesn't: // // Section 2.8.3: Incremental navigation i.e. the full hierarchy is never returned // in one call since this is likely to flood the resources available to the control // point (memory, network bandwidth, etc.). } @Override public void endElement(String uri, String localName, String qName) throws SAXException { super.endElement(uri, localName, qName); if (DIDLObject.Property.UPNP.NAMESPACE.URI.equals(uri)) { if ("searchClass".equals(localName)) { getInstance().getSearchClasses().add( new DIDLObject.Class( getCharacters(), getAttributes().getValue("name"), "true".equals(getAttributes().getValue("includeDerived")) ) ); } else if ("createClass".equals(localName)) { getInstance().getCreateClasses().add( new DIDLObject.Class( getCharacters(), getAttributes().getValue("name"), "true".equals(getAttributes().getValue("includeDerived")) ) ); } } } @Override protected boolean isLastElement(String uri, String localName, String qName) { if (DIDLContent.NAMESPACE_URI.equals(uri) && "container".equals(localName)) { if (getInstance().getTitle() == null) { log.warning("In DIDL content, missing 'dc:title' element for container: " + getInstance().getId()); } if (getInstance().getClazz() == null) { log.warning("In DIDL content, missing 'upnp:class' element for container: " + getInstance().getId()); } return true; } return false; } } public class ItemHandler extends DIDLObjectHandler<Item> { public ItemHandler(Item instance, Handler parent) { super(instance, parent); } @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("res")) { Res res = createResource(attributes); if (res != null) { getInstance().addResource(res); createResHandler(res, this); } } else if (localName.equals("desc")) { DescMeta desc = createDescMeta(attributes); getInstance().addDescMetadata(desc); createDescMetaHandler(desc, this); } } @Override protected boolean isLastElement(String uri, String localName, String qName) { if (DIDLContent.NAMESPACE_URI.equals(uri) && "item".equals(localName)) { if (getInstance().getTitle() == null) { log.warning("In DIDL content, missing 'dc:title' element for item: " + getInstance().getId()); } if (getInstance().getClazz() == null) { log.warning("In DIDL content, missing 'upnp:class' element for item: " + getInstance().getId()); } return true; } return false; } } protected class ResHandler extends Handler<Res> { public ResHandler(Res instance, Handler parent) { super(instance, parent); } @Override public void endElement(String uri, String localName, String qName) throws SAXException { super.endElement(uri, localName, qName); getInstance().setValue(getCharacters()); } @Override protected boolean isLastElement(String uri, String localName, String qName) { return DIDLContent.NAMESPACE_URI.equals(uri) && "res".equals(localName); } } /** * Extracts an <code>org.w3c.Document</code> from the nested elements in the {@code <desc>} element. * <p> * The root element of this document is a wrapper in the namespace * {@link org.teleal.cling.support.model.DIDLContent#DESC_WRAPPER_NAMESPACE_URI}. * </p> */ public class DescMetaHandler extends Handler<DescMeta> { protected Element current; public DescMetaHandler(DescMeta instance, Handler parent) { super(instance, parent); instance.setMetadata(instance.createMetadataDocument()); current = getInstance().getMetadata().getDocumentElement(); } @Override public DescMeta<Document> getInstance() { return super.getInstance(); } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { super.startElement(uri, localName, qName, attributes); Element newEl = getInstance().getMetadata().createElementNS(uri, qName); for (int i = 0; i < attributes.getLength(); i++) { newEl.setAttributeNS( attributes.getURI(i), attributes.getQName(i), attributes.getValue(i) ); } current.appendChild(newEl); current = newEl; } @Override public void endElement(String uri, String localName, String qName) throws SAXException { super.endElement(uri, localName, qName); if (isLastElement(uri, localName, qName)) return; // Ignore whitespace if (getCharacters().length() > 0 && !getCharacters().matches("[\\t\\n\\x0B\\f\\r\\s]+")) current.appendChild(getInstance().getMetadata().createTextNode(getCharacters())); current = (Element) current.getParentNode(); // Reset this so we can continue parsing child nodes with this handler characters = new StringBuilder(); attributes = null; } @Override protected boolean isLastElement(String uri, String localName, String qName) { return DIDLContent.NAMESPACE_URI.equals(uri) && "desc".equals(localName); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -