Search in sources :

Example 16 with XMLEvent

use of javax.xml.stream.events.XMLEvent in project S-argo by Expugn.

the class SettingsParser method tryRead.

private void tryRead(String configFile) throws FailedToReadSettingFileException {
    InputStream in;
    XMLEventReader eventReader;
    /* CREATE XMLInputFactory AND XMLEventReader */
    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    try {
        in = new FileInputStream(configFile);
    } catch (FileNotFoundException e) {
        throw new FailedToReadSettingFileException();
    }
    try {
        eventReader = inputFactory.createXMLEventReader(in);
    } catch (XMLStreamException e) {
        try {
            in.close();
        } catch (IOException ex) {
        /* IGNORED */
        }
        throw new FailedToReadSettingFileException();
    }
    ignoredChannelNames = new ArrayList<>();
    recordCrystalRates = new ArrayList<>();
    circulatingRecordCrystalRates = new ArrayList<>();
    // goldBanners = new ArrayList<>();
    // goldBannersv2 = new ArrayList<>();
    shopItems = new TreeMap<>();
    /* READ XML FILE */
    while (eventReader.hasNext()) {
        XMLEvent event;
        try {
            event = eventReader.nextEvent();
        } catch (XMLStreamException e) {
            try {
                in.close();
            } catch (IOException ex) {
            /* IGNORED */
            }
            try {
                eventReader.close();
            } catch (XMLStreamException ex) {
            /* IGNORED */
            }
            throw new FailedToReadSettingFileException();
        }
        if (event.isStartElement()) {
            /* BOT SETTINGS */
            if (event.asStartElement().getName().getLocalPart().equals("Token")) {
                try {
                    event = eventReader.nextEvent();
                } catch (XMLStreamException e) {
                    try {
                        in.close();
                    } catch (IOException ex) {
                    /* IGNORED */
                    }
                    try {
                        eventReader.close();
                    } catch (XMLStreamException ex) {
                    /* IGNORED */
                    }
                    throw new FailedToReadSettingFileException();
                }
                botToken = event.asCharacters().getData();
                continue;
            }
            if (event.asStartElement().getName().getLocalPart().equals("NoGUI")) {
                try {
                    event = eventReader.nextEvent();
                } catch (XMLStreamException e) {
                    try {
                        in.close();
                    } catch (IOException ex) {
                    /* IGNORED */
                    }
                    try {
                        eventReader.close();
                    } catch (XMLStreamException ex) {
                    /* IGNORED */
                    }
                    throw new FailedToReadSettingFileException();
                }
                isNoGUI = Boolean.parseBoolean(event.asCharacters().getData());
                continue;
            }
            if (event.asStartElement().getName().getLocalPart().equals("GitHubDataRepository")) {
                try {
                    event = eventReader.nextEvent();
                } catch (XMLStreamException e) {
                    try {
                        in.close();
                    } catch (IOException ex) {
                    /* IGNORED */
                    }
                    try {
                        eventReader.close();
                    } catch (XMLStreamException ex) {
                    /* IGNORED */
                    }
                    throw new FailedToReadSettingFileException();
                }
                gitHubRepoURL = event.asCharacters().getData();
                continue;
            }
            if (event.asStartElement().getName().getLocalPart().equals("BotOwnerID")) {
                try {
                    event = eventReader.nextEvent();
                } catch (XMLStreamException e) {
                    try {
                        in.close();
                    } catch (IOException ex) {
                    /* IGNORED */
                    }
                    try {
                        eventReader.close();
                    } catch (XMLStreamException ex) {
                    /* IGNORED */
                    }
                    throw new FailedToReadSettingFileException();
                }
                botOwnerDiscordID = event.asCharacters().getData();
                continue;
            }
            /* COMMAND SETTINGS */
            if (event.asStartElement().getName().getLocalPart().equals("UseMention")) {
                try {
                    event = eventReader.nextEvent();
                } catch (XMLStreamException e) {
                    try {
                        in.close();
                    } catch (IOException ex) {
                    /* IGNORED */
                    }
                    try {
                        eventReader.close();
                    } catch (XMLStreamException ex) {
                    /* IGNORED */
                    }
                    throw new FailedToReadSettingFileException();
                }
                useMention = Boolean.parseBoolean(event.asCharacters().getData());
                continue;
            }
            if (event.asStartElement().getName().getLocalPart().equals("CommandPrefix")) {
                try {
                    event = eventReader.nextEvent();
                } catch (XMLStreamException e) {
                    try {
                        in.close();
                    } catch (IOException ex) {
                    /* IGNORED */
                    }
                    try {
                        eventReader.close();
                    } catch (XMLStreamException ex) {
                    /* IGNORED */
                    }
                    throw new FailedToReadSettingFileException();
                }
                commandPrefix = event.asCharacters().getData().charAt(0);
                continue;
            }
            if (event.asStartElement().getName().getLocalPart().equals("DeleteUserMessage")) {
                try {
                    event = eventReader.nextEvent();
                } catch (XMLStreamException e) {
                    try {
                        in.close();
                    } catch (IOException ex) {
                    /* IGNORED */
                    }
                    try {
                        eventReader.close();
                    } catch (XMLStreamException ex) {
                    /* IGNORED */
                    }
                    throw new FailedToReadSettingFileException();
                }
                deleteUserMessage = Boolean.parseBoolean(event.asCharacters().getData());
                continue;
            }
            if (event.asStartElement().getName().getLocalPart().equals("iChannel")) {
                Iterator<Attribute> attributes = event.asStartElement().getAttributes();
                while (attributes.hasNext()) {
                    Attribute attribute = attributes.next();
                    if (attribute.getName().toString().equals("channelName")) {
                        ignoredChannelNames.add(attribute.getValue());
                    }
                }
                continue;
            }
            /* SCOUT SETTINGS */
            if (event.asStartElement().getName().getLocalPart().equals("DisableImages")) {
                try {
                    event = eventReader.nextEvent();
                } catch (XMLStreamException e) {
                    try {
                        in.close();
                    } catch (IOException ex) {
                    /* IGNORED */
                    }
                    try {
                        eventReader.close();
                    } catch (XMLStreamException ex) {
                    /* IGNORED */
                    }
                    throw new FailedToReadSettingFileException();
                }
                isDisableImages = Boolean.parseBoolean(event.asCharacters().getData());
                continue;
            }
            if (event.asStartElement().getName().getLocalPart().equals("SimpleMessage")) {
                try {
                    event = eventReader.nextEvent();
                } catch (XMLStreamException e) {
                    try {
                        in.close();
                    } catch (IOException ex) {
                    /* IGNORED */
                    }
                    try {
                        eventReader.close();
                    } catch (XMLStreamException ex) {
                    /* IGNORED */
                    }
                    throw new FailedToReadSettingFileException();
                }
                isSimpleMessage = Boolean.parseBoolean(event.asCharacters().getData());
                continue;
            }
            if (event.asStartElement().getName().getLocalPart().equals("RarityStars")) {
                try {
                    event = eventReader.nextEvent();
                } catch (XMLStreamException e) {
                    try {
                        in.close();
                    } catch (IOException ex) {
                    /* IGNORED */
                    }
                    try {
                        eventReader.close();
                    } catch (XMLStreamException ex) {
                    /* IGNORED */
                    }
                    throw new FailedToReadSettingFileException();
                }
                isRarityStars = Boolean.parseBoolean(event.asCharacters().getData());
                continue;
            }
            if (event.asStartElement().getName().getLocalPart().equals("ScoutMaster")) {
                try {
                    event = eventReader.nextEvent();
                } catch (XMLStreamException e) {
                    try {
                        in.close();
                    } catch (IOException ex) {
                    /* IGNORED */
                    }
                    try {
                        eventReader.close();
                    } catch (XMLStreamException ex) {
                    /* IGNORED */
                    }
                    throw new FailedToReadSettingFileException();
                }
                try {
                    scoutMaster = event.asCharacters().getData();
                } catch (ClassCastException e) {
                    scoutMaster = "";
                }
                continue;
            }
            if (event.asStartElement().getName().getLocalPart().equals("copper")) {
                try {
                    event = eventReader.nextEvent();
                } catch (XMLStreamException e) {
                    try {
                        in.close();
                    } catch (IOException ex) {
                    /* IGNORED */
                    }
                    try {
                        eventReader.close();
                    } catch (XMLStreamException ex) {
                    /* IGNORED */
                    }
                    throw new FailedToReadSettingFileException();
                }
                copperRates = Double.parseDouble(event.asCharacters().getData());
                continue;
            }
            if (event.asStartElement().getName().getLocalPart().equals("silver")) {
                try {
                    event = eventReader.nextEvent();
                } catch (XMLStreamException e) {
                    try {
                        in.close();
                    } catch (IOException ex) {
                    /* IGNORED */
                    }
                    try {
                        eventReader.close();
                    } catch (XMLStreamException ex) {
                    /* IGNORED */
                    }
                    throw new FailedToReadSettingFileException();
                }
                silverRates = Double.parseDouble(event.asCharacters().getData());
                continue;
            }
            if (event.asStartElement().getName().getLocalPart().equals("gold")) {
                try {
                    event = eventReader.nextEvent();
                } catch (XMLStreamException e) {
                    try {
                        in.close();
                    } catch (IOException ex) {
                    /* IGNORED */
                    }
                    try {
                        eventReader.close();
                    } catch (XMLStreamException ex) {
                    /* IGNORED */
                    }
                    throw new FailedToReadSettingFileException();
                }
                goldRates = Double.parseDouble(event.asCharacters().getData());
                continue;
            }
            if (event.asStartElement().getName().getLocalPart().equals("platinum")) {
                try {
                    event = eventReader.nextEvent();
                } catch (XMLStreamException e) {
                    try {
                        in.close();
                    } catch (IOException ex) {
                    /* IGNORED */
                    }
                    try {
                        eventReader.close();
                    } catch (XMLStreamException ex) {
                    /* IGNORED */
                    }
                    throw new FailedToReadSettingFileException();
                }
                platinumRates = Double.parseDouble(event.asCharacters().getData());
                continue;
            }
            if (event.asStartElement().getName().getLocalPart().equals("RecordCrystal")) {
                double rate = 0.0;
                Iterator<Attribute> attributes = event.asStartElement().getAttributes();
                while (attributes.hasNext()) {
                    Attribute attribute = attributes.next();
                    if (attribute.getName().toString().equals("rate")) {
                        rate = Double.parseDouble(attribute.getValue());
                    }
                }
                recordCrystalRates.add(rate);
                continue;
            }
            if (event.asStartElement().getName().getLocalPart().equals("CirculatingRecordCrystal")) {
                double rate = 0.0;
                Iterator<Attribute> attributes = event.asStartElement().getAttributes();
                while (attributes.hasNext()) {
                    Attribute attribute = attributes.next();
                    if (attribute.getName().toString().equals("rate")) {
                        rate = Double.parseDouble(attribute.getValue());
                    }
                }
                circulatingRecordCrystalRates.add(rate);
                continue;
            }
            /* SHOP SETTINGS */
            if (event.asStartElement().getName().getLocalPart().equals("MaxShopLimit")) {
                try {
                    event = eventReader.nextEvent();
                } catch (XMLStreamException e) {
                    try {
                        in.close();
                    } catch (IOException ex) {
                    /* IGNORED */
                    }
                    try {
                        eventReader.close();
                    } catch (XMLStreamException ex) {
                    /* IGNORED */
                    }
                    throw new FailedToReadSettingFileException();
                }
                maxShopLimit = Integer.parseInt(event.asCharacters().getData());
                continue;
            }
            if (event.asStartElement().getName().getLocalPart().equals("shopItem")) {
                SortedMap<Double, Integer> priceAndAmount = new TreeMap<>();
                String itemName = "";
                Double price = 0.0;
                int amount = 0;
                Iterator<Attribute> attributes = event.asStartElement().getAttributes();
                while (attributes.hasNext()) {
                    Attribute attribute = attributes.next();
                    if (attribute.getName().toString().equals("itemName")) {
                        itemName = attribute.getValue();
                    }
                    if (attribute.getName().toString().equals("price")) {
                        price = Double.parseDouble(attribute.getValue());
                    }
                    if (attribute.getName().toString().equals("amount")) {
                        amount = Integer.parseInt(attribute.getValue());
                    }
                }
                priceAndAmount.put(price, amount);
                shopItems.put(itemName, priceAndAmount);
            }
        }
    }
}
Also used : Attribute(javax.xml.stream.events.Attribute) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) XMLStreamException(javax.xml.stream.XMLStreamException) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader) XMLInputFactory(javax.xml.stream.XMLInputFactory) FailedToReadSettingFileException(io.github.spugn.Sargo.Exceptions.FailedToReadSettingFileException)

Example 17 with XMLEvent

use of javax.xml.stream.events.XMLEvent 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)

Example 18 with XMLEvent

use of javax.xml.stream.events.XMLEvent in project nebula.widgets.nattable by eclipse.

the class RegexMarkupValue method applyMarkup.

@Override
public String applyMarkup(String input) {
    String result = "";
    if (getOriginalRegexValue() != null && !getOriginalRegexValue().isEmpty()) {
        XMLEventReader parser = null;
        try {
            parser = this.factory.createXMLEventReader(new StringReader(RichTextPainter.FAKE_ROOT_TAG_START + input + RichTextPainter.FAKE_ROOT_TAG_END));
            while (parser.hasNext()) {
                XMLEvent event = parser.nextEvent();
                switch(event.getEventType()) {
                    case XMLStreamConstants.START_DOCUMENT:
                        break;
                    case XMLStreamConstants.END_DOCUMENT:
                        parser.close();
                        break;
                    case XMLStreamConstants.CHARACTERS:
                        Characters characters = event.asCharacters();
                        String text = characters.getData();
                        if (this.caseInsensitive) {
                            int flags = this.unicodeCase ? Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE : Pattern.CASE_INSENSITIVE;
                            result += Pattern.compile(getOriginalRegexValue(), flags).matcher(text).replaceAll(this.markupValue);
                        } else {
                            result += Pattern.compile(getOriginalRegexValue()).matcher(text).replaceAll(this.markupValue);
                        }
                        break;
                    default:
                        result += event.toString();
                }
            }
        } catch (XMLStreamException e) {
            e.printStackTrace();
        } finally {
            if (parser != null) {
                try {
                    parser.close();
                } catch (XMLStreamException e) {
                    e.printStackTrace();
                }
            }
        }
        result = result.replace(RichTextPainter.FAKE_ROOT_TAG_START, "").replace(RichTextPainter.FAKE_ROOT_TAG_END, "");
    } else {
        result = input;
    }
    return result;
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) Characters(javax.xml.stream.events.Characters) StringReader(java.io.StringReader) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader)

Example 19 with XMLEvent

use of javax.xml.stream.events.XMLEvent in project pentaho-kettle by pentaho.

the class XMLInputStream method processEvent.

private Object[] processEvent() throws KettleException {
    Object[] outputRowData = RowDataUtil.allocateRowData(data.outputRowMeta.size());
    XMLEvent e = null;
    try {
        e = data.xmlEventReader.nextEvent();
    } catch (XMLStreamException ex) {
        throw new KettleException(ex);
    }
    int eventType = e.getEventType();
    if (data.pos_xml_data_type_numeric != -1) {
        outputRowData[data.pos_xml_data_type_numeric] = new Long(eventType);
    }
    if (data.pos_xml_data_type_description != -1) {
        if (eventType == 0 || eventType > eventDescription.length) {
            // unknown eventType
            outputRowData[data.pos_xml_data_type_description] = eventDescription[0] + "(" + eventType + ")";
        } else {
            outputRowData[data.pos_xml_data_type_description] = eventDescription[eventType];
        }
    }
    if (data.pos_xml_location_line != -1) {
        outputRowData[data.pos_xml_location_line] = new Long(e.getLocation().getLineNumber());
    }
    if (data.pos_xml_location_column != -1) {
        outputRowData[data.pos_xml_location_column] = new Long(e.getLocation().getColumnNumber());
    }
    switch(eventType) {
        case XMLStreamConstants.START_ELEMENT:
            data.elementLevel++;
            if (data.elementLevel > PARENT_ID_ALLOCATE_SIZE - 1) {
                throw new KettleException(BaseMessages.getString(PKG, "XMLInputStream.Log.TooManyNestedElements", PARENT_ID_ALLOCATE_SIZE));
            }
            if (data.elementParentID[data.elementLevel] == null) {
                data.elementParentID[data.elementLevel] = data.elementID;
            }
            data.elementID++;
            data.elementLevelID[data.elementLevel] = data.elementID;
            String xml_data_name;
            if (meta.isEnableNamespaces()) {
                String prefix = e.asStartElement().getName().getPrefix();
                if (Utils.isEmpty(prefix)) {
                    xml_data_name = e.asStartElement().getName().getLocalPart();
                } else {
                    // add namespace prefix:
                    xml_data_name = prefix + ":" + e.asStartElement().getName().getLocalPart();
                }
            } else {
                xml_data_name = e.asStartElement().getName().getLocalPart();
            }
            if (data.pos_xml_data_name >= 0) {
                outputRowData[data.pos_xml_data_name] = xml_data_name;
            }
            // store the name
            data.elementName[data.elementLevel] = xml_data_name;
            // store simple path
            data.elementPath[data.elementLevel] = data.elementPath[data.elementLevel - 1] + "/" + xml_data_name;
            // write Namespaces out
            if (meta.isEnableNamespaces()) {
                outputRowData = parseNamespaces(outputRowData, e);
            }
            // write Attributes out
            outputRowData = parseAttributes(outputRowData, e);
            break;
        case XMLStreamConstants.END_ELEMENT:
            parseEndElement(outputRowData, e.asEndElement());
            putRowOut(outputRowData);
            data.elementParentID[data.elementLevel + 1] = null;
            data.elementLevel--;
            // continue
            outputRowData = null;
            break;
        case XMLStreamConstants.SPACE:
            // ignore & continue
            outputRowData = null;
            break;
        case XMLStreamConstants.CHARACTERS:
        case XMLStreamConstants.CDATA:
            if (data.pos_xml_data_name >= 0) {
                outputRowData[data.pos_xml_data_name] = data.elementName[data.elementLevel];
            }
            String xml_data_value = e.asCharacters().getData();
            if (data.pos_xml_data_value >= 0) {
                if (meta.isEnableTrim()) {
                    // optional trim is also eliminating white spaces, tab, cr, lf
                    xml_data_value = Const.trim(xml_data_value);
                }
                outputRowData[data.pos_xml_data_value] = xml_data_value;
            }
            if (data.pos_xml_data_value < 0 || Utils.isEmpty((String) outputRowData[data.pos_xml_data_value])) {
                // ignore & continue
                outputRowData = null;
            }
            break;
        case XMLStreamConstants.PROCESSING_INSTRUCTION:
            // ignore & continue
            outputRowData = null;
            // TODO test if possible
            break;
        case XMLStreamConstants.COMMENT:
            // ignore & continue
            outputRowData = null;
            // TODO test if possible
            break;
        case XMLStreamConstants.ENTITY_REFERENCE:
            // should be resolved by default
            // ignore & continue
            outputRowData = null;
            break;
        case XMLStreamConstants.START_DOCUMENT:
            // just get this information out
            break;
        case XMLStreamConstants.END_DOCUMENT:
            // just get this information out
            break;
        default:
            logBasic("Event:" + eventType);
            // ignore & continue
            outputRowData = null;
    }
    return outputRowData;
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) XMLStreamException(javax.xml.stream.XMLStreamException) XMLEvent(javax.xml.stream.events.XMLEvent)

Example 20 with XMLEvent

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

the class SchemaUtils method mergeXsdsGroupedByNamespaceToSchemasWithoutIncludes.

/**
 * @return XSD's when xmlStreamWriter is null, otherwise write to
 *		 xmlStreamWriter
 */
public static Set<XSD> mergeXsdsGroupedByNamespaceToSchemasWithoutIncludes(IScopeProvider scopeProvider, Map<String, Set<XSD>> xsdsGroupedByNamespace, XMLStreamWriter xmlStreamWriter) throws XMLStreamException, IOException, ConfigurationException {
    Set<XSD> resultXsds = new HashSet<XSD>();
    for (String namespace : xsdsGroupedByNamespace.keySet()) {
        Set<XSD> xsds = xsdsGroupedByNamespace.get(namespace);
        // Get attributes of root elements and get import elements from all XSD's
        List<Attribute> rootAttributes = new ArrayList<Attribute>();
        List<Namespace> rootNamespaceAttributes = new ArrayList<Namespace>();
        List<XMLEvent> imports = new ArrayList<XMLEvent>();
        for (XSD xsd : xsds) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            XMLStreamWriter w = XmlUtils.REPAIR_NAMESPACES_OUTPUT_FACTORY.createXMLStreamWriter(byteArrayOutputStream, StreamUtil.DEFAULT_INPUT_STREAM_ENCODING);
            xsdToXmlStreamWriter(xsd, w, false, true, false, false, rootAttributes, rootNamespaceAttributes, imports, true);
        }
        // Write XSD's with merged root element
        XSD resultXsd = null;
        XMLStreamWriter w;
        if (xmlStreamWriter == null) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            resultXsd = new XSD();
            // resultXsd.setClassLoader(classLoader);
            // resultXsd.setNamespace(namespace);
            resultXsd.setByteArrayOutputStream(byteArrayOutputStream);
            // resultXsd.setSourceXsds(xsds);
            w = XmlUtils.REPAIR_NAMESPACES_OUTPUT_FACTORY.createXMLStreamWriter(byteArrayOutputStream, StreamUtil.DEFAULT_INPUT_STREAM_ENCODING);
        } else {
            w = xmlStreamWriter;
        }
        int i = 0;
        for (XSD xsd : xsds) {
            i++;
            boolean skipFirstElement = true;
            boolean skipLastElement = true;
            if (xsds.size() == 1) {
                skipFirstElement = false;
                skipLastElement = false;
            } else {
                if (i == 1) {
                    skipFirstElement = false;
                } else if (i == xsds.size()) {
                    skipLastElement = false;
                }
            }
            xsdToXmlStreamWriter(xsd, w, false, true, skipFirstElement, skipLastElement, rootAttributes, rootNamespaceAttributes, imports, false);
        }
        if (resultXsd != null) {
            XSD firstXsd = xsds.iterator().next();
            resultXsd.setImportedSchemaLocationsToIgnore(firstXsd.getImportedSchemaLocationsToIgnore());
            resultXsd.setUseBaseImportedSchemaLocationsToIgnore(firstXsd.isUseBaseImportedSchemaLocationsToIgnore());
            resultXsd.setImportedNamespacesToIgnore(firstXsd.getImportedNamespacesToIgnore());
            resultXsd.initFromXsds(namespace, scopeProvider, xsds);
            resultXsds.add(resultXsd);
        }
    }
    return resultXsds;
}
Also used : Attribute(javax.xml.stream.events.Attribute) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Namespace(javax.xml.stream.events.Namespace) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) XMLEvent(javax.xml.stream.events.XMLEvent) HashSet(java.util.HashSet)

Aggregations

XMLEvent (javax.xml.stream.events.XMLEvent)269 XMLEventReader (javax.xml.stream.XMLEventReader)114 StartElement (javax.xml.stream.events.StartElement)107 XMLStreamException (javax.xml.stream.XMLStreamException)96 XMLInputFactory (javax.xml.stream.XMLInputFactory)65 QName (javax.xml.namespace.QName)60 Attribute (javax.xml.stream.events.Attribute)52 EndElement (javax.xml.stream.events.EndElement)52 IOException (java.io.IOException)39 ArrayList (java.util.ArrayList)32 InputStream (java.io.InputStream)29 XMLEventWriter (javax.xml.stream.XMLEventWriter)24 Characters (javax.xml.stream.events.Characters)22 ByteArrayInputStream (java.io.ByteArrayInputStream)17 StringWriter (java.io.StringWriter)17 Test (org.junit.Test)17 StringReader (java.io.StringReader)14 HashMap (java.util.HashMap)14 XMLOutputFactory (javax.xml.stream.XMLOutputFactory)14 XMLEventFactory (javax.xml.stream.XMLEventFactory)13