Search in sources :

Example 81 with DocumentException

use of org.dom4j.DocumentException in project GenericKnimeNodes by genericworkflownodes.

the class SchemaValidator method validates.

/**
 * Validate the given xml stream against the stored schemata.
 *
 * @param xmlstream
 *            The stream to validate.
 * @return True if the file is valid, false otherwise.
 */
public boolean validates(InputStream xmlstream) {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
    SimpleErrorHandler errorHandler = new SimpleErrorHandler();
    try {
        factory.setSchema(schemaFactory.newSchema(getSchemaSources()));
        SAXParser parser = factory.newSAXParser();
        SAXReader reader = new SAXReader(parser.getXMLReader());
        reader.setValidation(false);
        reader.setErrorHandler(errorHandler);
        reader.read(xmlstream);
    } catch (SAXException e) {
        throw new RuntimeException(e);
    } catch (DocumentException e) {
        throw new RuntimeException(e);
    } catch (ParserConfigurationException e) {
        throw new RuntimeException(e);
    }
    if (!errorHandler.isValid()) {
        errorReport = errorHandler.getErrorReport();
        return false;
    }
    return true;
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) SAXReader(org.dom4j.io.SAXReader) DocumentException(org.dom4j.DocumentException) SAXParser(javax.xml.parsers.SAXParser) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXParserFactory(javax.xml.parsers.SAXParserFactory) SAXException(org.xml.sax.SAXException)

Example 82 with DocumentException

use of org.dom4j.DocumentException in project xwiki-platform by xwiki.

the class Package method Import.

/**
 * Load this package in memory from an InputStream. It may be installed later using {@link #install(XWikiContext)}.
 *
 * @param file an InputStream of a zipped package file
 * @param context current XWikiContext
 * @return an empty string, useless.
 * @throws IOException while reading the ZipFile
 * @throws XWikiException when package content is broken
 * @since 2.3M2
 */
public String Import(InputStream file, XWikiContext context) throws IOException, XWikiException {
    ZipArchiveInputStream zis;
    ArchiveEntry entry;
    Document description = null;
    try {
        zis = new ZipArchiveInputStream(file, XAR_FILENAME_ENCODING, false);
        List<XWikiDocument> docsToLoad = new LinkedList<XWikiDocument>();
        /*
             * Loop 1: Cycle through the zip input stream and load out all of the documents, when we find the
             * package.xml file we put it aside to so that we only include documents which are in the file.
             */
        while ((entry = zis.getNextEntry()) != null) {
            if (entry.isDirectory() || (entry.getName().indexOf("META-INF") != -1)) {
                // (we use that directory to put meta data such as LICENSE/NOTICE files.)
                continue;
            } else if (entry.getName().compareTo(DefaultPackageFileName) == 0) {
                // The entry is the manifest (package.xml). Read this differently.
                description = fromXml(new CloseShieldInputStream(zis));
            } else {
                XWikiDocument doc = null;
                try {
                    doc = readFromXML(new CloseShieldInputStream(zis));
                } catch (Throwable e) {
                    LOGGER.warn("Failed to parse document [{}] from XML during import, thus it will not be installed. " + "The error was: " + ExceptionUtils.getRootCauseMessage(e));
                    // It will be listed in the "failed documents" section after the import.
                    addToErrors(entry.getName().replaceAll("/", "."), context);
                    continue;
                }
                // if no filters throw exceptions, add it to the list to import.
                try {
                    this.filter(doc, context);
                    docsToLoad.add(doc);
                } catch (ExcludeDocumentException e) {
                    LOGGER.info("Skip the document '" + doc.getDocumentReference() + "'");
                }
            }
        }
        // Make sure a manifest was included in the package...
        if (description == null) {
            throw new PackageException(XWikiException.ERROR_XWIKI_UNKNOWN, "Could not find the package definition");
        }
        /*
             * Loop 2: Cycle through the list of documents and if they are in the manifest then add them, otherwise log
             * a warning and add them to the skipped list.
             */
        for (XWikiDocument doc : docsToLoad) {
            if (documentExistInPackageFile(doc.getFullName(), doc.getLanguage(), description)) {
                this.add(doc, context);
            } else {
                LOGGER.warn("document " + doc.getDocumentReference() + " does not exist in package definition." + " It will not be installed.");
                // It will be listed in the "skipped documents" section after the
                // import.
                addToSkipped(doc.getFullName(), context);
            }
        }
        updateFileInfos(description);
    } catch (DocumentException e) {
        throw new PackageException(XWikiException.ERROR_XWIKI_UNKNOWN, "Error when reading the XML");
    }
    return "";
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) ZipArchiveInputStream(org.apache.commons.compress.archivers.zip.ZipArchiveInputStream) DocumentException(org.dom4j.DocumentException) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) ArchiveEntry(org.apache.commons.compress.archivers.ArchiveEntry) DOMDocument(org.dom4j.dom.DOMDocument) Document(org.dom4j.Document) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) LinkedList(java.util.LinkedList) CloseShieldInputStream(org.apache.commons.io.input.CloseShieldInputStream)

Example 83 with DocumentException

use of org.dom4j.DocumentException in project xwiki-platform by xwiki.

the class AbstractXMLSerializer method parse.

@Override
public P parse(final InputStream stream) throws IOException {
    final SAXReader reader = new SAXReader();
    // Remove nodes generated by indentation.
    reader.setStripWhitespaceText(true);
    reader.setMergeAdjacentText(true);
    final Document domdoc;
    try {
        domdoc = reader.read(stream);
    } catch (DocumentException e) {
        throw new IOException("Failed to parse XML, probably malformed input.");
    }
    return this.parse(domdoc.getRootElement());
}
Also used : SAXReader(org.dom4j.io.SAXReader) DocumentException(org.dom4j.DocumentException) IOException(java.io.IOException) Document(org.dom4j.Document)

Example 84 with DocumentException

use of org.dom4j.DocumentException in project my_curd by qinyou.

the class ToolFormatXml method formatXML.

public static String formatXML(String inputXML) {
    String requestXML = null;
    Document document = null;
    try {
        SAXReader reader = new SAXReader();
        document = reader.read(new StringReader(inputXML));
    } catch (DocumentException e1) {
        e1.printStackTrace();
    }
    if (document != null) {
        XMLWriter writer = null;
        try {
            StringWriter stringWriter = new StringWriter();
            OutputFormat format = new OutputFormat("    ", true);
            writer = new XMLWriter(stringWriter, format);
            writer.write(document);
            writer.flush();
            requestXML = stringWriter.getBuffer().toString();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (IOException e) {
                }
            }
        }
    }
    return requestXML;
}
Also used : StringWriter(java.io.StringWriter) SAXReader(org.dom4j.io.SAXReader) DocumentException(org.dom4j.DocumentException) StringReader(java.io.StringReader) OutputFormat(org.dom4j.io.OutputFormat) IOException(java.io.IOException) Document(org.dom4j.Document) XMLWriter(org.dom4j.io.XMLWriter)

Example 85 with DocumentException

use of org.dom4j.DocumentException in project EssayJoke by qiyei2015.

the class Dom4jHelper method parseXml.

/**
 * 解析xml文件
 * @param xmlFile
 */
public static void parseXml(File xmlFile) {
    try {
        SAXReader saxReader = new SAXReader();
        Document document = saxReader.read(xmlFile);
        Element element = document.getRootElement();
        readeNode(element, "");
    } catch (DocumentException e) {
        e.printStackTrace();
    }
}
Also used : SAXReader(org.dom4j.io.SAXReader) Element(org.dom4j.Element) DocumentException(org.dom4j.DocumentException) Document(org.dom4j.Document)

Aggregations

DocumentException (org.dom4j.DocumentException)123 Document (org.dom4j.Document)80 SAXReader (org.dom4j.io.SAXReader)73 Element (org.dom4j.Element)51 IOException (java.io.IOException)45 File (java.io.File)27 InputStream (java.io.InputStream)21 StringReader (java.io.StringReader)15 InputStreamReader (java.io.InputStreamReader)11 ArrayList (java.util.ArrayList)10 XMLWriter (org.dom4j.io.XMLWriter)9 InputSource (org.xml.sax.InputSource)9 FileInputStream (java.io.FileInputStream)7 URL (java.net.URL)7 List (java.util.List)7 Node (org.dom4j.Node)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6 UnsupportedEncodingException (java.io.UnsupportedEncodingException)6 FileNotFoundException (java.io.FileNotFoundException)5 Reader (java.io.Reader)5