/** * {@inheritDoc} This implementation returns the tracked node instance * acting as root node of this model. */ @Override public ImmutableNode getInMemoryRepresentation() { return getNodeHandler().getRootNode(); }
/** * Returns the value of the attribute with the given name of the wrapped * node. * * @param key the key of the attribute * @return the value of this attribute */ public Object getAttribute(final String key) { return handler.getAttributeValue(node, key); }
/** * Visits the node. Checks if a value is defined. * * @param node the actual node */ @Override public void visitBeforeChildren(final T node, final NodeHandler<T> handler) { defined = handler.getValue(node) != null || !handler.getAttributes(node).isEmpty(); }
/** * Determines the index of the given child node in the node list of its * parent. * * @param parent the parent node * @param child the child node * @param handler the node handler * @param <T> the type of the nodes involved * @return the index of this child node */ private static <T> int determineIndex(final T parent, final T child, final NodeHandler<T> handler) { return handler.getChildren(parent, handler.nodeName(child)).indexOf( child) + 1; }
/** * Actually obtains the name of the root element. This method is called by * {@code getRootElementName()}. It just returns the name of the root node. * Subclasses that treat the root element name differently can override this * method. * * @return the name of this configuration's root element */ protected String getRootElementNameInternal() { final NodeHandler<T> nodeHandler = getModel().getNodeHandler(); return nodeHandler.nodeName(nodeHandler.getRootNode()); }
@Override public void visitBeforeChildren(final ImmutableNode node, final NodeHandler<ImmutableNode> handler) { if (isLeafNode(node, handler)) { handleLeafNode(node, handler); } else { final ImmutableNode.Builder builder = new ImmutableNode.Builder(handler.getChildrenCount( node, null)) .name(handler.nodeName(node)) .value(interpolate(handler.getValue(node))) .addAttributes( interpolateAttributes(node, handler)); push(builder); } }
/** * Returns a flag whether the given node is a leaf. This is the case if * it does not have children. * * @param node the node in question * @param handler the {@code NodeHandler} * @return a flag whether this is a leaf node */ private boolean isLeafNode(final ImmutableNode node, final NodeHandler<ImmutableNode> handler) { return handler.getChildren(node).isEmpty(); }
/** * {@inheritDoc} This implementation works similar to {@code nodeKey()}, but * always adds an index expression to the resulting key. */ @Override public <T> String canonicalKey(final T node, final String parentKey, final NodeHandler<T> handler) { final T parent = handler.getParent(node); if (parent == null) { // this is the root node return StringUtils.defaultString(parentKey); } final StringBuilder buf = new StringBuilder(BUF_SIZE); if (StringUtils.isNotEmpty(parentKey)) { buf.append(parentKey).append(PATH_DELIMITER); } buf.append(handler.nodeName(node)); buf.append(START_INDEX); buf.append(determineIndex(parent, node, handler)); buf.append(END_INDEX); return buf.toString(); }
/** * Tests if a child node of the second node can be combined with the given * child node of the first node. If this is the case, the corresponding node * will be returned, otherwise <b>null</b>. This implementation checks * whether the child node occurs only once in both hierarchies and is no * known list node. * * @param node1 the first node * @param node2 the second node * @param child the child node (of the first node) * @return a child of the second node, with which a combination is possible */ protected ImmutableNode canCombine(final ImmutableNode node1, final ImmutableNode node2, final ImmutableNode child) { if (HANDLER.getChildrenCount(node2, child.getNodeName()) == 1 && HANDLER.getChildrenCount(node1, child.getNodeName()) == 1 && !isListNode(child)) { return HANDLER.getChildren(node2, child.getNodeName()).get(0); } return null; } }
/** * Populates a map with interpolated attributes of the passed in node. * * @param node the current node to be processed * @param handler the {@code NodeHandler} * @param interpolatedAttributes a map for storing the results * @return a flag whether an attribute value was changed by * interpolation */ private boolean interpolateAttributes(final ImmutableNode node, final NodeHandler<ImmutableNode> handler, final Map<String, Object> interpolatedAttributes) { boolean attributeChanged = false; for (final String attr : handler.getAttributes(node)) { final Object attrValue = interpolate(handler.getAttributeValue(node, attr)); if (valueChanged(attrValue, handler.getAttributeValue(node, attr))) { attributeChanged = true; } interpolatedAttributes.put(attr, attrValue); } return attributeChanged; }
/** * {@inheritDoc} This implementation delegates to the handler with the * parent mapping. */ @Override public ImmutableNode getParent(final ImmutableNode node) { return getParentHandler().getParent(node); }
@Override public int getChildrenCount(final T node, final String name) { return getDecoratedNodeHandler().getChildrenCount(node, name); }
/** * Returns a set with the names of the attributes of the wrapped node. * * @return the attribute names of this node */ public Set<String> getAttributes() { return handler.getAttributes(node); }
/** * Returns a list with all child nodes of the given parent node which match * the specified node name. The match is done using the current node name * matcher. * * @param handler the {@code NodeHandler} * @param parent the parent node * @param nodeName the name of the current node * @param <T> the type of the nodes to be dealt with * @return a list with all matching child nodes */ private <T> List<T> findChildNodesByName(final NodeHandler<T> handler, final T parent, final String nodeName) { return handler.getMatchingChildren(parent, nameMatcher, nodeName); } }
@Override public <C> int getMatchingChildrenCount(final T node, final NodeMatcher<C> matcher, final C criterion) { return getDecoratedNodeHandler().getMatchingChildrenCount(node, matcher, criterion); }
@Override public T getChild(final T node, final int index) { return getDecoratedNodeHandler().getChild(node, index); }
/** * Obtains the list of selected nodes for a {@code NodeNameTest} with either * a simple or a qualified name. * * @param node the current node * @param name the name to be selected * @return the list with selected sub nodes */ private List<T> createSubNodeListForName(final T node, final QName name) { final String compareName = qualifiedName(name); final List<T> result = new ArrayList<>(); for (final T child : getNodeHandler().getChildren(node)) { if (StringUtils.equals(compareName, getNodeHandler() .nodeName(child))) { result.add(child); } } return result; }
@Override public List<T> getChildren(final T node) { return getDecoratedNodeHandler().getChildren(node); }
/** * {@inheritDoc} This implementation works similar to {@code nodeKey()}; * however, each key returned by this method has an index (except for the * root node). The parent key is prepended to the name of the current node * in any case and without further checks. If it is <b>null</b>, only the * name of the current node with its index is returned. */ @Override public <T> String canonicalKey(final T node, final String parentKey, final NodeHandler<T> handler) { final String nodeName = handler.nodeName(node); final T parent = handler.getParent(node); final DefaultConfigurationKey key = new DefaultConfigurationKey(this, parentKey); key.append(StringUtils.defaultString(nodeName)); if (parent != null) { // this is not the root key key.appendIndex(determineIndex(node, parent, nodeName, handler)); } return key.toString(); }
&& HANDLER.getChildrenCount(node1, child.getNodeName()) == 1 && HANDLER.getChildrenCount(node2, child.getNodeName()) == 1) HANDLER.getChildren(node2, child.getNodeName()).get(0); if (child2.getValue() == null)