Search in sources :

Example 6 with Attribute

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

the class NodeInfo method parseConf.

private static List<NodeInfo> parseConf(InputStream in) throws XMLStreamException {
    QName configuration = new QName("configuration");
    QName property = new QName("property");
    List<NodeInfo> nodes = new ArrayList<NodeInfo>();
    Stack<NodeInfo> parsed = new Stack<NodeInfo>();
    XMLInputFactory factory = XMLInputFactory.newInstance();
    XMLEventReader reader = factory.createXMLEventReader(in);
    while (reader.hasNext()) {
        XMLEvent event = reader.nextEvent();
        if (event.isStartElement()) {
            StartElement currentElement = event.asStartElement();
            NodeInfo currentNode = new NodeInfo(currentElement);
            if (parsed.isEmpty()) {
                if (!currentElement.getName().equals(configuration)) {
                    return null;
                }
            } else {
                NodeInfo parentNode = parsed.peek();
                QName parentName = parentNode.getStartElement().getName();
                if (parentName.equals(configuration) && currentNode.getStartElement().getName().equals(property)) {
                    @SuppressWarnings("unchecked") Iterator<Attribute> it = currentElement.getAttributes();
                    while (it.hasNext()) {
                        currentNode.addAttribute(it.next());
                    }
                } else if (parentName.equals(property)) {
                    parentNode.addElement(currentElement);
                }
            }
            parsed.push(currentNode);
        } else if (event.isEndElement()) {
            NodeInfo node = parsed.pop();
            if (parsed.size() == 1) {
                nodes.add(node);
            }
        } else if (event.isCharacters()) {
            if (2 < parsed.size()) {
                NodeInfo parentNode = parsed.pop();
                StartElement parentElement = parentNode.getStartElement();
                NodeInfo grandparentNode = parsed.peek();
                if (grandparentNode.getElement(parentElement) == null) {
                    grandparentNode.setElement(parentElement, event.asCharacters());
                }
                parsed.push(parentNode);
            }
        }
    }
    return nodes;
}
Also used : Attribute(javax.xml.stream.events.Attribute) QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) Stack(java.util.Stack) StartElement(javax.xml.stream.events.StartElement) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 7 with Attribute

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

the class ScoutMasterParser method readConfig.

/**
 * Reads the ScoutMaster file and saves the data found to variables.
 */
@SuppressWarnings("unchecked")
private void readConfig() {
    InputStream in = null;
    XMLEventReader eventReader = null;
    try {
        /* CREATE XMLInputFactory AND XMLEventReader */
        XMLInputFactory inputFactory = XMLInputFactory.newInstance();
        in = new FileInputStream("data/mods/" + scoutMasterName + ".xml");
        eventReader = inputFactory.createXMLEventReader(in);
        /* READ XML FILE */
        while (eventReader.hasNext()) {
            XMLEvent event = eventReader.nextEvent();
            if (event.isStartElement()) {
                if (event.asStartElement().getName().getLocalPart().equals("BotName")) {
                    Iterator<Attribute> attributes = event.asStartElement().getAttributes();
                    while (attributes.hasNext()) {
                        Attribute attribute = attributes.next();
                        if (attribute.getName().toString().equals("name")) {
                            botName = attribute.getValue();
                        }
                    }
                    continue;
                }
                if (event.asStartElement().getName().getLocalPart().equals("Smile")) {
                    Iterator<Attribute> attributes = event.asStartElement().getAttributes();
                    while (attributes.hasNext()) {
                        Attribute attribute = attributes.next();
                        if (attribute.getName().toString().equals("imageURL")) {
                            image1URL = attribute.getValue();
                        }
                    }
                    continue;
                }
                if (event.asStartElement().getName().getLocalPart().equals("Grin")) {
                    Iterator<Attribute> attributes = event.asStartElement().getAttributes();
                    while (attributes.hasNext()) {
                        Attribute attribute = attributes.next();
                        if (attribute.getName().toString().equals("imageURL")) {
                            image2URL = attribute.getValue();
                        }
                    }
                    continue;
                }
                if (event.asStartElement().getName().getLocalPart().equals("Smug")) {
                    Iterator<Attribute> attributes = event.asStartElement().getAttributes();
                    while (attributes.hasNext()) {
                        Attribute attribute = attributes.next();
                        if (attribute.getName().toString().equals("imageURL")) {
                            image3URL = attribute.getValue();
                        }
                    }
                    continue;
                }
                if (event.asStartElement().getName().getLocalPart().equals("Flowers")) {
                    Iterator<Attribute> attributes = event.asStartElement().getAttributes();
                    while (attributes.hasNext()) {
                        Attribute attribute = attributes.next();
                        if (attribute.getName().toString().equals("imageURL")) {
                            image4URL = attribute.getValue();
                        }
                    }
                    continue;
                }
                if (event.asStartElement().getName().getLocalPart().equals("Quote")) {
                    Iterator<Attribute> attributes = event.asStartElement().getAttributes();
                    while (attributes.hasNext()) {
                        Attribute attribute = attributes.next();
                        if (attribute.getName().toString().equals("say")) {
                            quotes.add("*\"" + attribute.getValue() + "\"*");
                        }
                    }
                }
            }
        }
    } catch (FileNotFoundException e) {
        System.out.println("[ScoutMasterParser] - File Not Found Exception");
        useDefaults = true;
    } catch (XMLStreamException e) {
        System.out.println("[ScoutMasterParser] - XML Stream Exception");
        useDefaults = true;
    } finally {
        try {
            if (in != null) {
                in.close();
            }
        } catch (IOException e) {
        /* IGNORED */
        }
        try {
            if (eventReader != null) {
                eventReader.close();
            }
        } catch (XMLStreamException e) {
        /* IGNORED */
        }
    }
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) Attribute(javax.xml.stream.events.Attribute) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 8 with Attribute

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

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

the class XmlFilterReader method bufferAttributes.

private void bufferAttributes(StartElement event, Element element) {
    Iterator attributes = event.getAttributes();
    while (attributes.hasNext()) {
        Attribute attribute = (Attribute) attributes.next();
        bufferAttribute(element, attribute);
    }
}
Also used : Attribute(javax.xml.stream.events.Attribute) Iterator(java.util.Iterator)

Example 10 with Attribute

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

the class XmlFilterReader method streamAttributes.

private void streamAttributes(StartElement event, Element element) throws XPathExpressionException {
    Iterator i = event.getAttributes();
    while (i.hasNext()) {
        Attribute attribute = (Attribute) i.next();
        streamAttribute(element, attribute);
    }
}
Also used : Attribute(javax.xml.stream.events.Attribute) Iterator(java.util.Iterator)

Aggregations

Attribute (javax.xml.stream.events.Attribute)140 QName (javax.xml.namespace.QName)71 StartElement (javax.xml.stream.events.StartElement)62 XMLEvent (javax.xml.stream.events.XMLEvent)52 XMLEventReader (javax.xml.stream.XMLEventReader)30 Namespace (javax.xml.stream.events.Namespace)26 EndElement (javax.xml.stream.events.EndElement)25 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)5 QNm (org.brackit.xquery.atomic.QNm)5