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 org.w3c.dom.Document;
22  import org.w3c.dom.Node;
23  import org.w3c.dom.Text;
24  
25  /**
26   * this class is used to store specific information about a text node. the
27   * node cannot have a child nor a sibling
28   * 
29   * @version $Revision: cbff5bfffc35 $
30   */
31  public final class TextNode extends AbstractBiNode {
32  
33      /** DOM-info: text of node. */
34      private String text;
35  
36      /** new line. */
37      private final String nl = System.getProperty("line.separator");
38  
39      /** ladder #. */
40      private final String ladder = "#";
41  
42      /**
43       * creates a new TextNode, constructor does not create a DOM-node.
44       * 
45       * @param length
46       *            length of child
47       * @param t
48       *            DOM-info
49       */
50      public TextNode(final int length, final String t) {
51          this.setLength(length);
52          this.text = t;
53      }
54  
55      /**
56       * get the type of node.
57       * 
58       * @return TEXT
59       */
60      public BiType getType() {
61          return BiType.TEXT;
62      }
63  
64      /**
65       * insert characters in TextNode, always reparse parent node.
66       * {@inheritDoc}
67       */
68      public void insert(final BiTree biTree, final int offset,
69              final int length, final int totalOffset) throws ReparseException {
70          throw new ReparseException();
71      }
72  
73      /**
74       * remove characters in TextNode, always reparse parent node.
75       * {@inheritDoc}
76       */
77      public void remove(final BiTree biTree, final int offset,
78              final int length, final int totalOffset) throws ReparseException {
79          throw new ReparseException();
80      }
81  
82      /**
83       * forward insert/remove to sibling not allowed at a TextNode.
84       * {@inheritDoc}
85       */
86      @Override
87      public void forwardToSibling(final boolean insert, final BiTree biTree,
88              final int offset, final int length, final int totalOffset)
89              throws ReparseException {
90          throw new UnsupportedOperationException("forwardToSibling "
91                  + "at textnode not allowed");
92      }
93  
94      /**
95       * get the text of TextNode.
96       * 
97       * @return text of TextNode
98       */
99      public String getText() {
100         final String ret;
101 
102         if (this.text == null) {
103             if (this.getNode() == null) {
104                 ret = null;
105             } else {
106                 ret = this.getNode().getTextContent();
107             }
108         } else {
109             ret = this.text;
110         }
111 
112         return ret;
113     }
114 
115     /**
116      * create a DOM-textnode.
117      * 
118      * @param doc
119      *            Document to create DOM-tree
120      * @return DOM-textnode
121      */
122     public Node createDOMSubtree(final Document doc) {
123         Text textNode;
124 
125         textNode = doc.createTextNode(this.text);
126         this.text = null;
127 
128         this.setNode(textNode);
129         return textNode;
130     }
131 
132     /** {@inheritDoc} */
133     @Override
134     public TextPosition searchNode(final Node node, final int totalOffset) {
135         return super.searchNode(node, totalOffset);
136     }
137 
138     @Override
139     public String toString() {
140         final StringBuffer sb = new StringBuffer(32);
141 
142         sb.append("[TEXT length:");
143         sb.append(this.getLength());
144         sb.append(" '");
145         sb.append(this.getText().replaceAll(this.nl, this.ladder));
146         sb.append("']");
147 
148         return sb.toString();
149     }
150 
151     /** {@inheritDoc} */
152     public String toString(final int level) {
153         final StringBuffer sb = new StringBuffer();
154 
155         sb.append(this.formatLength());
156         sb.append(':');
157         for (int i = 0; i <= level; i++) {
158             sb.append(' ');
159         }
160 
161         sb.append('\'');
162         sb.append(this.getText().replaceAll(this.nl, this.ladder));
163         sb.append('\'');
164 
165         return sb.toString();
166     }
167 }