Search in sources :

Example 96 with Attribute

use of javax.xml.stream.events.Attribute in project tomee by apache.

the class StaxUtils method writeAttributeEvent.

private static void writeAttributeEvent(XMLEvent event, XMLStreamWriter writer) throws XMLStreamException {
    Attribute attr = (Attribute) event;
    QName name = attr.getName();
    String nsURI = name.getNamespaceURI();
    String localName = name.getLocalPart();
    String prefix = name.getPrefix();
    String value = attr.getValue();
    if (prefix != null) {
        writer.writeAttribute(prefix, nsURI, localName, value);
    } else if (nsURI != null) {
        writer.writeAttribute(nsURI, localName, value);
    } else {
        writer.writeAttribute(localName, value);
    }
}
Also used : Attribute(javax.xml.stream.events.Attribute) QName(javax.xml.namespace.QName)

Example 97 with Attribute

use of javax.xml.stream.events.Attribute in project jena by apache.

the class StAX2SAX method convertAttrs.

private Attributes convertAttrs(Iterator<Attribute> attributes) {
    AttributesImpl toReturn = new AttributesImpl();
    while (attributes.hasNext()) {
        Attribute a = attributes.next();
        toReturn.addAttribute(a.getName().getNamespaceURI(), a.getName().getLocalPart(), qnameToS(a.getName()), a.getDTDType(), a.getValue());
    }
    return toReturn;
}
Also used : AttributesImpl(org.xml.sax.helpers.AttributesImpl) Attribute(javax.xml.stream.events.Attribute)

Example 98 with Attribute

use of javax.xml.stream.events.Attribute in project cxf by apache.

the class StaxUtils method writeAttributeEvent.

private static void writeAttributeEvent(XMLEvent event, XMLStreamWriter writer) throws XMLStreamException {
    Attribute attr = (Attribute) event;
    QName name = attr.getName();
    String nsURI = name.getNamespaceURI();
    String localName = name.getLocalPart();
    String prefix = name.getPrefix();
    String value = attr.getValue();
    if (prefix != null) {
        writer.writeAttribute(prefix, nsURI, localName, value);
    } else if (nsURI != null) {
        writer.writeAttribute(nsURI, localName, value);
    } else {
        writer.writeAttribute(localName, value);
    }
}
Also used : Attribute(javax.xml.stream.events.Attribute) QName(javax.xml.namespace.QName)

Example 99 with Attribute

use of javax.xml.stream.events.Attribute in project cxf by apache.

the class StaxUtils method readDocElement.

public static Node readDocElement(Document doc, Node parent, XMLEvent ev, StreamToDOMContext context) throws XMLStreamException {
    switch(ev.getEventType()) {
        case XMLStreamConstants.START_ELEMENT:
            {
                context.incrementCount();
                Element e;
                StartElement startElem = ev.asStartElement();
                QName name = startElem.getName();
                if (!StringUtils.isEmpty(name.getPrefix())) {
                    e = doc.createElementNS(name.getNamespaceURI(), name.getPrefix() + ":" + name.getLocalPart());
                } else {
                    e = doc.createElementNS(name.getNamespaceURI(), name.getLocalPart());
                }
                e = (Element) parent.appendChild(e);
                if (context.isRecordLoc()) {
                    context.setRecordLoc(addLocation(doc, e, startElem.getLocation(), context.isRecordLoc()));
                }
                if (context.isRepairing() && !isDeclared(e, name.getNamespaceURI(), name.getPrefix())) {
                    declare(e, name.getNamespaceURI(), name.getPrefix());
                }
                context.pushToStack(parent);
                if (context.isThreshold() && MAX_ELEMENT_DEPTH_VAL != -1 && context.getStackSize() >= MAX_ELEMENT_DEPTH_VAL) {
                    throw new DepthExceededStaxException("reach the innerElementLevelThreshold:" + MAX_ELEMENT_DEPTH_VAL);
                }
                if (context.isThreshold() && MAX_CHILD_ELEMENTS_VAL != -1 && context.getCount() >= MAX_CHILD_ELEMENTS_VAL) {
                    throw new DepthExceededStaxException("reach the innerElementCountThreshold:" + MAX_CHILD_ELEMENTS_VAL);
                }
                parent = e;
                break;
            }
        case XMLStreamConstants.END_ELEMENT:
            if (context.isStackEmpty()) {
                return parent;
            }
            parent = context.popFromStack();
            if (parent instanceof Document || parent instanceof DocumentFragment) {
                return parent;
            }
            break;
        case XMLStreamConstants.NAMESPACE:
            Namespace ns = (Namespace) ev;
            declare((Element) parent, ns.getNamespaceURI(), ns.getPrefix());
            break;
        case XMLStreamConstants.ATTRIBUTE:
            Attribute at = (Attribute) ev;
            QName qname = at.getName();
            String attName = qname.getLocalPart();
            String attPrefix = qname.getPrefix();
            if (attPrefix != null && attPrefix.length() > 0) {
                attName = attPrefix + ":" + attName;
            }
            Attr attr = doc.createAttributeNS(qname.getNamespaceURI(), attName);
            attr.setValue(at.getValue());
            ((Element) parent).setAttributeNode(attr);
            break;
        case XMLStreamConstants.CHARACTERS:
            Characters characters = ev.asCharacters();
            context.setRecordLoc(addLocation(doc, parent.appendChild(doc.createTextNode(characters.getData())), characters.getLocation(), context.isRecordLoc()));
            break;
        case XMLStreamConstants.COMMENT:
            parent.appendChild(doc.createComment(((javax.xml.stream.events.Comment) ev).getText()));
            break;
        case XMLStreamConstants.CDATA:
            Characters cdata = ev.asCharacters();
            context.setRecordLoc(addLocation(doc, parent.appendChild(doc.createCDATASection(cdata.getData())), cdata.getLocation(), context.isRecordLoc()));
            break;
        case XMLStreamConstants.PROCESSING_INSTRUCTION:
            parent.appendChild(doc.createProcessingInstruction(((ProcessingInstruction) ev).getTarget(), ((ProcessingInstruction) ev).getData()));
            break;
        case XMLStreamConstants.ENTITY_REFERENCE:
            javax.xml.stream.events.EntityReference er = (javax.xml.stream.events.EntityReference) ev;
            parent.appendChild(doc.createEntityReference(er.getName()));
            break;
        default:
            break;
    }
    return parent;
}
Also used : Comment(org.w3c.dom.Comment) Attribute(javax.xml.stream.events.Attribute) QName(javax.xml.namespace.QName) StartElement(javax.xml.stream.events.StartElement) Element(org.w3c.dom.Element) Characters(javax.xml.stream.events.Characters) Document(org.w3c.dom.Document) StartDocument(javax.xml.stream.events.StartDocument) Namespace(javax.xml.stream.events.Namespace) Attr(org.w3c.dom.Attr) StartElement(javax.xml.stream.events.StartElement) EntityReference(org.w3c.dom.EntityReference) DocumentFragment(org.w3c.dom.DocumentFragment) ProcessingInstruction(org.w3c.dom.ProcessingInstruction)

Example 100 with Attribute

use of javax.xml.stream.events.Attribute in project cxf by apache.

the class CorbaStreamReader method getAttributeLocalName.

public String getAttributeLocalName(int arg0) {
    String ret = null;
    List<Attribute> currentAttributes = eventProducer.getAttributes();
    if (currentAttributes != null) {
        Attribute a = currentAttributes.get(arg0);
        if (a != null) {
            ret = a.getName().getLocalPart();
        }
    }
    return ret;
}
Also used : Attribute(javax.xml.stream.events.Attribute)

Aggregations

Attribute (javax.xml.stream.events.Attribute)144 QName (javax.xml.namespace.QName)73 StartElement (javax.xml.stream.events.StartElement)63 XMLEvent (javax.xml.stream.events.XMLEvent)54 XMLEventReader (javax.xml.stream.XMLEventReader)30 EndElement (javax.xml.stream.events.EndElement)26 Namespace (javax.xml.stream.events.Namespace)26 ArrayList (java.util.ArrayList)23 XMLStreamException (javax.xml.stream.XMLStreamException)20 XMLInputFactory (javax.xml.stream.XMLInputFactory)18 InputStream (java.io.InputStream)14 IOException (java.io.IOException)12 Iterator (java.util.Iterator)11 ByteArrayInputStream (java.io.ByteArrayInputStream)7 FileInputStream (java.io.FileInputStream)7 HashMap (java.util.HashMap)7 Test (org.junit.Test)7 HashSet (java.util.HashSet)6 Characters (javax.xml.stream.events.Characters)6 QNm (org.brackit.xquery.atomic.QNm)5