in impl/maven-xml/src/main/java/org/apache/maven/internal/xml/DefaultXmlService.java [74:145]
private XmlNode doBuild(XMLStreamReader parser, boolean trim, InputLocationBuilder locationBuilder)
throws XMLStreamException {
boolean spacePreserve = false;
String lPrefix = null;
String lNamespaceUri = null;
String lName = null;
String lValue = null;
Object location = null;
Map<String, String> attrs = null;
List<XmlNode> children = null;
int eventType = parser.getEventType();
int lastStartTag = -1;
while (eventType != XMLStreamReader.END_DOCUMENT) {
if (eventType == XMLStreamReader.START_ELEMENT) {
lastStartTag = parser.getLocation().getLineNumber() * 1000
+ parser.getLocation().getColumnNumber();
if (lName == null) {
int namespacesSize = parser.getNamespaceCount();
lPrefix = parser.getPrefix();
lNamespaceUri = parser.getNamespaceURI();
lName = parser.getLocalName();
location = locationBuilder != null ? locationBuilder.toInputLocation(parser) : null;
int attributesSize = parser.getAttributeCount();
if (attributesSize > 0 || namespacesSize > 0) {
attrs = new HashMap<>();
for (int i = 0; i < namespacesSize; i++) {
String nsPrefix = parser.getNamespacePrefix(i);
String nsUri = parser.getNamespaceURI(i);
attrs.put(nsPrefix != null && !nsPrefix.isEmpty() ? "xmlns:" + nsPrefix : "xmlns", nsUri);
}
for (int i = 0; i < attributesSize; i++) {
String aName = parser.getAttributeLocalName(i);
String aValue = parser.getAttributeValue(i);
String aPrefix = parser.getAttributePrefix(i);
if (aPrefix != null && !aPrefix.isEmpty()) {
aName = aPrefix + ":" + aName;
}
attrs.put(aName, aValue);
spacePreserve = spacePreserve || ("xml:space".equals(aName) && "preserve".equals(aValue));
}
}
} else {
if (children == null) {
children = new ArrayList<>();
}
XmlNode child = doBuild(parser, trim, locationBuilder);
children.add(child);
}
} else if (eventType == XMLStreamReader.CHARACTERS || eventType == XMLStreamReader.CDATA) {
String text = parser.getText();
lValue = lValue != null ? lValue + text : text;
} else if (eventType == XMLStreamReader.END_ELEMENT) {
boolean emptyTag = lastStartTag
== parser.getLocation().getLineNumber() * 1000
+ parser.getLocation().getColumnNumber();
if (lValue != null && trim && !spacePreserve) {
lValue = lValue.trim();
}
return XmlNode.newBuilder()
.prefix(lPrefix)
.namespaceUri(lNamespaceUri)
.name(lName)
.value(children == null ? (lValue != null ? lValue : emptyTag ? null : "") : null)
.attributes(attrs)
.children(children)
.inputLocation(location)
.build();
}
eventType = parser.next();
}
throw new IllegalStateException("End of document found before returning to 0 depth");
}