View Javadoc

1   /*
2    * Copyright 2009 - 2010 JEuclid, http://jeuclid.sf.net
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  
17  /* $Id $ */
18  
19  package net.sourceforge.jeuclid.biparser;
20  
21  import net.sourceforge.jeuclid.elements.generic.DocumentElement;
22  
23  import org.w3c.dom.Document;
24  import org.w3c.dom.Node;
25  import org.w3c.dom.NodeList;
26  
27  /**
28   * this class if for creating a BiTree with ABiNodes while parsing a text.
29   * 
30   * @version $Revision: 0b66106c7ff7 $
31   */
32  public class BiTree {
33  
34      /** document (DOM-tree). */
35      private Document doc;
36  
37      /** root of tree. */
38      private IBiNode root;
39  
40      /** text of tree. */
41      private String text;
42  
43      /**
44       * create a new instance of BiTree.
45       */
46      public BiTree() {
47  
48      }
49  
50      /**
51       * create a new DOM tree from bitree and save it.
52       */
53      public final void createDOMTree() {
54          Node subtree;
55  
56          this.doc = new DocumentElement();
57  
58          subtree = this.getDOMTree(this.doc);
59  
60          if (subtree != null) {
61              this.doc.appendChild(subtree);
62          }
63      }
64  
65      /**
66       * create a dom tree from bitree and return root.
67       * 
68       * @param d
69       *            document to create DOM tree
70       * @return root of DOM tree
71       */
72      public final Node getDOMTree(final Document d) {
73          Node treeRoot;
74  
75          if (this.root.getType() == BiType.EMPTY) {
76              treeRoot = this.root.getSibling().createDOMSubtree(d);
77          } else {
78              treeRoot = this.root.createDOMSubtree(d);
79          }
80  
81          return treeRoot;
82      }
83  
84      /**
85       * get root of BiTree.
86       * 
87       * @return root of BiTree
88       */
89      public final IBiNode getRoot() {
90          return this.root;
91      }
92  
93      /**
94       * insert characters into BiTree.
95       * 
96       * @param offset
97       *            insert position in text
98       * @param length
99       *            number of characters to insert
100      * @param t
101      *            text where characters were inserted
102      * @throws ReparseException
103      *             if a sax parse exception occurs
104      * @throws NonIncrementalElementException
105      *             if the subtree contains an element which cannot be
106      *             incrementally updated.
107      */
108     public final void insert(final int offset, final int length, final String t)
109             throws ReparseException, NonIncrementalElementException {
110         this.text = t;
111         this.root.insert(this, offset, length, 0);
112     }
113 
114     /**
115      * remove characters from BiTree.
116      * 
117      * @param offset
118      *            remove position in text
119      * @param length
120      *            number of characters to remove
121      * @param t
122      *            text where characters were removed
123      * @throws ReparseException
124      *             if a sax parse exception occurs
125      * @throws NonIncrementalElementException
126      *             if the subtree contains an element which cannot be
127      *             incrementally updated.
128      */
129     public final void remove(final int offset, final int length, final String t)
130             throws ReparseException, NonIncrementalElementException {
131         this.text = t;
132         this.root.remove(this, offset, length, 0);
133     }
134 
135     /**
136      * set a new root in BiTree.
137      * 
138      * @param r
139      *            new root of BiTree
140      */
141     public final void setRoot(final IBiNode r) {
142 
143         if (r == null) {
144             this.doc = null;
145         } else {
146             r.setPrevious(null);
147         }
148 
149         this.root = r;
150     }
151 
152     /**
153      * get text of BiTree.
154      * 
155      * @return text of BiTree
156      */
157     public String getText() {
158         return this.text;
159     }
160 
161     /**
162      * get document of DOM Tree.
163      * 
164      * @return document of DOM Tree
165      */
166     public Node getDocument() {
167         return this.doc;
168     }
169 
170     /**
171      * search a DOM node in BiTree and return position of node. if node is not
172      * found return null
173      * 
174      * @param node
175      *            DOM node to search for
176      * @return search result of node in inputtext
177      */
178     public TextPosition searchNode(final Node node) {
179         if (this.root == null) {
180             return null;
181         } else {
182             return this.root.searchNode(node, 0);
183         }
184     }
185 
186     /**
187      * get a formatted output of BiTree.
188      * 
189      * @return formatted output of BiTree
190      */
191     @Override
192     public String toString() {
193         if (this.root == null) {
194             return "root is null";
195         } else {
196             return this.root.toString(0);
197         }
198     }
199 
200     /**
201      * get formatted output of DOM Tree (for debugging).
202      * 
203      * @return formatted ouput of DOM Tree
204      */
205     public String toStringDOM() {
206         return this.toStringDOM(0, this.doc.getDocumentElement());
207     }
208 
209     private String toStringDOM(final int level, final Node n) {
210         int i;
211         NodeList nl;
212         final StringBuilder sb = new StringBuilder(128);
213 
214         if (n == null) {
215             return "node is null";
216         }
217 
218         for (i = 0; i < level; i++) {
219             sb.append(" ");
220         }
221 
222         sb.append(" ");
223         if (n.getNodeType() == Node.TEXT_NODE) {
224             sb.append(n.getTextContent());
225         } else {
226             sb.append(n.getNodeName());
227         }
228         sb.append("\n");
229 
230         nl = n.getChildNodes();
231         for (i = 0; i < nl.getLength(); i++) {
232             sb.append(this.toStringDOM(level + 1, nl.item(i)));
233         }
234 
235         return sb.toString();
236     }
237 }