1 56 57 package org.jdom; 58 59 import java.util.*; 60 61 72 73 public class ProcessingInstruction extends Content { 74 75 private static final String CVS_ID = 76 "@(#) $RCSfile: ProcessingInstruction.java,v $ $Revision: 1.46 $ $Date: 2004/02/27 11:32:57 $ $Name: $"; 77 78 79 protected String target; 80 81 82 protected String rawData; 83 84 85 protected Map mapData; 86 87 91 protected ProcessingInstruction() { } 92 93 103 public ProcessingInstruction(String target, Map data) { 104 setTarget(target); 105 setData(data); 106 } 107 108 117 public ProcessingInstruction(String target, String data) { 118 setTarget(target); 119 setData(data); 120 } 121 122 128 public ProcessingInstruction setTarget(String newTarget) { 129 String reason; 130 if ((reason = Verifier.checkProcessingInstructionTarget(newTarget)) 131 != null) { 132 throw new IllegalTargetException(newTarget, reason); 133 } 134 135 target = newTarget; 136 return this; 137 } 138 139 145 public String getValue() { 146 return rawData; 147 } 148 149 150 155 public String getTarget() { 156 return target; 157 } 158 159 164 public String getData() { 165 return rawData; 166 } 167 168 175 public List getPseudoAttributeNames() { 176 Set mapDataSet = mapData.entrySet(); 177 List nameList = new ArrayList(); 178 for (Iterator i = mapDataSet.iterator(); i.hasNext();) { 179 String wholeSet = (i.next()).toString(); 180 String attrName = wholeSet.substring(0,(wholeSet.indexOf("="))); 181 nameList.add(attrName); 182 } 183 return nameList; 184 } 185 186 192 public ProcessingInstruction setData(String data) { 193 String reason = Verifier.checkProcessingInstructionData(data); 194 if (reason != null) { 195 throw new IllegalDataException(data, reason); 196 } 197 198 this.rawData = data; 199 this.mapData = parseData(data); 200 return this; 201 } 202 203 212 public ProcessingInstruction setData(Map data) { 213 String temp = toString(data); 214 215 String reason = Verifier.checkProcessingInstructionData(temp); 216 if (reason != null) { 217 throw new IllegalDataException(temp, reason); 218 } 219 220 this.rawData = temp; 221 this.mapData = data; 222 return this; 223 } 224 225 226 235 public String getPseudoAttributeValue(String name) { 236 return (String )mapData.get(name); 237 } 238 239 248 public ProcessingInstruction setPseudoAttribute(String name, String value) { 249 String reason = Verifier.checkProcessingInstructionData(name); 250 if (reason != null) { 251 throw new IllegalDataException(name, reason); 252 } 253 254 reason = Verifier.checkProcessingInstructionData(value); 255 if (reason != null) { 256 throw new IllegalDataException(value, reason); 257 } 258 259 this.mapData.put(name, value); 260 this.rawData = toString(mapData); 261 return this; 262 } 263 264 265 272 public boolean removePseudoAttribute(String name) { 273 if ((mapData.remove(name)) != null) { 274 rawData = toString(mapData); 275 return true; 276 } 277 278 return false; 279 } 280 281 287 private String toString(Map mapData) { 288 StringBuffer rawData = new StringBuffer (); 289 290 Iterator i = mapData.keySet().iterator(); 291 while (i.hasNext()) { 292 String name = (String )i.next(); 293 String value = (String )mapData.get(name); 294 rawData.append(name) 295 .append("=\"") 296 .append(value) 297 .append("\" "); 298 } 299 if (rawData.length() > 0) { 301 rawData.setLength(rawData.length() - 1); 302 } 303 304 return rawData.toString(); 305 } 306 307 311 private Map parseData(String rawData) { 312 323 Map data = new HashMap(); 324 325 327 String inputData = rawData.trim(); 329 330 while (!inputData.trim().equals("")) { 332 334 String name = ""; 336 String value = ""; 337 int startName = 0; 338 char previousChar = inputData.charAt(startName); 339 int pos = 1; 340 for (; pos<inputData.length(); pos++) { 341 char currentChar = inputData.charAt(pos); 342 if (currentChar == '=') { 343 name = inputData.substring(startName, pos).trim(); 344 int[] bounds = extractQuotedString( 347 inputData.substring(pos+1)); 348 if (bounds == null) { 350 return new HashMap(); 351 } 352 value = inputData.substring(bounds[0]+pos+1, 353 bounds[1]+pos+1); 354 pos += bounds[1] + 1; break; 356 } 357 else if (Character.isWhitespace(previousChar) 358 && !Character.isWhitespace(currentChar)) { 359 startName = pos; 360 } 361 362 previousChar = currentChar; 363 } 364 365 inputData = inputData.substring(pos); 367 368 371 if (name.length() > 0 && value != null) { 374 data.put(name, value); 380 } 382 } 383 384 return data; 385 } 386 387 402 private static int[] extractQuotedString(String rawData) { 403 boolean inQuotes = false; 405 406 char quoteChar = '"'; 408 409 int start = 0; 412 413 for (int pos=0; pos < rawData.length(); pos++) { 416 char currentChar = rawData.charAt(pos); 417 if (currentChar=='"' || currentChar=='\'') { 418 if (!inQuotes) { 419 quoteChar = currentChar; 421 inQuotes = true; 422 start = pos+1; 423 } 424 else if (quoteChar == currentChar) { 425 inQuotes = false; 427 return new int[] { start, pos }; 428 } 429 } 432 } 433 434 return null; 435 } 436 437 447 public String toString() { 448 return new StringBuffer () 449 .append("[ProcessingInstruction: ") 450 .append(new org.jdom.output.XMLOutputter().outputString(this)) 451 .append("]") 452 .toString(); 453 } 454 455 461 public Object clone() { 462 ProcessingInstruction pi = (ProcessingInstruction) super.clone(); 463 464 467 if (mapData != null) { 469 pi.mapData = parseData(rawData); 470 } 471 return pi; 472 } 473 } 474 | Popular Tags |