Search in sources :

Example 26 with Document

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

the class SendXmlMessageP2PCommand method incomingMessage.

public String incomingMessage(String remoteUser, String messageText) {
    String xml = StringUtil.fromXMLSafeString(messageText);
    SAXBuilder builder = new SAXBuilder();
    try {
        Document document = builder.build(new StringReader(xml));
        Element rootElement = document.getRootElement();
        Element response = createResponse(rootElement, StringUtil.fromXMLSafeString(remoteUser));
        if (response == null)
            return "";
        return new XMLOutputter().outputString(response);
    } catch (Throwable e) {
        LOG.info(e.getMessage(), e);
        return StringUtil.toXML(e);
    }
}
Also used : XMLOutputter(org.jdom.output.XMLOutputter) SAXBuilder(org.jdom.input.SAXBuilder) Element(org.jdom.Element) StringReader(java.io.StringReader) Document(org.jdom.Document)

Example 27 with Document

use of org.jdom.Document in project android by JetBrains.

the class CompatibilityChecksMetadataUpdater method fetchMetadata.

@NotNull
private static ActionCallback fetchMetadata() {
    ActionCallback callback = new ActionCallback();
    ApplicationManager.getApplication().executeOnPooledThread(() -> {
        String url = "https://dl.google.com/android/studio/metadata/android-component-compatibility.xml";
        try {
            Document metadata = HttpRequests.request(url).connect(request -> {
                try {
                    return loadDocument(request.getInputStream());
                } catch (JDOMException e) {
                    LOG.info("Failed to parse XML metadata", e);
                    return null;
                } catch (Throwable e) {
                    LOG.info("Failed to parse XML metadata", e);
                    return null;
                }
            });
            if (metadata != null) {
                VersionCompatibilityChecker.getInstance().updateMetadata(metadata);
                callback.setDone();
            }
        } catch (IOException e) {
            LOG.info(String.format("Failed to connect to '%1$s'", url), e);
        }
        callback.setRejected();
    });
    return callback;
}
Also used : ActionCallback(com.intellij.openapi.util.ActionCallback) IOException(java.io.IOException) Document(org.jdom.Document) JDOMUtil.loadDocument(com.intellij.openapi.util.JDOMUtil.loadDocument) JDOMException(org.jdom.JDOMException) NotNull(org.jetbrains.annotations.NotNull)

Example 28 with Document

use of org.jdom.Document in project maven-plugins by apache.

the class XmlAppendingTransformer method processResource.

public void processResource(String resource, InputStream is, List<Relocator> relocators) throws IOException {
    Document r;
    try {
        SAXBuilder builder = new SAXBuilder(false);
        builder.setExpandEntities(false);
        if (ignoreDtd) {
            builder.setEntityResolver(new EntityResolver() {

                public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
                    return new InputSource(new StringReader(""));
                }
            });
        }
        r = builder.build(is);
    } catch (JDOMException e) {
        throw new RuntimeException("Error processing resource " + resource + ": " + e.getMessage(), e);
    }
    if (doc == null) {
        doc = r;
    } else {
        Element root = r.getRootElement();
        for (@SuppressWarnings("unchecked") Iterator<Attribute> itr = root.getAttributes().iterator(); itr.hasNext(); ) {
            Attribute a = itr.next();
            itr.remove();
            Element mergedEl = doc.getRootElement();
            Attribute mergedAtt = mergedEl.getAttribute(a.getName(), a.getNamespace());
            if (mergedAtt == null) {
                mergedEl.setAttribute(a);
            }
        }
        for (@SuppressWarnings("unchecked") Iterator<Content> itr = root.getChildren().iterator(); itr.hasNext(); ) {
            Content n = itr.next();
            itr.remove();
            doc.getRootElement().addContent(n);
        }
    }
}
Also used : SAXBuilder(org.jdom.input.SAXBuilder) InputSource(org.xml.sax.InputSource) Attribute(org.jdom.Attribute) Element(org.jdom.Element) EntityResolver(org.xml.sax.EntityResolver) IOException(java.io.IOException) Document(org.jdom.Document) JDOMException(org.jdom.JDOMException) SAXException(org.xml.sax.SAXException) Content(org.jdom.Content) StringReader(java.io.StringReader)

Example 29 with Document

use of org.jdom.Document in project maven-plugins by apache.

the class PomWriter method write.

public static void write(Writer w, Model newModel, boolean namespaceDeclaration) throws IOException {
    Element root = new Element("project");
    if (namespaceDeclaration) {
        String modelVersion = newModel.getModelVersion();
        Namespace pomNamespace = Namespace.getNamespace("", "http://maven.apache.org/POM/" + modelVersion);
        root.setNamespace(pomNamespace);
        Namespace xsiNamespace = Namespace.getNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
        root.addNamespaceDeclaration(xsiNamespace);
        if (root.getAttribute("schemaLocation", xsiNamespace) == null) {
            root.setAttribute("schemaLocation", "http://maven.apache.org/POM/" + modelVersion + " http://maven.apache.org/maven-v" + modelVersion.replace('.', '_') + ".xsd", xsiNamespace);
        }
    }
    Document doc = new Document(root);
    MavenJDOMWriter writer = new MavenJDOMWriter();
    String encoding = newModel.getModelEncoding() != null ? newModel.getModelEncoding() : "UTF-8";
    Format format = Format.getPrettyFormat().setEncoding(encoding);
    writer.write(newModel, doc, w, format);
}
Also used : Format(org.jdom.output.Format) Element(org.jdom.Element) Document(org.jdom.Document) Namespace(org.jdom.Namespace)

Example 30 with Document

use of org.jdom.Document in project gora by apache.

the class CassandraMappingManager method loadConfiguration.

/**
   * Primary class for loading Cassandra configuration from the 'MAPPING_FILE'.
   * 
   * @throws JDOMException
   * @throws IOException
   */
@SuppressWarnings("unchecked")
public void loadConfiguration() throws JDOMException, IOException {
    SAXBuilder saxBuilder = new SAXBuilder();
    // get mapping file
    InputStream inputStream = getClass().getClassLoader().getResourceAsStream(MAPPING_FILE);
    if (inputStream == null) {
        LOG.warn("Mapping file '" + MAPPING_FILE + "' could not be found!");
        throw new IOException("Mapping file '" + MAPPING_FILE + "' could not be found!");
    }
    Document document = saxBuilder.build(inputStream);
    if (document == null) {
        LOG.warn("Mapping file '" + MAPPING_FILE + "' could not be found!");
        throw new IOException("Mapping file '" + MAPPING_FILE + "' could not be found!");
    }
    Element root = document.getRootElement();
    // find cassandra keyspace element
    List<Element> keyspaces = root.getChildren(KEYSPACE_ELEMENT);
    if (keyspaces == null || keyspaces.size() == 0) {
        LOG.error("Error locating Cassandra Keyspace element!");
    } else {
        for (Element keyspace : keyspaces) {
            // log name, cluster and host for given keyspace(s)
            String keyspaceName = keyspace.getAttributeValue(NAME_ATTRIBUTE);
            String clusterName = keyspace.getAttributeValue(CLUSTER_ATTRIBUTE);
            String hostName = keyspace.getAttributeValue(HOST_ATTRIBUTE);
            if (LOG.isDebugEnabled()) {
                LOG.debug("Located Cassandra Keyspace: '" + keyspaceName + "' in cluster '" + clusterName + "' on host '" + hostName + "'.");
            }
            if (keyspaceName == null) {
                LOG.error("Error locating Cassandra Keyspace name attribute!");
                continue;
            }
            keyspaceMap.put(keyspaceName, keyspace);
        }
    }
    // load column definitions    
    List<Element> mappings = root.getChildren(MAPPING_ELEMENT);
    if (mappings == null || mappings.size() == 0) {
        LOG.error("Error locating Cassandra Mapping class element!");
    } else {
        for (Element mapping : mappings) {
            // associate persistent and class names for keyspace(s)
            String className = mapping.getAttributeValue(NAME_ATTRIBUTE);
            String keyClassName = mapping.getAttributeValue(KEYCLASS_ATTRIBUTE);
            String keyspaceName = mapping.getAttributeValue(KEYSPACE_ELEMENT);
            if (LOG.isDebugEnabled()) {
                LOG.debug("Located Cassandra Mapping: keyClass: '" + keyClassName + "' in storage class '" + className + "' for Keyspace '" + keyspaceName + "'.");
            }
            if (className == null) {
                LOG.error("Error locating Cassandra Mapping class name attribute!");
                continue;
            }
            mappingMap.put(className, mapping);
        }
    }
}
Also used : SAXBuilder(org.jdom.input.SAXBuilder) InputStream(java.io.InputStream) Element(org.jdom.Element) IOException(java.io.IOException) Document(org.jdom.Document)

Aggregations

Document (org.jdom.Document)144 Element (org.jdom.Element)102 SAXBuilder (org.jdom.input.SAXBuilder)51 IOException (java.io.IOException)49 JDOMException (org.jdom.JDOMException)29 File (java.io.File)27 ArrayList (java.util.ArrayList)22 XMLOutputter (org.jdom.output.XMLOutputter)22 List (java.util.List)16 StringReader (java.io.StringReader)15 Format (org.jdom.output.Format)12 VCDocument (org.vcell.util.document.VCDocument)11 XPath (org.jdom.xpath.XPath)10 StringWriter (java.io.StringWriter)9 InputStream (java.io.InputStream)8 URL (java.net.URL)8 NotNull (org.jetbrains.annotations.NotNull)7 Nullable (org.jetbrains.annotations.Nullable)7 FileNotFoundException (java.io.FileNotFoundException)5 Writer (java.io.Writer)5