001 /* 002 * Copyright 2009 - 2010 JEuclid, http://jeuclid.sf.net 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017 /* $Id $ */ 018 019 package net.sourceforge.jeuclid.biparser; 020 021 import org.w3c.dom.Document; 022 import org.w3c.dom.Node; 023 import org.w3c.dom.Text; 024 025 /** 026 * this class is used to store specific information about a text node. the 027 * node cannot have a child nor a sibling 028 * 029 * @version $Revision: cbff5bfffc35 $ 030 */ 031 public final class TextNode extends AbstractBiNode { 032 033 /** DOM-info: text of node. */ 034 private String text; 035 036 /** new line. */ 037 private final String nl = System.getProperty("line.separator"); 038 039 /** ladder #. */ 040 private final String ladder = "#"; 041 042 /** 043 * creates a new TextNode, constructor does not create a DOM-node. 044 * 045 * @param length 046 * length of child 047 * @param t 048 * DOM-info 049 */ 050 public TextNode(final int length, final String t) { 051 this.setLength(length); 052 this.text = t; 053 } 054 055 /** 056 * get the type of node. 057 * 058 * @return TEXT 059 */ 060 public BiType getType() { 061 return BiType.TEXT; 062 } 063 064 /** 065 * insert characters in TextNode, always reparse parent node. 066 * {@inheritDoc} 067 */ 068 public void insert(final BiTree biTree, final int offset, 069 final int length, final int totalOffset) throws ReparseException { 070 throw new ReparseException(); 071 } 072 073 /** 074 * remove characters in TextNode, always reparse parent node. 075 * {@inheritDoc} 076 */ 077 public void remove(final BiTree biTree, final int offset, 078 final int length, final int totalOffset) throws ReparseException { 079 throw new ReparseException(); 080 } 081 082 /** 083 * forward insert/remove to sibling not allowed at a TextNode. 084 * {@inheritDoc} 085 */ 086 @Override 087 public void forwardToSibling(final boolean insert, final BiTree biTree, 088 final int offset, final int length, final int totalOffset) 089 throws ReparseException { 090 throw new UnsupportedOperationException("forwardToSibling " 091 + "at textnode not allowed"); 092 } 093 094 /** 095 * get the text of TextNode. 096 * 097 * @return text of TextNode 098 */ 099 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 }