Example usage for org.jdom2 Element addContent

List of usage examples for org.jdom2 Element addContent

Introduction

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

Prototype

@Override
public Element addContent(final Collection<? extends Content> newContent) 

Source Link

Document

Appends all children in the given collection to the end of the content list.

Usage

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

License:Open Source License

/**
 * Add VALUES Element with OPTION child Elements.
 * @param values//  w  w  w  . ja  v  a 2  s .  c om
 * @param namespace
 */
protected void setValues(List<String> values, Namespace namespace) {
    if (values != null && !values.isEmpty()) {
        Element element = new Element("VALUES", namespace);
        for (String value : values) {
            Element option = new Element("OPTION", namespace);
            option.setAttribute("value", value);
            element.addContent(option);
        }
        addContent(element);
    }
}

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

License:Open Source License

/**
 * Write the VOTable to the specified Writer, only writing maxrec rows.
 * If the VOTable contains more than maxrec rows, appends an INFO element with
 * name="QUERY_STATUS" value="OVERFLOW" to the VOTable.
 *
 * @param votable VOTable object to write.
 * @param writer Writer to write to.//from  ww  w .jav a 2 s.c  om
 * @param maxRec maximum number of rows to write.
 * @throws IOException if problem writing to the writer.
 */
public void write(VOTable votable, Writer writer, Long maxrec) throws IOException {
    log.debug("write, maxrec=" + maxrec);

    // VOTable document and root element.
    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);
    if (votable.getResourceName() != null) {
        resource.setAttribute("name", votable.getResourceName());
    }
    root.addContent(resource);

    // Create the INFO element and add to the RESOURCE element.
    for (Info in : votable.getInfos()) {
        Element info = new Element("INFO", namespace);
        info.setAttribute("name", in.getName());
        info.setAttribute("value", in.getValue());
        resource.addContent(info);
    }

    // Create the TABLE element and add to the RESOURCE element.
    Element table = new Element("TABLE", namespace);
    resource.addContent(table);

    // Add the metadata elements.
    for (TableParam param : votable.getParams()) {
        table.addContent(new ParamElement(param, namespace));
    }
    for (TableField field : votable.getColumns()) {
        table.addContent(new FieldElement(field, namespace));
    }

    // Create the DATA and TABLEDATA elements.
    Element data = new Element("DATA", namespace);
    table.addContent(data);

    // Add content.
    try {
        Iterator<List<Object>> it = votable.getTableData().iterator();

        TabledataContentConverter elementConverter = new TabledataContentConverter(namespace);
        TabledataMaxIterations maxIterations = new TabledataMaxIterations(maxrec, resource, namespace);

        IterableContent<Element, List<Object>> tabledata = new IterableContent<Element, List<Object>>(
                "TABLEDATA", namespace, it, elementConverter, maxIterations);

        data.addContent(tabledata);
    } catch (Throwable t) {
        log.debug("failure while iterating", t);
        Element info = new Element("INFO", namespace);
        info.setAttribute("name", "QUERY_STATUS");
        info.setAttribute("value", "ERROR");
        info.setText(t.toString());
        resource.addContent(info);
    }

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

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./*from www. j  av a2s. co m*/
 *
 * @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  w  w w  . j  a v a  2 s  .  com
            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.uws.JobListWriter.java

License:Open Source License

/**
 * Create the XML for a short job description.
 * @param jobRef/*  w  ww. ja  va  2s  .c o  m*/
 * @return
 */
public Element getShortJobDescription(JobRef jobRef) {
    Element shortJobDescription = new Element(JobAttribute.JOB_REF.getAttributeName(), UWS.NS);
    shortJobDescription.setAttribute("id", jobRef.getJobID());
    shortJobDescription.addContent(getPhase(jobRef));
    shortJobDescription.addContent(getCreationTime(jobRef));
    shortJobDescription.addContent(getRunID(jobRef));
    shortJobDescription.addContent(getOwnerID(jobRef));
    return shortJobDescription;
}

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

License:Open Source License

/**
 * Get an Element representing the Job phase.
 *
 * @return The Job phase Element.// w w w  .j a v a  2 s  . c  o  m
 */
private Element getPhase(JobRef jobRef) {
    Element element = new Element(JobAttribute.EXECUTION_PHASE.getAttributeName(), UWS.NS);
    element.addContent(jobRef.getExecutionPhase().toString());
    return element;
}

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

License:Open Source License

/**
 * Get an Element representing the runID.
 *
 * @return The runID element./*  w w  w.jav  a 2  s.c  o m*/
 */
private Element getRunID(JobRef jobRef) {
    Element element = new Element(JobAttribute.RUN_ID.getAttributeName(), UWS.NS);
    String runID = jobRef.getRunID();
    if (runID == null)
        element.setAttribute("nil", "true", UWS.XSI_NS);
    else
        element.addContent(runID);
    return element;
}

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

License:Open Source License

/**
 * Get an Element representing the creation time.
 *
 * @return The creation time element.//  w  w w.j  av  a 2 s.c  o m
 */
private Element getCreationTime(JobRef jobRef) {
    Element element = new Element(JobAttribute.CREATION_TIME.getAttributeName(), UWS.NS);
    Date creationTime = jobRef.getCreationTime();
    if (creationTime == null)
        element.setAttribute("nil", "true", UWS.XSI_NS);
    else
        element.addContent(dateFormat.format(jobRef.getCreationTime()));
    return element;
}

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

License:Open Source License

/**
 * Get an Element representing the owner ID.
 *
 * @return The owner ID Element.//from w w w .  j ava  2 s .c  om
 */
private Element getOwnerID(JobRef jobRef) {
    Element element = new Element(JobAttribute.OWNER_ID.getAttributeName(), UWS.NS);
    element.addContent(jobRef.getOwnerID());
    return element;
}

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

License:Open Source License

/**
 * Create the root element of a job document.
 * @param job/*  w w w  .j  a v a 2 s .c om*/
 * @return
 */
public Element getRootElement(Job job) {
    Element root = new Element(JobAttribute.JOB.getAttributeName(), UWS.NS);
    root.addNamespaceDeclaration(UWS.NS);
    root.addNamespaceDeclaration(UWS.XLINK_NS);
    root.setAttribute(JobAttribute.VERSION.getAttributeName(), UWS.XSD_VERSION);

    root.addContent(getJobId(job));
    root.addContent(getRunId(job));
    root.addContent(getOwnerId(job));
    root.addContent(getPhase(job));
    root.addContent(getQuote(job));
    root.addContent(getStartTime(job));
    root.addContent(getEndTime(job));
    root.addContent(getCreationTime(job));
    root.addContent(getExecutionDuration(job));
    root.addContent(getDestruction(job));
    root.addContent(getParameters(job));
    root.addContent(getResults(job));
    Element errorSummary = getErrorSummary(job);
    if (errorSummary != null)
        root.addContent(errorSummary);

    Element jobInfo = getJobInfo(job);
    if (jobInfo != null)
        root.addContent(jobInfo);

    return root;
}