Search in sources :

Example 71 with DocumentException

use of org.dom4j.DocumentException in project tdi-studio-se by Talend.

the class ComponentFolderManager method writeXMLContent.

/**
     * DOC slanglois Comment method "writeXMLContent".
     * 
     * @param iFile
     * @param document
     * @param enCode
     * @throws CoreException
     */
private void writeXMLContent(IFile iFile, Document document, String enCode) throws CoreException {
    PrintWriter pw = null;
    XMLWriter writer = null;
    byte[] byteArray = null;
    // get xml content as inputstream
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = null;
    try {
        transformer = tf.newTransformer();
    } catch (TransformerConfigurationException e1) {
        // e1.printStackTrace();
        org.talend.componentdesigner.exception.ExceptionHandler.process(e1);
    }
    DOMSource source = new DOMSource(document);
    transformer.setOutputProperty(OutputKeys.ENCODING, enCode);
    //$NON-NLS-1$
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    ByteArrayOutputStream sw = new ByteArrayOutputStream();
    pw = new PrintWriter(sw);
    StreamResult result = new StreamResult(pw);
    try {
        transformer.transform(source, result);
    } catch (TransformerException e1) {
        // e1.printStackTrace();
        org.talend.componentdesigner.exception.ExceptionHandler.process(e1);
    }
    try {
        sw.flush();
    } catch (IOException e1) {
        // e1.printStackTrace();
        org.talend.componentdesigner.exception.ExceptionHandler.process(e1);
    } finally {
        if (pw != null) {
            pw.close();
        }
    }
    byteArray = sw.toByteArray();
    // format the xml content
    SAXReader saxReader = new SAXReader();
    org.dom4j.Document dom4jDocument = null;
    try {
        dom4jDocument = saxReader.read(new ByteArrayInputStream(byteArray));
    } catch (DocumentException e1) {
        // e1.printStackTrace();
        org.talend.componentdesigner.exception.ExceptionHandler.process(e1);
    }
    /** format the output like the webBrowser */
    OutputFormat format = OutputFormat.createPrettyPrint();
    /** give the xml encoding */
    format.setEncoding(enCode);
    sw = new ByteArrayOutputStream();
    try {
        writer = new XMLWriter(sw, format);
    } catch (UnsupportedEncodingException e1) {
        // e1.printStackTrace();
        org.talend.componentdesigner.exception.ExceptionHandler.process(e1);
    }
    try {
        writer.write(dom4jDocument);
        writer.flush();
    } catch (IOException e1) {
        // e1.printStackTrace();
        org.talend.componentdesigner.exception.ExceptionHandler.process(e1);
    } finally {
        if (writer != null) {
            try {
                writer.close();
            } catch (IOException e) {
                // e.printStackTrace();
                org.talend.componentdesigner.exception.ExceptionHandler.process(e);
            }
        }
    }
    byteArray = sw.toByteArray();
    // write content
    iFile.setContents(new ByteArrayInputStream(byteArray), true, false, null);
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) StreamResult(javax.xml.transform.stream.StreamResult) SAXReader(org.dom4j.io.SAXReader) OutputFormat(org.dom4j.io.OutputFormat) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) XMLWriter(org.dom4j.io.XMLWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) DocumentException(org.dom4j.DocumentException) TransformerException(javax.xml.transform.TransformerException) PrintWriter(java.io.PrintWriter)

Example 72 with DocumentException

use of org.dom4j.DocumentException in project gocd by gocd.

the class XMLArtifactImporter method importFile.

public void importFile(Graph graph, URIReference parentJob, InputStream in) {
    try {
        SAXReader saxReader = new SAXReader();
        if (systemEnvironment.get(SystemEnvironment.SHOULD_VALIDATE_XML_AGAINST_DTD)) {
            saxReader.setValidation(true);
        } else {
            saxReader.setEntityResolver(getCustomEntityResolver());
        }
        Document doc = saxReader.read(in);
        importXML(graph, parentJob, doc);
    } catch (DocumentException e) {
        LOGGER.warn("Invalid xml provided.", e);
    }
}
Also used : SAXReader(org.dom4j.io.SAXReader) DocumentException(org.dom4j.DocumentException) Document(org.dom4j.Document)

Example 73 with DocumentException

use of org.dom4j.DocumentException in project acs-community-packaging by Alfresco.

the class Column method fromXML.

/**
 * Deserialise this PageConfig instance from the specified XML stream.
 *
 * @param xml
 */
public void fromXML(DashboardsConfigElement config, String xml) {
    try {
        SAXReader reader = new SAXReader();
        Document document = reader.read(new StringReader(xml));
        Element rootElement = document.getRootElement();
        // walk the pages found in xml
        Iterator itrPages = rootElement.elementIterator(ELEMENT_PAGE);
        while (itrPages.hasNext()) {
            Element pageElement = (Element) itrPages.next();
            String layoutId = pageElement.attributeValue(ATTR_LAYOUTID);
            LayoutDefinition layoutDef = config.getLayoutDefinition(layoutId);
            if (layoutDef != null) {
                // found the layout now build the page and read the columns
                Page page = new Page(pageElement.attributeValue(ATTR_ID), layoutDef);
                Iterator itrColumns = pageElement.elementIterator(ELEMENT_COLUMN);
                while (itrColumns.hasNext()) {
                    Column column = new Column();
                    // read and resolve the dashlet definitions for this column
                    Element columnElement = (Element) itrColumns.next();
                    Iterator itrDashlets = columnElement.elementIterator(ELEMENT_DASHLET);
                    while (itrDashlets.hasNext()) {
                        String dashletId = ((Element) itrDashlets.next()).attributeValue(ATTR_REFID);
                        DashletDefinition dashletDef = config.getDashletDefinition(dashletId);
                        if (dashletDef != null) {
                            column.addDashlet(dashletDef);
                        } else if (logger.isWarnEnabled()) {
                            logger.warn("Failed to resolve Dashboard Dashlet Definition ID: " + dashletId);
                        }
                    }
                    // add the column of dashlets to the page
                    page.addColumn(column);
                }
                // add the page to this config instance
                this.addPage(page);
            } else if (logger.isWarnEnabled()) {
                logger.warn("Failed to resolve Dashboard Layout Definition ID: " + layoutId);
            }
        }
    } catch (DocumentException docErr) {
    // if we cannot parse, then we simply revert to default
    }
}
Also used : SAXReader(org.dom4j.io.SAXReader) Element(org.dom4j.Element) DashboardsConfigElement(org.alfresco.web.config.DashboardsConfigElement) DocumentException(org.dom4j.DocumentException) StringReader(java.io.StringReader) Iterator(java.util.Iterator) DashletDefinition(org.alfresco.web.config.DashboardsConfigElement.DashletDefinition) Document(org.dom4j.Document) LayoutDefinition(org.alfresco.web.config.DashboardsConfigElement.LayoutDefinition)

Example 74 with DocumentException

use of org.dom4j.DocumentException in project webanno by webanno.

the class TeiReader method nextTeiElement.

private void nextTeiElement() throws CollectionException, IOException {
    if (teiElementIterator == null) {
        currentTeiElement = null;
        return;
    }
    while (!teiElementIterator.hasNext() && super.hasNext()) {
        currentResource = nextFile();
        InputStream is = null;
        try {
            is = currentResource.getInputStream();
            if (currentResource.getPath().endsWith(".gz")) {
                is = new GZIPInputStream(is);
            }
            InputSource source = new InputSource(is);
            source.setPublicId(currentResource.getLocation());
            source.setSystemId(currentResource.getLocation());
            SAXReader reader = new SAXReader();
            Document xml = reader.read(source);
            final XPath teiPath = new Dom4jXPath("//tei:TEI");
            teiPath.addNamespace("tei", "http://www.tei-c.org/ns/1.0");
            @SuppressWarnings("unchecked") List<Element> teiElements = teiPath.selectNodes(xml);
            teiElementIterator = teiElements.iterator();
            currentTeiElementNumber = 0;
        } catch (DocumentException | JaxenException e) {
            throw new IOException(e);
        } finally {
            closeQuietly(is);
        }
    }
    currentTeiElement = teiElementIterator.hasNext() ? teiElementIterator.next() : null;
    currentTeiElementNumber++;
    if (!super.hasNext() && !teiElementIterator.hasNext()) {
        // Mark end of processing.
        teiElementIterator = null;
    }
}
Also used : Dom4jXPath(org.jaxen.dom4j.Dom4jXPath) XPath(org.jaxen.XPath) InputSource(org.xml.sax.InputSource) GZIPInputStream(java.util.zip.GZIPInputStream) InputStream(java.io.InputStream) SAXReader(org.dom4j.io.SAXReader) Element(org.dom4j.Element) IOException(java.io.IOException) Document(org.dom4j.Document) GZIPInputStream(java.util.zip.GZIPInputStream) Dom4jXPath(org.jaxen.dom4j.Dom4jXPath) DocumentException(org.dom4j.DocumentException) JaxenException(org.jaxen.JaxenException)

Example 75 with DocumentException

use of org.dom4j.DocumentException in project Spark by igniterealtime.

the class PluginManager method loadPublicPlugin.

/**
 * Loads public plugins.
 *
 * @param pluginDir the directory of the expanded public plugin.
 * @return the new Plugin model for the Public Plugin.
 */
private Plugin loadPublicPlugin(File pluginDir) {
    File pluginFile = new File(pluginDir, "plugin.xml");
    SAXReader saxReader = new SAXReader();
    Document pluginXML = null;
    try {
        pluginXML = saxReader.read(pluginFile);
    } catch (DocumentException e) {
        Log.error("Unable to read plugin XML file from " + pluginDir, e);
    }
    Plugin pluginClass = null;
    List<? extends Node> plugins = pluginXML.selectNodes("/plugin");
    for (Node plugin : plugins) {
        PublicPlugin publicPlugin = new PublicPlugin();
        String clazz = null;
        String name;
        String minVersion;
        try {
            name = plugin.selectSingleNode("name").getText();
            clazz = plugin.selectSingleNode("class").getText();
            try {
                String lower = name.replaceAll("[^0-9a-zA-Z]", "").toLowerCase();
                // Dont load the plugin if its on the Blacklist
                if (_blacklistPlugins.contains(lower) || _blacklistPlugins.contains(clazz) || SettingsManager.getLocalPreferences().getDeactivatedPlugins().contains(name)) {
                    return null;
                }
            } catch (Exception e) {
                Log.warning("An exception occurred while checking the plugin blacklist for " + name, e);
                return null;
            }
            // Check for minimum Spark version
            try {
                minVersion = plugin.selectSingleNode("minSparkVersion").getText();
                String buildNumber = JiveInfo.getVersion();
                boolean ok = buildNumber.compareTo(minVersion) >= 0;
                if (!ok) {
                    return null;
                }
            } catch (Exception e) {
                Log.error("Unable to load plugin " + name + " due to missing <minSparkVersion>-Tag in plugin.xml.");
                return null;
            }
            // Check for minimum Java version
            try {
                String javaversion = plugin.selectSingleNode("java").getText().replaceAll("[^0-9]", "");
                javaversion = javaversion == null ? "0" : javaversion;
                int jv = Integer.parseInt(attachMissingZero(javaversion));
                String myversion = System.getProperty("java.version").replaceAll("[^0-9]", "");
                int mv = Integer.parseInt(attachMissingZero(myversion));
                boolean ok = (mv >= jv);
                if (!ok) {
                    Log.error("Unable to load plugin " + name + " due to old JavaVersion.\n" + "It Requires " + plugin.selectSingleNode("java").getText() + " you have " + System.getProperty("java.version"));
                    return null;
                }
            } catch (NullPointerException e) {
                Log.warning("Plugin " + name + " has no <java>-Tag, consider getting a newer Version");
            }
            // set dependencies
            try {
                List<? extends Node> dependencies = plugin.selectNodes("depends/plugin");
                for (Node depend1 : dependencies) {
                    Element depend = (Element) depend1;
                    PluginDependency dependency = new PluginDependency();
                    dependency.setVersion(depend.selectSingleNode("version").getText());
                    dependency.setName(depend.selectSingleNode("name").getText());
                    publicPlugin.addDependency(dependency);
                }
            } catch (Exception e) {
                Log.warning("An exception occurred during the setting of dependencies while loading plugin " + name, e);
            }
            // Do operating system check.
            boolean operatingSystemOK = isOperatingSystemOK(plugin);
            if (!operatingSystemOK) {
                return null;
            }
            publicPlugin.setPluginClass(clazz);
            publicPlugin.setName(name);
            try {
                String version = plugin.selectSingleNode("version").getText();
                publicPlugin.setVersion(version);
                String author = plugin.selectSingleNode("author").getText();
                publicPlugin.setAuthor(author);
                String email = plugin.selectSingleNode("email").getText();
                publicPlugin.setEmail(email);
                String description = plugin.selectSingleNode("description").getText();
                publicPlugin.setDescription(description);
                String homePage = plugin.selectSingleNode("homePage").getText();
                publicPlugin.setHomePage(homePage);
            } catch (Exception e) {
                Log.debug("An ignorable exception occurred while loading plugin " + name + ": " + e.getMessage());
            }
            try {
                pluginClass = (Plugin) getParentClassLoader().loadClass(clazz).newInstance();
                Log.debug(name + " has been loaded.");
                publicPlugin.setPluginDir(pluginDir);
                publicPlugins.add(publicPlugin);
                registerPlugin(pluginClass);
            } catch (Throwable e) {
                Log.error("Unable to load plugin " + clazz + ".", e);
            }
        } catch (Exception ex) {
            Log.error("Unable to load plugin " + clazz + ".", ex);
        }
    }
    return pluginClass;
}
Also used : PluginDependency(org.jivesoftware.spark.plugin.PluginDependency) SAXReader(org.dom4j.io.SAXReader) Node(org.dom4j.Node) Element(org.dom4j.Element) PublicPlugin(org.jivesoftware.spark.plugin.PublicPlugin) Document(org.dom4j.Document) DocumentException(org.dom4j.DocumentException) MalformedURLException(java.net.MalformedURLException) DocumentException(org.dom4j.DocumentException) JarFile(java.util.jar.JarFile) ZipFile(java.util.zip.ZipFile) PublicPlugin(org.jivesoftware.spark.plugin.PublicPlugin) Plugin(org.jivesoftware.spark.plugin.Plugin)

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