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.dali.tables.votable.FieldElement.java

License:Open Source License

/**
 * Add VALUES Element with OPTION child Elements.
 * @param values/*from  w  w w.ja v  a  2  s .co  m*/
 * @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.// www  .java2s . c  o m
 * @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   w w w .j  a  v 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.dali.tables.votable.VOTableWriter.java

License:Open Source License

/**
 * Builds a empty VOTable document with the appropriate namespaces and
 * attributes./* w w  w  . j  a  v a 2  s.  c o m*/
 *
 * @return VOTable document.
 */
protected Document createDocument() {
    // the root VOTABLE element
    Namespace vot = Namespace.getNamespace(VOTABLE_12_NS_URI);
    Namespace xsi = Namespace.getNamespace("xsi", XSI_SCHEMA);
    Element votable = new Element("VOTABLE", vot);
    votable.setAttribute("version", VOTABLE_VERSION);
    votable.addNamespaceDeclaration(xsi);

    Document document = new Document();
    document.addContent(votable);

    return document;
}

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 .  c  o m*/
            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

public Element getRootElement(Iterator<JobRef> jobs) {
    ContentConverter<Element, JobRef> contentConverter = new ContentConverter<Element, JobRef>() {
        @Override// w  w  w. j a v  a2s  .c  o  m
        public Element convert(final JobRef jobRef) {
            return getShortJobDescription(jobRef);
        }
    };

    Element root = new IterableContent<Element, JobRef>(JobAttribute.JOBS.getAttributeName(), UWS.NS, jobs,
            contentConverter);
    root.addNamespaceDeclaration(UWS.NS);
    root.addNamespaceDeclaration(UWS.XLINK_NS);
    root.setAttribute(JobAttribute.VERSION.getAttributeName(), UWS.XSD_VERSION);
    return root;
}

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

License:Open Source License

/**
 * Create the XML for a short job description.
 * @param jobRef/*from  w w w  .j  av a2s . 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.JobWriter.java

License:Open Source License

/**
 * Create the root element of a job document.
 * @param job/*from w ww  .  j av a2  s .  com*/
 * @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;
}

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

License:Open Source License

/**
 * Get an Element representing the Job jobref.
 *
 * @param host The host part of the Job request URL.
 * @return The Job jobref Element./*from w ww.ja v a 2 s  .  c  o m*/
 */
public Element getJobRef(String host, Job job) {
    Element element = new Element(JobAttribute.JOB_REF.getAttributeName(), UWS.NS);
    element.setAttribute("id", job.getID());
    element.setAttribute("xlink:href", host + job.getRequestPath() + "/" + job.getID());
    return element;
}

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

License:Open Source License

/**
 * Get an Element representing the Job parameters.
 *
 * @return The Job parameters Element.//from  w  w w.  ja va  2  s .c  om
 */
public Element getParameters(Job job) {
    Element element = new Element(JobAttribute.PARAMETERS.getAttributeName(), UWS.NS);
    for (Parameter parameter : job.getParameterList()) {
        Element e = new Element(JobAttribute.PARAMETER.getAttributeName(), UWS.NS);
        e.setAttribute("id", parameter.getName());
        e.addContent(parameter.getValue());
        element.addContent(e);
    }
    return element;
}