use of javax.xml.stream.events.EndElement in project omegat by omegat-org.
the class TMXReader2 method parseSegExtLevel2.
/**
* External TMX - level 2. Replace all tags into shortcuts.
*/
protected void parseSegExtLevel2() throws Exception {
segContent.setLength(0);
segInlineTag.setLength(0);
inlineTagHandler.reset();
int inlineLevel = 0;
while (true) {
XMLEvent e = xml.nextEvent();
switch(e.getEventType()) {
case XMLEvent.START_ELEMENT:
StartElement eStart = e.asStartElement();
if ("hi".equals(eStart.getName().getLocalPart())) {
// tag should be skipped
break;
}
inlineLevel++;
segInlineTag.setLength(0);
if ("bpt".equals(eStart.getName().getLocalPart())) {
inlineTagHandler.startBPT(getAttributeValue(eStart, "i"), getAttributeValue(eStart, "x"));
inlineTagHandler.setTagShortcutLetter(StringUtil.getFirstLetterLowercase(getAttributeValue(eStart, "type")));
} else if ("ept".equals(eStart.getName().getLocalPart())) {
inlineTagHandler.startEPT(getAttributeValue(eStart, "i"));
} else if ("it".equals(eStart.getName().getLocalPart())) {
inlineTagHandler.startOTHER();
inlineTagHandler.setOtherTagShortcutLetter(StringUtil.getFirstLetterLowercase(getAttributeValue(eStart, "type")));
inlineTagHandler.setCurrentPos(getAttributeValue(eStart, "pos"));
} else if ("ph".equals(eStart.getName().getLocalPart())) {
inlineTagHandler.startOTHER();
inlineTagHandler.setOtherTagShortcutLetter(StringUtil.getFirstLetterLowercase(getAttributeValue(eStart, "type")));
} else {
inlineTagHandler.startOTHER();
}
break;
case XMLEvent.END_ELEMENT:
EndElement eEnd = e.asEndElement();
if ("hi".equals(eEnd.getName().getLocalPart())) {
// tag should be skipped
break;
}
inlineLevel--;
if ("seg".equals(eEnd.getName().getLocalPart())) {
return;
}
boolean slashBefore = false;
boolean slashAfter = false;
int tagName = StringUtil.getFirstLetterLowercase(segInlineTag.toString());
Integer tagN;
if ("bpt".equals(eEnd.getName().getLocalPart())) {
if (tagName != 0) {
inlineTagHandler.setTagShortcutLetter(tagName);
} else {
tagName = inlineTagHandler.getTagShortcutLetter();
}
tagN = inlineTagHandler.endBPT();
} else if ("ept".equals(eEnd.getName().getLocalPart())) {
slashBefore = true;
tagName = inlineTagHandler.getTagShortcutLetter();
tagN = inlineTagHandler.endEPT();
} else if ("it".equals(eEnd.getName().getLocalPart())) {
if (tagName != 0) {
inlineTagHandler.setOtherTagShortcutLetter(tagName);
} else {
tagName = inlineTagHandler.getOtherTagShortcutLetter();
}
tagN = inlineTagHandler.endOTHER();
if ("end".equals(inlineTagHandler.getCurrentPos())) {
slashBefore = true;
}
} else if ("ph".equals(eEnd.getName().getLocalPart())) {
if (tagName != 0) {
inlineTagHandler.setOtherTagShortcutLetter(tagName);
} else {
tagName = inlineTagHandler.getOtherTagShortcutLetter();
}
tagN = inlineTagHandler.endOTHER();
if (useSlash) {
slashAfter = true;
}
} else {
tagN = inlineTagHandler.endOTHER();
if (useSlash) {
slashAfter = true;
}
}
if (tagName == 0) {
tagName = 'f';
}
if (tagN == null) {
// check error of TMX reading
Log.logErrorRB("TMX_ERROR_READING_LEVEL2", e.getLocation().getLineNumber(), e.getLocation().getColumnNumber());
errorsCount++;
segContent.setLength(0);
// wait for end seg
while (true) {
XMLEvent ev = xml.nextEvent();
switch(ev.getEventType()) {
case XMLEvent.END_ELEMENT:
EndElement evEnd = (EndElement) ev;
if ("seg".equals(evEnd.getName().getLocalPart())) {
return;
}
}
}
}
segContent.append('<');
if (slashBefore) {
segContent.append('/');
}
segContent.appendCodePoint(tagName);
segContent.append(Integer.toString(tagN));
if (slashAfter) {
segContent.append('/');
}
segContent.append('>');
break;
case XMLEvent.CHARACTERS:
Characters c = (Characters) e;
if (inlineLevel == 0) {
segContent.append(c.getData());
} else {
segInlineTag.append(c.getData());
}
break;
}
}
}
use of javax.xml.stream.events.EndElement in project omegat by omegat-org.
the class TMXReader2 method parseTu.
protected void parseTu(StartElement element) throws Exception {
currentTu.clear();
currentTu.changeid = getAttributeValue(element, "changeid");
currentTu.changedate = parseISO8601date(getAttributeValue(element, "changedate"));
currentTu.creationid = getAttributeValue(element, "creationid");
currentTu.creationdate = parseISO8601date(getAttributeValue(element, "creationdate"));
while (true) {
XMLEvent e = xml.nextEvent();
switch(e.getEventType()) {
case XMLEvent.START_ELEMENT:
StartElement eStart = (StartElement) e;
if ("tuv".equals(eStart.getName().getLocalPart())) {
parseTuv(eStart);
} else if ("prop".equals(eStart.getName().getLocalPart())) {
parseProp(eStart);
} else if ("note".equals(eStart.getName().getLocalPart())) {
parseNote(eStart);
}
break;
case XMLEvent.END_ELEMENT:
EndElement eEnd = (EndElement) e;
if ("tu".equals(eEnd.getName().getLocalPart())) {
return;
}
break;
}
}
}
use of javax.xml.stream.events.EndElement in project openmeetings by apache.
the class AtomReader method load.
public static void load(String url, JSONArray feed) {
HttpURLConnection con = null;
try {
con = getFeedConnection(url);
try (InputStream is = con.getInputStream()) {
XMLEventReader reader = inputFactory.createXMLEventReader(is);
int i = 0;
JSONObject obj = null;
StringBuilder val = null;
Spec spec = null;
Field f = null;
while (reader.hasNext()) {
XMLEvent evt = reader.nextEvent();
if (obj == null && evt.isStartElement()) {
StartElement start = (StartElement) evt;
String name = start.getName().getLocalPart();
if (specs.containsKey(name)) {
spec = specs.get(name);
obj = new JSONObject();
i++;
}
} else if (obj != null) {
if (evt.isStartElement()) {
StartElement start = (StartElement) evt;
String name = start.getName().getLocalPart();
if (spec.contains(name)) {
f = spec.get(name);
val = new StringBuilder();
if (f.getAttr() != null) {
Attribute a = start.getAttributeByName(new QName(f.getAttr()));
if (a != null) {
val.append(a.getValue());
}
}
}
} else if (f != null && evt.isCharacters()) {
val.append(((Characters) evt).getData());
} else if (f != null && evt.isEndElement() && f.getName().equals(((EndElement) evt).getName().getLocalPart())) {
if (!obj.has(f.getAlias())) {
obj.put(f.getAlias(), val.toString());
}
f = null;
} else if (evt.isEndElement() && spec.getName().equals(((EndElement) evt).getName().getLocalPart())) {
feed.put(obj);
obj = null;
}
}
if (i > MAX_ITEM_COUNT) {
break;
}
}
}
} catch (IOException | XMLStreamException e) {
log.error("Unexpected error while getting RSS", e);
} finally {
if (con != null) {
con.disconnect();
}
}
}
use of javax.xml.stream.events.EndElement in project hibernate-orm by hibernate.
the class FilteringXMLEventReader method internalNext.
protected final XMLEvent internalNext(boolean peek) throws XMLStreamException {
XMLEvent event = null;
if (peekedEvent != null) {
event = peekedEvent;
peekedEvent = null;
return event;
}
do {
event = super.getParent().nextEvent();
// If there are pruned elements in the queue filtering events is still needed
if (!prunedElements.isEmpty()) {
// If another start element add it to the queue
if (event.isStartElement()) {
final StartElement startElement = event.asStartElement();
prunedElements.push(startElement.getName());
} else // If end element pop the newest name of the queue and double check that the start/end elements match up
if (event.isEndElement()) {
final QName startElementName = prunedElements.pop();
final EndElement endElement = event.asEndElement();
final QName endElementName = endElement.getName();
if (!startElementName.equals(endElementName)) {
throw new IllegalArgumentException("Malformed XMLEvent stream. Expected end element for " + startElementName + " but found end element for " + endElementName);
}
}
event = null;
} else {
final XMLEvent filteredEvent = this.filterEvent(event, peek);
// end element need to be removed as well
if (filteredEvent == null && event.isStartElement()) {
final StartElement startElement = event.asStartElement();
final QName name = startElement.getName();
prunedElements.push(name);
}
event = filteredEvent;
}
} while (event == null);
return event;
}
use of javax.xml.stream.events.EndElement in project contribution by checkstyle.
the class CheckstyleReportsParser method parseXmlPortion.
/**
* Parses portion of the XML report.
*
* @param diffReport
* container for parsed data.
* @param reader
* StAX parser interface.
* @param numOfFilenames
* number of "file" tags to parse.
* @param index
* internal index of the parsed file.
* @throws XMLStreamException
* on internal parser error.
*/
private static void parseXmlPortion(DiffReport diffReport, XMLEventReader reader, int numOfFilenames, int index) throws XMLStreamException {
int counter = numOfFilenames;
String filename = null;
List<CheckstyleRecord> records = null;
while (reader.hasNext()) {
final XMLEvent event = reader.nextEvent();
if (event.isStartElement()) {
final StartElement startElement = event.asStartElement();
final String startElementName = startElement.getName().getLocalPart();
// file tag encounter
if (startElementName.equals(FILE_TAG)) {
counter--;
diffReport.getStatistics().incrementFileCount(index);
final Iterator<Attribute> attributes = startElement.getAttributes();
while (attributes.hasNext()) {
final Attribute attribute = attributes.next();
if (attribute.getName().toString().equals(FILENAME_ATTR)) {
filename = attribute.getValue();
}
}
records = new ArrayList<>();
} else // error tag encounter
if (startElementName.equals(ERROR_TAG)) {
records.add(parseErrorTag(startElement, diffReport.getStatistics(), index, filename));
}
}
if (event.isEndElement()) {
final EndElement endElement = event.asEndElement();
if (endElement.getName().getLocalPart().equals(FILE_TAG)) {
diffReport.addRecords(records, filename);
if (counter == 0) {
break;
}
}
}
}
}
Aggregations