View Javadoc
1   /*
2    * Copyright 2007 Edward Kuns
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   * $Id: ParentNode.java 0000 2007-01-11 00:00:00Z ekuns $
17   */
18  package org.castor.xmlctf.xmldiff.xml.nodes;
19  
20  import java.util.Iterator;
21  import java.util.LinkedList;
22  
23  
24  /**
25   * The base object for both Element and RootNode.  The children of a ParentNode
26   * can be any type of XMLNode.
27   *
28   * @author <a href="mailto:edward.kuns@aspect.com">Edward Kuns</a>
29   * @version $Revision: 0000 $ $Date: 2007-01-11 00:00:00 -0600 (Thu, 11 Jan 2007) $
30   * @since Castor 1.1
31   */
32  public abstract class ParentNode extends XMLNode {
33  
34      /** The list of all children.  Children may be any type of XMLNode. */
35      private final LinkedList _children = new LinkedList();
36  
37      /**
38       * Creates a new ParentNode.
39       *
40       * @param namespace the namespace URI for this node. (May be null.)
41       * @param localName the local-name of this node. (Cannot be null.)
42       * @param nodeType the node type being created
43       */
44      ParentNode(String namespace, String localName, int nodeType) {
45          super(namespace, localName, nodeType);
46      }
47  
48      /**
49       * Returns an Iterator over the list of child nodes.
50       * @return an Iterator over the list of child nodes.
51       */
52      public Iterator getChildIterator() {
53          return _children.iterator();
54      }
55  
56      /**
57       * Returns true if this node has any child nodes.
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
66       * concatenation of the 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 }