Example usage for org.dom4j DocumentFactory getInstance

List of usage examples for org.dom4j DocumentFactory getInstance

Introduction

In this page you can find the example usage for org.dom4j DocumentFactory getInstance.

Prototype

public static synchronized DocumentFactory getInstance() 

Source Link

Document

Access to singleton implementation of DocumentFactory which is used if no DocumentFactory is specified when building using the standard builders.

Usage

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 .ja va  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.collectionspace.chain.csp.persistence.services.XmlJsonConversion.java

License:Educational Community License

public static Document getXMLRelationship(Element[] listItems) {
    Document doc = DocumentFactory.getInstance().createDocument();
    Element root = doc.addElement(new QName("relations-common-list",
            new Namespace("ns3", "http://collectionspace.org/services/relation")));
    root.addNamespace("ns2", "http://collectionspace.org/services/jaxb");
    //<ns3:relations-common-list xmlns:ns3="http://collectionspace.org/services/relation" xmlns:ns2="http://collectionspace.org/services/jaxb">

    if (listItems != null) {
        for (Element bitdoc : listItems) {
            root.add(bitdoc);/*  w ww. j a  v  a  2 s  .co  m*/
        }
    }

    return doc;

}

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

License:Educational Community License

public static Document convertToXml(Record r, JSONObject in, String section, String permtype,
        Boolean useInstance) throws JSONException, UnderlyingStorageException {
    if (!useInstance) {
        return convertToXml(r, in, section, permtype);
    }/*from  w  ww. j  ava2  s.co m*/

    Document doc = DocumentFactory.getInstance().createDocument();
    String[] parts = r.getServicesRecordPath(section).split(":", 2);
    if (useInstance) {
        parts = r.getServicesInstancesPath(section).split(":", 2);
    }
    String[] rootel = parts[1].split(",");
    Element root = doc.addElement(new QName(rootel[1], new Namespace("ns2", rootel[0])));

    Element element = root.addElement("displayName");
    element.addText(in.getString("displayName"));
    Element element2 = root.addElement("shortIdentifier");
    element2.addText(in.getString("shortIdentifier"));
    if (in.has("vocabType")) {
        Element element3 = root.addElement("vocabType");
        element3.addText(in.getString("vocabType"));
    }

    return doc;
    //yes I know hardcode is bad - but I need this out of the door today
    /*
    <ns2:personauthorities_common xmlns:ns2="http://collectionspace.org/services/person">
    <displayName>PAHMA Person Authority</displayName>
    <vocabType>PersonAuthority</vocabType>
    <shortIdentifier>pamha</shortIdentifier>
    </ns2:personauthorities_common>
     */

}

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

License:Educational Community License

public static Document convertToXml(Record r, JSONObject in, String section, String operation)
        throws JSONException, UnderlyingStorageException {
    Document doc = DocumentFactory.getInstance().createDocument();
    try {/*from   w  ww  .ja v  a2 s.c o m*/
        String path = r.getServicesRecordPath(section);
        if (path != null) {
            String[] parts = path.split(":", 2);
            String[] rootel = parts[1].split(",");
            Element root = doc.addElement(new QName(rootel[1], new Namespace("ns2", rootel[0])));
            if (r.getAllServiceFieldTopLevel(operation, section).length > 0) {
                for (FieldSet f : r.getAllServiceFieldTopLevel(operation, section)) {
                    addFieldSetToXml(root, f, in, section, operation);
                }
                return doc;
            }
        } else {
            // Revert to DEBUG after v4.0 testing
            log.warn(String.format("Record %s lacks expected section %s", r.getRecordName(), section));
        }
    } catch (Exception ex) {
        log.debug("Error in XmlJsonConversion.convertToXml", ex);
        throw ex;
    }
    return null;
}

From source file:org.collectionspace.chain.csp.webui.nuispec.ServicesXsd.java

License:Educational Community License

private String serviceschema(Storage s, String path) throws UIException {
    if (path != null) {
        section = path;//  w ww .java  2 s  .  com
    }

    Document doc = DocumentFactory.getInstance().createDocument();
    Namespace ns = new Namespace("xs", "http://www.w3.org/2001/XMLSchema");
    String[] parts = record.getServicesRecordPath(section).split(":", 2);
    String[] rootel = parts[1].split(",");
    Element root = doc.addElement(new QName("schema", new Namespace("xs", "http://www.w3.org/2001/XMLSchema")));
    root.addAttribute("xmlns:ns", rootel[0]);
    root.addAttribute("xmlns", rootel[0]);
    root.addAttribute("targetNamespace", rootel[0]);
    root.addAttribute("version", "0.1");

    //      Element ele = root.addElement(new QName("element", ns));
    //      ele.addAttribute("name", rootel[1]);
    //      Element cele = ele.addElement(new QName("complexType", ns));

    // add toplevel items

    for (FieldSet fs : record.getAllFieldTopLevel("")) {
        generateDataEntry(root, fs, ns, root, false);
    }

    generateSearchList(root, ns);
    // log.info(doc.asXML());
    // return doc.asXML();

    return doc.asXML();

}

From source file:org.dentaku.gentaku.tools.cgen.visitor.JellyTemplateGeneratingVisitor.java

License:Apache License

public void handleChoice(Element choice) throws VisitorException {
    Branch parent = getCurrentParent();//  www. j a  v  a 2  s .  co  m
    QName qName = DocumentFactory.getInstance().createQName("forEach", "j", "jelly:core");
    Branch current = parent.addElement(qName).addAttribute("var", "val").addAttribute("items", "${items}");
    pushParent(current);
    for (Iterator it = choice.elementIterator(); it.hasNext();) {
        visit((Element) it.next());
    }
    popParent();
}

From source file:org.dentaku.gentaku.tools.cgen.visitor.JellyTemplateGeneratingVisitor.java

License:Apache License

public void handleElement(Element element) throws VisitorException {
    if (element.getParent().getName().equals("schema")) {
        QName rootName = DocumentFactory.getInstance().createQName("jelly", "j", "jelly:core");
        Branch parent = DocumentHelper.createDocument().addElement(rootName).addNamespace("x", "jelly:xml");

        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding(getEncoding());
        format.setSuppressDeclaration(false);
        format.setExpandEmptyElements(false);

        pushParent(parent.addElement(element.getName()));
        for (Iterator it = element.elementIterator(); it.hasNext();) {
            visit((Element) it.next());
        }//from  w  ww.j  a  va 2s.  com
        popParent();

        try {
            String filename = element.getName() + ".jelly";

            Writer out = new FileWriter(new File(getRootDir(), filename));
            final XMLWriter xmlWriter = new XMLWriter(out, format);
            xmlWriter.setEscapeText(false);

            xmlWriter.write(parent);
            xmlWriter.flush();
            xmlWriter.close();
        } catch (Exception e) {
            throw new VisitorException("Exception occurred when running Jelly", e);
        }

        topLevelElements.put(element.getName(), element);

    } else {
        String refName = element.attributeValue("ref");
        if (refName != null) {
            Branch parent = getCurrentParent();
            // create an include
            QName qName = DocumentFactory.getInstance().createQName("import", "j", "jelly:core");
            parent.addElement(qName).addAttribute("uri", refName + ".jelly").addAttribute("inherit", "true");
        }
    }
}

From source file:org.dentaku.gentaku.tools.cgen.visitor.JellyTemplateGeneratingVisitor.java

License:Apache License

public Branch getCurrentParent() throws VisitorException {
    Branch parent = null;/* ww w .  j a v a2s .  c  om*/
    if (parentStack.size() == 0) {
        QName rootName = DocumentFactory.getInstance().createQName("jelly", "j", "jelly:core");
        parent = DocumentHelper.createDocument().addElement(rootName).addNamespace("x", "jelly:xml");
        parentStack.push(parent);
    } else {
        parent = (Branch) parentStack.peek();
    }
    return parent;
}

From source file:org.dtolabs.rundeck.resources.format.XmlGenerator.java

License:Apache License

public void generate(final RundeckNodesRep nodesRep, final OutputStream out) throws IOException {
    //create dom/*from   ww  w. j  a va  2s . c o m*/
    final Document document = DocumentFactory.getInstance().createDocument();
    final Element root = document.addElement("project");

    for (final Map<RundeckNodeMeta, String> map : nodesRep.getContent()) {

        final Element element = root.addElement("node");
        element.addAttribute("type", "Node");

        for (final RundeckNodeMeta s1 : map.keySet()) {
            element.addAttribute(attrMapping.get(s1), map.get(s1));
        }
    }
    writeDocument(document, out);
}

From source file:org.eclipse.ecr.core.io.impl.ExportedDocumentImpl.java

License:Open Source License

private void readDocument(DocumentModel doc, boolean inlineBlobs) throws IOException, ClientException {
    document = DocumentFactory.getInstance().createDocument();
    document.setName(doc.getName());/*from   w  w  w . ja v a 2 s . c o m*/
    Element rootElement = document.addElement(ExportConstants.DOCUMENT_TAG);
    rootElement.addAttribute(ExportConstants.REP_NAME, doc.getRepositoryName());
    rootElement.addAttribute(ExportConstants.ID_ATTR, doc.getRef().toString());
    Element systemElement = rootElement.addElement(ExportConstants.SYSTEM_TAG);
    systemElement.addElement(ExportConstants.TYPE_TAG).addText(doc.getType());
    systemElement.addElement(ExportConstants.PATH_TAG).addText(path.toString());
    // lifecycle
    try {
        String lifeCycleState = doc.getCurrentLifeCycleState();
        if (lifeCycleState != null && lifeCycleState.length() > 0) {
            systemElement.addElement(ExportConstants.LIFECYCLE_STATE_TAG).addText(lifeCycleState);
        }
        String lifeCyclePolicy = doc.getLifeCyclePolicy();
        if (lifeCyclePolicy != null && lifeCyclePolicy.length() > 0) {
            systemElement.addElement(ExportConstants.LIFECYCLE_POLICY_TAG).addText(lifeCyclePolicy);
        }
    } catch (Exception e) {
        log.error(e, e);
    } // end of lifecycle

    // write security
    Element acpElement = systemElement.addElement(ExportConstants.ACCESS_CONTROL_TAG);
    ACP acp = doc.getACP();
    if (acp != null) {
        readACP(acpElement, acp);
    }

    // write schemas
    SchemaManager schemaManager = Framework.getLocalService(SchemaManager.class);
    String[] schemaNames = doc.getSchemas();
    for (String schemaName : schemaNames) {
        Element schemaElement = rootElement.addElement(ExportConstants.SCHEMA_TAG).addAttribute("name",
                schemaName);
        Schema schema = schemaManager.getSchema(schemaName);
        Namespace targetNs = schema.getNamespace();
        // If namespace prefix is empty, use schema name
        if (StringUtils.isEmpty(targetNs.prefix)) {
            targetNs = new Namespace(targetNs.uri, schema.getName());
        }
        schemaElement.addNamespace(targetNs.prefix, targetNs.uri);
        DataModel dataModel = doc.getDataModel(schemaName);
        for (Field field : schema.getFields()) {
            Object value = dataModel.getData(field.getName().getLocalName());
            readProperty(schemaElement, targetNs, field, value, inlineBlobs);
        }
    }
}