Example usage for org.jdom2 Element setText

List of usage examples for org.jdom2 Element setText

Introduction

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

Prototype

public Element setText(final String text) 

Source Link

Document

Sets the content of the element to be the text given.

Usage

From source file:ca.nrc.cadc.dali.tables.votable.VOTableWriter.java

License:Open Source License

/**
 * Write the Throwable to a VOTable, creating an INFO element with
 * name="QUERY_STATUS" value="ERROR" and setting the stacktrace as
 * the INFO text./* w w  w .  ja  v  a  2s .  com*/
 *
 * @param thrown Throwable to write.
 * @param output OutputStream to write to.
 * @throws IOException if problem writing to the stream.
 */
public void write(Throwable thrown, OutputStream output) throws IOException {
    Document document = createDocument();
    Element root = document.getRootElement();
    Namespace namespace = root.getNamespace();

    // Create the RESOURCE element and add to the VOTABLE element.
    Element resource = new Element("RESOURCE", namespace);
    resource.setAttribute("type", "results");
    root.addContent(resource);

    // Create the INFO element and add to the RESOURCE element.
    Element info = new Element("INFO", namespace);
    info.setAttribute("name", "QUERY_STATUS");
    info.setAttribute("value", "ERROR");
    info.setText(getThrownExceptions(thrown));
    resource.addContent(info);

    // Write out the VOTABLE.
    XMLOutputter outputter = new XMLOutputter();
    outputter.setFormat(org.jdom2.output.Format.getPrettyFormat());
    outputter.output(document, output);
}

From source file:ca.nrc.cadc.tap.writer.RssTableWriter.java

License:Open Source License

@Override
public void write(ResultSet resultSet, Writer out, Long maxrec) throws IOException {
    if (selectList == null)
        throw new IllegalStateException("SelectList cannot be null, set using setSelectList()");

    List<Format<Object>> formats = formatFactory.getFormats(selectList);

    if (resultSet != null)
        try {/*from   ww w.  j  a  v  a 2s  .c om*/
            log.debug("resultSet column count: " + resultSet.getMetaData().getColumnCount());
        } catch (Exception oops) {
            log.error("failed to check resultset column count", oops);
        }

    // JDOM document.
    Document document = new Document();

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

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

    // channel title.
    Element channelTitle = new Element("title");
    channelTitle.setText(info);
    channel.addContent(channelTitle);

    StringBuilder qp = new StringBuilder();
    qp.append("http://");
    qp.append(NetUtil.getServerName(null));
    qp.append(job.getRequestPath());
    qp.append("?");
    for (Parameter parameter : job.getParameterList()) {
        qp.append(parameter.getName());
        qp.append("=");
        qp.append(parameter.getValue());
        qp.append("&");
    }
    String queryString = qp.substring(0, qp.length() - 1); // strip trailing &
    Element link = new Element("link");
    link.setText(queryString);
    channel.addContent(link);

    // items.
    int itemCount = 0;
    try {
        while (resultSet.next()) {
            // item element.
            Element item = new Element("item");

            // item description.
            Element itemDescription = new Element("description");
            StringBuilder sb = new StringBuilder();
            sb.append("<table>");

            // Loop through the ResultSet adding the table data elements.
            for (int columnIndex = 1; columnIndex <= resultSet.getMetaData().getColumnCount(); columnIndex++) {
                String columnLabel = resultSet.getMetaData().getColumnLabel(columnIndex);

                if (columnLabel.equalsIgnoreCase("rss_title")) {
                    String titleStr = resultSet.getString("rss_title");
                    log.debug("item title: " + titleStr);
                    Element itemTitle = new Element("title");
                    itemTitle.setText(titleStr);
                    item.addContent(itemTitle);
                } else if (columnLabel.equalsIgnoreCase("rss_link")) {
                    String linkStr = resultSet.getString("rss_link");
                    log.debug("item link: " + linkStr);
                    Element itemLink = new Element("link");
                    itemLink.setText(linkStr);
                    item.addContent(itemLink);
                } else if (columnLabel.equalsIgnoreCase("rss_pubDate")) {
                    Timestamp ts = resultSet.getTimestamp("rss_pubDate");
                    String pubDateStr = dateFormat.format(ts);
                    log.debug("item pubDate: " + pubDateStr);
                    Element itemPubDate = new Element("pubDate");
                    itemPubDate.setText(pubDateStr);
                    item.addContent(itemPubDate);
                } else {
                    ParamDesc paramDesc = selectList.get(columnIndex - 1);
                    sb.append("<tr><td align=\"right\">");
                    sb.append(paramDesc.name);
                    sb.append("</td><td align=\"left\">");
                    Format<Object> format = formats.get(columnIndex - 1);
                    Object obj = null;
                    if (format instanceof ResultSetFormat)
                        obj = ((ResultSetFormat) format).extract(resultSet, columnIndex);
                    else
                        obj = resultSet.getObject(columnIndex);
                    sb.append(format.format(obj));
                    sb.append("</td></tr>");
                }
            }
            sb.append("</table>");
            itemDescription.setText(sb.toString());
            item.addContent(itemDescription);
            channel.addContent(item);
            itemCount++;

            // Write MaxRows
            if (itemCount == maxrec)
                break;
        }
    } catch (SQLException e) {
        throw new RuntimeException(e.getMessage());
    }

    // channel description.
    Element channelDescription = new Element("description");
    channelDescription.setText("The " + itemCount + " most recent from " + info);
    channel.addContent(channelDescription);

    // Write out the VOTABLE.
    XMLOutputter outputter = new XMLOutputter(org.jdom2.output.Format.getPrettyFormat());
    outputter.output(document, out);

}

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

License:Open Source License

/**
 * Builds a single node element./*from  ww w  .j a v a 2  s  .  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 properties Element of a Node.
 *
 * @param node Node./*  w  w w .  j  a  va 2  s.c  o m*/
 * @return properties Element.
 */
protected Element getPropertiesElement(Node node) {
    Element ret = new Element("properties", vosNamespace);
    for (NodeProperty nodeProperty : node.getProperties()) {
        Element property = new Element("property", vosNamespace);
        if (nodeProperty.isMarkedForDeletion())
            property.setAttribute(new Attribute("nil", "true", xsiNamespace));
        else
            property.setText(nodeProperty.getPropertyValue());
        property.setAttribute("uri", nodeProperty.getPropertyURI());
        property.setAttribute("readOnly", (nodeProperty.isReadOnly() ? "true" : "false"));
        ret.addContent(property);
    }
    return ret;
}

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.
 *///  w ww  .ja v  a 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.
 *//*w  w  w  . j av a  2  s . c om*/
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.TransferWriter.java

License:Open Source License

/**
 * Build root element for the transfer.//from   w w  w.  j a v a2s . co m
 * @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   ww w  .  j a  v  a2 s  . c o  m
    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;
}

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

License:Open Source License

/**
 * @param sd/*w ww .ja  va2  s . c o m*/
 * @return
 */
private Element toXmlElement(SchemaDesc sd, Namespace ns) {
    Element eleSchema = new Element("schema", ns);
    Element ele;
    ele = new Element("name");
    if (sd.getSchemaName() == null)
        ele.setText(DEFAULT_SCHEMA);
    else
        ele.setText(sd.getSchemaName());
    eleSchema.addContent(ele);
    if (sd.getTableDescs() != null)
        for (TableDesc td : sd.getTableDescs()) {
            eleSchema.addContent(toXmlElement(td, Namespace.NO_NAMESPACE));
        }
    return eleSchema;
}

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

License:Open Source License

/**
 * @param td//from  w  w  w.  ja v  a  2  s  .c o m
 * @return
 */
private Element toXmlElement(TableDesc td, Namespace ns) {
    Element eleTable = new Element("table", ns);
    eleTable.setAttribute("type", "output");

    Element ele;
    ele = new Element("name");
    ele.setText(td.getTableName());
    eleTable.addContent(ele);

    if (td.getColumnDescs() != null)
        for (ColumnDesc cd : td.getColumnDescs()) {
            Element e = toXmlElement(cd);
            if (e != null)
                eleTable.addContent(e);
        }
    if (td.getKeyDescs() != null)
        for (KeyDesc kd : td.getKeyDescs()) {
            Element e = toXmlElement(kd);
            if (e != null)
                eleTable.addContent(e);
        }
    return eleTable;
}