public List<List<T>> pathsToValue(final boolean includeValue, final Function<T, Boolean> pred) { final List<List<T>> paths = new ArrayList<>(); if (!isRoot() && pred.apply(this.value)) { List<T> path = new ArrayList<>(); if (includeValue) { path.add(this.value); } paths.add(path); } this.children().forEach(c -> { final List<List<T>> childPaths = c.pathsToValue(includeValue, pred); if (!childPaths.isEmpty() && !isRoot()) { childPaths.forEach(p -> p.add(0, this.value)); } paths.addAll(childPaths); }); return paths; }
public Set<String> attributesForElement(final List<SchemaElement> path) { Tree<SchemaElement> tree = schemaTree(); final Deque<SchemaElement> pathStack = new ArrayDeque<>(path); while (tree != null && !pathStack.isEmpty()) { final SchemaElement cur = pathStack.pop(); tree = tree.children().stream() .filter(t -> t.value().qname().equals(cur.qname())) .findFirst() .orElse(null); } final Set<String> ret = new HashSet<>(); if (tree != null && !tree.isRoot()) { ret.addAll(tree.value().attributes()); } return ret; }
private void resolveElementReferences(final Tree<SchemaElement> tree) { final SchemaElement el = tree.value(); if (el != null && el.isReference()) { final SchemaElement realEl = this.elements.get(el.qname()); el.delegate(realEl); //FIXME: what about child elements? we can't capture that currently } tree.children().forEach(this::resolveElementReferences); }
private void applyBaseToTypes() { // FIXME: this will fail if there are multiple levels of inheritance this.types.values().stream() .filter(type -> type.base() != null) .forEach(type -> { final ComplexType baseType = this.types.get(type.base()); if (baseType != null) { baseType.elements().children().forEach(t -> type.elements().addChild(t)); baseType.attributes().forEach(type::addAttribute); } }); }
public List<List<T>> pathsToValue(final Function<T, Boolean> pred) { return pathsToValue(false, pred); }
public Tree<SchemaElement> elements() { if (this.elements == null) { return new Tree<>(); } return this.elements; }
@Override public void endElement(String uri, String localName, String qName) throws SAXException { final Position pos = position(); stack.peek().value().endPosition(pos); storePosition(pos); stack.pop(); nsStack.pop(); }
@Override public boolean equals(Object obj) { if (obj == null) return false; if (this == obj) return true; if (!(obj instanceof Tree)) return false; final Tree that = (Tree) obj; return !((this.isRoot() && !that.isRoot()) || (!this.isRoot() && that.isRoot())) && !(!this.isRoot() && !this.value.equals(that.value)) && this.children.equals(that.children); }
@Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { final String namespace = attributes.getValue("xmlns"); if (namespace != null) { nsStack.push(namespace); } else { nsStack.push(nsStack.peek()); } stack.push(stack.peek().addChild(new DocElement(qname(qName), attributes) .startPosition(lastPosition))); }
public Set<SchemaElement> elementsForElement(final List<SchemaElement> path) { Tree<SchemaElement> tree = schemaTree(); final Deque<SchemaElement> pathStack = new ArrayDeque<>(path); while (tree != null && !pathStack.isEmpty()) { final SchemaElement cur = pathStack.pop(); tree = tree.children().stream() .filter(t -> t.value().qname().equals(cur.qname())) .findFirst() .orElse(null); } final Set<SchemaElement> ret = new HashSet<>(); if (tree != null && !tree.isRoot()) { ret.addAll(tree.children().stream() .map(Tree::value) .collect(Collectors.toList())); } return ret; }
private List<List<SchemaElement>> alternateElements(final boolean includeValue, final Function<SchemaElement, Boolean> pred) { return schemaTree().pathsToValue(includeValue, pred) .stream() .filter(this::allowPath) .map(this::schemaPathWithPrefix) .collect(Collectors.toList()); }
public Tree<DocElement> walk() { if (this.tree == null) { this.tree = new Tree<>(); final Deque<Tree<DocElement>> stack = new ArrayDeque<>(); stack.push(tree);
@Override public void endElement(String uri, String localName, String qName) throws SAXException { switch (qName) { case "xs:element": if (activeStack().peek().value() != null && currentElement.name().equals(activeStack().peek().value().name())) { activeStack().pop(); currentElement = activeStack().peek().value(); } else { currentElement = null; } break; case "xs:complexType": if (innerComplexTypeDepth > 0) { innerComplexTypeDepth--; } else if (currentType != null) { currentType.setElements(currentTypeElementsTree); currentTypeElementsStack = null; currentTypeElementsTree = null; currentType = null; } break; } }
@Override public int hashCode() { return (this.isRoot() ? 0 : this.value.hashCode()) + this.children.hashCode(); }
private void applyTypesToElement(final Tree<SchemaElement> tree) { final SchemaElement el = tree.value(); if (el != null) { if (el.base() != null) { applyTypeToElement(this.types.get(el.base()), tree); } if (el.type() != null) { applyTypeToElement(this.types.get(el.type()), tree); } } tree.children().forEach(this::applyTypesToElement); }
public List<List<DocElement>> pathsToDocElement(final Function<DocElement, Boolean> pred) { return documentTree().pathsToValue(true, pred); }