Search in sources :

Example 76 with SAXBuilder

use of org.jdom.input.SAXBuilder in project openremote by openremote.

the class SequenceNumberPersistencyManager method load.

public synchronized void load() {
    if (!fileExists()) {
        return;
    }
    File file = new File(filePath);
    SAXBuilder builder = new SAXBuilder();
    Document doc = null;
    try {
        doc = builder.build(file);
    } catch (JDOMException e) {
        LOG.severe("Failed to load sequence number file: '" + file.getPath() + "' because: " + e.getMessage());
        e.printStackTrace();
        return;
    } catch (IOException e) {
        LOG.severe("Failed to load sequence number file: '" + file.getPath() + "' because: " + e.getMessage());
        e.printStackTrace();
        return;
    }
    map.clear();
    Element root = doc.getRootElement();
    @SuppressWarnings("unchecked") List<Element> itemElements = root.getChildren(XML_TAG_SEQUENCE_NUMBER_ITEM);
    for (Element itemElement : itemElements) {
        Element keyElement = itemElement.getChild(XML_TAG_NETWORK_KEY);
        Element addressElement = itemElement.getChild(XML_TAG_ADDRESS);
        Element numberElement = itemElement.getChild(XML_TAG_SEQUENCE_NUMBER);
        if (keyElement == null || addressElement == null | numberElement == null) {
            continue;
        }
        String key = keyElement.getText();
        String address = addressElement.getText();
        String number = numberElement.getText();
        if (!map.containsKey(key)) {
            map.put(key, new HashMap<String, Integer>());
        }
        try {
            map.get(key).put(address, Integer.valueOf(number));
        } catch (NumberFormatException e) {
            LOG.severe("Error while loading sequence number file: '" + file.getPath() + "' because: " + e.getMessage());
            e.printStackTrace();
            continue;
        }
    }
}
Also used : SAXBuilder(org.jdom.input.SAXBuilder) Element(org.jdom.Element) IOException(java.io.IOException) Document(org.jdom.Document) JDOMException(org.jdom.JDOMException) File(java.io.File)

Example 77 with SAXBuilder

use of org.jdom.input.SAXBuilder in project checkstyle-idea by jshiell.

the class ConfigurationLocation method extractProperties.

private List<String> extractProperties(@Nullable final InputStream inputStream, @NotNull final ClassLoader checkstyleClassLoader) {
    if (inputStream != null) {
        try {
            final SAXBuilder saxBuilder = new SAXBuilder();
            saxBuilder.setEntityResolver(new CheckStyleEntityResolver(this, checkstyleClassLoader));
            final Document configDoc = saxBuilder.build(inputStream);
            return extractProperties(configDoc.getRootElement());
        } catch (Exception e) {
            LOG.warn("CheckStyle file could not be parsed for properties.", e);
        }
    }
    return new ArrayList<>();
}
Also used : SAXBuilder(org.jdom.input.SAXBuilder) CheckStyleEntityResolver(org.infernus.idea.checkstyle.util.CheckStyleEntityResolver) Document(org.jdom.Document) IOException(java.io.IOException)

Example 78 with SAXBuilder

use of org.jdom.input.SAXBuilder 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 79 with SAXBuilder

use of org.jdom.input.SAXBuilder in project intellij-plugins by JetBrains.

the class JabberFacadeImpl method getServers.

public String[] getServers() {
    SAXBuilder saxBuilder = new SAXBuilder();
    try {
        Document document = saxBuilder.build(getClass().getResource("servers.xml"));
        Element rootElement = document.getRootElement();
        List children = rootElement.getChildren("item", rootElement.getNamespace());
        List<String> result = new ArrayList<>();
        for (Object aChildren : children) {
            Element element = (Element) aChildren;
            result.add(element.getAttributeValue("jid"));
        }
        return ArrayUtil.toStringArray(result);
    } catch (JDOMException e) {
        LOG.error(e.getMessage(), e);
    } catch (IOException e) {
        LOG.error(e.getMessage(), e);
    }
    return new String[] { "jabber.org" };
}
Also used : SAXBuilder(org.jdom.input.SAXBuilder) Element(org.jdom.Element) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) IOException(java.io.IOException) Document(org.jdom.Document) JDOMException(org.jdom.JDOMException)

Example 80 with SAXBuilder

use of org.jdom.input.SAXBuilder in project intellij-plugins by JetBrains.

the class ProjectsData method serialize.

public Element serialize() {
    XStream xStream = new XStream(new DomDriver());
    String s = xStream.toXML(myStatus);
    try {
        return new SAXBuilder().build(new StringReader(s)).getRootElement();
    } catch (JDOMException e) {
        LOG.error(e.getMessage(), e);
    } catch (IOException e) {
        LOG.error(e.getMessage(), e);
    }
    return new Element("");
}
Also used : DomDriver(com.thoughtworks.xstream.io.xml.DomDriver) SAXBuilder(org.jdom.input.SAXBuilder) XStream(com.thoughtworks.xstream.XStream) Element(org.jdom.Element) StringReader(java.io.StringReader) IOException(java.io.IOException) JDOMException(org.jdom.JDOMException)

Aggregations

SAXBuilder (org.jdom.input.SAXBuilder)146 Document (org.jdom.Document)110 Element (org.jdom.Element)84 IOException (java.io.IOException)61 JDOMException (org.jdom.JDOMException)50 StringReader (java.io.StringReader)35 InputStream (java.io.InputStream)29 File (java.io.File)18 ApsSystemException (com.agiletec.aps.system.exception.ApsSystemException)13 ArrayList (java.util.ArrayList)12 XPath (org.jdom.xpath.XPath)11 HttpMethod (org.apache.commons.httpclient.HttpMethod)10 XMLOutputter (org.jdom.output.XMLOutputter)10 URL (java.net.URL)9 List (java.util.List)8 GoraException (org.apache.gora.util.GoraException)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 FileInputStream (java.io.FileInputStream)7 Namespace (org.jdom.Namespace)6 StringWriter (java.io.StringWriter)5