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;
}
Aggregations