/** * Returns true if this element is empty meaning all their attributes have default values and it * has no descendants. * * @return true if the element is empty, false otherwise */ private boolean isEmpty() { Map<String, String> attributesToWrite = attributesToWrite(); if (!attributesToWrite.isEmpty()) { return false; } // if we have children, we are not empty. return children.isEmpty(); }
/** * Updates the attribute value. * * <p>This would trigger the re-injection of the value. */ public void attribute(String name, String value) { if (value == null) { attributes.remove(name); } else { attributes.put(name, value); // TODO: // this re-injection has two problems. First, it forces an instantiation // even if that hasn't happened yet. Second, if the component is scoped, // this won't work correctly (but then, there's no way to make that work, // since we can't enumerate all scope instances.) getInjector().injectAttribute(this, name, get()); } }
/** * Returns the map of attributes names and values for attributes which value is neither null or * the default value. These attributes are considered having a non default value and must be * written out. * * @return map of attributes indexed by name that must be persisted */ private Map<String, String> attributesToWrite() { Map<String, String> attributesToWrite = new HashMap<String, String>(); Map<String, String> localAttr = new HashMap<String, String>(attributes); for (Map.Entry<String, String> a : localAttr.entrySet()) { ConfigModel.AttributeLeaf am = model.attributes.get(a.getKey()); String dv = am.getDefaultValue(); if (dv == null || !dv.equals(a.getValue())) { attributesToWrite.put(a.getKey(), a.getValue()); } } return attributesToWrite; }
/** * Obtians the attribute value without variable expansion. * * @return null if the attribute is not found. */ public String rawAttribute(String name) { String value = attributes.get(name); if (value == null && model.attributes.containsKey(name)) { value = model.attributes.get(name).getDefaultValue(); } return value; }
/** * 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); }
/*package*/ void fillAttributes(XMLStreamReader in) { for (int i = in.getAttributeCount() - 1; i >= 0; i--) { String n = in.getAttributeLocalName(i); if (model.attributes.containsKey(n)) { if (attributes == null) attributes = new HashMap<String, String>(); attributes.put(n, in.getAttributeValue(i)); } } if (attributes == null) attributes = Collections.emptyMap(); }
/** * Returns the list of attributes with a value on this config instance. This is by definition a * subset of the attributes names as known to the model {@see ConfigModel.getAttributeNames}. * * @return list of attributes names which have values on this config instance */ public Set<String> getAttributeNames() { return Collections.unmodifiableSet(attributes.keySet()); }