1 21 22 23 package nu.xom; 24 25 45 public class ProcessingInstruction extends Node { 46 47 private String target; 48 private String data; 49 50 51 64 public ProcessingInstruction(String target, String data) { 65 _setTarget(target); 66 _setValue(data); 67 } 68 69 70 78 public ProcessingInstruction(ProcessingInstruction instruction) { 79 this.target = instruction.target; 80 this.data = instruction.data; 81 } 82 83 84 private ProcessingInstruction() {} 85 86 static ProcessingInstruction build(String target, String data) { 87 ProcessingInstruction result = new ProcessingInstruction(); 88 result.target = target; 89 result.data = data; 90 return result; 91 } 92 93 94 101 public final String getTarget() { 102 return target; 103 } 104 105 106 117 public void setTarget(String target) { 118 _setTarget(target); 119 } 120 121 122 private void _setTarget(String target) { 123 124 try { 125 Verifier.checkNCName(target); 126 } 127 catch (IllegalNameException ex) { 128 IllegalTargetException tex = new IllegalTargetException(ex.getMessage()); 129 tex.setData(target); 130 throw tex; 131 } 132 133 if (target.equalsIgnoreCase("xml")) { 134 IllegalTargetException tex = new IllegalTargetException( 135 target + " is not a legal processing instruction target." 136 ); 137 tex.setData(target); 138 throw tex; 139 } 140 141 this.target = target; 142 143 } 144 145 146 156 public void setValue(String data) { 157 _setValue(data); 158 } 159 160 161 private void _setValue(String data) { 162 163 Verifier.checkPCDATA(data); 164 if (data.length() != 0) { 165 if (data.indexOf("?>") >= 0) { 166 IllegalDataException ex = new IllegalDataException( 167 "Processing instruction data must not contain \"?>\"" 168 ); 169 ex.setData(data); 170 throw ex; 171 } 172 if (data.indexOf('\r') >= 0) { 173 IllegalDataException ex = new IllegalDataException( 174 "Processing instruction data cannot contain carriage returns" 175 ); 176 ex.setData(data); 177 throw ex; 178 } 179 180 char first = data.charAt(0); 181 if (first == ' ' || first == '\n' || first == '\t') { 182 IllegalDataException ex = new IllegalDataException( 183 "Processing instruction data cannot contain " + 184 "leading white space" 185 ); 186 ex.setData(data); 187 throw ex; 188 } 189 } 190 this.data = data; 191 192 } 193 194 195 203 public final String getValue() { 204 return data; 205 } 206 207 208 222 public final Node getChild(int position) { 223 throw new IndexOutOfBoundsException ( 224 "LeafNodes do not have children"); 225 } 226 227 228 235 public final int getChildCount() { 236 return 0; 237 } 238 239 240 249 public final String toXML() { 250 251 StringBuffer result = new StringBuffer ("<?"); 252 result.append(target); 253 if (data.length() > 0) { 254 result.append(' '); 255 result.append(data); 256 } 257 result.append("?>"); 258 return result.toString(); 259 260 } 261 262 263 273 public Node copy() { 274 return new ProcessingInstruction(target, data); 275 } 276 277 278 boolean isProcessingInstruction() { 279 return true; 280 } 281 282 283 294 public final String toString() { 295 return "[" + getClass().getName() + ": target=\"" 296 + target + "\"; data=\"" 297 + Text.escapeLineBreaksAndTruncate(data) +"\"]"; 298 } 299 300 301 } 302 | Popular Tags |