Search in sources :

Example 31 with JDOMException

use of org.jdom.JDOMException in project vcell by virtualcell.

the class XmlUtil method readXML.

public static Document readXML(InputStream inputStream) throws RuntimeException {
    SAXBuilder builder = new SAXBuilder(false);
    Document sDoc = null;
    GenericXMLErrorHandler errorHandler = new GenericXMLErrorHandler();
    builder.setErrorHandler(errorHandler);
    try {
        sDoc = builder.build(inputStream);
        // Element root = null;
        // root = sDoc.getRootElement();
        // flush/replace previous error log with every read.
        String errorHandlerLog = errorHandler.getErrorLog();
        if (errorHandlerLog.length() > 0) {
            System.out.println(errorHandlerLog);
            XmlUtil.errorLog = errorHandlerLog;
        } else {
            XmlUtil.errorLog = "";
        }
    } catch (JDOMException e) {
        e.printStackTrace();
        throw new RuntimeException("source document is not well-formed\n" + e.getMessage());
    } catch (IOException e) {
        e.printStackTrace();
        throw new RuntimeException("Unable to read source document\n" + e.getMessage());
    }
    return sDoc;
}
Also used : SAXBuilder(org.jdom.input.SAXBuilder) IOException(java.io.IOException) Document(org.jdom.Document) JDOMException(org.jdom.JDOMException)

Example 32 with JDOMException

use of org.jdom.JDOMException in project che by eclipse.

the class EffectivePomWriter method addMavenNamespace.

/**
     * method from org.apache.maven.plugins.help.AbstractEffectiveMojo
     * Add a Pom/Settings namespaces to the effective XML content.
     *
     * @param effectiveXml not null the effective POM or Settings
     * @param isPom if <code>true</code> add the Pom xsd url, otherwise add the settings xsd url.
     * @return the content of the root element, i.e. &lt;project/&gt; or &lt;settings/&gt; with the Maven namespace or
     *         the original <code>effective</code> if an error occurred.
     * @see #POM_XSD_URL
     * @see #SETTINGS_XSD_URL
     */
protected static String addMavenNamespace(String effectiveXml, boolean isPom) {
    SAXBuilder builder = new SAXBuilder();
    try {
        Document document = builder.build(new StringReader(effectiveXml));
        Element rootElement = document.getRootElement();
        // added namespaces
        Namespace pomNamespace = Namespace.getNamespace("", "http://maven.apache.org/POM/4.0.0");
        rootElement.setNamespace(pomNamespace);
        Namespace xsiNamespace = Namespace.getNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
        rootElement.addNamespaceDeclaration(xsiNamespace);
        if (rootElement.getAttribute("schemaLocation", xsiNamespace) == null) {
            rootElement.setAttribute("schemaLocation", "http://maven.apache.org/POM/4.0.0 " + (isPom ? POM_XSD_URL : SETTINGS_XSD_URL), xsiNamespace);
        }
        ElementFilter elementFilter = new ElementFilter(Namespace.getNamespace(""));
        for (Iterator<?> i = rootElement.getDescendants(elementFilter); i.hasNext(); ) {
            Element e = (Element) i.next();
            e.setNamespace(pomNamespace);
        }
        StringWriter w = new StringWriter();
        Format format = Format.getPrettyFormat();
        XMLOutputter out = new XMLOutputter(format);
        out.output(document.getRootElement(), w);
        return w.toString();
    } catch (JDOMException e) {
        return effectiveXml;
    } catch (IOException e) {
        return effectiveXml;
    }
}
Also used : XMLOutputter(org.jdom.output.XMLOutputter) SAXBuilder(org.jdom.input.SAXBuilder) Format(org.jdom.output.Format) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) StringWriter(java.io.StringWriter) Element(org.jdom.Element) ElementFilter(org.jdom.filter.ElementFilter) StringReader(java.io.StringReader) IOException(java.io.IOException) Document(org.jdom.Document) JDOMException(org.jdom.JDOMException) Namespace(org.jdom.Namespace)

Example 33 with JDOMException

use of org.jdom.JDOMException in project voldemort by voldemort.

the class RestUtils method parseSerializerDefinition.

private static SerializerDefinition parseSerializerDefinition(String serializerInfoXml, String elementName) {
    SAXBuilder builder = new SAXBuilder();
    try {
        Document doc = builder.build(new StringReader(serializerInfoXml));
        Element root = doc.getRootElement();
        Element serializerElement = root.getChild(elementName);
        return StoreDefinitionsMapper.readSerializer(serializerElement);
    } catch (JDOMException e) {
        throw new MappingException(e);
    } catch (IOException e) {
        throw new MappingException(e);
    }
}
Also used : SAXBuilder(org.jdom.input.SAXBuilder) Element(org.jdom.Element) StringReader(java.io.StringReader) IOException(java.io.IOException) Document(org.jdom.Document) JDOMException(org.jdom.JDOMException) MappingException(voldemort.xml.MappingException)

Example 34 with JDOMException

use of org.jdom.JDOMException in project voldemort by voldemort.

the class ClusterMapper method readCluster.

@SuppressWarnings("unchecked")
public Cluster readCluster(Reader input, boolean verifySchema) {
    try {
        SAXBuilder builder = new SAXBuilder(false);
        Document doc = builder.build(input);
        if (verifySchema) {
            Validator validator = this.schema.newValidator();
            validator.validate(new JDOMSource(doc));
        }
        Element root = doc.getRootElement();
        if (!root.getName().equals(CLUSTER_ELMT))
            throw new MappingException("Invalid root element: " + doc.getRootElement().getName());
        String name = root.getChildText(CLUSTER_NAME_ELMT);
        List<Zone> zones = new ArrayList<Zone>();
        for (Element node : (List<Element>) root.getChildren(ZONE_ELMT)) zones.add(readZone(node));
        List<Node> servers = new ArrayList<Node>();
        for (Element node : (List<Element>) root.getChildren(SERVER_ELMT)) servers.add(readServer(node));
        return new Cluster(name, servers, zones);
    } catch (JDOMException e) {
        throw new MappingException(e);
    } catch (SAXException e) {
        throw new MappingException(e);
    } catch (IOException e) {
        throw new MappingException(e);
    }
}
Also used : SAXBuilder(org.jdom.input.SAXBuilder) Zone(voldemort.cluster.Zone) Element(org.jdom.Element) Node(voldemort.cluster.Node) ArrayList(java.util.ArrayList) JDOMSource(org.jdom.transform.JDOMSource) Cluster(voldemort.cluster.Cluster) IOException(java.io.IOException) Document(org.jdom.Document) JDOMException(org.jdom.JDOMException) SAXException(org.xml.sax.SAXException) ArrayList(java.util.ArrayList) List(java.util.List) Validator(javax.xml.validation.Validator)

Example 35 with JDOMException

use of org.jdom.JDOMException in project voldemort by voldemort.

the class StoreDefinitionsMapper method readStore.

public static StoreDefinition readStore(Reader input) {
    SAXBuilder builder = new SAXBuilder();
    try {
        Document doc = builder.build(input);
        Element root = doc.getRootElement();
        return readStore(root);
    } catch (JDOMException e) {
        throw new MappingException(e);
    } catch (IOException e) {
        throw new MappingException(e);
    }
}
Also used : SAXBuilder(org.jdom.input.SAXBuilder) Element(org.jdom.Element) IOException(java.io.IOException) Document(org.jdom.Document) JDOMException(org.jdom.JDOMException)

Aggregations

JDOMException (org.jdom.JDOMException)72 IOException (java.io.IOException)59 Element (org.jdom.Element)46 Document (org.jdom.Document)27 SAXBuilder (org.jdom.input.SAXBuilder)20 File (java.io.File)17 VirtualFile (com.intellij.openapi.vfs.VirtualFile)11 Nullable (org.jetbrains.annotations.Nullable)11 NotNull (org.jetbrains.annotations.NotNull)10 StringReader (java.io.StringReader)9 THashMap (gnu.trove.THashMap)6 ArrayList (java.util.ArrayList)6 List (java.util.List)6 InvalidDataException (com.intellij.openapi.util.InvalidDataException)5 Path (java.nio.file.Path)4 Logger (com.intellij.openapi.diagnostic.Logger)3 Project (com.intellij.openapi.project.Project)3 StringWriter (java.io.StringWriter)3 Format (org.jdom.output.Format)3 XMLOutputter (org.jdom.output.XMLOutputter)3