Example usage for org.dom4j DocumentHelper createElement

List of usage examples for org.dom4j DocumentHelper createElement

Introduction

In this page you can find the example usage for org.dom4j DocumentHelper createElement.

Prototype

public static Element createElement(String name) 

Source Link

Usage

From source file:edu.northwestern.bioinformatics.studycalendar.xml.AbstractStudyCalendarXmlSerializer.java

License:BSD License

protected Element element(String elementName) {
    // Using QName is the only way to attach the namespace to the element
    QName qNode = DocumentHelper.createQName(elementName, DEFAULT_NAMESPACE);
    return DocumentHelper.createElement(qNode);
}

From source file:edu.northwestern.bioinformatics.studycalendar.xml.XsdElement.java

License:BSD License

public Element create() {
    QName qNode = QName.get(xmlName(), AbstractStudyCalendarXmlSerializer.PSC_NS);
    return DocumentHelper.createElement(qNode);
}

From source file:edu.scripps.fl.pubchem.web.pug.PUGRequest.java

License:Apache License

private static void setResponseIds2(Document document, Type type, Collection<Object> ids) throws Exception {
    Node node = document.selectSingleNode("//PCT-ID-List_db");
    node.setText(type.getDatabase());/*from   ww  w  . j a v  a2 s .  co m*/
    node = document.selectSingleNode(".//PCT-ID-List_uids");
    for (Node child : (List<Node>) node.selectNodes("*"))
        child.detach();
    for (Object id : ids) {
        Element aidElem = DocumentHelper.createElement("PCT-ID-List_uids_E");
        aidElem.setText(id.toString());
        ((Element) node).add(aidElem);
    }
}

From source file:edu.scripps.fl.pubchem.web.pug.PUGRequest.java

License:Apache License

public static void setResponseIds(Document document, Type type, Collection<Object> ids) throws Exception {
    String idTypePath = "//PCT-QueryAssayData_";
    idTypePath += Type.AID.equals(type) ? "aids" : "scids";
    Node localRoot = document.selectSingleNode(idTypePath);
    Node node = localRoot.selectSingleNode(".//PCT-ID-List_db");
    node.setText(type.getDatabase());//  w  w w. j a v  a  2 s  .c om
    node = localRoot.selectSingleNode(".//PCT-ID-List_uids");
    //      if (node == null)
    //         throw new Exception("Cannot find PCT-ID-List_uids node");
    for (Node child : (List<Node>) node.selectNodes("*"))
        child.detach();
    for (Object id : ids) {
        Element aidElem = DocumentHelper.createElement("PCT-ID-List_uids_E");
        aidElem.setText(id.toString());
        ((Element) node).add(aidElem);
    }
}

From source file:edu.upenn.cis.orchestra.reconciliation.bdbstore.BdbDataSetFactory.java

License:Apache License

/**
 * Returns a {@code FlatXmlDataSet} representation of the underlying
 * Berkeley database.// w ww. j av a 2  s . c  o m
 * 
 * @return a {@code FlatXmlDataSet} representation of the Berkeley database.
 * @throws Exception
 */
public FlatXmlDataSet getDataSet() throws Exception {

    Element root = DocumentHelper.createElement("dataset");
    Document dsDoc = DocumentHelper.createDocument(root);
    DefaultDataSet dataset = new DefaultDataSet();
    initializeEnvironments(usHome, cdss, peerNames);
    BdbEnvironment envFormat = env.getFormat();

    List<Database> dbs = env.getDbs();

    processDatabases(root, dataset, envFormat, dbs);
    for (IBdbStoreEnvironment ss : stateStoreEnvs) {
        BdbEnvironment stateStoreFormat = ss.getFormat();
        List<Database> ssDbs = ss.getDbs();
        processDatabases(root, dataset, stateStoreFormat, ssDbs);
    }
    close();
    return new FlatXmlDataSet(new StringReader(dsDoc.asXML()), false, true, false);
}

From source file:edu.vt.middleware.ldap.dsml.AbstractDsml.java

License:Open Source License

/**
 * This will take an LDAP search result and convert it to a DSML entry
 * element.//  w  w  w  .j  a  v a2  s .c  o  m
 *
 * @param  entryName  <code>QName</code> name of element to create
 * @param  ldapEntry  <code>LdapEntry</code> to convert
 * @param  ns  <code>Namespace</code> of DSML
 *
 * @return  <code>Document</code>
 */
protected Element createDsmlEntry(final QName entryName, final LdapEntry ldapEntry, final Namespace ns) {
    // create Element to hold result content
    final Element entryElement = DocumentHelper.createElement(entryName);

    if (ldapEntry != null) {

        final String dn = ldapEntry.getDn();
        if (dn != null) {
            entryElement.addAttribute("dn", dn);
        }

        for (Element e : createDsmlAttributes(ldapEntry.getLdapAttributes(), ns)) {
            entryElement.add(e);
        }
    }

    return entryElement;
}

From source file:edu.vt.middleware.ldap.dsml.AbstractDsml.java

License:Open Source License

/**
 * This will take an attribute name and it's values and return a DSML
 * attribute element./*from w  w w. j av a 2s  .  c  om*/
 *
 * @param  attrName  <code>String</code>
 * @param  attrValues  <code>Set</code>
 * @param  ns  <code>Namespace</code> of DSML
 * @param  elementName  <code>String</code> of the attribute element
 * @param  elementAttrName  <code>String</code> of the attribute element
 * @param  elementValueName  <code>String</code> of the value element
 *
 * @return  <code>Element</code>
 */
protected Element createDsmlAttribute(final String attrName, final Set<?> attrValues, final Namespace ns,
        final String elementName, final String elementAttrName, final String elementValueName) {
    final Element attrElement = DocumentHelper.createElement("");

    if (attrName != null) {

        attrElement.setQName(new QName(elementName, ns));
        if (elementAttrName != null) {
            attrElement.addAttribute(elementAttrName, attrName);
        }
        if (attrValues != null) {
            final Iterator<?> i = attrValues.iterator();
            while (i.hasNext()) {
                final Object rawValue = i.next();
                String value = null;
                boolean isBase64 = false;
                if (rawValue instanceof String) {
                    value = (String) rawValue;
                } else if (rawValue instanceof byte[]) {
                    value = LdapUtil.base64Encode((byte[]) rawValue);
                    isBase64 = true;
                } else {
                    if (this.logger.isWarnEnabled()) {
                        this.logger.warn("Could not cast attribute value as a byte[]" + " or a String");
                    }
                }
                if (value != null) {
                    final Element valueElement = attrElement.addElement(new QName(elementValueName, ns));
                    valueElement.addText(value);
                    if (isBase64) {
                        valueElement.addAttribute("encoding", "base64");
                    }
                }
            }
        }
    }

    return attrElement;
}

From source file:eml.studio.server.oozie.workflow.WFGraph.java

License:Open Source License

/**
 * Transform the Graph into an workflow xml definition
 * @param jobname the job name of Oozie job, can't be null
 * @return workflow xml//from w ww.j  a  v a  2  s.  co m
 */
public String toWorkflow(String jobname) {
    Namespace xmlns = new Namespace("", "uri:oozie:workflow:0.4"); // root namespace uri
    QName qName = QName.get("workflow-app", xmlns); // your root element's name
    Element workflow = DocumentHelper.createElement(qName);
    Document xmldoc = DocumentHelper.createDocument(workflow);
    // Create workflow root
    workflow.addAttribute("xmlns", "uri:oozie:workflow:0.4");
    // <workflow-app name='xxx'></workflow-app>
    if (jobname == null || "".equals(jobname))
        workflow.addAttribute("name", "Not specified");
    else
        workflow.addAttribute("name", jobname);

    Queue<NodeDef> que = new LinkedList<NodeDef>();
    que.add(start);

    while (!que.isEmpty()) {
        NodeDef cur = que.remove();

        cur.append2XML(workflow);

        for (NodeDef toNode : cur.getOutNodes()) {
            toNode.delInNode(cur);
            if (toNode.getInDegree() == 0)
                que.add(toNode);
        }
    }

    // Set XML document format
    OutputFormat outputFormat = OutputFormat.createPrettyPrint();
    // Set XML encoding, use the specified encoding to save the XML document to the string, it can be specified GBK or ISO8859-1
    outputFormat.setEncoding("UTF-8");
    outputFormat.setSuppressDeclaration(true); // Whether generate xml header
    outputFormat.setIndent(true); // Whether set indentation
    outputFormat.setIndent("    "); // Implement indentation with four spaces
    outputFormat.setNewlines(true); // Set whether to wrap

    try {
        // stringWriter is used to save xml document
        StringWriter stringWriter = new StringWriter();
        // xmlWriter is used to write XML document to string(tool)
        XMLWriter xmlWriter = new XMLWriter(stringWriter, outputFormat);

        // Write the created XML document into the string
        xmlWriter.write(xmldoc);

        xmlWriter.close();

        System.out.println(stringWriter.toString().trim());
        // Print the string, that is, the XML document
        return stringWriter.toString().trim();

    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return null;
}

From source file:eml.studio.server.oozie.workflow.WFGraph.java

License:Open Source License

public static void main(String args[]) {

    Namespace rootNs = new Namespace("", "uri:oozie:workflow:0.4"); // root namespace uri
    QName rootQName = QName.get("workflow-app", rootNs); // your root element's name
    Element workflow = DocumentHelper.createElement(rootQName);
    Document doc = DocumentHelper.createDocument(workflow);

    workflow.addAttribute("name", "test");
    Element test = workflow.addElement("test");
    test.addText("hello");
    OutputFormat outputFormat = OutputFormat.createPrettyPrint();
    outputFormat.setEncoding("UTF-8");
    outputFormat.setIndent(true);//from   w  ww .j a  va  2  s.co m
    outputFormat.setIndent("    ");
    outputFormat.setNewlines(true);
    try {
        StringWriter stringWriter = new StringWriter();
        XMLWriter xmlWriter = new XMLWriter(stringWriter);
        xmlWriter.write(doc);
        xmlWriter.close();
        System.out.println(doc.asXML());
        System.out.println(stringWriter.toString().trim());

    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:gr.abiss.calipso.util.XmlUtils.java

License:Open Source License

public static Element getNewElement(String name) {
    return DocumentHelper.createElement(name);
}