Example usage for org.jdom2 Element setAttribute

List of usage examples for org.jdom2 Element setAttribute

Introduction

In this page you can find the example usage for org.jdom2 Element setAttribute.

Prototype

public Element setAttribute(final String name, final String value) 

Source Link

Document

This sets an attribute value for this element.

Usage

From source file:ca.nrc.cadc.uws.JobWriter.java

License:Open Source License

/**
 * Get an Element representing the Job results.
 *
 * @return The Job results Element./*from  w  ww.  j  ava  2s  .c o m*/
 */
public Element getResults(Job job) {
    Element element = new Element(JobAttribute.RESULTS.getAttributeName(), UWS.NS);
    for (Result result : job.getResultsList()) {
        Element e = new Element(JobAttribute.RESULT.getAttributeName(), UWS.NS);
        e.setAttribute("id", result.getName());
        e.setAttribute("href", result.getURI().toASCIIString(), UWS.XLINK_NS);
        element.addContent(e);
    }
    return element;
}

From source file:ca.nrc.cadc.uws.JobWriter.java

License:Open Source License

/**
 * Get an Element representing the Job errorSummary.
 *
 * @return The Job errorSummary Element.
 *///  w  ww  .  j a v a  2  s  .co  m
public Element getErrorSummary(Job job) {
    Element eleErrorSummary = null;
    ErrorSummary es = job.getErrorSummary();
    if (es != null) {
        eleErrorSummary = new Element(JobAttribute.ERROR_SUMMARY.getAttributeName(), UWS.NS);
        eleErrorSummary.setAttribute("type", es.getErrorType().toString().toLowerCase());
        eleErrorSummary.setAttribute("hasDetail", Boolean.toString(es.getHasDetail()));

        Element eleMessage = new Element(JobAttribute.ERROR_SUMMARY_MESSAGE.getAttributeName(), UWS.NS);
        eleMessage.addContent(job.getErrorSummary().getSummaryMessage());
        eleErrorSummary.addContent(eleMessage);
    }
    return eleErrorSummary;
}

From source file:ca.nrc.cadc.vos.NodeWriter.java

License:Open Source License

/**
 * Builds a single node element.//from  w  ww .ja va 2s. c o m
 *
 * @param node
 * @return
 */
protected Element getNodeElement(Node node) {
    Element ret = new Element("node", vosNamespace);
    ret.setAttribute("uri", node.getUri().toString());
    ret.setAttribute("type", vosNamespace.getPrefix() + ":" + node.getClass().getSimpleName(), xsiNamespace);

    Element props = getPropertiesElement(node);
    ret.addContent(props);

    if (node instanceof ContainerNode) {
        ContainerNode cn = (ContainerNode) node;
        ret.addContent(getNodesElement(cn));
    } else if ((node instanceof DataNode) || (node instanceof UnstructuredDataNode)
            || (node instanceof StructuredDataNode)) {
        ret.addContent(getAcceptsElement(node));
        ret.addContent(getProvidesElement(node));
        DataNode dn = (DataNode) node;
        ret.setAttribute("busy", (dn.getBusy().equals(NodeBusyState.notBusy) ? "false" : "true"));
    } else if (node instanceof LinkNode) {
        LinkNode ln = (LinkNode) node;
        Element targetEl = new Element("target", vosNamespace);
        targetEl.setText(ln.getTarget().toString());
        ret.addContent(targetEl);
    }
    return ret;
}

From source file:ca.nrc.cadc.vos.NodeWriter.java

License:Open Source License

/**
 * Build the accepts Element of a Node./*from   w  w  w.j a  v a2s . com*/
 *
 * @param node Node.
 * @return accepts Element.
 */
protected Element getAcceptsElement(Node node) {
    Element accepts = new Element("accepts", vosNamespace);
    for (URI viewURI : node.accepts()) {
        Element viewElement = new Element("view", vosNamespace);
        viewElement.setAttribute("uri", viewURI.toString());
        accepts.addContent(viewElement);
    }
    return accepts;
}

From source file:ca.nrc.cadc.vos.NodeWriter.java

License:Open Source License

/**
 * Build the accepts Element of a Node.//  www. java 2  s .  com
 *
 * @param node Node.
 * @return accepts Element.
 */
protected Element getProvidesElement(Node node) {
    Element provides = new Element("provides", vosNamespace);
    for (URI viewURI : node.provides()) {
        Element viewElement = new Element("view", vosNamespace);
        viewElement.setAttribute("uri", viewURI.toString());
        provides.addContent(viewElement);
    }
    return provides;
}

From source file:ca.nrc.cadc.vos.server.RssFeed.java

License:Open Source License

/**
 * Builds a JDOM Element representing the RRS feed of set of Nodes.
 *
 * @param parent The parent Node of all other Nodes in the feed.
 * @param nodes Collection of nodes to display in the feed.
 * @param baseURL base url for the nodes resource
 * @return JDOM Element representing the RRS feed.
 *///from  w ww .  j  a  va 2 s  . c  om
public static Element createFeed(Node parent, Collection<RssFeedItem> nodes, String baseURL) {
    // Get the title and description from the parent properties.
    String parentTitle = null;
    String parentDescription = null;
    for (NodeProperty nodeProperty : parent.getProperties()) {
        if (nodeProperty.getPropertyURI().equals(VOS.PROPERTY_URI_TITLE)) {
            parentTitle = nodeProperty.getPropertyValue();
        }
        if (nodeProperty.getPropertyURI().equals(VOS.PROPERTY_URI_DESCRIPTION)) {
            parentDescription = nodeProperty.getPropertyValue();
        }
    }

    // Root element.
    Element rss = new Element("rss");
    rss.setAttribute("version", "2.0");

    // channel element.
    Element channel = new Element("channel");
    rss.addContent(channel);

    // channel title.
    if (parentTitle == null) {
        parentTitle = "Last modified nodes for " + parent.getName();
    }
    Element channelTitle = new Element("title");
    channelTitle.setText(parentTitle);
    channel.addContent(channelTitle);

    // channel description.
    if (parentDescription == null) {
        parentDescription = nodes.size() + " last modified nodes";
    }
    Element channelDescription = new Element("description");
    channelDescription.setText(parentDescription);
    channel.addContent(channelDescription);

    // channel link.
    Element link = new Element("link");
    link.setText(baseURL + parent.getUri().getPath() + "?view=rss");
    channel.addContent(link);

    // channel author
    NodeProperty creator = parent.findProperty(VOS.PROPERTY_URI_CREATOR);
    if (creator != null && StringUtil.hasText(creator.getPropertyValue())) {
        Element author = new Element("author");
        author.setText(creator.getPropertyValue());
        channel.addContent(author);
    }
    log.debug("num nodes: " + nodes.size());
    // Create an item for each Node and add to channel.
    for (RssFeedItem rssFeedItem : nodes) {
        // Get the title, description, and date from the node properties.
        String nodeTitle = null;
        String nodeDescription = null;
        String nodeDate = null;
        for (NodeProperty nodeProperty : rssFeedItem.node.getProperties()) {
            if (nodeProperty.getPropertyURI().equals(VOS.PROPERTY_URI_TITLE)) {
                nodeTitle = nodeProperty.getPropertyValue();
            }
            if (nodeProperty.getPropertyURI().equals(VOS.PROPERTY_URI_DESCRIPTION)) {
                nodeDescription = nodeProperty.getPropertyValue();
            }
            if (nodeProperty.getPropertyURI().equals(VOS.PROPERTY_URI_DATE)) {
                nodeDate = nodeProperty.getPropertyValue();
            }
        }

        // item element.
        Element item = new Element("item");

        // item title.
        if (nodeTitle == null) {
            nodeTitle = rssFeedItem.node.getName();
        }
        Element itemTitle = new Element("title");
        itemTitle.setText(nodeTitle);
        item.addContent(itemTitle);

        // item description.
        if (nodeDescription == null) {
            if (rssFeedItem.node instanceof DataNode) {
                nodeDescription = "File";
            } else {
                nodeDescription = "Directory";
            }
        }
        Element itemDescription = new Element("description");
        itemDescription.setText(nodeDescription);
        item.addContent(itemDescription);

        // item pubDate
        if (nodeDate != null) {
            Element itemPubDate = new Element("pubDate");
            itemPubDate.setText(nodeDate);
            item.addContent(itemPubDate);
        }

        // item link, comment
        Element itemLink = new Element("link");
        Element comments = new Element("comments");
        String linkText = baseURL + rssFeedItem.node.getUri().getPath();
        if (rssFeedItem.node instanceof DataNode) {
            linkText += "?view=data";
            comments.setText("Click to download this file.");
        } else {
            linkText += "?view=rss";
            comments.setText("Click to see the last modified nodes within this directory.");
        }
        itemLink.setText(linkText);
        item.addContent(itemLink);
        item.addContent(comments);

        // item author
        creator = rssFeedItem.node.findProperty(VOS.PROPERTY_URI_CREATOR);
        if (creator != null && StringUtil.hasText(creator.getPropertyValue())) {
            Element author = new Element("author");
            author.setText(creator.getPropertyValue());
            item.addContent(author);
        }

        // Add item to channel
        channel.addContent(item);
    }
    return rss;
}

From source file:ca.nrc.cadc.vos.server.RssFeed.java

License:Open Source License

/**
 * Builds a JDOM Element representing a RRS feed that displays an error state.
 *
 * @param node The Node causing the error.
 * @param message The message to display.
 * @param baseURL base url for the nodes resource
 * @return JDOM Element representing the RRS feed.
 *//*from   w  ww  .j  av a  2s  .  c o m*/
public static Element createErrorFeed(Node node, String message, String baseURL) {
    // Root element.
    Element rss = new Element("rss");
    rss.setAttribute("version", "2.0");

    // channel element.
    Element channel = new Element("channel");
    rss.addContent(channel);

    // channel title.
    Element title = new Element("title");
    String nodeName = node.getName();
    if (nodeName == null) {
        nodeName = "unknown node";
    }
    title.setText("Error processing Node " + nodeName);
    channel.addContent(title);

    // channel link.
    Element link = new Element("link");
    String nodePath = node.getUri().getPath();
    if (nodePath == null) {
        link.setText("unknown link");
    } else {
        link.setText(baseURL + nodePath + "?view=rss");
    }
    channel.addContent(link);

    // channel description.
    Element description = new Element("description");
    description.setText(message);
    channel.addContent(description);

    return rss;
}

From source file:ca.nrc.cadc.vos.server.ViewsWriter.java

License:Open Source License

/**
 * Create a URI list for the views./*w  w  w  .ja v a2 s. c  o  m*/
 * @param name
 * @param uris
 * @return
 */
protected Element getViewsListElement(String name, List<String> uris) {
    Element viewList = new Element(name, defaultNamespace);
    for (String uri : uris) {
        Element property = new Element("view", defaultNamespace);
        property.setAttribute("uri", uri);
        viewList.addContent(property);
    }
    return viewList;
}

From source file:ca.nrc.cadc.vos.TransferWriter.java

License:Open Source License

/**
 * Build root element for the transfer.//from w w  w  .  jav a 2s.c  om
 * @param transfer
 * @return root element
 */
private Element buildRoot(Transfer transfer) {
    Element root = new Element("transfer", TransferReader.VOS_NS);
    root.addNamespaceDeclaration(TransferReader.VOS_NS);

    Element e = null;

    e = new Element("target", TransferReader.VOS_NS);
    e.addContent(transfer.getTarget().getURIObject().toASCIIString());
    root.addContent(e);

    e = new Element("direction", TransferReader.VOS_NS);
    e.addContent(transfer.getDirection().getValue());
    root.addContent(e);

    e = new Element("view", TransferReader.VOS_NS);
    if (transfer.getView() != null) {
        e.setAttribute("uri", transfer.getView().getURI().toString());
        for (View.Parameter param : transfer.getView().getParameters()) {
            Element pm = new Element("param", TransferReader.VOS_NS);
            pm.setAttribute("uri", param.getUri().toString());
            pm.setText(param.getValue());
            e.addContent(pm);
        }
        root.addContent(e);
    }

    if (transfer.getProtocols() != null) {
        for (Protocol protocol : transfer.getProtocols()) {
            Element pr = new Element("protocol", TransferReader.VOS_NS);
            pr.setAttribute("uri", protocol.getUri());
            if (protocol.getEndpoint() != null) {
                Element ep = new Element("endpoint", TransferReader.VOS_NS);
                ep.addContent(protocol.getEndpoint());
                pr.addContent(ep);
            }
            root.addContent(pr);
        }
    }

    e = new Element("keepBytes", TransferReader.VOS_NS);
    e.addContent(new Boolean(transfer.isKeepBytes()).toString());
    root.addContent(e);

    return root;
}

From source file:ca.nrc.cadc.vosi.Capability.java

License:Open Source License

public Element toXmlElement(Namespace xsi, Namespace cap, Namespace vor) {
    Element eleCapability = new Element("capability");
    eleCapability.setAttribute("standardID", _standardID);

    Element eleInterface = new Element("interface");
    eleCapability.addContent(eleInterface);

    Attribute attType = new Attribute("type", vor.getPrefix() + ":ParamHTTP", xsi);
    eleInterface.setAttribute(attType);/*from w ww  . j  ava 2  s  .c om*/
    if (_role != null)
        eleInterface.setAttribute("role", _role);

    Element eleAccessURL = new Element("accessURL");
    eleInterface.addContent(eleAccessURL);

    eleAccessURL.setAttribute("use", "full");
    eleAccessURL.setText(_hostContext + _resourceName);

    return eleCapability;
}