Search in sources :

Example 1 with FailedToReadBannerFileException

use of io.github.spugn.Sargo.Exceptions.FailedToReadBannerFileException in project S-argo by Expugn.

the class BannerParser method tryRead.

private List<Banner> tryRead(String configFile) throws FailedToReadBannerFileException {
    List<Banner> banners = new ArrayList();
    goldBanners = new ArrayList();
    goldBannersv2 = new ArrayList();
    InputStream in;
    XMLEventReader eventReader;
    Banner banner = null;
    ArrayList<Character> characters = null;
    Character character;
    ArrayList<Weapon> weapons = null;
    Weapon weapon;
    /* CREATE XMLInputFactory AND XMLEventReader */
    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    try {
        in = new FileInputStream(configFile);
    } catch (FileNotFoundException e) {
        throw new FailedToReadBannerFileException();
    }
    try {
        eventReader = inputFactory.createXMLEventReader(in);
    } catch (XMLStreamException e) {
        try {
            in.close();
        } catch (IOException ex) {
        /* IGNORED */
        }
        throw new FailedToReadBannerFileException();
    }
    /* 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 FailedToReadBannerFileException();
        }
        if (event.isStartElement()) {
            StartElement startElement = event.asStartElement();
            /* CREATE NEW BANNER AND CHARACTER LIST OBJECT */
            if (startElement.getName().getLocalPart().equals("Banner")) {
                banner = new Banner();
                characters = new ArrayList();
                weapons = new ArrayList<>();
                /* GET AND SAVE BANNER ID */
                Iterator<Attribute> attributes = startElement.getAttributes();
                while (attributes.hasNext()) {
                    Attribute attribute = attributes.next();
                    if (attribute.getName().toString().equals("id")) {
                        banner.setBannerID(Integer.parseInt(attribute.getValue()));
                    }
                }
            }
            /* GET AND SAVE BANNER NAME */
            if (event.isStartElement()) {
                if (event.asStartElement().getName().getLocalPart().equals("name")) {
                    try {
                        event = eventReader.nextEvent();
                    } catch (XMLStreamException e) {
                        try {
                            in.close();
                        } catch (IOException ex) {
                        /* IGNORED */
                        }
                        try {
                            eventReader.close();
                        } catch (XMLStreamException ex) {
                        /* IGNORED */
                        }
                        throw new FailedToReadBannerFileException();
                    }
                    banner.setBannerName(event.asCharacters().getData());
                    continue;
                }
            }
            /* GET AND SAVE BANNER TYPE */
            if (event.asStartElement().getName().getLocalPart().equals("type")) {
                try {
                    event = eventReader.nextEvent();
                } catch (XMLStreamException e) {
                    try {
                        in.close();
                    } catch (IOException ex) {
                    /* IGNORED */
                    }
                    try {
                        eventReader.close();
                    } catch (XMLStreamException ex) {
                    /* IGNORED */
                    }
                    throw new FailedToReadBannerFileException();
                }
                banner.setBannerType(Integer.parseInt(event.asCharacters().getData()));
                continue;
            }
            /* GET AND SAVE WEAPON BANNER TYPE */
            if (event.asStartElement().getName().getLocalPart().equals("wepType")) {
                try {
                    event = eventReader.nextEvent();
                } catch (XMLStreamException e) {
                    try {
                        in.close();
                    } catch (IOException ex) {
                    /* IGNORED */
                    }
                    try {
                        eventReader.close();
                    } catch (XMLStreamException ex) {
                    /* IGNORED */
                    }
                    throw new FailedToReadBannerFileException();
                }
                banner.setBannerWepType(Integer.parseInt(event.asCharacters().getData()));
                continue;
            }
            /* GET AND SAVE IF BANNER IS INCLUDED WITH GOLD BANNERS/GOLD BANNERS V2 */
            if (event.asStartElement().getName().getLocalPart().equals("include")) {
                try {
                    event = eventReader.nextEvent();
                } catch (XMLStreamException e) {
                    try {
                        in.close();
                    } catch (IOException ex) {
                    /* IGNORED */
                    }
                    try {
                        eventReader.close();
                    } catch (XMLStreamException ex) {
                    /* IGNORED */
                    }
                    throw new FailedToReadBannerFileException();
                }
                String includeType = event.asCharacters().getData();
                if (includeType.equals("GoldBanners")) {
                    try {
                        goldBanners.add(banner.getBannerID());
                    } catch (NullPointerException e) {
                    /* IGNORED */
                    }
                } else if (includeType.equals("GoldBannersv2")) {
                    try {
                        goldBannersv2.add(banner.getBannerID());
                    } catch (NullPointerException e) {
                    /* IGNORED */
                    }
                }
                continue;
            }
            /* GET AND SAVE CHARACTER */
            if (event.asStartElement().getName().getLocalPart().equals("Character")) {
                character = new Character();
                Iterator<Attribute> attributes = event.asStartElement().getAttributes();
                while (attributes.hasNext()) {
                    Attribute attribute = attributes.next();
                    if (attribute.getName().toString().equals("prefix")) {
                        character.setPrefix(attribute.getValue());
                    }
                    if (attribute.getName().toString().equals("char")) {
                        character.setName(attribute.getValue());
                    }
                    if (attribute.getName().toString().equals("rarity")) {
                        character.setRarity(Integer.parseInt(attribute.getValue()));
                    }
                }
                /* GENERATE IMAGE FILE PATH*/
                String characterImage = character.getPrefix() + " " + character.getName();
                character.setImagePath("images/Characters/" + characterImage.replaceAll(" ", "_") + ".png");
                /* ADD CHARACTER TO CHARACTER LIST */
                characters.add(character);
            }
            /* GET AND SAVE WEAPON */
            if (event.asStartElement().getName().getLocalPart().equals("Weapon")) {
                weapon = new Weapon();
                Iterator<Attribute> attributes = event.asStartElement().getAttributes();
                while (attributes.hasNext()) {
                    Attribute attribute = attributes.next();
                    if (attribute.getName().toString().equals("name")) {
                        weapon.setName(attribute.getValue());
                    }
                    if (attribute.getName().toString().equals("rarity")) {
                        weapon.setRarity(Integer.parseInt(attribute.getValue()));
                    }
                }
                /* GENERATE IMAGE FILE PATH*/
                weapon.setImagePath("images/Weapons/" + weapon.getName().replaceAll(" ", "_") + ".png");
                /* ADD WEAPON TO WEAPON LIST */
                weapons.add(weapon);
            }
        }
        /* END OF BANNER ELEMENT. FINALIZE CHARACTER LIST AND ADD OBJECT TO ArrayList */
        if (event.isEndElement()) {
            EndElement endElement = event.asEndElement();
            if (endElement.getName().getLocalPart().equals("Banner")) {
                banner.setCharacters(characters);
                banner.setWeapons(weapons);
                banners.add(banner);
            // System.out.println("added " + banner.getBannerName());
            }
        }
    }
    return banners;
}
Also used : FailedToReadBannerFileException(io.github.spugn.Sargo.Exceptions.FailedToReadBannerFileException) Character(io.github.spugn.Sargo.Objects.Character) Attribute(javax.xml.stream.events.Attribute) EndElement(javax.xml.stream.events.EndElement) Banner(io.github.spugn.Sargo.Objects.Banner) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) Weapon(io.github.spugn.Sargo.Objects.Weapon) FileInputStream(java.io.FileInputStream) StartElement(javax.xml.stream.events.StartElement) XMLStreamException(javax.xml.stream.XMLStreamException) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Aggregations

FailedToReadBannerFileException (io.github.spugn.Sargo.Exceptions.FailedToReadBannerFileException)1 Banner (io.github.spugn.Sargo.Objects.Banner)1 Character (io.github.spugn.Sargo.Objects.Character)1 Weapon (io.github.spugn.Sargo.Objects.Weapon)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 EndElement (javax.xml.stream.events.EndElement)1 StartElement (javax.xml.stream.events.StartElement)1 XMLEvent (javax.xml.stream.events.XMLEvent)1