/* package */ void ensureConstraints(List<Child> children) { Set<String> nullElements = new HashSet<String>(model.getElementNames()); for (Child child : children) { nullElements.remove(child.name); } for (String s : nullElements) { ConfigModel.Property p = model.getElement(s); for (String annotation : p.getAnnotations()) { if (annotation.equals(NotNull.class.getName())) { if (p instanceof ConfigModel.Node) { ConfigModel childModel = ((ConfigModel.Node) p).model; Dom child = document.make(getHabitat(), null, this, childModel); child.register(); children.add(new Dom.NodeChild(s, child)); // recursive call to ensure the children constraints are also respected List<Child> grandChildren = new ArrayList<Child>(); child.ensureConstraints(grandChildren); if (!grandChildren.isEmpty()) { child.setChildren(grandChildren); } child.initializationCompleted(); } } } } }
/** * Locates the DOM that serves as the symbol space root. * * @return always non-null. */ public Dom getSymbolSpaceRoot(String typeName) { Dom dom = this; while (!dom.model.symbolSpaces.contains(typeName)) { Dom p = dom.parent(); if (p == null) return dom; // root dom = p; } return dom; }
/** * Copy constructor, used to get a deep copy of the passed instance * * @param source the instance to copy */ public Dom(Dom source, Dom parent) { this(source.getHabitat(), source.document, parent, source.model); List<Child> newChildren = new ArrayList<Child>(); for (Child child : source.children) { newChildren.add(child.deepCopy(this)); } setChildren(newChildren); attributes.putAll(source.attributes); }
/** * Replaces an existing {@link NodeChild} with another one. * * @see #insertAfter(Dom, String, Dom) */ public synchronized void replaceChild(Dom reference, String name, Dom newNode) { ListIterator<Child> itr = children.listIterator(); while (itr.hasNext()) { Child child = itr.next(); if (child instanceof NodeChild) { NodeChild nc = (NodeChild) child; if (nc.dom == reference) { reference.release(); newNode.domDescriptor = addWithAlias(getHabitat(), newNode, newNode.getProxyType(), newNode.getKey()); itr.set(new NodeChild(name, newNode)); return; } } } throw new IllegalArgumentException( reference + " is not a valid child of " + this + ". Children=" + children); }
/** * Inserts a new {@link Dom} node right after the given DOM element. * * @param reference If null, the new element will be inserted at the very beginning. * @param name The element name of the newly inserted item. "*" to indicate that the element name * be determined by the model of the new node. */ public synchronized void insertAfter(Dom reference, String name, Dom newNode) { // TODO: reparent newNode if (name.equals("*")) name = newNode.model.tagName; NodeChild newChild = new NodeChild(name, newNode); if (children.size() == 0) { children = new ArrayList<Child>(); } if (reference == null) { children.add(0, newChild); newNode.domDescriptor = addWithAlias(getHabitat(), newNode, newNode.getProxyType(), newNode.getKey()); return; } ListIterator<Child> itr = children.listIterator(); while (itr.hasNext()) { Child child = itr.next(); if (child instanceof NodeChild) { NodeChild nc = (NodeChild) child; if (nc.dom == reference) { itr.add(newChild); newNode.domDescriptor = addWithAlias(getHabitat(), newNode, newNode.getProxyType(), newNode.getKey()); return; } } } throw new IllegalArgumentException( reference + " is not a valid child of " + this + ". Children=" + children); }
/** Removes an existing {@link NodeChild} */ public synchronized void removeChild(final Dom reference) { ListIterator<Child> itr = children.listIterator(); while (itr.hasNext()) { Child child = itr.next(); if (child instanceof NodeChild) { NodeChild nc = (NodeChild) child; if (nc.dom == reference) { itr.remove(); reference.release(); return; } } } throw new IllegalArgumentException( reference + " is not a valid child of " + this + ". Children=" + children); }
@Override public String toString() { return "Dom.NodeChild(" + dom.getImplementation() + "," + super.toString() + ")"; }
@Override protected boolean isEmpty() { return dom.isEmpty(); }
@Override protected Child deepCopy(Dom parent) { return new NodeChild(name, dom.copy(parent)); }
protected void writeTo(XMLStreamWriter w) throws XMLStreamException { dom.writeTo(name, w); }