Example usage for org.jdom2 Element Element

List of usage examples for org.jdom2 Element Element

Introduction

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

Prototype

public Element(final String name, final String uri) 

Source Link

Document

Creates a new element with the supplied (local) name and a namespace given by a URI.

Usage

From source file:Question.java

License:Apache License

/**
 * This adds one radio button selection to a radio button based HIT template.  If the questionTemplateFile doesn't
 * contain a StyleSuggestion element containing "radiobutton", then this method won't work.
 * //from ww  w. jav a 2s  .c om
 * @param radioButtonText
 * @param radioButtonIdentifier
 * @throws StoryException
 */
public void addRadioButtonSpecification(String radioButtonText, String radioButtonIdentifier)
        throws StoryException {
    // This should REALLY be done using Xpath, but Amazon's mturk doesn't use a prefix to their xmlns definition
    // and XPath maps this into the null namespace.
    logger.debug("getting the XML element for the Selection...");
    Element question = root.getChild("Question", null);
    if (question == null) {
        throw new StoryException("The XML template file doesn't contain a proper Question element");
    }
    Element answerSpecification = question.getChild("AnswerSpecification", null);
    if (answerSpecification == null) {
        throw new StoryException("The XML template file doesn't contain a proper AnswerSpecification element");
    }
    Element selectionAnswer = answerSpecification.getChild("SelectionAnswer", null);
    if (selectionAnswer == null) {
        throw new StoryException("The XML template file doesn't contain a proper SelectionAnswer element");
    }
    Element selections = selectionAnswer.getChild("Selections", null);
    if (selections == null) {
        throw new StoryException("The XML template file's QuestionContent element doesn't have a Text element");
    }

    // Adding in the namespace URI is the only way to keep JDOM2 from adding a null namespace to the element
    Element selection = new Element("Selection",
            "http://mechanicalturk.amazonaws.com/AWSMechanicalTurkDataSchemas/2005-10-01/QuestionForm.xsd");
    Element selectionIdentifier = new Element("SelectionIdentifier",
            "http://mechanicalturk.amazonaws.com/AWSMechanicalTurkDataSchemas/2005-10-01/QuestionForm.xsd");
    Element text = new Element("Text",
            "http://mechanicalturk.amazonaws.com/AWSMechanicalTurkDataSchemas/2005-10-01/QuestionForm.xsd");
    text.addContent(radioButtonText);
    selectionIdentifier.addContent(radioButtonIdentifier);
    selection.addContent(selectionIdentifier);
    selection.addContent(text);
    selections.addContent(selection);

}

From source file:ac.simons.syndication.modules.atom.AtomModuleGenerator.java

License:BSD License

private void addLinks(final Element parent, final AtomContent content) {
    for (final Link link : content.getLinks()) {
        final Element e = new Element("link", ATOM_NS);

        if (!isBlank(link.getRel())) {
            e.setAttribute(new Attribute("rel", link.getRel()));
        }//from w  w  w  .j a v a  2s  . c  o  m
        if (!isBlank(link.getType())) {
            e.setAttribute(new Attribute("type", link.getType()));
        }
        if (!isBlank(link.getHref())) {
            e.setAttribute(new Attribute("href", link.getHref()));
        }
        if (!isBlank(link.getHreflang())) {
            e.setAttribute(new Attribute("hreflang", link.getHreflang()));
        }
        if (!isBlank(link.getTitle())) {
            e.setAttribute(new Attribute("title", link.getTitle()));
        }
        if (link.getLength() != 0) {
            e.setAttribute(new Attribute("length", Long.toString(link.getLength())));
        }
        parent.addContent(e);
    }
}

From source file:ac.simons.syndication.utils.SyndicationContent.java

License:BSD License

public Element toElement() {
    final Element element = new Element("encoded",
            Namespace.getNamespace("content", "http://purl.org/rss/1.0/modules/content/"));
    element.addContent(new CDATA(content.getValue()));
    return element;
}

From source file:broadwick.graph.writer.GraphMl.java

License:Apache License

/**
 * Get the string representation of the network.
 * @param network  the network object to be written.
 * @param directed a boolean flag, true if the network is directed.
 * @return a string representing a document.
 *///from  w ww . ja  v  a  2s  .c  o m
public static String toString(final Graph<? extends Vertex, ? extends Edge<?>> network,
        final boolean directed) {
    // graphml document header
    final Element graphml = new Element("graphml", "http://graphml.graphdrawing.org/xmlns");
    final Document document = new Document(graphml);
    final Namespace xsi = Namespace.getNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
    final Namespace schemLocation = Namespace.getNamespace("schemLocation",
            "http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0rc/graphml.xsd");

    // add Namespace
    graphml.addNamespaceDeclaration(xsi);
    graphml.addNamespaceDeclaration(schemLocation);

    // keys for graphic representation
    for (VertexAttribute attr : network.getVertexAttributes()) {
        final Element element = new Element("key");
        element.setAttribute("id", attr.getName());
        element.setAttribute("for", "node");
        element.setAttribute("attr.name", attr.getName());
        element.setAttribute("attr.type", attr.getType().getName());
        if (attr.getDefaultValue() != null) {
            final Element defaultValueElement = new Element("default");
            defaultValueElement.addContent(attr.getDefaultValue().toString());
            element.addContent(defaultValueElement);
        }
        graphml.addContent(element);
    }

    for (EdgeAttribute attr : network.getEdgeAttributes()) {
        final Element element = new Element("key");
        element.setAttribute("id", attr.getName());
        element.setAttribute("for", "edge");
        element.setAttribute("attr.name", attr.getName());
        element.setAttribute("attr.type", attr.getType().getName());
        if (attr.getDefaultValue() != null) {
            final Element defaultValueElement = new Element("default");
            defaultValueElement.addContent(attr.getDefaultValue());
            element.addContent(defaultValueElement);
        }
        graphml.addContent(element);
    }

    final Element graph = new Element("graph");
    graph.setAttribute("id", "G");
    if (directed) {
        graph.setAttribute("edgedefault", "directed");
    } else {
        graph.setAttribute("edgedefault", "undirected");
    }
    graphml.addContent(graph);

    final ImmutableList<Vertex> vertices = ImmutableList.copyOf(network.getVertices());
    final Iterator<Vertex> vertexIterator = vertices.iterator();
    while (vertexIterator.hasNext()) {
        final Vertex vertex = vertexIterator.next();
        addNode(vertex, graph);
    }

    final ImmutableList<Edge<? extends Vertex>> edges;
    edges = (ImmutableList<Edge<? extends Vertex>>) ImmutableList.copyOf(network.getEdges());
    final UnmodifiableIterator<Edge<? extends Vertex>> edgeIterator = edges.iterator();
    while (edgeIterator.hasNext()) {
        addEdge(edgeIterator.next(), graph);
    }

    final XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
    return outputter.outputString(document).replaceAll("xmlns=\"\" ", "");
}

From source file:ca.nrc.cadc.caom2.xml.ObservationWriter.java

License:Open Source License

/**
 * Builds a JDOM representation Element with the given name and sets the
 * text to the given value, then adds the element to the parent.
 * //  w w w . j  ava2 s  .  co m
 * @param name
 *            The name of the element.
 * @param value
 *            The value for the element.
 * @param parent
 *            The parent element for this child element.
 */
protected void addElement(String name, String value, Element parent) {
    if (value == null) {
        return;
    }

    Element element = new Element(name, caom2Namespace);
    element.setText(value);
    parent.addContent(element);
}

From source file:ca.nrc.cadc.caom2.xml.ObservationWriter.java

License:Open Source License

/**
 * Builds a JDOM representation Element with the given name and sets the
 * text to the given value, then adds the element to the parent.
 * /*from w ww  .  jav a 2 s .  co m*/
 * @param name
 *            The name of the element.
 * @param number
 *            The value for the element.
 * @param parent
 *            The parent element for this child element.
 */
protected void addNumberElement(String name, Number number, Element parent) {
    if (number == null) {
        return;
    }

    Element element = new Element(name, caom2Namespace);
    element.setText(number.toString());
    parent.addContent(element);
}

From source file:ca.nrc.cadc.caom2.xml.ObservationWriter.java

License:Open Source License

/**
 * Builds a JDOM representation Element with the given name and sets the
 * text to true if the value is true, else sets the text to false, then adds
 * the element to the parent./* ww w. j  a va 2 s .c  o m*/
 * 
 * @param name
 *            The name of the element.
 * @param value
 *            The boolean value for the element.
 * @param parent
 *            The parent element for this child element.
 */
protected void addBooleanElement(String name, Boolean value, Element parent) {
    if (value == null) {
        return;
    }

    Element element = new Element(name, caom2Namespace);
    element.setText(value ? "true" : "false");
    parent.addContent(element);
}

From source file:ca.nrc.cadc.caom2.xml.ObservationWriter.java

License:Open Source License

/**
 * Builds a JDOM representation Element with the given name and sets the
 * text to the given value, then adds the element to the parent.
 * //from   w ww.  j  a  va2 s  .  com
 * @param name
 *            The name of the element.
 * @param uri
 *            The URI value for the element.
 * @param parent
 *            The parent element for this child element.
 */
protected void addURIElement(String name, URI uri, Element parent) {
    if (uri == null) {
        return;
    }

    Element element = new Element(name, caom2Namespace);
    element.setText(uri.toString());
    parent.addContent(element);
}

From source file:ca.nrc.cadc.caom2.xml.ObservationWriter.java

License:Open Source License

/**
 * Builds a JDOM representation Element with the given name and adds space
 * delimited List values as the text, then adds the element to the parent.
 * /*w  w w. j av a 2  s.com*/
 * @param name
 *            The name of the element.
 * @param values
 *            The List of Strings for the element.
 * @param parent
 *            The parent element for this child element.
 */
protected void addStringListElement(String name, Collection<String> values, Element parent) {
    if (values == null || (values.isEmpty() && !writeEmptyCollections)) {
        return;
    }

    Element element = new Element(name, caom2Namespace);
    StringBuilder sb = new StringBuilder();
    for (String value : values) {
        sb.append(value).append(" ");
    }
    element.setText(sb.toString().trim());
    parent.addContent(element);
}

From source file:ca.nrc.cadc.caom2.xml.ObservationWriter.java

License:Open Source License

protected void addKeywordsElement(Collection<String> values, Element parent) {
    if (values == null || (values.isEmpty() && !writeEmptyCollections)) {
        return;//from w  w w.java 2  s  .  c o  m
    }

    Element element = new Element("keywords", caom2Namespace);
    StringBuilder sb = new StringBuilder();
    for (String value : values) {
        Element kw = new Element("keyword", caom2Namespace);
        kw.addContent(value);
        element.addContent(kw);
    }
    parent.addContent(element);
}