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:ch.epfl.codimsd.qep.QEPFactory.java

License:Open Source License

/**
 * Creates the xml structure where we embed the operators.
 * /*from   www  .  j ava2  s  .c  om*/
 * @param type type of the xml template.
 * @return dom4j Document.
 */
private static Document createTemplate(String type) {

    // Create empty dom4j document.
    Document document = DocumentHelper.createDocument();

    // Add "QEPTemlate", "op", "qep",  tags.
    Element root = document.addElement("QEPTemplate", "http://giga03.lncc.br/DIP/WP4/CoDIMS-D");
    root.addNamespace("op", "http://giga03.lncc.br/DIP/WP4/CoDIMS-D/Operator");
    root.addNamespace("qep", "http://giga03.lncc.br/DIP/WP4/CoDIMS-D/QEP");
    root.addElement("qep:QEP").addAttribute("type", type);

    return document;
}

From source file:com.alibaba.citrus.springext.support.SchemaUtil.java

License:Open Source License

/** contributionsschema?schema */
public static Document createConfigurationPointSchema(ConfigurationPoint configurationPoint, String version) {
    Document doc = createDocument();

    // <xsd:schema>
    Element schemaRoot = doc.addElement("xsd:schema", W3C_XML_SCHEMA_NS_URI);

    schemaRoot.addNamespace("xsd", W3C_XML_SCHEMA_NS_URI);
    schemaRoot.addNamespace("beans", BEANS_NAMESPACE_URI);
    schemaRoot.addNamespace("springext", SPRINGEXT_BASE_URI);
    schemaRoot.addNamespace("", configurationPoint.getNamespaceUri());

    schemaRoot.addAttribute("targetNamespace", configurationPoint.getNamespaceUri());

    // <xsd:include schemaLocation="contribution schema" />
    Set<String> includings = createTreeSet();

    for (Contribution contrib : configurationPoint.getContributions()) {
        Schema contribSchema = contrib.getSchemas().getVersionedSchema(version);

        if (contribSchema == null) {
            contribSchema = contrib.getSchemas().getMainSchema();
        }// www .  j ava2  s.c om

        if (contribSchema != null) {
            includings.add(contribSchema.getName());
        }
    }

    for (String including : includings) {
        Element includeElement = schemaRoot.addElement("xsd:include");

        includeElement.addAttribute("schemaLocation", including);
    }

    if (configurationPoint.getDefaultElementName() != null) {
        //  <xsd:import namespace="http://www.springframework.org/schema/beans"
        //              schemaLocation="http://www.springframework.org/schema/beans/spring-beans.xsd" />
        Element importBeans = schemaRoot.addElement("xsd:import");
        importBeans.addAttribute("namespace", BEANS_NAMESPACE_URI);
        importBeans.addAttribute("schemaLocation", BEANS_NAMESPACE_URI + "/spring-beans.xsd");

        //  <xsd:import namespace="http://www.alibaba.com/schema/springext/base"
        //              schemaLocation="http://www.alibaba.com/schema/springext/springext-base.xsd" />
        Element importSpringextBase = schemaRoot.addElement("xsd:import");
        importSpringextBase.addAttribute("namespace", SPRINGEXT_BASE_URI);
        importSpringextBase.addAttribute("schemaLocation", SPRINGEXT_BASE_XSD);

        // <xsd:element name="defaultElementName" type="springext:referenceableBeanType" />
        Element element = schemaRoot.addElement("xsd:element");
        element.addAttribute("name", configurationPoint.getDefaultElementName());
        element.addAttribute("type", "springext:referenceableBeanType");
    }

    return doc;
}

From source file:com.collabnet.ccf.core.ga.GenericArtifactHelper.java

License:Open Source License

/**
 * Adds the root-element to the XML document
 * /*from  w  w w .  j  a v a 2  s.  co m*/
 * @param document
 *            XML document
 * @param rootElementName
 *            name of root-element
 * @param rootElementNamespace
 *            name namespace, root-element belongs to
 * @return newly create root-element of XML document
 */
private static Element addRootElement(Document document, String rootElementName, String rootElementNamespace) {
    return document.addElement(rootElementName, rootElementNamespace);
}

From source file:com.haulmont.cuba.core.sys.persistence.MappingFileCreator.java

License:Apache License

private Document createDocument(Map<Class<?>, List<Attr>> mappings) {
    Document doc = DocumentHelper.createDocument();
    Element rootEl = doc.addElement("entity-mappings", XMLNS);
    Namespace xsi = new Namespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
    rootEl.add(xsi);/*from w w  w  . jav  a  2 s  . co m*/
    rootEl.addAttribute(new QName("schemaLocation", xsi), SCHEMA_LOCATION);
    rootEl.addAttribute("version", PERSISTENCE_VER);

    for (Map.Entry<Class<?>, List<Attr>> entry : mappings.entrySet()) {
        if (entry.getKey().getAnnotation(MappedSuperclass.class) != null) {
            Element entityEl = rootEl.addElement("mapped-superclass", XMLNS);
            entityEl.addAttribute("class", entry.getKey().getName());
            createAttributes(entry, entityEl);
        }
    }
    for (Map.Entry<Class<?>, List<Attr>> entry : mappings.entrySet()) {
        if (entry.getKey().getAnnotation(Entity.class) != null) {
            Element entityEl = rootEl.addElement("entity", XMLNS);
            entityEl.addAttribute("class", entry.getKey().getName());
            entityEl.addAttribute("name", entry.getKey().getAnnotation(Entity.class).name());
            createAttributes(entry, entityEl);
        }
    }
    for (Map.Entry<Class<?>, List<Attr>> entry : mappings.entrySet()) {
        if (entry.getKey().getAnnotation(Embeddable.class) != null) {
            Element entityEl = rootEl.addElement("embeddable", XMLNS);
            entityEl.addAttribute("class", entry.getKey().getName());
            createAttributes(entry, entityEl);
        }
    }

    return doc;
}

From source file:com.petpet.c3po.analysis.ProfileGenerator.java

License:Apache License

private Element createRootElement(final Document doc, final String collection, final long count) {
    final Element profile = doc.addElement("profile", "http://ifs.tuwien.ac.at/dp/c3po")
            .addAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance")
            .addAttribute("collection", collection).addAttribute("date", new Date() + "")
            .addAttribute("count", count + "");

    return profile;
}

From source file:com.thinkberg.moxo.dav.LockHandler.java

License:Apache License

private void sendLockAcquiredResponse(HttpServletResponse response, Lock lock) throws IOException {
    response.setContentType("text/xml");
    response.setCharacterEncoding("UTF-8");
    response.setHeader(HEADER_LOCK_TOKEN, "<" + lock.getToken() + ">");

    Document propDoc = DocumentHelper.createDocument();
    Element propEl = propDoc.addElement(TAG_PROP, "DAV:");
    Element lockdiscoveryEl = propEl.addElement(TAG_LOCKDISCOVERY);

    lock.serializeToXml(lockdiscoveryEl);

    XMLWriter xmlWriter = new XMLWriter(response.getWriter());
    xmlWriter.write(propDoc);/*from   ww  w . j  av a2 s  .com*/
    log(propDoc);
}

From source file:com.thinkberg.moxo.dav.PropFindHandler.java

License:Apache License

private Document getMultiStatusRespons(FileObject object, List<String> requestedProperties, URL baseUrl,
        int depth, boolean ignoreValues) throws FileSystemException {
    Document propDoc = DocumentHelper.createDocument();
    propDoc.setXMLEncoding("UTF-8");

    Element multiStatus = propDoc.addElement(TAG_MULTISTATUS, "DAV:");
    FileObject[] children = object.findFiles(new DepthFileSelector(depth));
    for (FileObject child : children) {
        Element responseEl = multiStatus.addElement(TAG_RESPONSE);
        try {//ww w  .ja  va 2 s  . co m
            URL url = new URL(baseUrl, URLEncoder.encode(child.getName().getPath(), "UTF-8"));
            log("!! " + url);
            responseEl.addElement(TAG_HREF).addText(url.toExternalForm());
        } catch (Exception e) {
            e.printStackTrace();
        }
        DavResource resource = DavResourceFactory.getInstance().getDavResource(child);
        resource.setIgnoreValues(ignoreValues);
        resource.serializeToXml(responseEl, requestedProperties);
    }
    return propDoc;
}

From source file:com.thinkberg.moxo.dav.PropPatchHandler.java

License:Apache License

public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
    FileObject object = getResourceManager().getFileObject(request.getPathInfo());

    try {/*from   ww w.  j av  a  2s  .  c  o  m*/
        LockManager.getInstance().checkCondition(object, getIf(request));
    } catch (LockException e) {
        if (e.getLocks() != null) {
            response.sendError(SC_LOCKED);
        } else {
            response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED);
        }
        return;
    }

    SAXReader saxReader = new SAXReader();
    try {
        Document propDoc = saxReader.read(request.getInputStream());
        //      log(propDoc);

        response.setContentType("text/xml");
        response.setCharacterEncoding("UTF-8");
        response.setStatus(SC_MULTI_STATUS);

        if (object.exists()) {
            Document resultDoc = DocumentHelper.createDocument();
            Element multiStatusResponse = resultDoc.addElement("multistatus", "DAV:");
            Element responseEl = multiStatusResponse.addElement("response");
            try {
                URL url = new URL(getBaseUrl(request), URLEncoder.encode(object.getName().getPath(), "UTF-8"));
                log("!! " + url);
                responseEl.addElement("href").addText(url.toExternalForm());
            } catch (Exception e) {
                e.printStackTrace();
            }

            Element propstatEl = responseEl.addElement("propstat");
            Element propEl = propstatEl.addElement("prop");

            Element propertyUpdateEl = propDoc.getRootElement();
            for (Object elObject : propertyUpdateEl.elements()) {
                Element el = (Element) elObject;
                if ("set".equals(el.getName())) {
                    for (Object propObject : el.elements()) {
                        setProperty(propEl, object, (Element) propObject);
                    }
                } else if ("remove".equals(el.getName())) {
                    for (Object propObject : el.elements()) {
                        removeProperty(propEl, object, (Element) propObject);
                    }
                }
            }
            propstatEl.addElement("status").addText(DavResource.STATUS_403);

            //        log(resultDoc);

            // write the actual response
            XMLWriter writer = new XMLWriter(response.getWriter(), OutputFormat.createCompactFormat());
            writer.write(resultDoc);
            writer.flush();
            writer.close();
        } else {
            log("!! " + object.getName().getPath() + " NOT FOUND");
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
        }
    } catch (DocumentException e) {
        log("!! inavlid request: " + e.getMessage());
        response.sendError(HttpServletResponse.SC_BAD_REQUEST);
    }
}

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

License:Apache License

private void sendLockAcquiredResponse(HttpServletResponse response, Lock lock) throws IOException {
    if (!lock.getObject().exists()) {
        response.setStatus(SC_CREATED);/* w w  w.jav a 2 s. c  o  m*/
    }
    response.setContentType("text/xml");
    response.setCharacterEncoding("UTF-8");
    response.setHeader(HEADER_LOCK_TOKEN, "<" + lock.getToken() + ">");

    Document propDoc = DocumentHelper.createDocument();
    Element propEl = propDoc.addElement(TAG_PROP, "DAV:");
    Element lockdiscoveryEl = propEl.addElement(TAG_LOCKDISCOVERY);

    lock.serializeToXml(lockdiscoveryEl);

    XMLWriter xmlWriter = new XMLWriter(response.getWriter());
    xmlWriter.write(propDoc);

    logXml(propDoc);
}

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

License:Apache License

/**
 * Create a multistatus response by requesting all properties and writing a response for each
 * the found and the non-found properties
 *
 * @param object  the context object the propfind request applies to
 * @param propEl  the &lt;prop&gt; element containing the actual properties
 * @param baseUrl the base url of this server
 * @param depth   a depth argument for how deep the find will go
 * @return an XML document that is the response
 * @throws FileSystemException if there was an error executing the propfind request
 *///from ww w.  j a  va 2s .c  o  m
private Document getMultiStatusResponse(FileObject object, Element propEl, URL baseUrl, int depth)
        throws FileSystemException {
    Document propDoc = DocumentHelper.createDocument();
    propDoc.setXMLEncoding("UTF-8");

    Element multiStatus = propDoc.addElement(TAG_MULTISTATUS, NAMESPACE_DAV);
    FileObject[] children = object.findFiles(new DepthFileSelector(depth));
    for (FileObject child : children) {
        Element responseEl = multiStatus.addElement(TAG_RESPONSE);
        try {
            URL url = new URL(baseUrl, URLEncoder.encode(child.getName().getPath(), "UTF-8"));
            responseEl.addElement(TAG_HREF).addText(url.toExternalForm());
        } catch (Exception e) {
            LOG.error("can't set href in response", e);
        }
        DavResource resource = DavResourceFactory.getInstance().getDavResource(child);
        resource.getPropertyValues(responseEl, propEl);
    }
    return propDoc;
}