1 21 22 package nu.xom; 23 24 38 public class Comment extends Node { 39 40 41 private String data; 42 43 54 public Comment(String data) { 55 _setValue(data); 56 } 57 58 59 67 public Comment(Comment comment) { 68 this.data = comment.data; 69 } 70 71 72 private Comment() {} 73 74 static Comment build(String data) { 75 Comment result = new Comment(); 76 result.data = data; 77 return result; 78 } 79 80 81 91 public final String getValue() { 92 return data; 93 } 94 95 96 110 public void setValue(String data) { 111 _setValue(data); 112 } 113 114 115 private void _setValue(String data) { 116 117 if (data == null) data = ""; 118 else { 119 Verifier.checkPCDATA(data); 120 121 if (data.indexOf("--") != -1) { 122 IllegalDataException ex = new IllegalDataException( 123 "Comment data contains a double hyphen (--)."); 124 ex.setData(data); 125 throw ex; 126 } 127 128 if (data.indexOf('\r') != -1) { 129 IllegalDataException ex = new IllegalDataException( 130 "Comment data cannot contain carriage returns."); 131 ex.setData(data); 132 throw ex; 133 } 134 135 if (data.endsWith("-")) { 136 IllegalDataException ex = new IllegalDataException( 137 "Comment data ends with a hyphen."); 138 ex.setData(data); 139 throw ex; 140 } 141 142 } 143 this.data = data; 144 145 } 146 147 148 162 public final Node getChild(int position) { 163 throw new IndexOutOfBoundsException ( 164 "LeafNodes do not have children"); 165 } 166 167 168 175 public final int getChildCount() { 176 return 0; 177 } 178 179 180 191 public Node copy() { 192 return new Comment(data); 193 } 194 195 196 206 public final String toXML() { 207 StringBuffer result = new StringBuffer ("<!--"); 208 result.append(data); 209 result.append("-->"); 210 return result.toString(); 211 } 212 213 214 224 public final String toString() { 225 226 String value = getValue(); 227 if (value.length() <= 40) { 228 return "[" + getClass().getName() + ": " 229 + Text.escapeLineBreaksAndTruncate(value) + "]"; 230 } 231 232 return "[" + getClass().getName() + ": " 233 + Text.escapeLineBreaksAndTruncate(value.substring(0, 35)) + "...]"; 234 235 } 236 237 238 boolean isComment() { 239 return true; 240 } 241 242 243 } 244 | Popular Tags |