Search in sources :

Example 61 with Attribute

use of javax.xml.stream.events.Attribute in project spring-framework by spring-projects.

the class StaxEventXMLReader method handleStartElement.

private void handleStartElement(StartElement startElement) throws SAXException {
    if (getContentHandler() != null) {
        QName qName = startElement.getName();
        if (hasNamespacesFeature()) {
            for (Iterator i = startElement.getNamespaces(); i.hasNext(); ) {
                Namespace namespace = (Namespace) i.next();
                startPrefixMapping(namespace.getPrefix(), namespace.getNamespaceURI());
            }
            for (Iterator i = startElement.getAttributes(); i.hasNext(); ) {
                Attribute attribute = (Attribute) i.next();
                QName attributeName = attribute.getName();
                startPrefixMapping(attributeName.getPrefix(), attributeName.getNamespaceURI());
            }
            getContentHandler().startElement(qName.getNamespaceURI(), qName.getLocalPart(), toQualifiedName(qName), getAttributes(startElement));
        } else {
            getContentHandler().startElement("", "", toQualifiedName(qName), getAttributes(startElement));
        }
    }
}
Also used : Attribute(javax.xml.stream.events.Attribute) QName(javax.xml.namespace.QName) Iterator(java.util.Iterator) Namespace(javax.xml.stream.events.Namespace)

Example 62 with Attribute

use of javax.xml.stream.events.Attribute in project spring-framework by spring-projects.

the class StaxEventXMLReader method getAttributes.

private Attributes getAttributes(StartElement event) {
    AttributesImpl attributes = new AttributesImpl();
    for (Iterator i = event.getAttributes(); i.hasNext(); ) {
        Attribute attribute = (Attribute) i.next();
        QName qName = attribute.getName();
        String namespace = qName.getNamespaceURI();
        if (namespace == null || !hasNamespacesFeature()) {
            namespace = "";
        }
        String type = attribute.getDTDType();
        if (type == null) {
            type = "CDATA";
        }
        attributes.addAttribute(namespace, qName.getLocalPart(), toQualifiedName(qName), type, attribute.getValue());
    }
    if (hasNamespacePrefixesFeature()) {
        for (Iterator i = event.getNamespaces(); i.hasNext(); ) {
            Namespace namespace = (Namespace) i.next();
            String prefix = namespace.getPrefix();
            String namespaceUri = namespace.getNamespaceURI();
            String qName;
            if (StringUtils.hasLength(prefix)) {
                qName = "xmlns:" + prefix;
            } else {
                qName = "xmlns";
            }
            attributes.addAttribute("", "", qName, "CDATA", namespaceUri);
        }
    }
    return attributes;
}
Also used : AttributesImpl(org.xml.sax.helpers.AttributesImpl) Attribute(javax.xml.stream.events.Attribute) QName(javax.xml.namespace.QName) Iterator(java.util.Iterator) Namespace(javax.xml.stream.events.Namespace)

Example 63 with Attribute

use of javax.xml.stream.events.Attribute in project kernel by exoplatform.

the class InitialContextBinder method readBindings.

/**
 * Import references from xml-file.
 *
 * @return map with bind name - references
 *
 * @throws XMLStreamException
 *          if errors occurs during import
 * @throws FileNotFoundException
 *          if can't open input stream from file
 */
protected Map<String, Reference> readBindings() throws FileNotFoundException, XMLStreamException {
    Stack<RefEntity> stack = new Stack<RefEntity>();
    Map<String, Reference> importedRefs = new HashMap<String, Reference>();
    XMLInputFactory factory = XMLInputFactory.newInstance();
    XMLEventReader reader = factory.createXMLEventReader(PrivilegedFileHelper.fileInputStream(bindingsStorePath), "UTF-8");
    while (reader.hasNext()) {
        XMLEvent event = reader.nextEvent();
        switch(event.getEventType()) {
            case XMLStreamConstants.START_ELEMENT:
                StartElement startElement = event.asStartElement();
                Map<String, String> attr = new HashMap<String, String>();
                Iterator attributes = startElement.getAttributes();
                while (attributes.hasNext()) {
                    Attribute attribute = (Attribute) attributes.next();
                    attr.put(attribute.getName().getLocalPart(), attribute.getValue());
                }
                String localName = startElement.getName().getLocalPart();
                if (localName.equals(REFERENCE_ELEMENT)) {
                    String bindName = attr.get(BIND_NAME_ATTR);
                    String className = attr.get(CLASS_NAME_ATTR);
                    String factoryName = attr.get(FACTORY_ATTR);
                    String factoryLocation = attr.get(FACTORY_LOCATION_ATTR);
                    Reference reference = new Reference(className, factoryName, factoryLocation);
                    stack.push(new RefEntity(bindName, reference));
                } else if (localName.equals(PROPERTY_ELEMENT)) {
                    RefEntity refEntity = stack.pop();
                    Reference reference = refEntity.getValue();
                    for (Entry<String, String> entry : attr.entrySet()) {
                        reference.add(new StringRefAddr(entry.getKey(), entry.getValue()));
                    }
                    refEntity.setValue(reference);
                    stack.push(refEntity);
                }
                break;
            case XMLStreamConstants.END_ELEMENT:
                EndElement endElement = event.asEndElement();
                localName = endElement.getName().getLocalPart();
                if (localName.equals(REFERENCE_ELEMENT)) {
                    RefEntity refEntity = stack.pop();
                    importedRefs.put(refEntity.getKey(), refEntity.getValue());
                }
                break;
            default:
                break;
        }
    }
    return importedRefs;
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Attribute(javax.xml.stream.events.Attribute) EndElement(javax.xml.stream.events.EndElement) Reference(javax.naming.Reference) Stack(java.util.Stack) StartElement(javax.xml.stream.events.StartElement) Entry(java.util.Map.Entry) StringRefAddr(javax.naming.StringRefAddr) XMLEvent(javax.xml.stream.events.XMLEvent) Iterator(java.util.Iterator) XMLEventReader(javax.xml.stream.XMLEventReader) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 64 with Attribute

use of javax.xml.stream.events.Attribute in project Payara by payara.

the class DomainXmlTransformer method getReplaceAttributeInStartEvent.

/**
 * Create a new start element based on the original but that replaces attribute value
 */
private StartElement getReplaceAttributeInStartEvent(XMLEvent event, String attr_name, String attr_value) {
    Set attributes = new HashSet();
    for (java.util.Iterator i = event.asStartElement().getAttributes(); i.hasNext(); ) {
        Attribute a = (Attribute) i.next();
        if (!a.getName().getLocalPart().equals(attr_name)) {
            attributes.add(a);
        }
    }
    Attribute newAttribute = xmlEventFactory.createAttribute(attr_name, attr_value);
    attributes.add(newAttribute);
    StartElement oldStartEvent = event.asStartElement();
    return xmlEventFactory.createStartElement(oldStartEvent.getName(), attributes.iterator(), oldStartEvent.getNamespaces());
}
Also used : StartElement(javax.xml.stream.events.StartElement) Set(java.util.Set) HashSet(java.util.HashSet) Attribute(javax.xml.stream.events.Attribute) HashSet(java.util.HashSet)

Example 65 with Attribute

use of javax.xml.stream.events.Attribute in project Payara by payara.

the class DomainXmlTransformer method getSkippedElementStartEvent.

/**
 * Create a new start element based on the original but that does not include
 * the specified attribute.
 */
private StartElement getSkippedElementStartEvent(XMLEvent event) {
    Set attributes = new HashSet();
    for (java.util.Iterator i = event.asStartElement().getAttributes(); i.hasNext(); ) {
        Attribute a = (Attribute) i.next();
        if (!DISABLE_SUB_ELEMENTS.contains(a.getName().getLocalPart())) {
            attributes.add(a);
        }
    }
    StartElement oldStartEvent = event.asStartElement();
    return xmlEventFactory.createStartElement(oldStartEvent.getName(), attributes.iterator(), oldStartEvent.getNamespaces());
}
Also used : StartElement(javax.xml.stream.events.StartElement) Set(java.util.Set) HashSet(java.util.HashSet) Attribute(javax.xml.stream.events.Attribute) HashSet(java.util.HashSet)

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