Example usage for org.dom4j.io SAXReader setMergeAdjacentText

List of usage examples for org.dom4j.io SAXReader setMergeAdjacentText

Introduction

In this page you can find the example usage for org.dom4j.io SAXReader setMergeAdjacentText.

Prototype

public void setMergeAdjacentText(boolean mergeAdjacentText) 

Source Link

Document

Sets whether or not adjacent text nodes should be merged together when parsing.

Usage

From source file:org.hibernate.build.gradle.publish.auth.maven.SettingsXmlCredentialsProvider.java

License:Open Source License

private SAXReader buildSAXReader() {
    SAXReader saxReader = new SAXReader(new DocumentFactory());
    saxReader.setMergeAdjacentText(true);
    return saxReader;
}

From source file:org.hibernate.build.gradle.upload.StandardMavenAuthenticationProvider.java

License:Open Source License

private SAXReader buildSAXReader() {
    SAXReader saxReader = new SAXReader();
    saxReader.setMergeAdjacentText(true);
    return saxReader;
}

From source file:org.hibernate.cfg.AnnotationConfiguration.java

License:Open Source License

@Override
public AnnotationConfiguration addInputStream(InputStream xmlInputStream) throws MappingException {
    try {/*w w w  .j a v  a 2s. c  om*/
        /*
         * try and parse the document:
         *  - try and validate the document with orm_2_0.xsd
          * - if it fails because of the version attribute mismatch, try and validate the document with orm_1_0.xsd
         */
        List<SAXParseException> errors = new ArrayList<SAXParseException>();
        SAXReader saxReader = new SAXReader();
        saxReader.setEntityResolver(getEntityResolver());
        saxReader.setErrorHandler(new ErrorLogger(errors));
        saxReader.setMergeAdjacentText(true);
        saxReader.setValidation(true);

        setValidationFor(saxReader, "orm_2_0.xsd");

        org.dom4j.Document doc = null;
        try {
            doc = saxReader.read(new InputSource(xmlInputStream));
        } catch (DocumentException e) {
            //the document is syntactically incorrect

            //DOM4J sometimes wraps the SAXParseException wo much interest
            final Throwable throwable = e.getCause();
            if (e.getCause() == null || !(throwable instanceof SAXParseException)) {
                throw new MappingException("Could not parse JPA mapping document", e);
            }
            errors.add((SAXParseException) throwable);
        }

        boolean isV1Schema = false;
        if (errors.size() != 0) {
            SAXParseException exception = errors.get(0);
            final String errorMessage = exception.getMessage();
            //does the error look like a schema mismatch?
            isV1Schema = doc != null && errorMessage.contains("1.0") && errorMessage.contains("2.0")
                    && errorMessage.contains("version");
        }
        if (isV1Schema) {
            //reparse with v1
            errors.clear();
            setValidationFor(saxReader, "orm_1_0.xsd");
            try {
                //too bad we have to reparse to validate again :(
                saxReader.read(new StringReader(doc.asXML()));
            } catch (DocumentException e) {
                //oops asXML fails even if the core doc parses initially
                throw new AssertionFailure("Error in DOM4J leads to a bug in Hibernate", e);
            }

        }
        if (errors.size() != 0) {
            //report errors in exception
            StringBuilder errorMessage = new StringBuilder();
            for (SAXParseException error : errors) {
                errorMessage.append("Error parsing XML (line").append(error.getLineNumber())
                        .append(" : column ").append(error.getColumnNumber()).append("): ")
                        .append(error.getMessage()).append("\n");
            }
            throw new MappingException("Invalid ORM mapping file.\n" + errorMessage.toString());
        }
        add(doc);
        return this;
    } finally {
        try {
            xmlInputStream.close();
        } catch (IOException ioe) {
            log.warn("Could not close input stream", ioe);
        }
    }
}

From source file:org.hibernate.internal.util.xml.MappingReader.java

License:LGPL

private XmlDocument legacyReadMappingDocument(EntityResolver entityResolver, InputSource source,
        Origin origin) {/*from   w  w  w.j  a v a2  s  .c om*/
    // IMPL NOTE : this is the legacy logic as pulled from the old AnnotationConfiguration code

    Exception failure;

    ErrorLogger errorHandler = new ErrorLogger();

    SAXReader saxReader = new SAXReader();
    saxReader.setEntityResolver(entityResolver);
    saxReader.setErrorHandler(errorHandler);
    saxReader.setMergeAdjacentText(true);
    saxReader.setValidation(true);

    Document document = null;
    try {
        // first try with orm 2.1 xsd validation
        setValidationFor(saxReader, "orm_2_1.xsd");
        document = saxReader.read(source);
        if (errorHandler.hasErrors()) {
            throw errorHandler.getErrors().get(0);
        }
        return new XmlDocumentImpl(document, origin.getType(), origin.getName());
    } catch (Exception e) {
        if (LOG.isDebugEnabled()) {
            LOG.debugf("Problem parsing XML using orm 2.1 xsd, trying 2.0 xsd : %s", e.getMessage());
        }
        failure = e;
        errorHandler.reset();

        if (document != null) {
            // next try with orm 2.0 xsd validation
            try {
                setValidationFor(saxReader, "orm_2_0.xsd");
                document = saxReader.read(new StringReader(document.asXML()));
                if (errorHandler.hasErrors()) {
                    errorHandler.logErrors();
                    throw errorHandler.getErrors().get(0);
                }
                return new XmlDocumentImpl(document, origin.getType(), origin.getName());
            } catch (Exception e2) {
                if (LOG.isDebugEnabled()) {
                    LOG.debugf("Problem parsing XML using orm 2.0 xsd, trying 1.0 xsd : %s", e2.getMessage());
                }
                errorHandler.reset();

                if (document != null) {
                    // next try with orm 1.0 xsd validation
                    try {
                        setValidationFor(saxReader, "orm_1_0.xsd");
                        document = saxReader.read(new StringReader(document.asXML()));
                        if (errorHandler.hasErrors()) {
                            errorHandler.logErrors();
                            throw errorHandler.getErrors().get(0);
                        }
                        return new XmlDocumentImpl(document, origin.getType(), origin.getName());
                    } catch (Exception e3) {
                        if (LOG.isDebugEnabled()) {
                            LOG.debugf("Problem parsing XML using orm 1.0 xsd : %s", e3.getMessage());
                        }
                    }
                }
            }
        }
    }
    throw new InvalidMappingException("Unable to read XML", origin.getType(), origin.getName(), failure);
}

From source file:org.hibernate.tool.xml.XMLHelper.java

License:Open Source License

private static SAXReader resolveSAXReader() {
    SAXReader saxReader = new SAXReader();
    saxReader.setMergeAdjacentText(true);
    saxReader.setValidation(true);/* w  ww. ja v a  2s .  c  om*/
    return saxReader;
}

From source file:org.hibernate.util.xml.MappingReader.java

License:Open Source License

public XmlDocument readMappingDocument(EntityResolver entityResolver, InputSource source, Origin origin) {
    // IMPL NOTE : this is the legacy logic as pulled from the old AnnotationConfiguration code

    Exception failure;/* w w  w  . j a v  a2s. c om*/
    ErrorLogger errorHandler = new ErrorLogger();

    SAXReader saxReader = new SAXReader();
    saxReader.setEntityResolver(entityResolver);
    saxReader.setErrorHandler(errorHandler);
    saxReader.setMergeAdjacentText(true);
    saxReader.setValidation(true);

    Document document = null;
    try {
        // first try with orm 2.0 xsd validation
        setValidationFor(saxReader, "orm_2_0.xsd");
        document = saxReader.read(source);
        if (errorHandler.getError() != null) {
            throw errorHandler.getError();
        }
        return new XmlDocumentImpl(document, origin.getType(), origin.getName());
    } catch (Exception orm2Problem) {
        log.debug("Problem parsing XML using orm 2 xsd : {}", orm2Problem.getMessage());
        failure = orm2Problem;
        errorHandler.reset();

        if (document != null) {
            // next try with orm 1.0 xsd validation
            try {
                setValidationFor(saxReader, "orm_1_0.xsd");
                document = saxReader.read(new StringReader(document.asXML()));
                if (errorHandler.getError() != null) {
                    throw errorHandler.getError();
                }
                return new XmlDocumentImpl(document, origin.getType(), origin.getName());
            } catch (Exception orm1Problem) {
                log.debug("Problem parsing XML using orm 1 xsd : {}", orm1Problem.getMessage());
            }
        }
    }
    throw new InvalidMappingException("Unable to read XML", origin.getType(), origin.getName(), failure);
}

From source file:org.jboss.seam.wiki.util.XmlDeploymentHandler.java

License:LGPL

public Map<String, Element> getDescriptorsAsXmlElements() {
    // Lazy access to streams
    if (elements == null) {
        elements = new HashMap<String, Element>();
        for (FileDescriptor fileDescriptor : getResources()) {
            try {
                SAXReader saxReader = new SAXReader();
                saxReader.setMergeAdjacentText(true);

                if (isSchemaValidating()) {
                    saxReader.setEntityResolver(new DTDEntityResolver());
                    saxReader.setValidation(true);
                    saxReader.setFeature("http://apache.org/xml/features/validation/schema", true);
                }/*from   w  w w . jav a 2s. c o m*/

                elements.put(fileDescriptor.getName(),
                        saxReader.read(fileDescriptor.getUrl().openStream()).getRootElement());

            } catch (DocumentException dex) {
                Throwable nested = dex.getNestedException();
                if (nested != null) {
                    if (nested instanceof FileNotFoundException) {
                        throw new RuntimeException("Can't find schema/DTD reference for file: "
                                + fileDescriptor.getName() + "':  " + nested.getMessage(), dex);
                    } else if (nested instanceof UnknownHostException) {
                        throw new RuntimeException("Cannot connect to host from schema/DTD reference: "
                                + nested.getMessage() + " - check that your schema/DTD reference is current",
                                dex);
                    }
                }
                throw new RuntimeException("Could not parse XML file: " + fileDescriptor.getName(), dex);
            } catch (Exception ex) {
                throw new RuntimeException("Could not parse XML file: " + fileDescriptor.getName(), ex);
            }
        }
    }
    return elements;
}

From source file:org.lushlife.guicexml.internal.xml.XML.java

License:Apache License

public static Element getRootElement(InputStream stream) throws DocumentException {
    try {/*from   ww w. j  a va2  s  .co  m*/
        SAXReader saxReader = new SAXReader();
        saxReader.setMergeAdjacentText(true);
        return saxReader.read(stream).getRootElement();
    } catch (DocumentException e) {
        Throwable nested = e.getNestedException();
        if (nested != null) {
            if (nested instanceof FileNotFoundException) {
                throw new RuntimeException("Can't find schema/DTD reference: " + nested.getMessage(), e);
            } else if (nested instanceof UnknownHostException) {
                throw new RuntimeException("Cannot connect to host from schema/DTD reference: "
                        + nested.getMessage() + " - check that your schema/DTD reference is current", e);
            }
        }
        throw e;
    }
}

From source file:org.nuxeo.ecm.platform.ui.web.rest.StaticNavigationHandler.java

License:Apache License

/**
 * Gets the root element of the document.
 *
 * @since 5.6/*from  w  ww . j a  v a 2  s.  c om*/
 */
protected static Element getDocumentRoot(InputStream stream) {
    try {
        SAXReader saxReader = new SAXReader();
        saxReader.setEntityResolver(new DTDEntityResolver());
        saxReader.setMergeAdjacentText(true);
        return saxReader.read(stream).getRootElement();
    } catch (DocumentException de) {
        throw new RuntimeException(de);
    }
}

From source file:org.nuxeo.ecm.platform.webdav.request.tests.FakeResponse.java

License:Open Source License

public Element getXMLOutput() throws DocumentException, IOException {
    SAXReader saxReader = new SAXReader();
    saxReader.setMergeAdjacentText(true);
    saxReader.setEncoding("UTF-8");
    saxReader.setValidation(false);/*from   w ww.  j av  a2 s .  co m*/
    saxReader.setIncludeExternalDTDDeclarations(false);
    saxReader.setIncludeInternalDTDDeclarations(false);
    String xml = getOutput();
    InputStream in = new StringBlob(xml).getStream();
    return saxReader.read(in).getRootElement();
}