use of javax.xml.stream.events.XMLEvent in project solr-cmd-utils by tblsoft.
the class SolrXmlParser method parse.
public void parse() throws Exception {
XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
InputStream is = IOUtils.getInputStream(getInputFileName());
XMLEventReader xmlEventReader = xmlInputFactory.createXMLEventReader(is);
boolean isDoc = false;
boolean isFacet = false;
boolean isArr = false;
int lstCounter = 0;
String currentValue = "";
String currentName = "";
List<String> possibleStartElements = Arrays.asList("date,arr,str,bool,double,float,long,int".split(","));
while (xmlEventReader.hasNext()) {
XMLEvent xmlEvent = xmlEventReader.nextEvent();
if (xmlEvent.isStartElement()) {
StartElement startElement = xmlEvent.asStartElement();
if (startElement.getAttributeByName(new QName("name")) != null) {
currentName = startElement.getAttributeByName(new QName("name")).getValue();
}
if (startElement.getName().getLocalPart().equals("doc")) {
isDoc = true;
}
if (startElement.getName().getLocalPart().equals("lst")) {
lstCounter++;
if (isFacet)
if ("facet_fields".equals(currentName)) {
isFacet = true;
}
}
} else if (xmlEvent.isEndElement()) {
EndElement endElement = xmlEvent.asEndElement();
if (endElement.getName().getLocalPart().equals("doc")) {
isDoc = false;
endDocument();
} else if (endElement.getName().getLocalPart().equals("arr")) {
currentName = "";
} else if (isDoc && possibleStartElements.contains(endElement.getName().getLocalPart())) {
if (currentName != null && !"".equals(currentName)) {
field(currentName, currentValue);
}
currentValue = "";
} else if (endElement.getName().getLocalPart().equals("lst")) {
lstCounter--;
} else {
}
} else if (xmlEvent.isCharacters()) {
Characters characters = xmlEvent.asCharacters();
currentValue = characters.getData();
}
}
is.close();
xmlEventReader.close();
}
use of javax.xml.stream.events.XMLEvent in project contribution by checkstyle.
the class CheckstyleConfigurationsParser method parseModule.
/**
* Recursively processes parsing of the configuration XML file with StAX parser.
* Configuration XML has recursive tree structure
* where module tags are nested into other module tags, also
* duplicates of the same modules may be present in one configuration,
* so there are no other option but to parse configuration
* into the same tree structure.
*
* @param reader
* StAX parser interface.
* @param parent
* parent module tag.
* @throws XMLStreamException
* on internal StAX parser error.
*/
private static void parseModule(XMLEventReader reader, ConfigurationModule parent) throws XMLStreamException {
while (reader.hasNext()) {
final XMLEvent event = reader.nextEvent();
if (event.isStartElement()) {
final StartElement startElement = event.asStartElement();
final String startElementName = startElement.getName().getLocalPart();
// module tag encounter
if (startElementName.equals(MODULE_TAG)) {
processModuleTag(reader, startElement, parent);
} else // property tag encounter
if (startElementName.equals(PROPERTY_TAG)) {
processPropertyTag(startElement, parent);
} else // message tag encounter
if (startElementName.equals(MESSAGE_TAG)) {
processMessageTag(startElement, parent);
}
}
if (event.isEndElement()) {
final EndElement endElement = event.asEndElement();
if (endElement.getName().getLocalPart().equals(MODULE_TAG)) {
// return from recursive method
break;
}
}
}
}
use of javax.xml.stream.events.XMLEvent in project scout.rt by eclipse.
the class XmlUtilityTest method testNewXMLInputFactory.
@Test
public void testNewXMLInputFactory() throws XMLStreamException {
XMLInputFactory factory = XmlUtility.newXMLInputFactory();
Assert.assertNotNull(factory);
XMLEventReader reader = factory.createXMLEventReader(new ByteArrayInputStream(SIMPLE_XML.getBytes(StandardCharsets.UTF_8)));
StringBuilder b = new StringBuilder();
while (reader.hasNext()) {
XMLEvent event = reader.nextEvent();
b.append(event.getEventType());
}
assertEquals("711228", b.toString());
}
use of javax.xml.stream.events.XMLEvent in project jvarkit by lindenb.
the class Biostar165777 method doWork.
@Override
public int doWork(final List<String> args) {
if (this.outputFile == null) {
LOG.error("output file must be defined.");
return -1;
}
if (!this.outputFile.getName().contains(SPLIT_TOKEN)) {
LOG.error("output file " + outputFile + " should contains the word " + SPLIT_TOKEN);
return -1;
}
if (this.tagName == null) {
LOG.error("Tag name was not defined");
return -1;
}
if (this.count < 1) {
LOG.error("bad count " + this.count);
return -1;
}
XMLEventReader r = null;
try {
final List<FileOutputStream> fwriters = new ArrayList<>();
final List<XMLEventWriter> writers = new ArrayList<>();
final XMLOutputFactory xof = XMLOutputFactory.newFactory();
for (int i = 0; i < this.count; ++i) {
File xmlout = new File(this.outputFile.getParentFile(), this.outputFile.getName().replaceAll(SPLIT_TOKEN, String.format("%03d", (i + 1))));
FileOutputStream fos = new FileOutputStream(xmlout);
fwriters.add(fos);
writers.add(xof.createXMLEventWriter(fos, "UTF-8"));
}
// READ Whole XML file
final XMLInputFactory xif = XMLInputFactory.newFactory();
final String inputFile = oneFileOrNull(args);
if (inputFile == null) {
r = xif.createXMLEventReader(new StreamSource(stdin()));
} else {
r = xif.createXMLEventReader(new StreamSource(new File(inputFile)));
}
int numberOfItems = 0;
final XMLEventFactory xef = XMLEventFactory.newFactory();
while (r.hasNext()) {
XMLEvent evt = r.nextEvent();
if (evt.isStartElement() && evt.asStartElement().getName().getLocalPart().equals(this.tagName)) {
++numberOfItems;
XMLEventWriter w = writers.get(numberOfItems % this.count);
int depth = 1;
w.add(evt);
while (r.hasNext()) {
evt = r.nextEvent();
if (evt.isStartElement()) {
++depth;
} else if (evt.isEndElement()) {
--depth;
}
w.add(evt);
if (depth == 0)
break;
}
while (r.hasNext()) {
evt = r.peek();
if (!evt.isCharacters())
break;
if (!evt.asCharacters().getData().trim().isEmpty()) {
break;
}
r.nextEvent();
}
continue;
}
if (evt.isStartDocument()) {
evt = xef.createStartDocument("UTF-8", "1.0");
}
for (XMLEventWriter w : writers) w.add(evt);
}
r.close();
for (XMLEventWriter w : writers) {
w.flush();
CloserUtil.close(w);
}
for (FileOutputStream w : fwriters) {
CloserUtil.close(w);
}
return RETURN_OK;
} catch (Exception err) {
LOG.error(err);
return -1;
} finally {
}
}
use of javax.xml.stream.events.XMLEvent in project jvarkit by lindenb.
the class DiseaseOntoglogyTree method parse.
public static DiseaseOntoglogyTree parse(Reader xmlIn) throws IOException, XMLStreamException {
DiseaseOntoglogyTree tree = new DiseaseOntoglogyTree();
XMLInputFactory fact = XMLInputFactory.newFactory();
fact.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.TRUE);
XMLEventReader r = fact.createXMLEventReader(xmlIn);
while (r.hasNext()) {
XMLEvent evt = r.nextEvent();
if (evt.isStartElement()) {
StartElement E = evt.asStartElement();
QName qN = E.getName();
if (qN.getLocalPart().equals("Class") && OWL.equals(qN.getNamespaceURI())) {
tree.parseTerm(E, r);
}
}
}
r.close();
return tree;
}
Aggregations