Search in sources :

Example 1 with AttributeEvent

use of javanet.staxutils.events.AttributeEvent 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

ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 XMLStreamEventWriter (javanet.staxutils.XMLStreamEventWriter)1 AttributeEvent (javanet.staxutils.events.AttributeEvent)1 StartElementEvent (javanet.staxutils.events.StartElementEvent)1 XMLEventReader (javax.xml.stream.XMLEventReader)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 Attribute (javax.xml.stream.events.Attribute)1 EndElement (javax.xml.stream.events.EndElement)1 StartElement (javax.xml.stream.events.StartElement)1 XMLEvent (javax.xml.stream.events.XMLEvent)1 ConfigurationException (nl.nn.adapterframework.configuration.ConfigurationException)1