Search in sources :

Example 1 with Content

use of org.jdom.Content 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 2 with Content

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

the class MavenJDOMWriter method updateElement.

// -- void updateDistributionManagement(DistributionManagement, String, Counter, Element)
/**
     * Method updateElement
     *
     * @param counter
     * @param shouldExist
     * @param name
     * @param parent
     */
protected Element updateElement(Counter counter, Element parent, String name, boolean shouldExist) {
    Element element = parent.getChild(name, parent.getNamespace());
    if (element != null && shouldExist) {
        counter.increaseCount();
    }
    if (element == null && shouldExist) {
        element = factory.element(name, parent.getNamespace());
        insertAtPreferredLocation(parent, element, counter);
        counter.increaseCount();
    }
    if (!shouldExist && element != null) {
        int index = parent.indexOf(element);
        if (index > 0) {
            Content previous = parent.getContent(index - 1);
            if (previous instanceof Text) {
                Text txt = (Text) previous;
                if (txt.getTextTrim().length() == 0) {
                    parent.removeContent(txt);
                }
            }
        }
        parent.removeContent(element);
    }
    return element;
}
Also used : Content(org.jdom.Content) Element(org.jdom.Element) Text(org.jdom.Text)

Example 3 with Content

use of org.jdom.Content in project intellij-community by JetBrains.

the class AbstractCollectionBinding method serialize.

@Nullable
@Override
public Object serialize(@NotNull Object o, @Nullable Object context, @Nullable SerializationFilter filter) {
    Collection<Object> collection = getIterable(o);
    String tagName = getTagName(o);
    if (tagName == null) {
        List<Object> result = new SmartList<Object>();
        if (!ContainerUtil.isEmpty(collection)) {
            for (Object item : collection) {
                ContainerUtil.addAllNotNull(result, serializeItem(item, result, filter));
            }
        }
        return result;
    } else {
        Element result = new Element(tagName);
        if (!ContainerUtil.isEmpty(collection)) {
            for (Object item : collection) {
                Content child = (Content) serializeItem(item, result, filter);
                if (child != null) {
                    result.addContent(child);
                }
            }
        }
        return result;
    }
}
Also used : Content(org.jdom.Content) Element(org.jdom.Element) SmartList(com.intellij.util.SmartList) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with Content

use of org.jdom.Content in project intellij-community by JetBrains.

the class XmlSerializerImpl method getTextValue.

@NotNull
static String getTextValue(@NotNull Element element, @NotNull String defaultText) {
    List<Content> content = element.getContent();
    int size = content.size();
    StringBuilder builder = null;
    for (int i = 0; i < size; i++) {
        Content child = content.get(i);
        if (child instanceof Text) {
            String value = child.getValue();
            if (builder == null && i == (size - 1)) {
                return value;
            }
            if (builder == null) {
                builder = new StringBuilder();
            }
            builder.append(value);
        }
    }
    return builder == null ? defaultText : builder.toString();
}
Also used : Content(org.jdom.Content) Text(org.jdom.Text) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with Content

use of org.jdom.Content in project intellij-community by JetBrains.

the class CommonCodeStyleSettingsManager method writeExternal.

public void writeExternal(@NotNull Element element) throws WriteExternalException {
    synchronized (this) {
        if (myCommonSettingsMap == null) {
            return;
        }
        final Map<String, Language> idToLang = new THashMap<>();
        for (Language language : myCommonSettingsMap.keySet()) {
            idToLang.put(language.getID(), language);
        }
        String[] languages = ArrayUtil.toStringArray(ContainerUtil.union(myUnknownSettingsMap.keySet(), idToLang.keySet()));
        Arrays.sort(languages);
        for (String id : languages) {
            final Language language = idToLang.get(id);
            if (language != null) {
                final CommonCodeStyleSettings commonSettings = myCommonSettingsMap.get(language);
                Element commonSettingsElement = new Element(COMMON_SETTINGS_TAG);
                commonSettings.writeExternal(commonSettingsElement);
                commonSettingsElement.setAttribute(LANGUAGE_ATTR, language.getID());
                if (!commonSettingsElement.getChildren().isEmpty()) {
                    element.addContent(commonSettingsElement);
                }
            } else {
                final Content unknown = myUnknownSettingsMap.get(id);
                if (unknown != null) {
                    element.addContent(unknown.detach());
                }
            }
        }
    }
}
Also used : THashMap(gnu.trove.THashMap) Language(com.intellij.lang.Language) Content(org.jdom.Content) Element(org.jdom.Element)

Aggregations

Content (org.jdom.Content)7 Element (org.jdom.Element)5 Text (org.jdom.Text)2 Language (com.intellij.lang.Language)1 SmartList (com.intellij.util.SmartList)1 THashMap (gnu.trove.THashMap)1 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 List (java.util.List)1 Attribute (org.jdom.Attribute)1 Document (org.jdom.Document)1 JDOMException (org.jdom.JDOMException)1 SAXBuilder (org.jdom.input.SAXBuilder)1 NotNull (org.jetbrains.annotations.NotNull)1 Nullable (org.jetbrains.annotations.Nullable)1 EntityResolver (org.xml.sax.EntityResolver)1 InputSource (org.xml.sax.InputSource)1 SAXException (org.xml.sax.SAXException)1