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")) {
HashMap<Double, Integer> priceAndAmount = new HashMap<>();
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);
}
}
}
}
use of javax.xml.stream.events.Attribute in project S-argo by Expugn.
the class ScoutSettingsParser method read.
private void read() throws FailedToReadSettingFileException {
InputStream in;
XMLEventReader eventReader;
/* CREATE XMLInputFactory AND XMLEventReader */
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
try {
in = new FileInputStream(FILE_PATH);
} catch (FileNotFoundException e) {
throw new FailedToReadSettingFileException();
}
try {
eventReader = inputFactory.createXMLEventReader(in);
} catch (XMLStreamException e) {
try {
in.close();
} catch (IOException ex) {
/* IGNORED */
}
throw new FailedToReadSettingFileException();
}
recordCrystalRates = new ArrayList<>();
circulatingRecordCrystalRates = new ArrayList<>();
/* 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()) {
if (event.asStartElement().getName().getLocalPart().equals(DISABLE_IMAGES)) {
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(SIMPLE_MESSAGE)) {
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(RARITY_STARS)) {
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(SCOUT_MASTER)) {
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();
}
copperRate = 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();
}
silverRate = 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();
}
goldRate = 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();
}
platinumRate = Double.parseDouble(event.asCharacters().getData());
continue;
}
if (event.asStartElement().getName().getLocalPart().equals(PLATINUM6)) {
try {
event = eventReader.nextEvent();
} catch (XMLStreamException e) {
try {
in.close();
} catch (IOException ex) {
/* IGNORED */
}
try {
eventReader.close();
} catch (XMLStreamException ex) {
/* IGNORED */
}
throw new FailedToReadSettingFileException();
}
platinum6Rate = Double.parseDouble(event.asCharacters().getData());
continue;
}
if (event.asStartElement().getName().getLocalPart().equals(RECORD_CRYSTAL)) {
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(CIRCULATING_RECORD_CRYSTAL)) {
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);
}
}
}
}
use of javax.xml.stream.events.Attribute in project hibernate-orm by hibernate.
the class JpaNamespaceTransformingEventReader method transform.
private StartElement transform(StartElement startElement) {
String elementName = startElement.getName().getLocalPart();
// use the start element to determine whether we have a persistence.xml or orm.xml
if (START_ELEMENT_TO_NAMESPACE_URI.containsKey(elementName)) {
currentDocumentNamespaceUri = START_ELEMENT_TO_NAMESPACE_URI.get(elementName);
}
List<Attribute> newElementAttributeList = updateElementAttributes(startElement);
List<Namespace> newNamespaceList = updateElementNamespaces(startElement);
// create the new element
return xmlEventFactory.createStartElement(new QName(currentDocumentNamespaceUri, startElement.getName().getLocalPart()), newElementAttributeList.iterator(), newNamespaceList.iterator());
}
use of javax.xml.stream.events.Attribute in project bazel by bazelbuild.
the class DataResourceXml method parse.
/**
* Parses xml resources from a Path to the provided overwritable and combining collections.
*
* <p>This method is a bit tricky in the service of performance -- creating several collections
* and merging them was more expensive than writing to mutable collections directly.
*
* @param xmlInputFactory Used to create an XMLEventReader from the supplied resource path.
* @param path The path to the xml resource to be parsed.
* @param fqnFactory Used to create {@link FullyQualifiedName}s from the resource names.
* @param overwritingConsumer A consumer for overwritable {@link DataResourceXml}s.
* @param combiningConsumer A consumer for combining {@link DataResourceXml}s.
* @throws XMLStreamException Thrown with the resource format is invalid.
* @throws FactoryConfigurationError Thrown with the {@link XMLInputFactory} is misconfigured.
* @throws IOException Thrown when there is an error reading a file.
*/
public static void parse(XMLInputFactory xmlInputFactory, Path path, Factory fqnFactory, KeyValueConsumer<DataKey, DataResource> overwritingConsumer, KeyValueConsumer<DataKey, DataResource> combiningConsumer) throws XMLStreamException, FactoryConfigurationError, IOException {
XMLEventReader eventReader = xmlInputFactory.createXMLEventReader(new BufferedInputStream(Files.newInputStream(path)), StandardCharsets.UTF_8.toString());
try {
// TODO(corysmith): Make the xml parsing more readable.
for (StartElement resources = XmlResourceValues.moveToResources(eventReader); resources != null; resources = XmlResourceValues.moveToResources(eventReader)) {
// Record attributes on the <resources> tag.
Iterator<Attribute> attributes = XmlResourceValues.iterateAttributesFrom(resources);
while (attributes.hasNext()) {
Attribute attribute = attributes.next();
Namespaces namespaces = Namespaces.from(attribute.getName());
String attributeName = attribute.getName().getNamespaceURI().isEmpty() ? attribute.getName().getLocalPart() : attribute.getName().getPrefix() + ":" + attribute.getName().getLocalPart();
overwritingConsumer.consume(fqnFactory.create(VirtualType.RESOURCES_ATTRIBUTE, attributeName), DataResourceXml.createWithNamespaces(path, ResourcesAttribute.of(attributeName, attribute.getValue()), namespaces));
}
// Process resource declarations.
for (StartElement start = XmlResourceValues.findNextStart(eventReader); start != null; start = XmlResourceValues.findNextStart(eventReader)) {
Namespaces.Collector namespacesCollector = Namespaces.collector();
if (XmlResourceValues.isEatComment(start) || XmlResourceValues.isSkip(start)) {
continue;
}
ResourceType resourceType = getResourceType(start);
if (resourceType == null) {
throw new XMLStreamException(path + " contains an unrecognized resource type: " + start, start.getLocation());
}
if (resourceType == DECLARE_STYLEABLE) {
// Styleables are special, as they produce multiple overwrite and combining values,
// so we let the value handle the assignments.
XmlResourceValues.parseDeclareStyleable(fqnFactory, path, overwritingConsumer, combiningConsumer, eventReader, start);
} else {
// Of simple resources, only IDs and Public are combining.
KeyValueConsumer<DataKey, DataResource> consumer = (resourceType == ID || resourceType == PUBLIC) ? combiningConsumer : overwritingConsumer;
String elementName = XmlResourceValues.getElementName(start);
if (elementName == null) {
throw new XMLStreamException(String.format("resource name is required for %s", resourceType), start.getLocation());
}
FullyQualifiedName key = fqnFactory.create(resourceType, elementName);
XmlResourceValue xmlResourceValue = parseXmlElements(resourceType, eventReader, start, namespacesCollector);
consumer.consume(key, DataResourceXml.createWithNamespaces(path, xmlResourceValue, namespacesCollector.toNamespaces()));
}
}
}
} catch (XMLStreamException e) {
throw new XMLStreamException(path + ": " + e.getMessage(), e.getLocation(), e);
} catch (RuntimeException e) {
throw new RuntimeException("Error parsing " + path, e);
}
}
use of javax.xml.stream.events.Attribute in project uPortal by Jasig.
the class LayoutPortalDataType method postProcessSinglePortalDataKey.
@SuppressWarnings("deprecation")
@Override
protected PortalDataKey postProcessSinglePortalDataKey(String systemId, PortalDataKey portalDataKey, XMLEventReader reader) {
/* Fragment layouts are differentiated _only_ by file extension; these
* layouts must be imported BEFORE non-fragment layouts (i.e. layouts of
* regular users)
*/
if (systemId.endsWith(".fragment-layout.xml") || systemId.endsWith(".fragment-layout")) /* legacy file extension */
{
/* NOTE: In the future we could consider handling this case
* similarly to how "default" users are handled (below): by reading
* the username attribute and checking it against the list of known
* fragment layout owners.
*/
return convertToFragmentKey(portalDataKey);
}
// in 2.6 and 3.0 format layout files)
if (IMPORT_26_DATA_KEY.equals(portalDataKey) || IMPORT_30_DATA_KEY.equals(portalDataKey)) {
final StartElement startElement = StaxUtils.getRootElement(reader);
final Attribute usernameAttr = startElement.getAttributeByName(new QName("username"));
if (usernameAttr != null) {
final String username = usernameAttr.getValue();
if (this.userIdentityStore.isDefaultUser(username)) {
return convertToDefaultUserKey(portalDataKey);
}
} else {
logger.warn("No username attribute on StartElement for {}", systemId);
}
}
return portalDataKey;
}
Aggregations