use of javax.xml.stream.XMLStreamReader in project felix by apache.
the class StaxParser method parseRepository.
public RepositoryImpl parseRepository(InputStream is, URI baseUri) throws Exception {
XMLStreamReader reader = getFactory().createXMLStreamReader(is);
int event = reader.nextTag();
if (event != XMLStreamConstants.START_ELEMENT || !REPOSITORY.equals(reader.getLocalName())) {
throw new Exception("Expected element 'repository' at the root of the document");
}
return parseRepository(reader);
}
use of javax.xml.stream.XMLStreamReader in project felix by apache.
the class StaxParser method parseProperty.
public PropertyImpl parseProperty(Reader r) throws Exception {
XMLStreamReader reader = getFactory().createXMLStreamReader(r);
int event = reader.nextTag();
if (event != XMLStreamConstants.START_ELEMENT || !P.equals(reader.getLocalName())) {
throw new Exception("Expected element 'p'");
}
return parseProperty(reader);
}
use of javax.xml.stream.XMLStreamReader in project felix by apache.
the class StaxParser method parseResource.
public ResourceImpl parseResource(Reader r) throws Exception {
XMLStreamReader reader = getFactory().createXMLStreamReader(r);
int event = reader.nextTag();
if (event != XMLStreamConstants.START_ELEMENT || !RESOURCE.equals(reader.getLocalName())) {
throw new Exception("Expected element 'resource'");
}
return parseResource(reader);
}
use of javax.xml.stream.XMLStreamReader in project cxf by apache.
the class ToolsStaxUtils method getTags.
public static List<Tag> getTags(final File source) throws Exception {
List<Tag> tags = new ArrayList<>();
List<String> ignoreEmptyTags = Arrays.asList(new String[] { "sequence" });
try (InputStream is = new BufferedInputStream(Files.newInputStream(source.toPath()))) {
XMLStreamReader reader = StaxUtils.createXMLStreamReader(is);
Tag newTag = null;
int count = 0;
QName checkingPoint = null;
Stack<Tag> stack = new Stack<Tag>();
while (reader.hasNext()) {
int event = reader.next();
if (checkingPoint != null) {
count++;
}
if (event == XMLStreamConstants.START_ELEMENT) {
newTag = new Tag();
newTag.setName(reader.getName());
if (ignoreEmptyTags.contains(reader.getLocalName())) {
checkingPoint = reader.getName();
}
for (int i = 0; i < reader.getAttributeCount(); i++) {
newTag.getAttributes().put(reader.getAttributeName(i), reader.getAttributeValue(i));
}
stack.push(newTag);
}
if (event == XMLStreamConstants.CHARACTERS) {
newTag.setText(reader.getText());
}
if (event == XMLStreamConstants.END_ELEMENT) {
Tag startTag = stack.pop();
if (checkingPoint != null && checkingPoint.equals(reader.getName())) {
if (count == 1) {
// Tag is empty, and it's in the ignore collection, so we just skip this tag
} else {
tags.add(startTag);
}
count = 0;
checkingPoint = null;
} else {
tags.add(startTag);
}
}
}
reader.close();
}
return tags;
}
use of javax.xml.stream.XMLStreamReader in project cxf by apache.
the class ToolsStaxUtils method getTagTree.
public static Tag getTagTree(final InputStream is, final List<String> ignoreAttr, Map<QName, Set<String>> types) throws Exception {
Tag root = new Tag();
root.setName(new QName("root", "root"));
XMLStreamReader reader = StaxUtils.createXMLStreamReader(is);
Tag newTag = null;
Tag currentTag = root;
while (reader.hasNext()) {
int event = reader.next();
if (event == XMLStreamConstants.START_ELEMENT) {
newTag = new Tag();
newTag.setName(reader.getName());
if (!ignoreAttr.isEmpty()) {
newTag.getIgnoreAttr().addAll(ignoreAttr);
}
for (int i = 0; i < reader.getAttributeCount(); i++) {
// probably a qname to a type, pull namespace in differently
String tp = reader.getAttributeValue(i);
if (isType(types, reader.getName(), reader.getAttributeName(i))) {
int idx = tp.indexOf(':');
if (idx > 0 && tp.length() > idx && tp.substring(idx + 1).indexOf(':') == -1) {
String pfx = tp.substring(0, idx);
String ns = reader.getNamespaceURI(pfx);
if (ns != null) {
tp = "{" + ns + "}" + tp.substring(idx + 1);
}
} else {
String ns = reader.getNamespaceURI("");
if (ns != null) {
tp = "{" + ns + "}" + tp.substring(idx + 1);
}
}
}
newTag.getAttributes().put(reader.getAttributeName(i), tp);
}
newTag.setParent(currentTag);
currentTag.getTags().add(newTag);
currentTag = newTag;
}
if (event == XMLStreamConstants.CHARACTERS) {
newTag.setText(reader.getText());
}
if (event == XMLStreamConstants.END_ELEMENT) {
currentTag = currentTag.getParent();
}
}
reader.close();
return root;
}
Aggregations