Example usage for org.dom4j Document addElement

List of usage examples for org.dom4j Document addElement

Introduction

In this page you can find the example usage for org.dom4j Document addElement.

Prototype

Element addElement(String qualifiedName, String namespaceURI);

Source Link

Document

Adds a new Element node with the given qualified name and namespace URI to this branch and returns a reference to the new node.

Usage

From source file:com.thinkberg.webdav.PropPatchHandler.java

License:Apache License

/**
 * Get a multistatus response for each of the property set/remove requests.
 *
 * @param object              the context object the property requests apply to
 * @param requestedProperties the properties that should be set or removed
 * @param baseUrl             the base url of this server
 * @return an XML document that is the response
 * @throws FileSystemException if there is an error setting or removing a property
 *///from w  ww .j av  a 2s.  co  m
private Document getMultiStatusResponse(FileObject object, List<Element> requestedProperties, URL baseUrl)
        throws FileSystemException {
    Document propDoc = DocumentHelper.createDocument();
    propDoc.setXMLEncoding("UTF-8");

    Element multiStatus = propDoc.addElement(TAG_MULTISTATUS, NAMESPACE_DAV);
    Element responseEl = multiStatus.addElement(TAG_RESPONSE);
    try {
        URL url = new URL(baseUrl, URLEncoder.encode(object.getName().getPath(), "UTF-8"));
        responseEl.addElement(TAG_HREF).addText(url.toExternalForm());
    } catch (Exception e) {
        LOG.error("can't set HREF tag in response", e);
    }
    DavResource resource = DavResourceFactory.getInstance().getDavResource(object);
    resource.setPropertyValues(responseEl, requestedProperties);
    return propDoc;
}

From source file:dk.nsi.stamdata.replication.webservice.AtomFeedWriter.java

License:Mozilla Public License

public <T extends View> org.w3c.dom.Document write(Class<T> viewClass, List<T> records) throws IOException {
    checkNotNull(viewClass);/*  w ww . j a va  2  s . c om*/
    checkNotNull(records);

    String entityName = Views.getViewPath(viewClass);

    try {
        Document document = DocumentHelper.createDocument();
        document.setXMLEncoding("utf-8");

        // Start the feed.

        Element feed = document.addElement("atom:feed", ATOM_NS);

        // Get the namespace of the view class.

        String viewNS = viewClass.getPackage().getAnnotation(XmlSchema.class).namespace();
        Namespace namespace = new Namespace(null, viewNS);
        document.getRootElement().add(namespace);

        writeFeedMetadata(entityName, feed);

        // Write each record as an ATOM entry.

        Marshaller marshaller = viewXmlHelper.createMarshaller(viewClass);
        marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
        marshaller.setProperty(Marshaller.JAXB_ENCODING, STREAM_ENCODING);

        for (Object record : records) {
            View view = (View) record;
            writeEntry(feed, entityName, view, marshaller);
        }

        return convertToW3C(document);
    } catch (Exception e) {
        throw new IOException("Failed while writing ATOM feed.", e);
    }
}

From source file:dk.nsi.stamdata.replication.webservice.RecordXmlGenerator.java

License:Mozilla Public License

public org.w3c.dom.Document generateXml(List<RecordMetadata> records, String register, String datatype,
        DateTime updated) throws TransformerException {
    String stamdataNamespaceUri = STAMDATA_NAMESPACE_URI_PREFIX + register;

    Document document = DocumentHelper.createDocument();
    document.setXMLEncoding("utf-8");

    Element root = document.addElement("atom:feed", ATOM_NAMESPACE_URI);

    addElement(root, ATOM_NAMESPACE_URI, "atom:id",
            String.format("tag:nsi.dk,2011:%s/%s/v1", register, datatype));
    addElement(root, ATOM_NAMESPACE_URI, "atom:updated", AtomDate.toString(updated.toDate()));
    addElement(root, ATOM_NAMESPACE_URI, "atom:title", "Stamdata Registry Feed");
    Element author = addElement(root, ATOM_NAMESPACE_URI, "atom:author", null);
    addElement(author, ATOM_NAMESPACE_URI, "atom:name", "National Sundheds IT");

    for (RecordMetadata metadata : records) {
        Element entry = addElement(root, ATOM_NAMESPACE_URI, "atom:entry", null);
        String atomId = String.format("tag:nsi.dk,2011:%s/%s/v1/%d%07d", register, datatype,
                metadata.getModifiedDate().getMillis(), metadata.getPid());
        addElement(entry, ATOM_NAMESPACE_URI, "atom:id", atomId);
        addElement(entry, ATOM_NAMESPACE_URI, "atom:title", null);

        addElement(entry, ATOM_NAMESPACE_URI, "atom:updated",
                AtomDate.toString(metadata.getModifiedDate().toDate()));

        Element content = addElement(entry, ATOM_NAMESPACE_URI, "atom:content", null);
        content.addAttribute("type", "application/xml");

        Element recordElement = addElement(content, stamdataNamespaceUri, datatype, null);
        for (FieldSpecification fieldSpecification : recordSpecification.getFieldSpecs()) {
            addElement(recordElement, stamdataNamespaceUri, fieldSpecification.name,
                    valueAsString(metadata.getRecord(), fieldSpecification));
        }/*from  w  ww .jav  a  2s  .c om*/
        DateTimeFormatter formatter = ISODateTimeFormat.dateTime();
        addElement(recordElement, stamdataNamespaceUri, "ValidFrom",
                metadata.getValidFrom().toString(formatter));
        if (metadata.getValidTo() == null) {
            addElement(recordElement, stamdataNamespaceUri, "ValidTo", END_OF_TIME.toString(formatter));
        } else {
            addElement(recordElement, stamdataNamespaceUri, "ValidTo",
                    metadata.getValidTo().toString(formatter));
        }
        addElement(recordElement, stamdataNamespaceUri, "ModifiedDate",
                metadata.getModifiedDate().toString(formatter));
    }

    return convertToW3C(document);
}

From source file:org.collectionspace.chain.csp.persistence.services.vocab.VocabInstanceCache.java

License:Educational Community License

private Document createList(String namespace, String tag, String id, String vocab_type) throws ExistException {
    Document out = DocumentFactory.getInstance().createDocument();
    String[] path = tag.split("/");
    Element root = out.addElement("ns2:" + path[0], namespace);
    for (int i = 1; i < path.length; i++) {
        root = root.addElement(path[i]);
    }//  w  w w.j  av a 2  s  .c o m
    Element nametag = root.addElement("displayName");
    nametag.addText(vocabByShortIdentifier(id));

    Element sidtag = root.addElement("shortIdentifier");
    sidtag.addText(id);

    Element vocabtag = root.addElement("vocabType");
    vocabtag.addText(vocab_type);
    //   log.info(out.asXML());
    return out;
}

From source file:org.dentaku.gentaku.cartridge.event.WerkflowTag.java

License:Apache License

Document generateDocument(Object modelElement) throws JellyTagException {
    Document doc = DocumentHelper.createDocument();
    Element root = doc.addElement("processes", "werkflow:basic").addNamespace("j", "jelly:core")
            .addNamespace("jelly", "werkflow:jelly").addNamespace("java", "werkflow:java")
            .addNamespace("python", "werkflow:python").addNamespace("ognl", "werkflow:ognl");
    Element process = root.addElement("process");
    ActivityGraph ag = null;//from  w  w w.  ja v a 2  s.  c o  m
    try {
        ag = (ActivityGraph) findOneItem(((Namespace) modelElement), ActivityGraph.class);
    } catch (GenerationException e) {
        throw new JellyTagException(e);
    }
    Element seq = process.addElement("sequence");
    Pseudostate startState = findInitialState(((CompositeState) ag.getTop()).getSubvertex());
    if (startState != null) {
        Collection subvertex = ((CompositeState) ag.getTop()).getSubvertex();
        GraphProcessor gp = new GraphProcessor();
        JMIUMLIterator nav = new JMIUMLIterator();
        try {
            gp.validate(subvertex, nav);
        } catch (GraphException e) {
            throw new JellyTagException(e);
        }
        Collection topological = gp.getTopological();
        for (Iterator it = topological.iterator(); it.hasNext();) {
            StateVertex vertex = (StateVertex) it.next();
            if (vertex instanceof SimpleState) {
                String vertexClass = ((ModelElementImpl) modelElement).getFullyQualifiedName();
                seq.addElement("java:action").setText(vertexClass + "Workflow.getInstance()."
                        + ((SimpleState) vertex).getEntry().getName() + "();");
            }
        }
    }
    return doc;
}

From source file:org.intalio.tempo.workflow.fds.dispatches.NotifyDispatcher.java

License:Open Source License

public Document dispatchResponse(Document response) throws InvalidInputFormatException {
    XPath xpath = DocumentHelper.createXPath("/soapenv:Envelope/soapenv:Body/soapenv:Fault");
    xpath.setNamespaceURIs(MessageConstants.get_nsMap());
    List fault = xpath.selectNodes(response);
    if (fault.size() != 0) {
        // return fault as-is
        LOG.error("Fault response during notify:\n" + response.asXML());
        return response;
    }/*from   w  w  w .j  a  v a  2 s  .c o  m*/
    Document notifyResponse = DocumentHelper.createDocument();
    Element rootElement = notifyResponse.addElement("notifyResponse", userProcessNamespace);
    Element statusElement = rootElement.addElement("status", userProcessNamespace);
    statusElement.setText("OK");
    return notifyResponse;
}

From source file:org.intalio.tempo.workflow.fds.tools.SoapTools.java

License:Open Source License

public static Document wrapMessage(Document message) {
    Document document = DocumentHelper.createDocument();
    Element envelopeElement = document.addElement("Envelope", SOAP_NS);
    Element bodyElement = envelopeElement.addElement("Body", SOAP_NS);
    Element rootElementCopy = (Element) message.getRootElement().clone();

    bodyElement.add(rootElementCopy);//w  w w .  ja  v a2 s.  c o  m

    return document;
}

From source file:org.jboss.tools.jbpm.convert.bpmnto.util.DomXmlWriter.java

License:Open Source License

public static Document createDomTree(boolean useNamespace, String url, String rootElementName) {
    Document document = DocumentHelper.createDocument();
    Element root = null;/*  ww  w .ja v  a 2 s . c o m*/

    if (useNamespace) {
        Namespace jbpmNamespace = new Namespace(null, url);
        root = document.addElement(rootElementName, jbpmNamespace.getURI());
    } else {
        root = document.addElement(rootElementName);
    }
    root.addText(System.getProperty("line.separator"));

    return document;
}

From source file:org.jbpm.jpdl.xml.JpdlXmlWriter.java

License:Open Source License

private Document createDomTree(ProcessDefinition processDefinition) {
    Document document = DocumentHelper.createDocument();
    Element root = null;//  w ww.  j  av a 2  s. c o m

    if (useNamespace)
        root = document.addElement("process-definition", jbpmNamespace.getURI());
    else
        root = document.addElement("process-definition");
    addAttribute(root, "name", processDefinition.getName());

    // write the start-state
    if (processDefinition.getStartState() != null) {
        writeComment(root, "START-STATE");
        writeStartNode(root, (StartState) processDefinition.getStartState());
    }
    // write the nodeMap
    if ((processDefinition.getNodes() != null) && (processDefinition.getNodes().size() > 0)) {
        writeComment(root, "NODES");
        writeNodes(root, processDefinition.getNodes());
    }
    // write the process level actions
    if (processDefinition.hasEvents()) {
        writeComment(root, "PROCESS-EVENTS");
        writeEvents(root, processDefinition);
    }
    if (processDefinition.hasActions()) {
        writeComment(root, "ACTIONS");
        List namedProcessActions = getNamedProcessActions(processDefinition.getActions());
        writeActions(root, namedProcessActions);
    }

    root.addText(System.getProperty("line.separator"));

    return document;
}