use of org.apache.cxf.tools.common.Tag 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 org.apache.cxf.tools.common.Tag 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;
}
use of org.apache.cxf.tools.common.Tag in project cxf by apache.
the class StAXUtilTest method testGetTags.
@Test
public void testGetTags() throws Exception {
File file = new File(getClass().getResource("resources/test.wsdl").toURI());
file = getResource("resources/test2.wsdl");
Tag tag1 = ToolsStaxUtils.getTagTree(file);
assertEquals(1, tag1.getTags().size());
Tag def1 = tag1.getTags().get(0);
assertEquals(6, def1.getTags().size());
Tag types1 = def1.getTags().get(0);
Tag schema1 = types1.getTags().get(0);
assertEquals(4, schema1.getTags().size());
file = getResource("resources/test3.wsdl");
Tag tag2 = ToolsStaxUtils.getTagTree(file);
assertEquals(1, tag2.getTags().size());
Tag def2 = tag2.getTags().get(0);
assertEquals(6, def2.getTags().size());
Tag types2 = def2.getTags().get(0);
Tag schema2 = types2.getTags().get(0);
assertEquals(4, schema2.getTags().size());
assertTagEquals(schema1, schema2);
}
use of org.apache.cxf.tools.common.Tag in project cxf by apache.
the class ToolsStaxUtilsTest method testGetTags.
@Test
public void testGetTags() throws Exception {
Tag tag1 = ToolsStaxUtils.getTagTree(getClass().getResourceAsStream("resources/test2.wsdl"));
assertEquals(1, tag1.getTags().size());
Tag def1 = tag1.getTags().get(0);
assertEquals(6, def1.getTags().size());
Tag types1 = def1.getTags().get(0);
Tag schema1 = types1.getTags().get(0);
assertEquals(4, schema1.getTags().size());
Tag tag2 = ToolsStaxUtils.getTagTree(getClass().getResourceAsStream("resources/test3.wsdl"));
assertEquals(1, tag2.getTags().size());
Tag def2 = tag2.getTags().get(0);
assertEquals(6, def2.getTags().size());
Tag types2 = def2.getTags().get(0);
Tag schema2 = types2.getTags().get(0);
assertEquals(4, schema2.getTags().size());
assertTagEquals(schema1, schema2);
}
use of org.apache.cxf.tools.common.Tag in project cxf by apache.
the class ToolsStaxUtils method getTagTree.
public static Tag getTagTree(final InputStream is, final Collection<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;
} else if (event == XMLStreamConstants.CHARACTERS) {
newTag.setText(reader.getText());
} else if (event == XMLStreamConstants.END_ELEMENT) {
currentTag = currentTag.getParent();
}
}
reader.close();
return root;
}
Aggregations