vgritsenko 2004/01/28 06:10:23
Modified: java/src/org/apache/xindice/util Configuration.java Log: * getChildNodes() can not return null (as per DOM javadoc) * Add setDirty() / isDirty() * Add getChildren(String name) method Revision Changes Path 1.17 +80 -46 xml-xindice/java/src/org/apache/xindice/util/Configuration.java Index: Configuration.java =================================================================== RCS file: /home/cvs/xml-xindice/java/src/org/apache/xindice/util/Configuration.java,v retrieving revision 1.16 retrieving revision 1.17 diff -u -r1.16 -r1.17 --- Configuration.java 24 Dec 2003 13:49:53 -0000 1.16 +++ Configuration.java 28 Jan 2004 14:10:22 -0000 1.17 @@ -87,11 +87,10 @@ private static final Configuration[] EMPTY = new Configuration[0]; - // commented out 2002-07-16: not used - //private static final Map Modified = Collections.synchronizedMap(new HashMap()); // Temporary HACK - - private Element config = null; - private boolean readOnly = true; + private Element config; + private boolean readOnly; + private boolean isDirty; + private Configuration root; public Configuration(Element config, boolean readOnly) { @@ -103,17 +102,47 @@ this(config.getDocumentElement(), readOnly); } + /** + * Create read only configuration out of Element + */ public Configuration(Element config) { this(config, true); } + /** + * Create read only configuration out of Document + */ public Configuration(Document config) { this(config.getDocumentElement()); } + private Configuration(Configuration root, Element config, boolean readOnly) { + this.root = root; + this.config = config; + this.readOnly = readOnly; + } + + private void setDirty() { + if (root != null) { + root.setDirty(); + } else { + this.isDirty = true; + } + } + + public boolean isDirty() { + if (root != null) { + return root.isDirty(); + } else { + return isDirty; + } + } + /** - * getElement returns the Element being managed by this - * Configuration. + * getElement returns the Element being managed by this Configuration. + * + * Returned Element should not be modified, use Configuration's methods + * to modify managed Element. * * @return The Configuration Element * @throws ReadOnlyException If the Configuration is Read-only @@ -144,13 +173,6 @@ return config.getAttributes().getLength() > 0; } - /* - // commented out 2002-07-16: not doing anything - public void setDirty() { - //Modified.put(config.getOwnerDocument(), Boolean.TRUE); - } - */ - /** * getAttribute returns an attribute from the Configuration node. * @@ -638,18 +660,15 @@ */ public Configuration[] getChildren() { NodeList list = config.getChildNodes(); - if (list == null) { - return EMPTY; - } - - List tmp = new ArrayList(); int size = list.getLength(); + + List children = new ArrayList(); for (int i = 0; i < size; i++) { if (list.item(i).getNodeType() == Node.ELEMENT_NODE) { - tmp.add(new Configuration((Element) list.item(i), readOnly)); + children.add(new Configuration(this, (Element) list.item(i), readOnly)); } } - return (Configuration[]) tmp.toArray(EMPTY); + return (Configuration[]) children.toArray(EMPTY); } /** @@ -660,15 +679,11 @@ */ public void processChildren(ConfigurationCallback callback) { NodeList list = config.getChildNodes(); - if (list == null) { - return; - } - int size = list.getLength(); try { for (int i = 0; i < size; i++) { if (list.item(i).getNodeType() == Node.ELEMENT_NODE) { - callback.process(new Configuration((Element) list.item(i), readOnly)); + callback.process(new Configuration(this, (Element) list.item(i), readOnly)); } } } catch (Exception e) { @@ -687,9 +702,6 @@ */ public void processChildren(String name, ConfigurationCallback callback) { NodeList list = config.getChildNodes(); - if (list == null) { - return; - } Node[] nl = new Node[list.getLength()]; for (int i = 0; i < nl.length; i++) { @@ -700,7 +712,7 @@ try { for (int i = 0; i < size; i++) { if (nl[i].getNodeType() == Node.ELEMENT_NODE && nl[i].getNodeName().equals(name)) { - callback.process(new Configuration((Element) nl[i], readOnly)); + callback.process(new Configuration(this, (Element) nl[i], readOnly)); } } } catch (Exception e) { @@ -711,6 +723,27 @@ } /** + * getChildren returns all children Configuration nodes that matches the + * specified name. + * + * @param name The Configuration node name + * @return Children Configuration + */ + public Configuration[] getChildren(String name) { + NodeList list = config.getChildNodes(); + int size = list.getLength(); + + List children = new ArrayList(); + for (int i = 0; i < size; i++) { + if (list.item(i).getNodeType() == Node.ELEMENT_NODE && list.item(i).getNodeName().equals(name)) { + children.add(new Configuration(this, (Element) list.item(i), readOnly)); + } + } + + return (Configuration[])children.toArray(EMPTY); + } + + /** * getChild returns the first child Configuration node that matches the * specified name. * @@ -725,14 +758,11 @@ for (int i = 0; i < size; i++) { if (list.item(i).getNodeType() == Node.ELEMENT_NODE && list.item(i).getNodeName().equals(name)) { - return new Configuration((Element) list.item(i), readOnly); + return new Configuration(this, (Element) list.item(i), readOnly); } } - if (create) { - return add(name); - } else { - return null; - } + + return create? add(name) : null; } /** @@ -771,10 +801,11 @@ if (readOnly) { throw new ReadOnlyException(); } + Element elem = config.getOwnerDocument().createElement(name); config.appendChild(elem); - //setDirty(); - return new Configuration(elem, readOnly); + setDirty(); + return new Configuration(this, elem, readOnly); } /** @@ -790,10 +821,11 @@ if (readOnly) { throw new ReadOnlyException(); } + Node imported = config.getOwnerDocument().importNode(newConfig.config, true); config.appendChild(imported); newConfig.config = (Element) imported; - //setDirty(); + setDirty(); } /** @@ -809,8 +841,7 @@ if (config.getParentNode() != null) { config.getParentNode().removeChild(config); } - - //setDirty(); + setDirty(); } /** @@ -823,8 +854,9 @@ if (readOnly) { throw new ReadOnlyException(); } + config.removeAttribute(name); - //setDirty(); + setDirty(); } /** @@ -838,8 +870,9 @@ if (readOnly) { throw new ReadOnlyException(); } + config.setAttribute(name, value); - //setDirty(); + setDirty(); } /** @@ -940,6 +973,7 @@ if (readOnly) { throw new ReadOnlyException(); } + NodeList list = config.getChildNodes(); if (list.getLength() == 1 && list.item(0).getNodeType() == Node.TEXT_NODE) { list.item(0).setNodeValue(value); @@ -947,7 +981,7 @@ Text text = config.getOwnerDocument().createTextNode(value); config.appendChild(text); } - //setDirty(); + setDirty(); } /**