View Javadoc
1   /*
2    * Copyright 2007 Edward Kuns
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5    * in compliance with the License. You may obtain a copy of the License at
6    *
7    * http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software distributed under the License
10   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11   * or implied. See the License for the specific language governing permissions and limitations under
12   * the License.
13   *
14   * $Id: ParentNode.java 0000 2007-01-11 00:00:00Z ekuns $
15   */
16  package org.castor.xmlctf.xmldiff.xml.nodes;
17  
18  import java.util.Iterator;
19  import java.util.LinkedList;
20  
21  
22  /**
23   * The base object for both Element and RootNode. The children of a ParentNode can be any type of
24   * XMLNode.
25   *
26   * @author <a href="mailto:edward.kuns@aspect.com">Edward Kuns</a>
27   * @version $Revision: 0000 $ $Date: 2007-01-11 00:00:00 -0600 (Thu, 11 Jan 2007) $
28   * @since Castor 1.1
29   */
30  public abstract class ParentNode extends XMLNode {
31  
32    /** The list of all children. Children may be any type of XMLNode. */
33    private final LinkedList _children = new LinkedList();
34  
35    /**
36     * Creates a new ParentNode.
37     *
38     * @param namespace the namespace URI for this node. (May be null.)
39     * @param localName the local-name of this node. (Cannot be null.)
40     * @param nodeType the node type being created
41     */
42    ParentNode(String namespace, String localName, int nodeType) {
43      super(namespace, localName, nodeType);
44    }
45  
46    /**
47     * Returns an Iterator over the list of child nodes.
48     * 
49     * @return an Iterator over the list of child nodes.
50     */
51    public Iterator getChildIterator() {
52      return _children.iterator();
53    }
54  
55    /**
56     * Returns true if this node has any child nodes.
57     * 
58     * @return True if this node has any child nodes.
59     */
60    public boolean hasChildNodes() {
61      return !_children.isEmpty();
62    }
63  
64    /**
65     * Returns the string value of this parent node. The string value is the concatenation of the
66     * string value of all child nodes.
67     *
68     * @return The string value of the node
69     */
70    public String getStringValue() {
71      if (_children.isEmpty()) {
72        return "";
73      }
74  
75      StringBuffer sb = new StringBuffer();
76      for (Iterator i = _children.iterator(); i.hasNext();) {
77        XMLNode child = (XMLNode) i.next();
78        sb.append(child.getStringValue());
79      }
80      return sb.toString();
81    }
82  
83    /**
84     * Adds the given child node to this ParentNode.
85     *
86     * @param node the child node to add
87     */
88    public void addChild(XMLNode node) {
89      if (node == null) {
90        return;
91      }
92  
93      // Normalize text nodes if necessary
94      XMLNode last = (_children.isEmpty()) ? null : (XMLNode) _children.getLast();
95      if (last != null && last.getNodeType() == XMLNode.TEXT && node.getNodeType() == XMLNode.TEXT) {
96        Text text = (Text) last;
97        text.setValue(text.getStringValue() + node.getStringValue());
98      } else {
99        node.setParent(this);
100       _children.add(node);
101     }
102   }
103 
104 }