Search in sources :

Example 6 with EndElement

use of javax.xml.stream.events.EndElement in project omegat by omegat-org.

the class TMXReader2 method parseSegExtLevel1.

/**
 * External TMX - level 1. Skip text inside inline tags.
 */
protected void parseSegExtLevel1() throws Exception {
    segContent.setLength(0);
    int inlineLevel = 0;
    while (true) {
        XMLEvent e = xml.nextEvent();
        switch(e.getEventType()) {
            case XMLEvent.START_ELEMENT:
                inlineLevel++;
                break;
            case XMLEvent.END_ELEMENT:
                inlineLevel--;
                EndElement eEnd = (EndElement) e;
                if ("seg".equals(eEnd.getName().getLocalPart())) {
                    return;
                }
                break;
            case XMLEvent.CHARACTERS:
                if (inlineLevel == 0) {
                    Characters c = (Characters) e;
                    segContent.append(c.getData());
                }
                break;
        }
    }
}
Also used : EndElement(javax.xml.stream.events.EndElement) Characters(javax.xml.stream.events.Characters) XMLEvent(javax.xml.stream.events.XMLEvent)

Example 7 with EndElement

use of javax.xml.stream.events.EndElement in project omegat by omegat-org.

the class TMXReader2 method parseNote.

protected void parseNote(StartElement element) throws Exception {
    noteContent.setLength(0);
    while (true) {
        XMLEvent e = xml.nextEvent();
        switch(e.getEventType()) {
            case XMLEvent.END_ELEMENT:
                EndElement eEnd = (EndElement) e;
                if ("note".equals(eEnd.getName().getLocalPart())) {
                    currentTu.note = noteContent.toString();
                    return;
                }
                break;
            case XMLEvent.CHARACTERS:
                Characters c = (Characters) e;
                noteContent.append(c.getData());
                break;
        }
    }
}
Also used : EndElement(javax.xml.stream.events.EndElement) Characters(javax.xml.stream.events.Characters) XMLEvent(javax.xml.stream.events.XMLEvent)

Example 8 with EndElement

use of javax.xml.stream.events.EndElement 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 9 with EndElement

use of javax.xml.stream.events.EndElement in project wso2-synapse by wso2.

the class SimpleXMLEventFactoryTest method testCreateEndElement_Prefix_NamespaceURI_LocalName.

@Test
public void testCreateEndElement_Prefix_NamespaceURI_LocalName() throws XMLStreamException {
    EndElement event = factory.createEndElement("foo", "http://foo", "bar");
    verify(event, XMLStreamConstants.END_ELEMENT, "</foo:bar>");
}
Also used : EndElement(javax.xml.stream.events.EndElement) Test(org.junit.Test)

Example 10 with EndElement

use of javax.xml.stream.events.EndElement in project iaf by ibissource.

the class SchemaUtils method xsdToXmlStreamWriter.

/**
 * Including a {@link nl.nn.adapterframework.validation.XSD} into an
 * {@link javax.xml.stream.XMLStreamWriter} while parsing it. It is parsed
 * (using a low level {@link javax.xml.stream.XMLEventReader} so that
 * certain things can be corrected on the fly.
 * @param xsd
 * @param xmlStreamWriter
 * @param standalone
 * When standalone the start and end document contants are ignored, hence
 * the xml declaration is ignored.
 * @param stripSchemaLocationFromImport
 * Useful when generating a WSDL which should contain all XSD's inline
 * (without includes or imports). The XSD might have an import with
 * schemaLocation to make it valid on it's own, when
 * stripSchemaLocationFromImport is true it will be removed.
 * @throws java.io.IOException
 * @throws javax.xml.stream.XMLStreamException
 */
public static void xsdToXmlStreamWriter(final XSD xsd, XMLStreamWriter xmlStreamWriter, boolean standalone, boolean stripSchemaLocationFromImport, boolean skipRootStartElement, boolean skipRootEndElement, List<Attribute> rootAttributes, List<Attribute> rootNamespaceAttributes, List<XMLEvent> imports, boolean noOutput) throws IOException, ConfigurationException {
    Map<String, String> namespacesToCorrect = new HashMap<String, String>();
    NamespaceCorrectingXMLStreamWriter namespaceCorrectingXMLStreamWriter = new NamespaceCorrectingXMLStreamWriter(xmlStreamWriter, namespacesToCorrect);
    final XMLStreamEventWriter streamEventWriter = new XMLStreamEventWriter(namespaceCorrectingXMLStreamWriter);
    InputStream in = null;
    in = xsd.getInputStream();
    if (in == null) {
        throw new IllegalStateException("" + xsd + " not found");
    }
    XMLEvent event = null;
    try {
        XMLEventReader er = XmlUtils.INPUT_FACTORY.createXMLEventReader(in, XmlUtils.STREAM_FACTORY_ENCODING);
        while (er.hasNext()) {
            event = er.nextEvent();
            switch(event.getEventType()) {
                case XMLStreamConstants.START_DOCUMENT:
                case XMLStreamConstants.END_DOCUMENT:
                    if (!standalone) {
                        continue;
                    }
                // fall through
                case XMLStreamConstants.SPACE:
                case XMLStreamConstants.COMMENT:
                    break;
                case XMLStreamConstants.START_ELEMENT:
                    StartElement startElement = event.asStartElement();
                    if (SCHEMA.equals(startElement.getName())) {
                        if (skipRootStartElement) {
                            continue;
                        }
                        if (rootAttributes != null) {
                            // Collect or write attributes of schema element.
                            if (noOutput) {
                                // First call to this method collecting
                                // schema attributes.
                                Iterator<Attribute> iterator = startElement.getAttributes();
                                while (iterator.hasNext()) {
                                    Attribute attribute = iterator.next();
                                    boolean add = true;
                                    for (Attribute attribute2 : rootAttributes) {
                                        if (XmlUtils.attributesEqual(attribute, attribute2)) {
                                            add = false;
                                        }
                                    }
                                    if (add) {
                                        rootAttributes.add(attribute);
                                    }
                                }
                                iterator = startElement.getNamespaces();
                                while (iterator.hasNext()) {
                                    Attribute attribute = iterator.next();
                                    boolean add = true;
                                    for (Attribute attribute2 : rootNamespaceAttributes) {
                                        if (XmlUtils.attributesEqual(attribute, attribute2)) {
                                            add = false;
                                        }
                                    }
                                    if (add) {
                                        rootNamespaceAttributes.add(attribute);
                                    }
                                }
                            } else {
                                // Second call to this method writing attributes
                                // from previous call.
                                startElement = XmlUtils.EVENT_FACTORY.createStartElement(startElement.getName().getPrefix(), startElement.getName().getNamespaceURI(), startElement.getName().getLocalPart(), rootAttributes.iterator(), rootNamespaceAttributes.iterator(), startElement.getNamespaceContext());
                            }
                        }
                        // (see http://www.w3.org/TR/xml-names/#ns-decl).
                        if (xsd.isAddNamespaceToSchema() && !xsd.getNamespace().equals("http://www.w3.org/XML/1998/namespace")) {
                            event = XmlUtils.mergeAttributes(startElement, Arrays.asList(new AttributeEvent(TNS, xsd.getNamespace()), new AttributeEvent(ELFORMDEFAULT, "qualified")).iterator(), Arrays.asList(XmlUtils.EVENT_FACTORY.createNamespace(xsd.getNamespace())).iterator(), XmlUtils.EVENT_FACTORY);
                            if (!event.equals(startElement)) {
                                Attribute tns = startElement.getAttributeByName(TNS);
                                if (tns != null) {
                                    String s = tns.getValue();
                                    if (!s.equals(xsd.getNamespace())) {
                                        namespacesToCorrect.put(s, xsd.getNamespace());
                                    }
                                }
                            }
                        } else {
                            event = startElement;
                        }
                        if (imports != null && !noOutput) {
                            // 2 on every iteration.
                            for (int i = 0; i < imports.size(); i = i + 2) {
                                boolean skip = false;
                                for (int j = 0; j < i; j = j + 2) {
                                    Attribute attribute1 = imports.get(i).asStartElement().getAttributeByName(NAMESPACE);
                                    Attribute attribute2 = imports.get(j).asStartElement().getAttributeByName(NAMESPACE);
                                    if (attribute1 != null && attribute2 != null && attribute1.getValue().equals(attribute2.getValue())) {
                                        skip = true;
                                    }
                                }
                                if (!skip) {
                                    streamEventWriter.add(event);
                                    event = imports.get(i);
                                    streamEventWriter.add(event);
                                    event = imports.get(i + 1);
                                }
                            }
                        }
                    } else if (startElement.getName().equals(INCLUDE)) {
                        continue;
                    // } else if (startElement.getName().equals(REDEFINE)) {
                    // continue;
                    } else if (startElement.getName().equals(IMPORT)) {
                        if (imports == null || noOutput) {
                            // Not collecting or writing import elements.
                            Attribute schemaLocation = startElement.getAttributeByName(SCHEMALOCATION);
                            if (schemaLocation != null) {
                                String location = schemaLocation.getValue();
                                if (stripSchemaLocationFromImport) {
                                    List<Attribute> attributes = new ArrayList<Attribute>();
                                    Iterator<Attribute> iterator = startElement.getAttributes();
                                    while (iterator.hasNext()) {
                                        Attribute a = iterator.next();
                                        if (!SCHEMALOCATION.equals(a.getName())) {
                                            attributes.add(a);
                                        }
                                    }
                                    event = new StartElementEvent(startElement.getName(), attributes.iterator(), startElement.getNamespaces(), startElement.getNamespaceContext(), startElement.getLocation(), startElement.getSchemaType());
                                } else {
                                    String relativeTo = xsd.getParentLocation();
                                    if (relativeTo.length() > 0 && location.startsWith(relativeTo)) {
                                        location = location.substring(relativeTo.length());
                                    }
                                    event = XMLStreamUtils.mergeAttributes(startElement, Collections.singletonList(new AttributeEvent(SCHEMALOCATION, location)).iterator(), XmlUtils.EVENT_FACTORY);
                                }
                            }
                        }
                        if (imports != null) {
                            // Collecting or writing import elements.
                            if (noOutput) {
                                // First call to this method collecting
                                // imports.
                                imports.add(event);
                            }
                            continue;
                        }
                    }
                    break;
                case XMLStreamConstants.END_ELEMENT:
                    EndElement endElement = event.asEndElement();
                    if (endElement.getName().equals(SCHEMA)) {
                        if (skipRootEndElement) {
                            continue;
                        }
                    } else if (endElement.getName().equals(INCLUDE)) {
                        continue;
                    // } else if (endElement.getName().equals(REDEFINE)) {
                    // continue;
                    } else if (imports != null) {
                        if (endElement.getName().equals(IMPORT)) {
                            if (noOutput) {
                                imports.add(event);
                            }
                            continue;
                        }
                    }
                    break;
                default:
            }
            if (!noOutput) {
                streamEventWriter.add(event);
            }
        }
        streamEventWriter.flush();
    } catch (XMLStreamException e) {
        throw new ConfigurationException(xsd.toString() + " (" + event.getLocation() + "): " + e.getMessage(), e);
    }
}
Also used : HashMap(java.util.HashMap) Attribute(javax.xml.stream.events.Attribute) StartElementEvent(javanet.staxutils.events.StartElementEvent) EndElement(javax.xml.stream.events.EndElement) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) XMLStreamEventWriter(javanet.staxutils.XMLStreamEventWriter) AttributeEvent(javanet.staxutils.events.AttributeEvent) StartElement(javax.xml.stream.events.StartElement) XMLStreamException(javax.xml.stream.XMLStreamException) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader)

Aggregations

EndElement (javax.xml.stream.events.EndElement)41 XMLEvent (javax.xml.stream.events.XMLEvent)32 StartElement (javax.xml.stream.events.StartElement)30 QName (javax.xml.namespace.QName)15 XMLEventReader (javax.xml.stream.XMLEventReader)12 Attribute (javax.xml.stream.events.Attribute)12 Characters (javax.xml.stream.events.Characters)11 XMLStreamException (javax.xml.stream.XMLStreamException)10 XMLInputFactory (javax.xml.stream.XMLInputFactory)9 InputStream (java.io.InputStream)5 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)4 Test (org.junit.Test)4 IOException (java.io.IOException)3 Namespace (javax.xml.stream.events.Namespace)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 BigInteger (java.math.BigInteger)2 Base64 (java.util.Base64)2 Iterator (java.util.Iterator)2