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:com.eurelis.opencms.admin.xmltransformation.ui.CmsXmlTransformationDialog.java

License:Open Source License

public List<String> processRequest(CmsXmlTransformation cmsXmlTransformation) {
    CmsJspActionElement jsp = this.getJsp();
    CmsObject cmsObject = this.getCms();

    String uri = CmsEncoder.decode(jsp.getRequest().getParameter(CmsDialog.PARAM_RESOURCE));

    boolean contentTypeValid = false;

    List<String> errorList = new ArrayList<String>();
    XMLTransformationBuilder xmlTransformationBuilder = XMLTransformationBuilder.newInstance();

    try {// w  w  w. j a  va2s.  c o m
        CmsFile cmsFile = cmsObject.readFile(uri);

        CmsXmlContent xmlTransformationContent = CmsXmlContentFactory.unmarshal(cmsObject, cmsFile);
        List<Locale> localeList = xmlTransformationContent.getLocales();

        String contentType = null;

        if (localeList.size() == 1) {
            Locale locale = localeList.get(0);

            I_CmsXmlContentValue fileTypeContentValue = xmlTransformationContent.getValue("ContentType",
                    locale);
            contentType = fileTypeContentValue.getStringValue(cmsObject);

            I_CmsResourceType resourceType = OpenCms.getResourceManager().getResourceType(contentType);

            if (resourceType != null) {
                contentTypeValid = true;
                cmsXmlTransformation.setContentType(resourceType.getTypeId());
            }

            if (!contentTypeValid) {
                errorList.add(String.format(
                        Messages.getLbl(Messages.GUI_XMLTRANSFORMATION_BADCONTENTTYPE_FORMAT_0, getLocale()),
                        contentType));
                cmsXmlTransformation.setContentType(-1);
            }

            List<I_CmsXmlContentValue> unitaryTransformationList = xmlTransformationContent
                    .getValues("UnitaryTransformation", locale);

            int unitaryTransformationIndex = 0;
            for (I_CmsXmlContentValue unitaryTransformation : unitaryTransformationList) {
                unitaryTransformationIndex++; // indice a 1

                UnitaryTransformationBuilder unitaryTransformationBuilder = xmlTransformationBuilder
                        .newUnitaryTransformationBuilder();

                boolean validSource = false;

                String sourcePath = String.format("UnitaryTransformation[%d]/Source",
                        unitaryTransformationIndex);
                String destinationXPath = String.format("UnitaryTransformation[%d]/Destination/XPath",
                        unitaryTransformationIndex);
                String destinationSXPath = String.format("UnitaryTransformation[%d]/Destination/SXPath",
                        unitaryTransformationIndex);
                String destinationPosition = String.format("UnitaryTransformation[%d]/Destination/Position",
                        unitaryTransformationIndex);
                String templatePath = String.format("UnitaryTransformation[%d]/Template",
                        unitaryTransformationIndex);

                String sourceContentString = null;

                I_CmsXmlContentValue sourceContentValue = xmlTransformationContent.getValue(sourcePath, locale);

                if (sourceContentValue != null) {
                    sourceContentString = sourceContentValue.getStringValue(cmsObject);

                    if (sourceContentString != null && !sourceContentString.isEmpty()) {
                        try {
                            XPath sourceXPath = DocumentFactory.getInstance().createXPath(sourceContentString);

                            unitaryTransformationBuilder.setSource(sourceXPath);

                            validSource = true;

                        } catch (InvalidXPathException e) {

                        }

                    }

                }

                if (!validSource) {
                    errorList.add(String.format(
                            Messages.getLbl(Messages.GUI_UNITARYTRANSFORMATION_BADSOURCE_FORMAT_0, getLocale()),
                            sourceContentString, sourcePath));
                }

                // destination
                I_CmsXmlContentValue xPathContentValue = xmlTransformationContent.getValue(destinationXPath,
                        locale);
                I_CmsXmlContentValue sxPathContentValue = xmlTransformationContent.getValue(destinationSXPath,
                        locale);
                I_CmsXmlContentValue positionContentValue = xmlTransformationContent
                        .getValue(destinationPosition, locale);

                if (xPathContentValue != null && sxPathContentValue != null) {
                    String destinationXPathString = xPathContentValue.getStringValue(cmsObject);
                    String sxPathString = sxPathContentValue.getStringValue(cmsObject);

                    String position = positionContentValue.getStringValue(cmsObject);

                    Position pos = Position.LAST;

                    if (position != null) {
                        if ("FIRST".equalsIgnoreCase(position)) {
                            pos = Position.FIRST;
                        } else if ("LAST".equalsIgnoreCase(position)) {
                            pos = Position.LAST;
                        } else if ("BEFORE".equalsIgnoreCase(position)) {
                            pos = Position.BEFORE;
                        } else if ("AFTER".equalsIgnoreCase(position)) {
                            pos = Position.AFTER;
                        }
                    }

                    String xPathTestString = null;

                    if (destinationXPathString.charAt(destinationXPathString.length() - 1) == '/') {
                        xPathTestString = destinationXPathString.substring(0,
                                destinationXPathString.length() - 1);
                    } else {
                        xPathTestString = destinationXPathString;
                    }

                    boolean isSimplified = Boolean.valueOf(sxPathString);
                    boolean isValidPosition = true;

                    if (isSimplified) {
                        if (pos == Position.BEFORE || pos == Position.AFTER) {
                            isValidPosition = false;
                        }
                    }

                    if (!isValidPosition) {
                        errorList.add(String.format(
                                Messages.getLbl(Messages.GUI_UNITARYTRANSFORMATION_BADSXPATH_POSITION_FORMAT_0,
                                        getLocale()),
                                position, destinationPosition));
                    }

                    boolean validXPath = false;

                    try {
                        DocumentFactory.getInstance().createXPath(xPathTestString);
                        validXPath = true;
                    } catch (InvalidXPathException e) {

                    }

                    if (validXPath && isSimplified) {

                        try {
                            XPATHParser.getInstance().parseFailOverPath(destinationXPathString, pos);
                        } catch (java.lang.IllegalArgumentException e) {
                            validXPath = false;
                        }

                    }

                    if (!validXPath) {
                        errorList.add(String.format(
                                Messages.getLbl(Messages.GUI_UNITARYTRANSFORMATION_BADDESTINATION_FORMAT_0,
                                        getLocale()),
                                isSimplified ? 1 : 0, destinationXPathString, destinationXPath));

                    } else {
                        if (isSimplified) {
                            unitaryTransformationBuilder.setSXPathDestination(destinationXPathString, pos);
                        } else {
                            unitaryTransformationBuilder.setXPathDestination(destinationXPathString, pos);
                        }

                    }

                }

                // template
                I_CmsXmlContentValue templateContentValue = xmlTransformationContent.getValue(templatePath,
                        locale);
                if (templateContentValue != null) {
                    Element templateElement = templateContentValue.getElement();

                    boolean isValidTemplateString = false;
                    String templateString = templateElement.selectSingleNode("Template").getText().trim();

                    TemplateTransformationBuilder templateTransformationBuilder = unitaryTransformationBuilder
                            .getTemplateTransformationBuilder();

                    try {
                        templateTransformationBuilder.setTemplate(templateString);
                        isValidTemplateString = true;
                    } catch (DocumentException e) {

                    }

                    if (!isValidTemplateString) {
                        errorList.add(String.format(Messages
                                .getLbl(Messages.GUI_TEMPLATETRANSFORMATION_BADTEMPLATE_FORMAT_0, getLocale()),
                                templateString, templatePath));
                    }

                    int templateParameterIndex = 0;
                    @SuppressWarnings("unchecked")
                    List<Element> templateParameterList = templateElement.selectNodes("Parameters");

                    for (Element templateParameterElement : templateParameterList) {
                        templateParameterIndex++;
                        String templateParameterPath = String.format(
                                "UnitaryTransformation[%d]/Template/Parameters[%d]/",
                                unitaryTransformationIndex, templateParameterIndex);

                        String templateParameterName = templateParameterElement
                                .elementTextTrim("ParameterName");
                        String templateParameterXPathValue = templateParameterElement
                                .elementTextTrim("ParameterXPathValue");

                        boolean templateParameterIsValid = false;

                        if (!templateParameterName.isEmpty() && !templateParameterXPathValue.isEmpty()) {
                            try {
                                templateTransformationBuilder.addParameter(templateParameterName,
                                        DocumentFactory.getInstance().createXPath(templateParameterXPathValue));
                                templateParameterIsValid = true;
                            } catch (InvalidXPathException e) {

                            }

                        }

                        if (!templateParameterIsValid) {
                            errorList.add(String.format(
                                    Messages.getLbl(Messages.GUI_TEMPLATETRANSFORMATION_BADPARAMETER_FORMAT_0,
                                            getLocale()),
                                    templateParameterName, templateParameterXPathValue, templateParameterPath));
                        }

                    }

                }

            }

        }

    } catch (CmsException e) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("processRequest " + e);
        }
        LOG.error("processRequest " + e.getMessage());
        e.printStackTrace();
    }

    if (errorList.isEmpty()) {
        cmsXmlTransformation.setXmlTransformation(xmlTransformationBuilder.build());
        cmsXmlTransformation.setXmlTransformationProcessingErrors(null);
    } else {
        cmsXmlTransformation.setXmlTransformation(null);
        cmsXmlTransformation.setXmlTransformationProcessingErrors(errorList);
    }

    return errorList;

}

From source file:com.haulmont.yarg.structure.xml.impl.DefaultXmlWriter.java

License:Apache License

@Override
public String buildXml(Report report) {
    try {// w  ww .  ja v a 2 s.  c om
        Document document = DocumentFactory.getInstance().createDocument();
        Element root = document.addElement("report");

        root.addAttribute("name", report.getName());
        writeTemplates(report, root);
        writeInputParameters(report, root);
        writeValueFormats(report, root);
        writeRootBand(report, root);

        StringWriter stringWriter = new StringWriter();
        new XMLWriter(stringWriter, OutputFormat.createPrettyPrint()).write(document);
        return stringWriter.toString();
    } catch (IOException e) {
        throw new ReportingException(e);
    }
}

From source file:com.jonschang.ai.ga.GenericGene.java

License:LGPL

@Override
public Element getXml() {
    DocumentFactory f = DocumentFactory.getInstance();
    Element toRet = f.createElement("gene");
    toRet.addAttribute("name", this.getName());
    toRet.addAttribute("expressiveness", this.getExpressiveness().toString());
    return toRet;
}

From source file:com.jonschang.ai.ga.GenericPhenotype.java

License:LGPL

public Element getXml() throws XmlException {
    DocumentFactory f = DocumentFactory.getInstance();
    Element toRet = f.createElement("phenotype");
    toRet.addAttribute("class", "com.jonschang.ai.ga.GenericPhenotype");
    toRet.addAttribute("score", ((Double) (score != null ? score : 0.0)).toString());
    Element geneEle = null;//from w  w  w .j a  v a  2  s . co  m
    for (Gene gene : this.genes) {
        toRet.add(gene.getXml());
    }
    return toRet;
}

From source file:com.liferay.alloy.tools.transformer.AlloyDocsTransformer.java

License:Open Source License

private void _createXML() {
    ArrayList<Component> components = getComponents();

    Document doc = DocumentFactory.getInstance().createDocument();
    Element root = doc.addElement("components");

    root.addAttribute("short-name", _DEFAULT_TAGLIB_SHORT_NAME);
    root.addAttribute("uri", _DEFAULT_TAGLIB_URI);
    root.addAttribute("tlib-version", _DEFAULT_TAGLIB_VERSION);

    for (Component component : components) {
        Element componentNode = root.addElement("component");

        componentNode.addAttribute("name", component.getName());
        componentNode.addAttribute("module", component.getModule());
        componentNode.addAttribute("package", component.getPackage());
        componentNode.addAttribute("bodyContent", String.valueOf(component.isBodyContent()));
        componentNode.addAttribute("alloyComponent", String.valueOf(component.isAlloyComponent()));

        Element descriptionNode = componentNode.addElement("description");
        descriptionNode.addCDATA(component.getDescription());
        Element attributesNode = componentNode.addElement("attributes");
        Element eventsNode = componentNode.addElement("events");

        for (Attribute attribute : component.getAttributes()) {
            Element attributeNode = attributesNode.addElement("attribute");

            Element defaultValueNode = attributeNode.addElement("defaultValue");
            Element attributeDescriptionNode = attributeNode.addElement("description");
            Element javaScriptTypeNode = attributeNode.addElement("javaScriptType");
            Element nameNode = attributeNode.addElement("name");
            Element readOnlyNode = attributeNode.addElement("readOnly");

            defaultValueNode.setText(attribute.getDefaultValue());
            attributeDescriptionNode.addCDATA(_getAttributeDescription(attribute));
            javaScriptTypeNode.setText(attribute.getJavaScriptType());
            nameNode.setText(attribute.getName());
            readOnlyNode.setText(Boolean.toString(attribute.isReadOnly()));
        }//from ww  w  .  j  a  v a 2 s  . c  om

        for (Attribute event : component.getEvents()) {
            Element eventNode = eventsNode.addElement("event");
            Element nameNode = eventNode.addElement("name");
            Element typeNode = eventNode.addElement("type");
            Element elementDescriptionNode = eventNode.addElement("description");

            nameNode.setText(event.getName());
            elementDescriptionNode.addCDATA(_getAttributeDescription(event));
            typeNode.setText(event.getType());
        }
    }

    try {
        File file = new File(_outputXML);

        file.getParentFile().mkdirs();

        FileOutputStream fos = new FileOutputStream(file);

        OutputFormat format = OutputFormat.createPrettyPrint();

        XMLWriter writer = new XMLWriter(fos, format);

        writer.write(doc);
        writer.flush();

        System.out.println("Writing " + _outputXML);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.liferay.alloy.tools.xmlbuilder.XMLBuilder.java

License:Open Source License

private void _createXML() {
    ArrayList<Component> components = getComponents();

    Document doc = DocumentFactory.getInstance().createDocument();
    Element root = doc.addElement("taglibs");

    root.addAttribute("short-name", _DEFAULT_TAGLIB_SHORT_NAME);
    root.addAttribute("uri", _DEFAULT_TAGLIB_URI);
    root.addAttribute("tlib-version", _DEFAULT_TAGLIB_VERSION);

    for (Component component : components) {
        Element componentNode = root.addElement("component");

        componentNode.addAttribute("name", component.getName());
        componentNode.addAttribute("module", component.getModule());
        componentNode.addAttribute("package", component.getPackage());
        componentNode.addAttribute("bodyContent", String.valueOf(component.isBodyContent()));

        componentNode.addAttribute("alloyComponent", String.valueOf(component.isAlloyComponent()));

        Element attributesNode = componentNode.addElement("attributes");
        Element eventsNode = componentNode.addElement("events");

        for (Attribute attribute : component.getAttributes()) {
            Element attributeNode = attributesNode.addElement("attribute");
            Element nameNode = attributeNode.addElement("name");
            Element inputTypeNode = attributeNode.addElement("inputType");
            Element outputTypeNode = attributeNode.addElement("outputType");
            Element defaultValueNode = attributeNode.addElement("defaultValue");

            Element descriptionNode = attributeNode.addElement("description");

            nameNode.setText(attribute.getName());
            inputTypeNode.setText(attribute.getInputType());
            outputTypeNode.setText(attribute.getOutputType());
            defaultValueNode.setText(attribute.getDefaultValue());
            descriptionNode.addCDATA(_getAttributeDescription(attribute));
        }//from  w w w.  jav  a2 s  .c  o m

        for (Attribute event : component.getEvents()) {
            Element eventNode = eventsNode.addElement("event");
            Element nameNode = eventNode.addElement("name");
            Element typeNode = eventNode.addElement("type");
            Element descriptionNode = eventNode.addElement("description");

            nameNode.setText(event.getName());
            typeNode.setText(event.getInputType());
            descriptionNode.addCDATA(_getAttributeDescription(event));
        }
    }

    try {
        FileOutputStream fos = new FileOutputStream(_componentXML);

        OutputFormat format = OutputFormat.createPrettyPrint();

        XMLWriter writer = new XMLWriter(fos, format);

        writer.write(doc);
        writer.flush();

        System.out.println("Writing " + _componentXML);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.liferay.portal.xml.SAXReaderImpl.java

License:Open Source License

public Attribute createAttribute(Element element, QName qName, String value) {

    ElementImpl elementImpl = (ElementImpl) element;
    QNameImpl qNameImpl = (QNameImpl) qName;

    DocumentFactory documentFactory = DocumentFactory.getInstance();

    return new AttributeImpl(documentFactory.createAttribute(elementImpl.getWrappedElement(),
            qNameImpl.getWrappedQName(), value));
}

From source file:com.liferay.portal.xml.SAXReaderImpl.java

License:Open Source License

public Attribute createAttribute(Element element, String name, String value) {

    ElementImpl elementImpl = (ElementImpl) element;

    DocumentFactory documentFactory = DocumentFactory.getInstance();

    return new AttributeImpl(documentFactory.createAttribute(elementImpl.getWrappedElement(), name, value));
}

From source file:com.liferay.portal.xml.SAXReaderImpl.java

License:Open Source License

public Document createDocument(String encoding) {
    DocumentFactory documentFactory = DocumentFactory.getInstance();

    return new DocumentImpl(documentFactory.createDocument(encoding));
}

From source file:com.liferay.portlet.PortletPreferencesSerializer.java

License:Open Source License

public static String toXML(PortletPreferencesImpl prefs) throws SystemException {

    try {//from www.j a  va2  s  .co m
        Map preferences = prefs.getPreferences();

        DocumentFactory docFactory = DocumentFactory.getInstance();

        Element portletPreferences = docFactory.createElement("portlet-preferences");

        Iterator itr = preferences.entrySet().iterator();

        while (itr.hasNext()) {
            Map.Entry entry = (Map.Entry) itr.next();

            Preference preference = (Preference) entry.getValue();

            Element prefEl = docFactory.createElement("preference");

            Element nameEl = docFactory.createElement("name");
            nameEl.addText(preference.getName());

            prefEl.add(nameEl);

            String[] values = preference.getValues();

            for (int i = 0; i < values.length; i++) {
                Element valueEl = docFactory.createElement("value");
                valueEl.addText(values[i]);

                prefEl.add(valueEl);
            }

            if (preference.isReadOnly()) {
                Element valueEl = docFactory.createElement("read-only");
                valueEl.addText("true");
            }

            portletPreferences.add(prefEl);
        }

        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        XMLWriter writer = new XMLWriter(baos, OutputFormat.createCompactFormat());

        writer.write(portletPreferences);

        return baos.toString();
    } catch (IOException ioe) {
        throw new SystemException(ioe);
    }
}