Search in sources :

Example 6 with Content

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

the class JDOMBuilder method tag.

public static Element tag(String name, Content... content) {
    final Element element = new Element(name);
    for (Content c : content) {
        if (c instanceof AttrContent) {
            AttrContent attrContent = (AttrContent) c;
            element.setAttribute(attrContent.myName, attrContent.myValue);
        } else {
            element.addContent(c);
        }
    }
    return element;
}
Also used : Content(org.jdom.Content) Element(org.jdom.Element)

Example 7 with Content

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

the class Binding method addContent.

protected static void addContent(@NotNull final Element targetElement, final Object node) {
    if (node instanceof Content) {
        Content content = (Content) node;
        targetElement.addContent(content);
    } else if (node instanceof List) {
        //noinspection unchecked
        targetElement.addContent((List) node);
    } else {
        throw new IllegalArgumentException("Wrong node: " + node);
    }
}
Also used : Content(org.jdom.Content) List(java.util.List)

Example 8 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)

Example 9 with Content

use of org.jdom.Content in project cxf by apache.

the class StaxBuilder method buildTree.

/**
 * This takes a <code>XMLStreamReader</code> and builds up a JDOM tree.
 * Recursion has been eliminated by using local stack of open elements; this
 * improves performance somewhat (classic
 * recursion-by-iteration-and-explicit stack transformation)
 *
 * @param node <code>Code</node> to examine.
 * @param doc JDOM <code>Document</code> being built.
 */
private void buildTree(JDOMFactory f, XMLStreamReader r, Document doc) throws XMLStreamException {
    // At top level
    Element current = null;
    int event = r.getEventType();
    // if we're at the start then we need to do a next
    if (event == -1) {
        event = r.next();
    }
    while (true) {
        boolean noadd = false;
        Content child = null;
        switch(event) {
            case XMLStreamConstants.CDATA:
                child = f.cdata(r.getText());
                break;
            case XMLStreamConstants.SPACE:
                if (cfgIgnoreWS) {
                    noadd = true;
                    break;
                }
            case XMLStreamConstants.CHARACTERS:
                /*
                 * Small complication: although (ignorable) white space is
                 * allowed in prolog/epilog, and StAX may report such event,
                 * JDOM barfs if trying to add it. Thus, let's just ignore all
                 * textual stuff outside the tree:
                 */
                if (current == null) {
                    noadd = true;
                    break;
                }
                child = f.text(r.getText());
                break;
            case XMLStreamConstants.COMMENT:
                child = f.comment(r.getText());
                break;
            case XMLStreamConstants.END_DOCUMENT:
                return;
            case XMLStreamConstants.END_ELEMENT:
                /**
                 * If current.getParentElement() previously returned null and we
                 * get this event again we shouldn't bail out with a
                 * NullPointerException
                 */
                if (current != null) {
                    current = current.getParentElement();
                }
                noadd = true;
                if (isReadingMidStream && current == null)
                    return;
                break;
            case XMLStreamConstants.ENTITY_DECLARATION:
            case XMLStreamConstants.NOTATION_DECLARATION:
                /*
                 * Shouldn't really get these, but maybe some stream readers do
                 * provide the info. If so, better ignore it -- DTD event should
                 * have most/all we need.
                 */
                noadd = true;
                break;
            case XMLStreamConstants.ENTITY_REFERENCE:
                child = f.entityRef(r.getLocalName());
                break;
            case XMLStreamConstants.PROCESSING_INSTRUCTION:
                child = f.processingInstruction(r.getPITarget(), r.getPIData());
                break;
            case XMLStreamConstants.START_ELEMENT:
                {
                    // Ok, need to add a new element and simulate recursion
                    Element newElem = null;
                    String nsURI = r.getNamespaceURI();
                    // needed for special
                    String elemPrefix = r.getPrefix();
                    // handling of elem's
                    // namespace
                    String ln = r.getLocalName();
                    if (nsURI == null || nsURI.length() == 0) {
                        if (elemPrefix == null || elemPrefix.length() == 0) {
                            newElem = f.element(ln);
                        } else {
                            /*
                         * Happens when a prefix is bound to the default (empty)
                         * namespace...
                         */
                            newElem = f.element(ln, elemPrefix, "");
                        }
                    } else {
                        newElem = f.element(ln, elemPrefix, nsURI);
                    }
                    /*
                 * Let's add element right away (probably have to do it to bind
                 * attribute namespaces, too)
                 */
                    if (current == null) {
                        // at root
                        doc.setRootElement(newElem);
                        if (additionalNamespaces != null) {
                            for (Iterator<String> iter = additionalNamespaces.keySet().iterator(); iter.hasNext(); ) {
                                String prefix = iter.next();
                                String uri = additionalNamespaces.get(prefix);
                                newElem.addNamespaceDeclaration(Namespace.getNamespace(prefix, uri));
                            }
                        }
                    } else {
                        f.addContent(current, newElem);
                    }
                    // Any declared namespaces?
                    int i;
                    int len;
                    for (i = 0, len = r.getNamespaceCount(); i < len; ++i) {
                        String prefix = r.getNamespacePrefix(i);
                        Namespace ns = Namespace.getNamespace(prefix, r.getNamespaceURI(i));
                        // JDOM has special handling for element's "own" ns:
                        if (prefix != null && prefix.equals(elemPrefix)) {
                        // already set by when it was constructed...
                        } else {
                            f.addNamespaceDeclaration(newElem, ns);
                        }
                    }
                    // And then the attributes:
                    for (i = 0, len = r.getAttributeCount(); i < len; ++i) {
                        String prefix = r.getAttributePrefix(i);
                        Namespace ns;
                        if (prefix == null || prefix.length() == 0) {
                            // Attribute not in any namespace
                            ns = Namespace.NO_NAMESPACE;
                        } else {
                            ns = newElem.getNamespace(prefix);
                        }
                        Attribute attr = f.attribute(r.getAttributeLocalName(i), r.getAttributeValue(i), resolveAttrType(r.getAttributeType(i)), ns);
                        f.setAttribute(newElem, attr);
                    }
                    // And then 'push' new element...
                    current = newElem;
                    // Already added the element, can continue
                    noadd = true;
                    break;
                }
            case XMLStreamConstants.START_DOCUMENT:
            case XMLStreamConstants.DTD:
            // case XMLStreamConstants.NAMESPACE:
            default:
                /*
                 * throw new XMLStreamException("Unrecognized iterator event
                 * type: " + r.getEventType() + "; should not receive such types
                 * (broken stream reader?)");
                 */
                break;
        }
        if (!noadd && child != null) {
            if (current == null) {
                f.addContent(doc, child);
            } else {
                f.addContent(current, child);
            }
        }
        if (r.hasNext()) {
            event = r.next();
        } else {
            break;
        }
    }
}
Also used : Attribute(org.jdom.Attribute) Content(org.jdom.Content) Element(org.jdom.Element) Iterator(java.util.Iterator) Namespace(org.jdom.Namespace)

Example 10 with Content

use of org.jdom.Content in project cxf by apache.

the class StaxSerializer method writeElement.

public void writeElement(Element e, XMLStreamWriter writer) throws XMLStreamException {
    // need to check if the namespace is declared before we write the
    // start element because that will put the namespace in the context.
    String elPrefix = e.getNamespacePrefix();
    String elUri = e.getNamespaceURI();
    String boundPrefix = writer.getPrefix(elUri);
    boolean writeElementNS = false;
    if (boundPrefix == null || !elPrefix.equals(boundPrefix)) {
        writeElementNS = true;
    }
    writer.writeStartElement(elPrefix, e.getName(), elUri);
    List<?> namespaces = e.getAdditionalNamespaces();
    for (Iterator<?> itr = namespaces.iterator(); itr.hasNext(); ) {
        Namespace ns = (Namespace) itr.next();
        String prefix = ns.getPrefix();
        String uri = ns.getURI();
        writer.setPrefix(prefix, uri);
        writer.writeNamespace(prefix, uri);
        if (elUri.equals(uri) && elPrefix.equals(prefix)) {
            writeElementNS = false;
        }
    }
    for (Iterator<?> itr = e.getAttributes().iterator(); itr.hasNext(); ) {
        Attribute attr = (Attribute) itr.next();
        String attPrefix = attr.getNamespacePrefix();
        String attUri = attr.getNamespaceURI();
        if (attUri == null || attUri.equals("")) {
            writer.writeAttribute(attr.getName(), attr.getValue());
        } else {
            writer.writeAttribute(attPrefix, attUri, attr.getName(), attr.getValue());
            if (!isDeclared(writer, attPrefix, attUri)) {
                if (elUri.equals(attUri) && elPrefix.equals(attPrefix)) {
                    if (writeElementNS) {
                        writer.setPrefix(attPrefix, attUri);
                        writer.writeNamespace(attPrefix, attUri);
                        writeElementNS = false;
                    }
                } else {
                    writer.setPrefix(attPrefix, attUri);
                    writer.writeNamespace(attPrefix, attUri);
                }
            }
        }
    }
    if (writeElementNS) {
        if (elPrefix == null || elPrefix.length() == 0) {
            writer.writeDefaultNamespace(elUri);
        } else {
            writer.writeNamespace(elPrefix, elUri);
        }
    }
    for (Iterator<?> itr = e.getContent().iterator(); itr.hasNext(); ) {
        Content n = (Content) itr.next();
        if (n instanceof CDATA) {
            writer.writeCData(n.getValue());
        } else if (n instanceof Text) {
            writer.writeCharacters(((Text) n).getText());
        } else if (n instanceof Element) {
            writeElement((Element) n, writer);
        } else if (n instanceof Comment) {
            writer.writeComment(n.getValue());
        } else if (n instanceof EntityRef) {
        // EntityRef ref = (EntityRef) n;
        // writer.writeEntityRef(ref.)
        }
    }
    writer.writeEndElement();
}
Also used : Comment(org.jdom.Comment) Attribute(org.jdom.Attribute) Content(org.jdom.Content) Element(org.jdom.Element) Text(org.jdom.Text) CDATA(org.jdom.CDATA) EntityRef(org.jdom.EntityRef) Namespace(org.jdom.Namespace)

Aggregations

Content (org.jdom.Content)10 Element (org.jdom.Element)8 Attribute (org.jdom.Attribute)3 Text (org.jdom.Text)3 Namespace (org.jdom.Namespace)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 Iterator (java.util.Iterator)1 List (java.util.List)1 CDATA (org.jdom.CDATA)1 Comment (org.jdom.Comment)1 Document (org.jdom.Document)1 EntityRef (org.jdom.EntityRef)1 JDOMException (org.jdom.JDOMException)1 SAXBuilder (org.jdom.input.SAXBuilder)1 NotNull (org.jetbrains.annotations.NotNull)1 Nullable (org.jetbrains.annotations.Nullable)1