Java Code Examples for org.fourthline.cling.support.model.DIDLContent

The following examples show how to use org.fourthline.cling.support.model.DIDLContent. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source Project: airsonic-advanced   Author: airsonic-advanced   File: GenreUpnpProcessor.java    License: GNU General Public License v3.0 6 votes vote down vote up
/**
 * Browses the top-level content of a type.
 */
public BrowseResult browseRoot(String filter, long firstResult, long maxResults, SortCriterion[] orderBy) throws Exception {
    // we have to override this to do an index-based id.
    DIDLContent didl = new DIDLContent();
    List<Genre> allItems = getAllItems();
    if (filter != null) {
        // filter items
    }
    if (orderBy != null) {
        // sort items
    }
    List<Genre> selectedItems = Util.subList(allItems, firstResult, maxResults);
    for (int i = 0; i < selectedItems.size(); i++) {
        Genre item = selectedItems.get(i);
        didl.addContainer(createContainer(item, (int) (i + firstResult)));
    }
    return createBrowseResult(didl, (int) didl.getCount(), allItems.size());
}
 
Example #2
Source Project: airsonic-advanced   Author: airsonic-advanced   File: UpnpContentProcessor.java    License: GNU General Public License v3.0 6 votes vote down vote up
/**
 * Browses the top-level content of a type.
 */
public BrowseResult browseRoot(String filter, long firstResult, long maxResults, SortCriterion[] orderBy) throws Exception {
    DIDLContent didl = new DIDLContent();
    List<T> allItems = getAllItems();
    if (filter != null) {
        // filter items (not implemented yet)
    }
    if (orderBy != null) {
        // sort items (not implemented yet)
    }
    List<T> selectedItems = Util.subList(allItems, firstResult, maxResults);
    for (T item : selectedItems) {
        addItem(didl, item);
    }

    return createBrowseResult(didl, (int) didl.getCount(), allItems.size());
}
 
Example #3
Source Project: DroidDLNA   Author: offbye   File: DIDLParser.java    License: GNU General Public License v3.0 6 votes vote down vote up
@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);

    }
}
 
Example #4
Source Project: airsonic-advanced   Author: airsonic-advanced   File: UpnpContentProcessor.java    License: GNU General Public License v3.0 6 votes vote down vote up
/**
 * Browses a child of the container.
 */
public BrowseResult browseObject(String id, String filter, long firstResult, long maxResults, SortCriterion[] orderBy) throws Exception {
    T item = getItemById(id);
    List<U> allChildren = getChildren(item);
    if (filter != null) {
        // filter items (not implemented yet)
    }
    if (orderBy != null) {
        // sort items (not implemented yet)
    }
    List<U> selectedChildren = Util.subList(allChildren, firstResult, maxResults);

    DIDLContent didl = new DIDLContent();
    for (U child : selectedChildren) {
        addChild(didl, child);
    }
    return createBrowseResult(didl, selectedChildren.size(), allChildren.size());
}
 
Example #5
Source Project: airsonic-advanced   Author: airsonic-advanced   File: UpnpContentProcessor.java    License: GNU General Public License v3.0 6 votes vote down vote up
public BrowseResult searchByName(String name,
                                 long firstResult, long maxResults,
                                 SortCriterion[] orderBy) {
    DIDLContent didl = new DIDLContent();

    Class clazz = (Class) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];

    try {
        List<MusicFolder> allFolders = getDispatchingContentDirectory().getSettingsService().getAllMusicFolders();
        ParamSearchResult<T> result = getDispatcher().getSearchService().searchByName(name, (int) firstResult, (int) maxResults, allFolders, clazz);
        List<T> selectedItems = result.getItems();
        for (T item : selectedItems) {
            addItem(didl, item);
        }

        return createBrowseResult(didl, (int) didl.getCount(), result.getTotalHits());
    } catch (Exception e) {
        return null;
    }
}
 
Example #6
Source Project: DroidDLNA   Author: offbye   File: DIDLParser.java    License: GNU General Public License v3.0 6 votes vote down vote up
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);
    }
}
 
Example #7
Source Project: TVRemoteIME   Author: kingthy   File: DIDLParser.java    License: GNU General Public License v2.0 6 votes vote down vote up
@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);

    }
}
 
Example #8
Source Project: TVRemoteIME   Author: kingthy   File: DIDLParser.java    License: GNU General Public License v2.0 6 votes vote down vote up
@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);

    }
}
 
Example #9
Source Project: airsonic   Author: airsonic   File: GenreUpnpProcessor.java    License: GNU General Public License v3.0 6 votes vote down vote up
/**
 * Browses the top-level content of a type.
 */
public BrowseResult browseRoot(String filter, long firstResult, long maxResults, SortCriterion[] orderBy) throws Exception {
    // we have to override this to do an index-based id.
    DIDLContent didl = new DIDLContent();
    List<Genre> allItems = getAllItems();
    if (filter != null) {
        // filter items
    }
    if (orderBy != null) {
        // sort items
    }
    List<Genre> selectedItems = Util.subList(allItems, firstResult, maxResults);
    for (int i = 0; i < selectedItems.size(); i++) {
        Genre item = selectedItems.get(i);
        didl.addContainer(createContainer(item, (int) (i + firstResult)));
    }
    return createBrowseResult(didl, (int) didl.getCount(), allItems.size());
}
 
Example #10
Source Project: airsonic   Author: airsonic   File: UpnpContentProcessor.java    License: GNU General Public License v3.0 6 votes vote down vote up
/**
 * Browses the top-level content of a type.
 */
public BrowseResult browseRoot(String filter, long firstResult, long maxResults, SortCriterion[] orderBy) throws Exception {
    DIDLContent didl = new DIDLContent();
    List<T> allItems = getAllItems();
    if (filter != null) {
        // filter items (not implemented yet)
    }
    if (orderBy != null) {
        // sort items (not implemented yet)
    }
    List<T> selectedItems = Util.subList(allItems, firstResult, maxResults);
    for (T item : selectedItems) {
        addItem(didl, item);
    }

    return createBrowseResult(didl, (int) didl.getCount(), allItems.size());
}
 
Example #11
Source Project: airsonic   Author: airsonic   File: UpnpContentProcessor.java    License: GNU General Public License v3.0 6 votes vote down vote up
/**
 * Browses a child of the container.
 */
public BrowseResult browseObject(String id, String filter, long firstResult, long maxResults, SortCriterion[] orderBy) throws Exception {
    T item = getItemById(id);
    List<U> allChildren = getChildren(item);
    if (filter != null) {
        // filter items (not implemented yet)
    }
    if (orderBy != null) {
        // sort items (not implemented yet)
    }
    List<U> selectedChildren = Util.subList(allChildren, firstResult, maxResults);

    DIDLContent didl = new DIDLContent();
    for (U child : selectedChildren) {
        addChild(didl, child);
    }
    return createBrowseResult(didl, selectedChildren.size(), allChildren.size());
}
 
Example #12
Source Project: DroidDLNA   Author: offbye   File: DIDLParser.java    License: GNU General Public License v3.0 6 votes vote down vote up
@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);

    }
}
 
Example #13
Source Project: airsonic   Author: airsonic   File: RecentAlbumUpnpProcessor.java    License: GNU General Public License v3.0 6 votes vote down vote up
/**
 * Browses the top-level content.
 */
public BrowseResult browseRoot(String filter, long firstResult, long maxResults, SortCriterion[] orderBy) throws Exception {
    // AlbumUpnpProcessor overrides browseRoot() with an optimization;
    // this restores the default behavior for the subclass.
    DIDLContent didl = new DIDLContent();
    List<Album> allItems = getAllItems();
    if (filter != null) {
        // filter items (not implemented yet)
    }
    if (orderBy != null) {
        // sort items (not implemented yet)
    }
    List<Album> selectedItems = Util.subList(allItems, firstResult, maxResults);
    for (Album item : selectedItems) {
        addItem(didl, item);
    }

    return createBrowseResult(didl, (int) didl.getCount(), allItems.size());
}
 
Example #14
Source Project: BeyondUPnP   Author: kevinshine   File: ContentContainerActivity.java    License: Apache License 2.0 6 votes vote down vote up
private void playItem(Item item){
    if (item == null) return;

    Res res = item.getFirstResource();
    String uri = res.getValue();

    DIDLContent content = new DIDLContent();
    content.addItem(item);
    DIDLParser didlParser = new DIDLParser();
    String metadata = null;
    try {
        metadata = didlParser.generate(content);
    } catch (Exception e) {
        //ignore
    }
    //Log.d(TAG,"Item metadata:" + metadata);
    //Play on the selected device.
    PlaybackCommand.playNewItem(uri,metadata);
}
 
Example #15
Source Project: BeyondUPnP   Author: kevinshine   File: BeyondContentDirectoryService.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public BrowseResult browse(String objectID, BrowseFlag browseFlag, String filter, long firstResult, long maxResults, SortCriterion[] orderby) throws ContentDirectoryException {
    String address = Utils.getIPAddress(true);
    String serverUrl = "http://" + address + ":" + JettyResourceServer.JETTY_SERVER_PORT;

    //Create container by id
    Container resultBean = ContainerFactory.createContainer(objectID, serverUrl);
    DIDLContent content = new DIDLContent();

    for (Container c : resultBean.getContainers())
        content.addContainer(c);

    for (Item item : resultBean.getItems())
        content.addItem(item);

    int count = resultBean.getChildCount();
    String contentModel = "";
    try {
        contentModel = new DIDLParser().generate(content);
    } catch (Exception e) {
        throw new ContentDirectoryException(
                ContentDirectoryErrorCode.CANNOT_PROCESS, e.toString());
    }

    return new BrowseResult(contentModel, count, count);
}
 
Example #16
Source Project: airsonic-advanced   Author: airsonic-advanced   File: MediaFileUpnpProcessor.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
// overriding for the case of browsing a file
public BrowseResult browseObjectMetadata(String id) throws Exception {
    MediaFile item = getItemById(id);
    DIDLContent didl = new DIDLContent();
    addChild(didl, item);
    return createBrowseResult(didl, 1, 1);
}
 
Example #17
Source Project: airsonic-advanced   Author: airsonic-advanced   File: MediaFileUpnpProcessor.java    License: GNU General Public License v3.0 5 votes vote down vote up
public void addItem(DIDLContent didl, MediaFile item) {
    if (item.isFile()) {
        didl.addItem(createItem(item));
    } else {
        didl.addContainer(createContainer(item));
    }
}
 
Example #18
Source Project: airsonic-advanced   Author: airsonic-advanced   File: MediaFileUpnpProcessor.java    License: GNU General Public License v3.0 5 votes vote down vote up
public void addChild(DIDLContent didl, MediaFile child) {
    if (child.isFile()) {
        didl.addItem(createItem(child));
    } else {
        didl.addContainer(createContainer(child));
    }
}
 
Example #19
Source Project: airsonic-advanced   Author: airsonic-advanced   File: AlbumUpnpProcessor.java    License: GNU General Public License v3.0 5 votes vote down vote up
/**
 * Browses the top-level content of a type.
 */
public BrowseResult browseRoot(String filter, long firstResult, long maxResults, SortCriterion[] orderBy) throws Exception {
    DIDLContent didl = new DIDLContent();

    List<MusicFolder> allFolders = getDispatchingContentDirectory().getSettingsService().getAllMusicFolders();
    List<Album> selectedItems = getAlbumDao().getAlphabeticalAlbums((int) firstResult, (int) maxResults, false, true, allFolders);
    for (Album item : selectedItems) {
        addItem(didl, item);
    }

    return createBrowseResult(didl, (int) didl.getCount(), getAllItemsSize());
}
 
Example #20
Source Project: DroidDLNA   Author: offbye   File: AbstractContentDirectoryService.java    License: GNU General Public License v3.0 5 votes vote down vote up
/**
 * Override this method to implement searching of your content.
 * <p>
 * The default implementation returns an empty result.
 * </p>
 */
public BrowseResult search(String containerId, String searchCriteria, String filter,
                           long firstResult, long maxResults, SortCriterion[] orderBy) throws ContentDirectoryException {

    try {
        return new BrowseResult(new DIDLParser().generate(new DIDLContent()), 0, 0);
    } catch (Exception ex) {
        throw new ContentDirectoryException(ErrorCode.ACTION_FAILED, ex.toString());
    }
}
 
Example #21
Source Project: TVRemoteIME   Author: kingthy   File: Browse.java    License: GNU General Public License v2.0 5 votes vote down vote up
public void success(ActionInvocation invocation) {
    log.fine("Successful browse action, reading output argument values");

    BrowseResult result = new BrowseResult(
            invocation.getOutput("Result").getValue().toString(),
            (UnsignedIntegerFourBytes) invocation.getOutput("NumberReturned").getValue(),
            (UnsignedIntegerFourBytes) invocation.getOutput("TotalMatches").getValue(),
            (UnsignedIntegerFourBytes) invocation.getOutput("UpdateID").getValue()
    );

    boolean proceed = receivedRaw(invocation, result);

    if (proceed && result.getCountLong() > 0 && result.getResult().length() > 0) {

        try {

            DIDLParser didlParser = new DIDLParser();
            DIDLContent didl = didlParser.parse(result.getResult());
            received(invocation, didl);
            updateStatus(Status.OK);

        } catch (Exception ex) {
            invocation.setFailure(
                    new ActionException(ErrorCode.ACTION_FAILED, "Can't parse DIDL XML response: " + ex, ex)
            );
            failure(invocation, null);
        }

    } else {
        received(invocation, new DIDLContent());
        updateStatus(Status.NO_CONTENT);
    }
}
 
Example #22
Source Project: TVRemoteIME   Author: kingthy   File: Search.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public void success(ActionInvocation actionInvocation) {
    log.fine("Successful search action, reading output argument values");

    SearchResult result = new SearchResult(
            actionInvocation.getOutput("Result").getValue().toString(),
            (UnsignedIntegerFourBytes) actionInvocation.getOutput("NumberReturned").getValue(),
            (UnsignedIntegerFourBytes) actionInvocation.getOutput("TotalMatches").getValue(),
            (UnsignedIntegerFourBytes) actionInvocation.getOutput("UpdateID").getValue());

    boolean proceed = receivedRaw(actionInvocation, result);

    if (proceed && result.getCountLong() > 0 && result.getResult().length() > 0) {
        try {
            DIDLParser didlParser = new DIDLParser();
            DIDLContent didl = didlParser.parse(result.getResult());
            received(actionInvocation, didl);
            updateStatus(Status.OK);
        } catch (Exception ex) {
            actionInvocation.setFailure(
                    new ActionException(ErrorCode.ACTION_FAILED, "Can't parse DIDL XML response: " + ex, ex)
            );
            failure(actionInvocation, null);
        }
    } else {
        received(actionInvocation, new DIDLContent());
        updateStatus(Status.NO_CONTENT);
    }
}
 
Example #23
Source Project: TVRemoteIME   Author: kingthy   File: AbstractContentDirectoryService.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Override this method to implement searching of your content.
 * <p>
 * The default implementation returns an empty result.
 * </p>
 */
public BrowseResult search(String containerId, String searchCriteria, String filter,
                           long firstResult, long maxResults, SortCriterion[] orderBy) throws ContentDirectoryException {

    try {
        return new BrowseResult(new DIDLParser().generate(new DIDLContent()), 0, 0);
    } catch (Exception ex) {
        throw new ContentDirectoryException(ErrorCode.ACTION_FAILED, ex.toString());
    }
}
 
Example #24
Source Project: TVRemoteIME   Author: kingthy   File: DIDLParser.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Uses the current thread's context classloader to read and unmarshall the given resource.
 *
 * @param resource The resource on the classpath.
 * @return The unmarshalled DIDL content model.
 * @throws Exception
 */
public DIDLContent parseResource(String resource) throws Exception {
    InputStream is = null;
    try {
        is = Thread.currentThread().getContextClassLoader().getResourceAsStream(resource);
        return parse(IO.readLines(is));
    } finally {
        if (is != null) is.close();
    }
}
 
Example #25
Source Project: DroidDLNA   Author: offbye   File: DIDLParser.java    License: GNU General Public License v3.0 5 votes vote down vote up
@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;
}
 
Example #26
Source Project: DroidDLNA   Author: offbye   File: DIDLParser.java    License: GNU General Public License v3.0 5 votes vote down vote up
@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;
}
 
Example #27
Source Project: TVRemoteIME   Author: kingthy   File: DIDLParser.java    License: GNU General Public License v2.0 5 votes vote down vote up
@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;
}
 
Example #28
Source Project: TVRemoteIME   Author: kingthy   File: DIDLParser.java    License: GNU General Public License v2.0 5 votes vote down vote up
@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.).
}
 
Example #29
Source Project: TVRemoteIME   Author: kingthy   File: DIDLParser.java    License: GNU General Public License v2.0 5 votes vote down vote up
@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;
}
 
Example #30
Source Project: airsonic   Author: airsonic   File: MediaFileUpnpProcessor.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
// overriding for the case of browsing a file
public BrowseResult browseObjectMetadata(String id) throws Exception {
    MediaFile item = getItemById(id);
    DIDLContent didl = new DIDLContent();
    addChild(didl, item);
    return createBrowseResult(didl, 1, 1);
}