Android Open Source - DroidUPnP Track Metadata






From Project

Back to project page DroidUPnP.

License

The source code is released under:

GNU General Public License

If you think the Android project DroidUPnP listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.droidupnp.model.cling;
//from w  ww .  j a va 2  s. c om
import java.io.StringReader;
import java.io.StringWriter;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import org.xmlpull.v1.XmlSerializer;

import android.util.Log;
import android.util.Xml;

public class TrackMetadata {

  protected static final String TAG = "TrackMetadata";

  @Override
  public String toString()
  {
    return "TrackMetadata [id=" + id + ", title=" + title + ", artist=" + artist + ", genre=" + genre + ", artURI="
        + artURI + "res=" + res + ", itemClass=" + itemClass + "]";
  }

  public TrackMetadata(String xml)
  {
    parseTrackMetadata(xml);
  }

  public TrackMetadata()
  {
  }

  public TrackMetadata(String id, String title, String artist, String genre, String artURI, String res,
      String itemClass)
  {
    super();
    this.id = id;
    this.title = title;
    this.artist = artist;
    this.genre = genre;
    this.artURI = artURI;
    this.res = res;
    this.itemClass = itemClass;
  }

  public String id;
  public String title;
  public String artist;
  public String genre;
  public String artURI;
  public String res;
  public String itemClass;

  private XMLReader initializeReader() throws ParserConfigurationException, SAXException
  {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    SAXParser parser = factory.newSAXParser();
    XMLReader xmlreader = parser.getXMLReader();
    return xmlreader;
  }

  public void parseTrackMetadata(String xml)
  {
    if (xml == null)
      return;

    Log.d(TAG, "XML : " + xml);

    try
    {
      XMLReader xmlreader = initializeReader();
      UpnpItemHandler upnpItemHandler = new UpnpItemHandler();

      xmlreader.setContentHandler(upnpItemHandler);
      xmlreader.parse(new InputSource(new StringReader(xml)));
    }
    catch (Exception e)
    {
      // e.printStackTrace();
      Log.w(TAG, "Error while parsing metadata !");
      Log.w(TAG, "XML : " + xml);
    }
  }

  public String getXML()
  {
    XmlSerializer s = Xml.newSerializer();
    StringWriter sw = new StringWriter();

    try {
      s.setOutput(sw);

      s.startDocument(null,null);
      s.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);

      //start a tag called "root"
      s.startTag(null, "DIDL-Lite");
      s.attribute(null, "xmlns", "urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/");
      s.attribute(null, "xmlns:dc", "http://purl.org/dc/elements/1.1/");
      s.attribute(null, "xmlns:upnp", "urn:schemas-upnp-org:metadata-1-0/upnp/");
      s.attribute(null, "xmlns:dlna", "urn:schemas-dlna-org:metadata-1-0/");

      s.startTag(null, "item");
      s.attribute(null, "id", ""+id);
      s.attribute(null, "parentID", "");
      s.attribute(null, "restricted", "1");

      if(title!=null)
      {
        s.startTag(null, "dc:title");
        s.text(title);
        s.endTag(null, "dc:title");
      }

      if(artist!=null)
      {
        s.startTag(null, "dc:creator");
        s.text(artist);
        s.endTag(null, "dc:creator");
      }

      if(genre!=null)
      {
        s.startTag(null, "upnp:genre");
        s.text(genre);
        s.endTag(null, "upnp:genre");
      }

      if(artURI!=null)
      {
        s.startTag(null, "upnp:albumArtURI");
        s.attribute(null, "dlna:profileID", "JPEG_TN");
        s.text(artURI);
        s.endTag(null, "upnp:albumArtURI");
      }

      if(res!=null)
      {
        s.startTag(null, "res");
        s.text(res);
        s.endTag(null, "res");
      }

      if(itemClass!=null)
      {
        s.startTag(null, "upnp:class");
        s.text(itemClass);
        s.endTag(null, "upnp:class");
      }

      s.endTag(null, "item");

      s.endTag(null, "DIDL-Lite");

      s.endDocument();
      s.flush();

    } catch (Exception e) {
      Log.e(TAG, "error occurred while creating xml file : " + e.toString() );
      e.printStackTrace();
    }

    String xml = sw.toString();
    Log.d(TAG, "TrackMetadata : " + xml);

    return xml;
  }

  public class UpnpItemHandler extends DefaultHandler {

    private final StringBuffer buffer = new StringBuffer();

    @Override
    public void startElement(String namespaceURI, String localName, String qName, Attributes atts)
        throws SAXException
    {
      buffer.setLength(0);
      // Log.v(TAG, "startElement, localName="+ localName + ", qName=" + qName);

      if (localName.equals("item"))
      {
        id = atts.getValue("id");
      }
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException
    {
      // Log.v(TAG, "endElement, localName="+ localName + ", qName=" + qName + ", buffer=" +
      // buffer.toString());

      if (localName.equals("title"))
      {
        title = buffer.toString();
      }
      else if (localName.equals("creator"))
      {
        artist = buffer.toString();
      }
      else if (localName.equals("genre"))
      {
        genre = buffer.toString();
      }
      else if (localName.equals("albumArtURI"))
      {
        artURI = buffer.toString();
      }
      else if (localName.equals("class"))
      {
        itemClass = buffer.toString();
      }
      else if (localName.equals("res"))
      {
        res = buffer.toString();
      }
    }

    @Override
    public void characters(char[] ch, int start, int length)
    {
      buffer.append(ch, start, length);
    }
  }
}




Java Source Code List

fi.iki.elonen.NanoHTTPD.java
fi.iki.elonen.ServerRunner.java
fi.iki.elonen.SimpleWebServer.java
org.droidupnp.DrawerFragment.java
org.droidupnp.Main.java
org.droidupnp.controller.cling.ContentDirectoryCommand.java
org.droidupnp.controller.cling.Factory.java
org.droidupnp.controller.cling.RendererCommand.java
org.droidupnp.controller.cling.ServiceController.java
org.droidupnp.controller.cling.ServiceListener.java
org.droidupnp.controller.upnp.IUpnpServiceController.java
org.droidupnp.controller.upnp.UpnpDebugListener.java
org.droidupnp.model.CObservable.java
org.droidupnp.model.cling.CDevice.java
org.droidupnp.model.cling.CRegistryListener.java
org.droidupnp.model.cling.RendererState.java
org.droidupnp.model.cling.TrackMetadata.java
org.droidupnp.model.cling.UpnpRegistry.java
org.droidupnp.model.cling.UpnpServiceController.java
org.droidupnp.model.cling.UpnpService.java
org.droidupnp.model.cling.didl.ClingAudioItem.java
org.droidupnp.model.cling.didl.ClingDIDLContainer.java
org.droidupnp.model.cling.didl.ClingDIDLItem.java
org.droidupnp.model.cling.didl.ClingDIDLObject.java
org.droidupnp.model.cling.didl.ClingDIDLParentContainer.java
org.droidupnp.model.cling.didl.ClingImageItem.java
org.droidupnp.model.cling.didl.ClingVideoItem.java
org.droidupnp.model.cling.localContent.AlbumContainer.java
org.droidupnp.model.cling.localContent.ArtistContainer.java
org.droidupnp.model.cling.localContent.AudioContainer.java
org.droidupnp.model.cling.localContent.CustomContainer.java
org.droidupnp.model.cling.localContent.DynamicContainer.java
org.droidupnp.model.cling.localContent.ImageContainer.java
org.droidupnp.model.cling.localContent.VideoContainer.java
org.droidupnp.model.mediaserver.ContentDirectoryService.java
org.droidupnp.model.mediaserver.MediaServer.java
org.droidupnp.model.upnp.ARendererState.java
org.droidupnp.model.upnp.CallableContentDirectoryFilter.java
org.droidupnp.model.upnp.CallableRendererFilter.java
org.droidupnp.model.upnp.ContentDirectoryDiscovery.java
org.droidupnp.model.upnp.DeviceDiscovery.java
org.droidupnp.model.upnp.DeviceListener.java
org.droidupnp.model.upnp.ICallableFilter.java
org.droidupnp.model.upnp.IContentDirectoryCommand.java
org.droidupnp.model.upnp.IDeviceDiscoveryObserver.java
org.droidupnp.model.upnp.IFactory.java
org.droidupnp.model.upnp.IRegistryListener.java
org.droidupnp.model.upnp.IRendererCommand.java
org.droidupnp.model.upnp.IRendererState.java
org.droidupnp.model.upnp.IServiceListener.java
org.droidupnp.model.upnp.IUpnpDevice.java
org.droidupnp.model.upnp.IUpnpRegistry.java
org.droidupnp.model.upnp.PeeringConnectionManager.java
org.droidupnp.model.upnp.RendererDiscovery.java
org.droidupnp.model.upnp.didl.DIDLDevice.java
org.droidupnp.model.upnp.didl.IDIDLContainer.java
org.droidupnp.model.upnp.didl.IDIDLItem.java
org.droidupnp.model.upnp.didl.IDIDLObject.java
org.droidupnp.model.upnp.didl.IDIDLParentContainer.java
org.droidupnp.view.ContentDirectoryDeviceFragment.java
org.droidupnp.view.ContentDirectoryDialog.java
org.droidupnp.view.ContentDirectoryEnabler.java
org.droidupnp.view.ContentDirectoryFragment.java
org.droidupnp.view.Content.java
org.droidupnp.view.DIDLObjectDisplay.java
org.droidupnp.view.DeviceDisplay.java
org.droidupnp.view.DeviceFragment.java
org.droidupnp.view.DeviceInfoDialog.java
org.droidupnp.view.PlaylistFragment.java
org.droidupnp.view.RendererDeviceFragment.java
org.droidupnp.view.RendererDialog.java
org.droidupnp.view.RendererFragment.java
org.droidupnp.view.ServiceDiscoveryFragment.java
org.droidupnp.view.SettingsActivity.java
org.droidupnp.view.UpnpDeviceListFragment.java