1 19 20 package org.netbeans.modules.xml.xdm.nodes; 21 import java.util.List ; 22 import java.util.regex.Pattern ; 23 import org.netbeans.modules.xml.xdm.visitor.XMLNodeVisitor; 24 25 29 public class Text extends NodeImpl implements Node, org.w3c.dom.Text { 30 31 public void accept(XMLNodeVisitor visitor) { 32 visitor.visit(this); 33 } 34 35 Text() { 36 super(); 37 } 38 39 Text(String text) { 40 this(); 41 setText(text); 42 } 43 44 @Override 45 public String getNodeValue() { 46 return getText(); 47 } 48 49 public short getNodeType() { 50 return Node.TEXT_NODE; 51 } 52 53 public String getNodeName() { 54 return "#text"; } 56 57 @Override 58 public String getNamespaceURI() { 59 return null; 60 } 61 62 public String getText() { 63 StringBuilder txtBuf = new StringBuilder (); 64 for (Token token: getTokens()) { 65 if(token.getType()==TokenType.TOKEN_CHARACTER_DATA) 66 txtBuf.append(token.getValue()); 67 } 68 return removeEntityReference(txtBuf.toString()); 69 } 70 71 public void setText(String text) { 72 checkNotInTree(); 73 List <Token> tokens = getTokensForWrite(); 74 if(!tokens.isEmpty())tokens.clear(); 75 if(text==null) text = ""; 77 text = insertEntityReference(text); 78 tokens.add(Token.create(text, TokenType.TOKEN_CHARACTER_DATA)); 79 } 80 81 89 public boolean isElementContentWhitespace() { 90 return false; 92 } 93 94 99 public int getLength() { 100 return -1; 102 } 103 104 146 public org.w3c.dom.Text replaceWholeText(String content) { 147 return null; 149 } 150 151 168 public org.w3c.dom.Text splitText(int offset) { 169 return null; 171 } 172 173 181 public String getWholeText() { 182 return null; 184 } 185 186 197 public void setData(String text) { 198 checkNotInTree(); 199 List <Token> tokens = getTokensForWrite(); 200 if(!tokens.isEmpty())tokens.clear(); 201 if(text==null) text = ""; 203 tokens.add(Token.create(text, TokenType.TOKEN_CHARACTER_DATA)); 204 } 205 206 214 public void appendData(String arg) { 215 } 217 218 237 public void replaceData(int offset, int count, String arg) { 238 } 240 241 251 public void insertData(int offset, String arg) { 252 } 254 255 270 public String substringData(int offset, int count) { 271 return null; 273 } 274 275 288 public String getData() { 289 StringBuilder txtBuf = new StringBuilder (); 290 for (Token token: getTokens()) { 291 if(token.getType()==TokenType.TOKEN_CHARACTER_DATA) 292 txtBuf.append(token.getValue()); 293 } 294 return txtBuf.toString(); 295 } 296 297 312 public void deleteData(int offset, int count) { 313 } 315 316 private String insertEntityReference(String text) { 317 String result = removeEntityReference(text); 319 result = result.replaceAll("&","&"); result = result.replaceAll("<","<"); result = result.replaceAll(">",">"); return result; 325 } 326 327 private String removeEntityReference(String text) { 328 String result = text; 329 result = AMPERSAND_PATTERN.matcher(result).replaceAll("&"); result = LESS_THAN_PATTERN.matcher(result).replaceAll("<"); result = GREATER_THAN_PATTERN.matcher(result).replaceAll(">"); return result; 335 } 336 337 private static final Pattern AMPERSAND_PATTERN = Pattern.compile("&"); private static final Pattern LESS_THAN_PATTERN = Pattern.compile("<"); private static final Pattern GREATER_THAN_PATTERN = Pattern.compile(">"); } 341 | Popular Tags |