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.nuxeo.ecm.platform.publisher.remoting.marshaling.DefaultMarshaler.java

License:Open Source License

protected String buildParameterEnvelope(int nbParams) {

    org.dom4j.Element rootElem = DocumentFactory.getInstance().createElement(rootParametersTag);
    rootElem.addNamespace(publisherSerializerNSPrefix, publisherSerializerNS);
    org.dom4j.Document rootDoc = DocumentFactory.getInstance().createDocument(rootElem);

    for (int i = 1; i <= nbParams; i++) {
        org.dom4j.Element pathElem = rootElem.addElement(parameterTag);
        pathElem.setText(PARAM_PATTERN + i + "$");
    }//w w w  .j  a v  a  2  s  .c o  m
    return rootDoc.asXML();
}

From source file:org.nuxeo.ecm.platform.publisher.remoting.marshaling.DefaultMarshaler.java

License:Open Source License

protected String buildResultEnvelope() {

    org.dom4j.Element rootElem = DocumentFactory.getInstance().createElement(rootResultTag);
    rootElem.addNamespace(publisherSerializerNSPrefix, publisherSerializerNS);
    org.dom4j.Document rootDoc = DocumentFactory.getInstance().createDocument(rootElem);
    rootElem.setText(RESULT_PATTERN);//from w w w  . j a v  a 2  s  .  c  o m
    return rootDoc.asXML();
}

From source file:org.nuxeo.ecm.platform.publisher.remoting.marshaling.DefaultPublicationNodeMarshaler.java

License:Open Source License

public String marshalPublicationNode(PublicationNode node) throws PublishingMarshalingException {

    org.dom4j.Element rootElem = DocumentFactory.getInstance().createElement(rootTag);
    rootElem.addNamespace(publisherSerializerNSPrefix, publisherSerializerNS);
    org.dom4j.Document rootDoc = DocumentFactory.getInstance().createDocument(rootElem);

    org.dom4j.Element pathElem = rootElem.addElement(nodePathTag);
    pathElem.setText(node.getPath());/*from w w  w .  j  a v  a2 s .c om*/

    org.dom4j.Element titleElem = rootElem.addElement(nodeTitleTag);
    try {
        titleElem.setText(node.getTitle());
    } catch (ClientException e) {
        throw new PublishingMarshalingException(e);
    }

    org.dom4j.Element typeElem = rootElem.addElement(nodeTypeTag);
    typeElem.setText(node.getNodeType());

    org.dom4j.Element treeElem = rootElem.addElement(treeNameTag);
    treeElem.setText(node.getTreeConfigName());

    org.dom4j.Element sidElem = rootElem.addElement(sidTag);
    if (node.getSessionId() != null) {
        sidElem.setText(node.getSessionId());
    } else {
        sidElem.setText("");
    }

    String data = rootDoc.asXML();

    return cleanUpXml(data);
}

From source file:org.nuxeo.ecm.platform.publisher.remoting.marshaling.DefaultPublishedDocumentMarshaler.java

License:Open Source License

public String marshalPublishedDocument(PublishedDocument pubDoc) {
    if (pubDoc == null)
        return "";

    org.dom4j.Element rootElem = DocumentFactory.getInstance().createElement(rootTag);
    rootElem.addNamespace(publisherSerializerNSPrefix, publisherSerializerNS);
    org.dom4j.Document rootDoc = DocumentFactory.getInstance().createDocument(rootElem);

    org.dom4j.Element sourceElem = rootElem.addElement(sourceRefTag);
    sourceElem.setText(pubDoc.getSourceDocumentRef().toString());

    org.dom4j.Element repoElem = rootElem.addElement(repositoryNameTag);
    repoElem.setText(pubDoc.getSourceRepositoryName());

    org.dom4j.Element srvElem = rootElem.addElement(serverNameTag);
    srvElem.setText(pubDoc.getSourceServer());

    org.dom4j.Element versionElem = rootElem.addElement(versionLabelTag);
    versionElem.setText("" + pubDoc.getSourceVersionLabel());

    org.dom4j.Element pathElem = rootElem.addElement(pathTag);
    pathElem.setText("" + pubDoc.getPath());

    org.dom4j.Element parentPathElem = rootElem.addElement(parentPathTag);
    parentPathElem.setText("" + pubDoc.getParentPath());

    org.dom4j.Element isPendingElem = rootElem.addElement(isPendingTag);
    isPendingElem.setText("" + pubDoc.isPending());

    String data = rootDoc.asXML();

    return cleanUpXml(data);

}

From source file:org.nuxeo.ecm.platform.syndication.serializer.SimpleXMLSerializer.java

License:Open Source License

public void serialize(ResultSummary summary, List<DashBoardItem> workItems, String columnsDefinition,
        List<String> labels, String lang, Response res, HttpServletRequest req) {
    if (workItems == null) {
        return;/*from w ww .  j ava 2  s .c  o  m*/
    }

    QName tasksTag = DocumentFactory.getInstance().createQName(rootTaskNodeName, taskNSPrefix, taskNS);
    QName taskTag = DocumentFactory.getInstance().createQName(taskNodeName, taskNSPrefix, taskNS);
    QName taskCategoryTag = DocumentFactory.getInstance().createQName(taskCategoryNodeName, taskNSPrefix,
            taskNS);

    org.dom4j.Element rootElem = DocumentFactory.getInstance().createElement(tasksTag);
    rootElem.addNamespace(taskNSPrefix, taskNS);
    org.dom4j.Document rootDoc = DocumentFactory.getInstance().createDocument(rootElem);

    Map<String, List<DashBoardItem>> sortedDashBoardItem = new HashMap<String, List<DashBoardItem>>();
    for (DashBoardItem item : workItems) {
        String category = item.getDirective();
        if (category == null) {
            category = "None";
        }

        if (!sortedDashBoardItem.containsKey(category)) {
            sortedDashBoardItem.put(category, new ArrayList<DashBoardItem>());
        }
        sortedDashBoardItem.get(category).add(item);
    }

    for (String category : sortedDashBoardItem.keySet()) {
        org.dom4j.Element categoryElem = rootElem.addElement(taskCategoryTag);
        categoryElem.addAttribute("category", category);

        for (DashBoardItem item : sortedDashBoardItem.get(category)) {
            org.dom4j.Element taskElem = categoryElem.addElement(taskTag);
            taskElem.addAttribute("name", item.getName());
            taskElem.addAttribute("directive", item.getDirective());
            taskElem.addAttribute("description", item.getDescription());
            taskElem.addAttribute("id", item.getId().toString());
            taskElem.addAttribute("link", item.getDocumentLink());
            if (item.getDueDate() != null) {
                taskElem.addAttribute("dueDate", DateFormat.getDateInstance().format(item.getDueDate()));
            }
            if (item.getStartDate() != null) {
                taskElem.addAttribute("startDate", DateFormat.getDateInstance().format(item.getStartDate()));
            }
            String currentLifeCycle = "";

            try {
                currentLifeCycle = item.getDocument().getCurrentLifeCycleState();
            } catch (ClientException e) {
                log.debug("No LifeCycle found");
            }

            taskElem.addAttribute("currentDocumentLifeCycle", currentLifeCycle);

            // not thread-safe so don't use a static instance
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if (item.getDueDate() != null) {
                taskElem.addAttribute("dueDate", dateFormat.format(item.getDueDate()));
            }
            if (item.getStartDate() != null) {
                taskElem.addAttribute("startDate", dateFormat.format(item.getStartDate()));
            }
            if (item.getComment() != null) {
                taskElem.setText(item.getComment());
            }
        }
    }
    QName translationTag = DocumentFactory.getInstance().createQName(translationNodeName, taskNSPrefix, taskNS);
    org.dom4j.Element translationElem = rootElem.addElement(translationTag);

    Map<String, String> translatedWords = getTranslationsForWorkflow("en");
    for (String key : translatedWords.keySet()) {
        translationElem.addAttribute(key, translatedWords.get(key));
    }

    res.setEntity(rootDoc.asXML(), MediaType.TEXT_XML);
    res.getEntity().setCharacterSet(CharacterSet.UTF_8);
}

From source file:org.nuxeo.ecm.platform.template.XMLSerializer.java

License:Open Source License

public static String serialize(List<TemplateInput> params) {

    Element root = DocumentFactory.getInstance().createElement(fieldsTag);

    for (TemplateInput input : params) {

        Element field = root.addElement(fieldTag);

        field.addAttribute("name", input.getName());

        InputType type = input.getType();
        if (type == null) {
            System.out.println(input.getName() + " is null");
        }/*  w  w w  . ja  v  a 2  s.c o  m*/
        field.addAttribute("type", type.getValue());

        if (input.isReadOnly()) {
            field.addAttribute("readonly", "true");
        }

        if (input.isAutoLoop()) {
            field.addAttribute("autoloop", "true");
        }

        if (InputType.StringValue.equals(type)) {
            field.addAttribute("value", input.getStringValue());
        } else if (InputType.DateValue.equals(type)) {
            field.addAttribute("value", dateFormat.format(input.getDateValue()));
        } else if (InputType.BooleanValue.equals(type)) {
            field.addAttribute("value", input.getBooleanValue().toString());
        } else {
            field.addAttribute("source", input.getSource());
        }

        if (input.getDesciption() != null) {
            field.setText(input.getDesciption());
        }
    }
    return root.asXML();
}

From source file:org.nuxeo.ecm.platform.webdav.helpers.DavResponseXMLHelper.java

License:Open Source License

public void initResponse() {
    if (root == null) {
        root = DocumentFactory.getInstance().createElement(multistatusTag);
        rootDoc = DocumentFactory.getInstance().createDocument(root);
    }//www .  j  av a 2  s. co  m
}

From source file:org.nuxeo.ecm.platform.webdav.helpers.DavResponseXMLHelper.java

License:Open Source License

public void addResourcePropertiesToResponse(String schemaURI, Map<String, String> props,
        Map<String, Element> propsNodes) {
    // get prefix for schemaURI
    String prefix = NameSpaceHelper.getNameSpacePrefix(schemaURI);

    // add NS alias
    Namespace ns = new Namespace(prefix, schemaURI);
    for (Element propsNode : propsNodes.values()) {
        propsNode.add(ns);//from   w w w  . ja v  a  2 s.  c o  m
    }

    // get prop node
    Element prop = propsNodes.get(String.valueOf(WebDavConst.SC_OK)).element(propTag);
    Element prop404 = propsNodes.get(String.valueOf(WebDavConst.SC_NOT_FOUND)).element(propTag);

    for (String propName : props.keySet()) {
        QName qn = DocumentFactory.getInstance().createQName(propName, prefix, schemaURI);

        String propValue = props.get(propName);
        if (propName.equals("supportedlock")) {
            // hard coded Lock config !!!

            Element tag = prop.addElement(qn);

            QName stqn = DocumentFactory.getInstance().createQName("lockentry", prefix, schemaURI);
            Element lentry = tag.addElement(stqn);

            QName stqn2 = DocumentFactory.getInstance().createQName("lockscope", prefix, schemaURI);
            Element lscope = lentry.addElement(stqn2);

            QName stqn3 = DocumentFactory.getInstance().createQName("exclusive", prefix, schemaURI);
            lscope.addElement(stqn3);

            QName stqn4 = DocumentFactory.getInstance().createQName("locktype", prefix, schemaURI);
            Element ltype = lentry.addElement(stqn4);

            QName stqn5 = DocumentFactory.getInstance().createQName("write", prefix, schemaURI);
            ltype.addElement(stqn5);

            continue;
        }

        if (propValue != null) {
            Element tag = prop.addElement(qn);
            if (propValue.startsWith(MappingHelper.PROP_AS_TAG_PREFIX)) {
                String subTagName = propValue.split(":")[1];
                QName stqn = DocumentFactory.getInstance().createQName(subTagName, prefix, schemaURI);
                tag.addElement(stqn);
            } else {
                // if (propName.equals(WebDavConst.DAV_PROP_DISPLAYNAME))
                if (propValue.contains("<") || propValue.contains(">")) {
                    tag.add(new DOMCDATA(propValue));
                } else {
                    tag.setText(propValue);
                }
            }
        } else {
            prop404.addElement(qn);
        }
    }
}

From source file:org.nuxeo.ecm.platform.webdav.helpers.DavResponseXMLHelper.java

License:Open Source License

public void addResourceMissingPropertiesToResponse(String schemaURI, Map<String, String> props,
        Element propsNode) {/*from   ww  w.j a  v a 2s  .c o m*/
    // get prefix for schemaURI
    String prefix = NameSpaceHelper.getNameSpacePrefix(schemaURI);

    // add NS alias
    Namespace ns = new Namespace(prefix, schemaURI);
    propsNode.add(ns);

    // get prop node
    Element prop = propsNode.element(propTag);

    for (String propName : props.keySet()) {
        QName qn = DocumentFactory.getInstance().createQName(propName, prefix, schemaURI);
        String propValue = props.get(propName);

        if (propValue == null) {
            Element tag = prop.addElement(qn);
        }
    }
}

From source file:org.nuxeo.ecm.webapp.liveedit.LiveEditBootstrapHelper.java

License:Apache License

/**
 * Creates the bootstrap file. It is called from the browser's addon. The URL composition tells the case and what to
 * create. The structure is depicted in the NXP-1881. Rux NXP-1959: add new tag on root level describing the action:
 * actionEdit, actionNew or actionFromTemplate.
 *
 * @return the bootstrap file content//w  ww . jav a 2s.c o  m
 */
public void getBootstrap() throws IOException {
    String currentRepoID = documentManager.getRepositoryName();

    CoreSession session = documentManager;
    CoreSession templateSession = documentManager;
    try {
        if (repoID != null && !currentRepoID.equals(repoID)) {
            session = CoreInstance.openCoreSession(repoID);
        }

        if (templateRepoID != null && !currentRepoID.equals(templateRepoID)) {
            templateSession = CoreInstance.openCoreSession(templateRepoID);
        }

        DocumentModel doc = null;
        DocumentModel templateDoc = null;
        String filename = null;
        if (ACTION_EDIT_DOCUMENT.equals(action)) {
            // fetch the document to edit to get its mimetype and document
            // type
            doc = session.getDocument(new IdRef(docRef));
            docType = doc.getType();
            Blob blob = null;
            if (blobPropertyName != null) {
                blob = (Blob) doc.getPropertyValue(blobPropertyName);
                if (blob == null) {
                    throw new NuxeoException(
                            String.format("could not find blob to edit with property '%s'", blobPropertyName));
                }
            } else {
                blob = (Blob) doc.getProperty(schema, blobField);
                if (blob == null) {
                    throw new NuxeoException(String.format(
                            "could not find blob to edit with schema '%s' and field '%s'", schema, blobField));
                }
            }
            mimetype = blob.getMimeType();
            if (filenamePropertyName != null) {
                filename = (String) doc.getPropertyValue(filenamePropertyName);
            } else {
                filename = (String) doc.getProperty(schema, filenameField);
            }
        } else if (ACTION_CREATE_DOCUMENT.equals(action)) {
            // creating a new document all parameters are read from the
            // request parameters
        } else if (ACTION_CREATE_DOCUMENT_FROM_TEMPLATE.equals(action)) {
            // fetch the template blob to get its mimetype
            templateDoc = templateSession.getDocument(new IdRef(templateDocRef));
            Blob blob = (Blob) templateDoc.getProperty(templateSchema, templateBlobField);
            if (blob == null) {
                throw new NuxeoException(
                        String.format("could not find template blob with schema '%s' and field '%s'",
                                templateSchema, templateBlobField));
            }
            mimetype = blob.getMimeType();
            // leave docType from the request query parameter
        } else {
            throw new NuxeoException(String.format(
                    "action '%s' is not a valid LiveEdit action: should be one of '%s', '%s' or '%s'", action,
                    ACTION_CREATE_DOCUMENT, ACTION_CREATE_DOCUMENT_FROM_TEMPLATE, ACTION_EDIT_DOCUMENT));
        }

        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
        HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();

        Element root = DocumentFactory.getInstance().createElement(liveEditTag);
        root.addNamespace("", XML_LE_NAMESPACE);
        // RUX NXP-1959: action id
        Element actionInfo = root.addElement(actionSelectorTag);
        actionInfo.setText(action);

        // Document related informations
        Element docInfo = root.addElement(documentTag);
        addTextElement(docInfo, docRefTag, docRef);
        Element docPathT = docInfo.addElement(docPathTag);
        Element docTitleT = docInfo.addElement(docTitleTag);
        if (doc != null) {
            docPathT.setText(doc.getPathAsString());
            docTitleT.setText(doc.getTitle());
        }
        addTextElement(docInfo, docRepositoryTag, repoID);

        addTextElement(docInfo, docSchemaNameTag, schema);
        addTextElement(docInfo, docFieldNameTag, blobField);
        addTextElement(docInfo, docBlobFieldNameTag, blobField);
        Element docFieldPathT = docInfo.addElement(docfieldPathTag);
        Element docBlobFieldPathT = docInfo.addElement(docBlobFieldPathTag);
        if (blobPropertyName != null) {
            // FIXME AT: NXP-2306: send blobPropertyName correctly (?)
            docFieldPathT.setText(blobPropertyName);
            docBlobFieldPathT.setText(blobPropertyName);
        } else {
            if (schema != null && blobField != null) {
                docFieldPathT.setText(schema + ':' + blobField);
                docBlobFieldPathT.setText(schema + ':' + blobField);
            }
        }
        addTextElement(docInfo, docFilenameFieldNameTag, filenameField);
        Element docFilenameFieldPathT = docInfo.addElement(docFilenameFieldPathTag);
        if (filenamePropertyName != null) {
            docFilenameFieldPathT.setText(filenamePropertyName);
        } else {
            if (schema != null && blobField != null) {
                docFilenameFieldPathT.setText(schema + ':' + filenameField);
            }
        }

        addTextElement(docInfo, docfileNameTag, filename);
        addTextElement(docInfo, docTypeTag, docType);
        addTextElement(docInfo, docMimetypeTag, mimetype);
        addTextElement(docInfo, docFileExtensionTag, getFileExtension(mimetype));

        Element docFileAuthorizedExtensions = docInfo.addElement(docFileAuthorizedExtensionsTag);
        List<String> authorizedExtensions = getFileExtensions(mimetype);
        if (authorizedExtensions != null) {
            for (String extension : authorizedExtensions) {
                addTextElement(docFileAuthorizedExtensions, docFileAuthorizedExtensionTag, extension);
            }
        }

        Element docIsVersionT = docInfo.addElement(docIsVersionTag);
        Element docIsLockedT = docInfo.addElement(docIsLockedTag);
        if (ACTION_EDIT_DOCUMENT.equals(action)) {
            docIsVersionT.setText(Boolean.toString(doc.isVersion()));
            docIsLockedT.setText(Boolean.toString(doc.isLocked()));
        }

        // template information for ACTION_CREATE_DOCUMENT_FROM_TEMPLATE

        Element templateDocInfo = root.addElement(templateDocumentTag);
        addTextElement(templateDocInfo, docRefTag, templateDocRef);
        docPathT = templateDocInfo.addElement(docPathTag);
        docTitleT = templateDocInfo.addElement(docTitleTag);
        if (templateDoc != null) {
            docPathT.setText(templateDoc.getPathAsString());
            docTitleT.setText(templateDoc.getTitle());
        }
        addTextElement(templateDocInfo, docRepositoryTag, templateRepoID);
        addTextElement(templateDocInfo, docSchemaNameTag, templateSchema);
        addTextElement(templateDocInfo, docFieldNameTag, templateBlobField);
        addTextElement(templateDocInfo, docBlobFieldNameTag, templateBlobField);
        docFieldPathT = templateDocInfo.addElement(docfieldPathTag);
        docBlobFieldPathT = templateDocInfo.addElement(docBlobFieldPathTag);
        if (templateSchema != null && templateBlobField != null) {
            docFieldPathT.setText(templateSchema + ":" + templateBlobField);
            docBlobFieldPathT.setText(templateSchema + ":" + templateBlobField);
        }
        addTextElement(templateDocInfo, docMimetypeTag, mimetype);
        addTextElement(templateDocInfo, docFileExtensionTag, getFileExtension(mimetype));

        Element templateFileAuthorizedExtensions = templateDocInfo.addElement(docFileAuthorizedExtensionsTag);
        if (authorizedExtensions != null) {
            for (String extension : authorizedExtensions) {
                addTextElement(templateFileAuthorizedExtensions, docFileAuthorizedExtensionTag, extension);
            }
        }

        // Browser request related informations
        Element requestInfo = root.addElement(requestInfoTag);
        Cookie[] cookies = request.getCookies();
        Element cookiesT = requestInfo.addElement(requestCookiesTag);
        for (Cookie cookie : cookies) {
            Element cookieT = cookiesT.addElement(requestCookieTag);
            cookieT.addAttribute("name", cookie.getName());
            cookieT.setText(cookie.getValue());
        }
        Element headersT = requestInfo.addElement(requestHeadersTag);
        Enumeration hEnum = request.getHeaderNames();
        while (hEnum.hasMoreElements()) {
            String hName = (String) hEnum.nextElement();
            if (!hName.equalsIgnoreCase("cookie")) {
                Element headerT = headersT.addElement(requestHeaderTag);
                headerT.addAttribute("name", hName);
                headerT.setText(request.getHeader(hName));
            }
        }
        addTextElement(requestInfo, requestBaseURLTag, BaseURL.getBaseURL(request));

        // User related informations
        String username = context.getExternalContext().getUserPrincipal().getName();
        Element userInfo = root.addElement(userInfoTag);
        addTextElement(userInfo, userNameTag, username);
        addTextElement(userInfo, userPasswordTag, "");
        addTextElement(userInfo, userTokenTag, "");
        addTextElement(userInfo, userLocaleTag, context.getViewRoot().getLocale().toString());
        // Rux NXP-1882: the wsdl locations
        String baseUrl = BaseURL.getBaseURL(request);
        Element wsdlLocations = root.addElement(wsdlLocationsTag);
        Element wsdlAccessWST = wsdlLocations.addElement(wsdlAccessWebServiceTag);
        wsdlAccessWST.setText(baseUrl + "webservices/nuxeoAccess?wsdl");
        Element wsdlEEWST = wsdlLocations.addElement(wsdlLEWebServiceTag);
        wsdlEEWST.setText(baseUrl + "webservices/nuxeoLEWS?wsdl");

        // Server related informations
        Element serverInfo = root.addElement(serverInfoTag);
        Element serverVersionT = serverInfo.addElement(serverVersionTag);
        serverVersionT.setText("5.1"); // TODO: use a buildtime generated
        // version tag instead

        // Client related informations
        Element editId = root.addElement(editIdTag);
        editId.setText(getEditId(doc, session, username));

        // serialize bootstrap XML document in the response
        Document xmlDoc = DocumentFactory.getInstance().createDocument();
        xmlDoc.setRootElement(root);
        response.setContentType("text/xml; charset=UTF-8");

        // use a formatter to make it easier to debug live edit client
        // implementations
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding("UTF-8");
        XMLWriter writer = new XMLWriter(response.getOutputStream(), format);
        writer.write(xmlDoc);

        response.flushBuffer();
        context.responseComplete();
    } finally {
        if (session != null && session != documentManager) {
            session.close();
        }
        if (templateSession != null && templateSession != documentManager) {
            templateSession.close();
        }
    }
}