Search in sources :

Example 1 with FailedToReadSettingFileException

use of io.github.spugn.Sargo.Exceptions.FailedToReadSettingFileException 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)

Aggregations

FailedToReadSettingFileException (io.github.spugn.Sargo.Exceptions.FailedToReadSettingFileException)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 XMLEventReader (javax.xml.stream.XMLEventReader)1 XMLInputFactory (javax.xml.stream.XMLInputFactory)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 Attribute (javax.xml.stream.events.Attribute)1 XMLEvent (javax.xml.stream.events.XMLEvent)1