use of javax.xml.stream.events.Attribute in project drill by apache.
the class XMLReader method processEvent.
/**
* This function processes an actual XMLEvent. There are three possibilities:
* 1. The event is a start event
* 2. The event contains text
* 3. The event is a closing tag
* There are other possible elements, but they are not relevant for our purposes.
*
* @param currentEvent The current event to be processed
* @param lastEvent The previous event which was processed
*/
private void processEvent(XMLEvent currentEvent, XMLEvent lastEvent) {
String mapName;
switch(currentEvent.getEventType()) {
/*
* This case handles start elements.
* Case 1: The current nesting level is less than the data level.
* In this case, increase the nesting level and stop processing.
*
* Case 2: The nesting level is higher than the data level.
* In this case, a few things must happen.
* 1. We capture the field name
* 2. If the row has not started, we start the row
* 3. Set the possible map flag
* 4. Process attributes
* 5. Push both the field name and writer to the stacks
*/
case XMLStreamConstants.START_ELEMENT:
currentNestingLevel++;
// Case 1: Current nesting level is less than the data level
if (currentNestingLevel < dataLevel) {
// Stop here if the current level of nesting has not reached the data.
break;
}
StartElement startElement = currentEvent.asStartElement();
// Get the field name
fieldName = startElement.getName().getLocalPart();
if (rootDataFieldName == null && currentNestingLevel == dataLevel) {
rootDataFieldName = fieldName;
logger.debug("Root field name: {}", rootDataFieldName);
}
if (!rowStarted) {
currentTupleWriter = startRow(rootRowWriter);
} else {
if (lastEvent != null && lastEvent.getEventType() == XMLStreamConstants.START_ELEMENT) {
/*
* Check the flag in the next section. If the next element is a character AND the flag is set,
* start a map. If not... ignore it all.
*/
changeState(xmlState.POSSIBLE_MAP);
rowWriterStack.push(currentTupleWriter);
}
fieldNameStack.push(fieldName);
if (currentNestingLevel > dataLevel) {
attributePrefix = XMLUtils.addField(attributePrefix, fieldName);
}
Iterator<Attribute> attributes = startElement.getAttributes();
if (attributes != null && attributes.hasNext()) {
writeAttributes(attributePrefix, attributes);
}
}
break;
/*
* This case processes character elements.
*/
case XMLStreamConstants.CHARACTERS:
/*
* This is the case for comments or other characters after a closing tag
*/
if (currentState == xmlState.ROW_ENDED) {
break;
}
// Get the field value but ignore characters outside of rows
if (rowStarted) {
if (currentState == xmlState.POSSIBLE_MAP && currentNestingLevel > dataLevel + 1) {
changeState(xmlState.NESTED_MAP_STARTED);
// Remove the current field name from the stack
if (fieldNameStack.size() > 1) {
fieldNameStack.pop();
}
// Get the map name and push to stack
mapName = fieldNameStack.pop();
currentTupleWriter = getMapWriter(mapName, currentTupleWriter);
} else {
changeState(xmlState.ROW_STARTED);
}
}
// Get the field value
fieldValue = currentEvent.asCharacters().getData().trim();
changeState(xmlState.GETTING_DATA);
break;
case XMLStreamConstants.END_ELEMENT:
currentNestingLevel--;
if (isSelfClosingEvent) {
logger.debug("Closing self-closing event {}. ", fieldName);
isSelfClosingEvent = false;
attributePrefix = XMLUtils.removeField(attributePrefix, fieldName);
break;
}
if (currentNestingLevel < dataLevel - 1) {
break;
} else if (currentEvent.asEndElement().getName().toString().compareTo(rootDataFieldName) == 0) {
// End the row
currentTupleWriter = endRow();
// Clear stacks
rowWriterStack.clear();
fieldNameStack.clear();
attributePrefix = "";
} else if (currentState == xmlState.FIELD_ENDED && currentNestingLevel >= dataLevel) {
// Pop tupleWriter off stack
if (rowWriterStack.size() > 0) {
currentTupleWriter = rowWriterStack.pop();
}
// Pop field name
if (fieldNameStack.size() > 0) {
fieldNameStack.pop();
}
attributePrefix = XMLUtils.removeField(attributePrefix, fieldName);
} else if (currentState != xmlState.ROW_ENDED) {
if (!isSelfClosingEvent) {
writeFieldData(fieldName, fieldValue, currentTupleWriter);
}
// Clear out field name and value
attributePrefix = XMLUtils.removeField(attributePrefix, fieldName);
// Pop field name
if (fieldNameStack.size() > 0) {
fieldNameStack.pop();
}
fieldName = null;
fieldValue = null;
}
break;
}
}
use of javax.xml.stream.events.Attribute in project cogcomp-nlp by CogComp.
the class SentenceStaxParser method parseFile.
@SuppressWarnings({ "unchecked", "null" })
public Pair<List<MascSentence>, List<MascSentenceGroup>> parseFile(String file) throws IllegalFormatException, FileNotFoundException, XMLStreamException {
// map id to sentence
Map<String, MascSentence> idToSentence = new HashMap<>();
// map id to type (quote or stmt)
Map<String, SentenceType> nodeIdToSentenceTypes = new HashMap<>();
// map node id to sentence ids
Map<String, Set<String>> nodeIdToSentenceId = new HashMap<>();
List<MascSentenceGroup> sentenceGroups = new ArrayList<>();
int numItems = 0;
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
InputStream in = new FileInputStream(file);
XMLEventReader eventReader = inputFactory.createXMLEventReader(in);
// will see this as attribute before link event
String nodeId = null;
while (eventReader.hasNext()) {
XMLEvent event = eventReader.nextEvent();
if (event.isStartElement()) {
StartElement startElement = event.asStartElement();
// a region defines a sentence
if (startElement.getName().getLocalPart().equals(REGION)) {
int start = 0;
int end = 0;
String id = null;
Iterator<Attribute> attributes = startElement.getAttributes();
while (attributes.hasNext()) {
Attribute attribute = attributes.next();
String attName = attribute.getName().getLocalPart();
if (attName.equals(ANCHORS)) {
String[] offsetStrs = attribute.getValue().split(" ");
if (offsetStrs.length != 2)
throw new IllegalStateException("Offset string expected two parts. Found '" + attribute.getValue() + "'.");
start = Integer.parseInt(offsetStrs[0]);
end = Integer.parseInt(offsetStrs[1]);
}
if (attName.equals(ID)) {
id = attribute.getValue();
}
}
if (null == id || (0 == end)) {
throw new IllegalStateException("read start of element '" + REGION + "' (item " + numItems + " in file), but did not " + "successfully read label or offsets (or both). File is '" + file + "'.");
}
idToSentence.put(id, new MascSentence(start, end, id));
numItems++;
} else // <node xml:id="s-n1">
if (startElement.getName().getLocalPart().equals(NODE)) {
Iterator<Attribute> attributes = startElement.getAttributes();
while (attributes.hasNext()) {
Attribute attribute = attributes.next();
String attName = attribute.getName().getLocalPart();
if (attName.equals(ID))
nodeId = attribute.getValue();
}
} else // <link targets="s-r1"/>
if (startElement.getName().getLocalPart().equals(LINK)) {
Iterator<Attribute> attributes = startElement.getAttributes();
while (attributes.hasNext()) {
Attribute attribute = attributes.next();
String attName = attribute.getName().getLocalPart();
if (attName.equals(TARGETS)) {
Set<String> targets = new HashSet<>(Arrays.asList(attribute.getValue().split(" ")));
nodeIdToSentenceId.put(nodeId, targets);
}
}
} else // <a xml:id="s-N65589" label="q" ref="s-n1" as="anc"/>
if (startElement.getName().getLocalPart().equals(A)) {
Iterator<Attribute> attributes = startElement.getAttributes();
String aId = null;
String label = null;
String ref = null;
String as = null;
while (attributes.hasNext()) {
Attribute attribute = attributes.next();
String attName = attribute.getName().getLocalPart();
if (attName.equals(ID))
aId = attribute.getValue();
else if (attName.equals(LABEL))
label = attribute.getValue();
else if (attName.equals(REF))
ref = attribute.getValue();
else if (attName.equals(AS))
as = attribute.getValue();
}
SentenceType typeLabel = SentenceType.STATEMENT;
if ("q".equalsIgnoreCase(label))
typeLabel = SentenceType.QUOTE;
else if ("s".equalsIgnoreCase(label))
typeLabel = SentenceType.STATEMENT;
else if ("head".equalsIgnoreCase(label))
typeLabel = SentenceType.HEADER;
else
throw new IllegalArgumentException("Found unrecognized symbol '" + label + "' for sentence type.");
sentenceGroups.add(new MascSentenceGroup(aId, label, ref, as, nodeIdToSentenceId.get(ref)));
nodeIdToSentenceTypes.put(ref, typeLabel);
}
}
}
TreeSet<MascSentence> sentences = new TreeSet<>(new Comparator<MascSentence>() {
@Override
public int compare(MascSentence first, MascSentence second) {
if (first.start > second.start)
return 1;
else if (first.start < second.start)
return -1;
else if (first.end > second.end)
return 1;
else if (first.end < second.end)
return -1;
return 0;
}
});
for (String localNodeId : nodeIdToSentenceTypes.keySet()) {
SentenceType t = nodeIdToSentenceTypes.get(localNodeId);
if (!t.equals(SentenceType.QUOTE)) {
Set<String> nodeSentIds = nodeIdToSentenceId.get(localNodeId);
for (String id : nodeSentIds) sentences.add(idToSentence.get(id));
}
}
return new Pair(new ArrayList<>(sentences), sentenceGroups);
}
use of javax.xml.stream.events.Attribute in project GCViewer by chewiebug.
the class DataReaderIBM_J9_R28 method getAttributeValue.
private String getAttributeValue(StartElement event, String name) {
String value = null;
Attribute attr = event.getAttributeByName(new QName(name));
if (attr != null) {
value = attr.getValue();
}
return value;
}
use of javax.xml.stream.events.Attribute in project tomee by apache.
the class StaxUtils method readDocElement.
public static Node readDocElement(Document doc, Node parent, XMLEvent ev, StreamToDOMContext context) throws XMLStreamException {
switch(ev.getEventType()) {
case XMLStreamConstants.START_ELEMENT:
{
context.incrementCount();
Element e;
StartElement startElem = ev.asStartElement();
QName name = startElem.getName();
if (!StringUtils.isEmpty(name.getPrefix())) {
e = doc.createElementNS(name.getNamespaceURI(), name.getPrefix() + ":" + name.getLocalPart());
} else {
e = doc.createElementNS(name.getNamespaceURI(), name.getLocalPart());
}
e = (Element) parent.appendChild(e);
if (context.isRecordLoc()) {
context.setRecordLoc(addLocation(doc, e, startElem.getLocation(), context.isRecordLoc()));
}
if (context.isRepairing() && !isDeclared(e, name.getNamespaceURI(), name.getPrefix())) {
declare(e, name.getNamespaceURI(), name.getPrefix());
}
context.pushToStack(parent);
if (context.isThreshold() && MAX_ELEMENT_DEPTH_VAL != -1 && context.getStackSize() >= MAX_ELEMENT_DEPTH_VAL) {
throw new DepthExceededStaxException("reach the innerElementLevelThreshold:" + MAX_ELEMENT_DEPTH_VAL);
}
if (context.isThreshold() && MAX_CHILD_ELEMENTS_VAL != -1 && context.getCount() >= MAX_CHILD_ELEMENTS_VAL) {
throw new DepthExceededStaxException("reach the innerElementCountThreshold:" + MAX_CHILD_ELEMENTS_VAL);
}
parent = e;
break;
}
case XMLStreamConstants.END_ELEMENT:
if (context.isStackEmpty()) {
return parent;
}
parent = context.popFromStack();
if (parent instanceof Document || parent instanceof DocumentFragment) {
return parent;
}
break;
case XMLStreamConstants.NAMESPACE:
Namespace ns = (Namespace) ev;
declare((Element) parent, ns.getNamespaceURI(), ns.getPrefix());
break;
case XMLStreamConstants.ATTRIBUTE:
Attribute at = (Attribute) ev;
QName qname = at.getName();
String attName = qname.getLocalPart();
String attPrefix = qname.getPrefix();
if (attPrefix != null && attPrefix.length() > 0) {
attName = attPrefix + ":" + attName;
}
Attr attr = doc.createAttributeNS(qname.getNamespaceURI(), attName);
attr.setValue(at.getValue());
((Element) parent).setAttributeNode(attr);
break;
case XMLStreamConstants.CHARACTERS:
Characters characters = ev.asCharacters();
context.setRecordLoc(addLocation(doc, parent.appendChild(doc.createTextNode(characters.getData())), characters.getLocation(), context.isRecordLoc()));
break;
case XMLStreamConstants.COMMENT:
parent.appendChild(doc.createComment(((javax.xml.stream.events.Comment) ev).getText()));
break;
case XMLStreamConstants.CDATA:
Characters cdata = ev.asCharacters();
context.setRecordLoc(addLocation(doc, parent.appendChild(doc.createCDATASection(cdata.getData())), cdata.getLocation(), context.isRecordLoc()));
break;
case XMLStreamConstants.PROCESSING_INSTRUCTION:
parent.appendChild(doc.createProcessingInstruction(((ProcessingInstruction) ev).getTarget(), ((ProcessingInstruction) ev).getData()));
break;
case XMLStreamConstants.ENTITY_REFERENCE:
javax.xml.stream.events.EntityReference er = (javax.xml.stream.events.EntityReference) ev;
parent.appendChild(doc.createEntityReference(er.getName()));
break;
default:
break;
}
return parent;
}
use of javax.xml.stream.events.Attribute in project uPortal by Jasig.
the class StylesheetAttributeSource method getAdditionalAttributes.
@Override
public final Iterator<Attribute> getAdditionalAttributes(HttpServletRequest request, HttpServletResponse response, StartElement event) {
final IStylesheetDescriptor stylesheetDescriptor = this.getStylesheetDescriptor(request);
final PreferencesScope stylesheetPreferencesScope = this.getStylesheetPreferencesScope(request);
final Collection<Attribute> attributes = new LinkedList<Attribute>();
for (final ILayoutAttributeDescriptor layoutAttributeDescriptor : stylesheetDescriptor.getLayoutAttributeDescriptors()) {
final Set<String> targetElementNames = layoutAttributeDescriptor.getTargetElementNames();
final QName eventName = event.getName();
final String localEventName = eventName.getLocalPart();
if (targetElementNames.contains(localEventName)) {
final Attribute subscribeIdAttr = event.getAttributeByName(IUserLayoutManager.ID_ATTR_NAME);
final String subscribeId = subscribeIdAttr.getValue();
final String name = layoutAttributeDescriptor.getName();
String value = this.stylesheetUserPreferencesService.getLayoutAttribute(request, stylesheetPreferencesScope, subscribeId, name);
if (value == null) {
value = layoutAttributeDescriptor.getDefaultValue();
}
if (value != null) {
if (this.shouldDoSpelEvaluationForAttributeValue(value)) {
final ServletWebRequest webRequest = new ServletWebRequest(request, response);
value = this.doSpelEvaluationForAttributeValue(webRequest, value);
}
if (value != null) {
final Attribute attribute = xmlEventFactory.createAttribute(name, value);
attributes.add(attribute);
}
}
}
}
return attributes.iterator();
}
Aggregations