Search in sources :

Example 31 with Namespace

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

the class PathwayIOUtil method extractPathwayFromJDOM.

public static PathwayModel extractPathwayFromJDOM(Document jdomDocument, RDFXMLContext context, ClientTaskStatusSupport statusSupport) {
    PathwayModel pathwayModel = null;
    Element rootElement = jdomDocument.getRootElement();
    @SuppressWarnings("unchecked") List<Namespace> namespaces = rootElement.getAdditionalNamespaces();
    boolean itIsLevel3 = false;
    for (Namespace namespace : namespaces) {
        if (namespace != null && namespace.getURI() != null && namespace.getURI().equals(BioPAX3.ns.uri)) {
            itIsLevel3 = true;
            break;
        }
    }
    if (itIsLevel3) {
        PathwayReaderBiopax3 pathwayReader = new PathwayReaderBiopax3(context);
        pathwayModel = pathwayReader.parse(rootElement, true);
    } else {
        // if it's not level3 we assume it to be level2
        // TODO: once biopax version3 becomes dominant change the code to use that as the default
        PathwayReader pathwayReader = new PathwayReader(context);
        pathwayModel = pathwayReader.parse(rootElement, statusSupport);
    }
    pathwayModel.reconcileReferences(statusSupport);
    pathwayModel.refreshParentMap();
    return pathwayModel;
}
Also used : Element(org.jdom.Element) PathwayModel(org.vcell.pathway.PathwayModel) Namespace(org.jdom.Namespace)

Example 32 with Namespace

use of org.jdom.Namespace 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 33 with Namespace

use of org.jdom.Namespace 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)

Example 34 with Namespace

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

the class JDOMStreamReader method getDeclaredURI.

private String getDeclaredURI(String string) {
    for (int i = namespaceStack.size() - 1; i == 0; i--) {
        Map<String, Namespace> nmspaces = namespaceStack.get(i);
        Namespace dec = nmspaces.get(string);
        if (dec != null) {
            return dec.getURI();
        }
    }
    return null;
}
Also used : Namespace(org.jdom.Namespace)

Example 35 with Namespace

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

the class JDOMStreamReader method setupNamespaces.

private void setupNamespaces(Element element) {
    namespaceContext.setElement(element);
    if (prefix2decNs != null) {
        namespaceStack.push(prefix2decNs);
    }
    prefix2decNs = new HashMap<>();
    namespaces.clear();
    for (Iterator<?> itr = element.getAdditionalNamespaces().iterator(); itr.hasNext(); ) {
        declare((Namespace) itr.next());
    }
    Namespace ns = element.getNamespace();
    if (shouldDeclare(ns)) {
        declare(ns);
    }
    for (Iterator<?> itr = element.getAttributes().iterator(); itr.hasNext(); ) {
        ns = ((Attribute) itr.next()).getNamespace();
        if (shouldDeclare(ns)) {
            declare(ns);
        }
    }
}
Also used : Namespace(org.jdom.Namespace)

Aggregations

Namespace (org.jdom.Namespace)35 Element (org.jdom.Element)32 Document (org.jdom.Document)7 IOException (java.io.IOException)5 VCDocument (org.vcell.util.document.VCDocument)5 StringReader (java.io.StringReader)4 ArrayList (java.util.ArrayList)4 BioPaxObject (org.vcell.pathway.BioPaxObject)4 Geometry (cbit.vcell.geometry.Geometry)3 Attribute (org.jdom.Attribute)3 ROI (cbit.vcell.VirtualMicroscopy.ROI)2 BioModel (cbit.vcell.biomodel.BioModel)2 MathDescription (cbit.vcell.math.MathDescription)2 ExpressionException (cbit.vcell.parser.ExpressionException)2 Simulation (cbit.vcell.solver.Simulation)2 StringWriter (java.io.StringWriter)2 Vector (java.util.Vector)2 XMLStreamException (javax.xml.stream.XMLStreamException)2 Content (org.jdom.Content)2 JDOMException (org.jdom.JDOMException)2