1 17 package org.eclipse.emf.ecore.xmi.impl; 18 19 20 import java.util.List ; 21 22 import org.eclipse.emf.common.util.BasicEList; 23 24 25 34 public class XMLString extends StringSegment 35 { 36 protected List elementNames; 37 38 protected List mixed; 39 40 protected boolean isUnformatted; 41 42 protected boolean isMixed; 43 44 protected List indents; 45 46 protected int depth; 47 48 protected int lineWidth; 49 50 protected int markedLineWidth; 51 52 protected int currentLineWidth; 53 54 protected boolean lastElementIsStart; 55 56 protected Object firstElementMark; 57 58 protected boolean seenRoot; 59 60 protected boolean saveDoctype; 61 62 protected String publicId; 63 64 protected String systemId; 65 66 public XMLString() 67 { 68 this(80); 69 } 70 71 public XMLString(int lineWidth) 72 { 73 this(lineWidth, null); 74 } 75 76 public XMLString(int lineWidth, String temporaryFileName) 77 { 78 super(temporaryFileName); 79 80 this.lineWidth = lineWidth; 81 elementNames = new BasicEList(); 82 mixed = new BasicEList(); 83 indents = new BasicEList(); 84 indents.add(""); 85 } 86 87 public XMLString(int lineWidth, String publicId, String systemId) 88 { 89 this(lineWidth, publicId, systemId, null); 90 } 91 92 public XMLString(int lineWidth, String publicId, String systemId, String temporaryFileName) 93 { 94 this(lineWidth, temporaryFileName); 95 if (publicId != null || systemId != null) 96 { 97 saveDoctype = true; 98 this.publicId = publicId; 99 this.systemId = systemId; 100 } 101 } 102 103 public void setLineWidth(int lineWidth) 104 { 105 this.lineWidth = lineWidth; 106 } 107 108 public void reset(String publicId, String systemId, int lineWidth, String temporaryFileName) 109 { 110 super.reset(); 111 setTemporaryFileName(temporaryFileName); 112 elementNames.clear(); 113 mixed.clear(); 114 indents.clear(); 115 indents.add(""); 116 if (publicId != null || systemId != null) 117 { 118 saveDoctype = true; 119 this.publicId = publicId; 120 this.systemId = systemId; 121 } 122 else 123 { 124 saveDoctype = false; 125 } 126 seenRoot = false; 127 lastElementIsStart = false; 128 isMixed = false; 129 isUnformatted = false; 130 depth = 0; 131 markedLineWidth = 0; 132 currentLineWidth = lineWidth; 133 firstElementMark = null; 134 } 135 136 public void startElement(String name) 137 { 138 if (lastElementIsStart) 139 { 140 closeStartElement(); 141 } 142 elementNames.add(name); 143 if (name != null) 144 { 145 saveDoctype(name); 146 ++depth; 147 if (!isMixed) 148 { 149 add(getElementIndent()); 150 } 151 add("<"); 152 add(name); 153 if (firstElementMark == null) 154 { 155 firstElementMark = mark(); 156 } 157 lastElementIsStart = true; 158 } 159 else if (!isMixed) 160 { 161 add(getElementIndent(1)); 162 } 163 164 mixed.add(isMixed ? Boolean.TRUE : Boolean.FALSE); 165 isMixed = isUnformatted; 166 } 167 168 public void saveNilElement(String name) 169 { 170 if (lastElementIsStart) 171 { 172 closeStartElement(); 173 } 174 saveDoctype(name); 175 176 ++depth; 177 if (!isMixed) 178 { 179 add(getElementIndent()); 180 } 181 add("<"); 182 add(name); 183 if (firstElementMark == null) 184 { 185 firstElementMark = mark(); 186 } 187 if (currentLineWidth > lineWidth) 188 { 189 addLine(); 190 add(getAttributeIndent()); 191 } 192 else 193 { 194 add(" "); 195 } 196 add("xsi:nil=\"true\"/>"); 197 198 --depth; 199 200 if (!isUnformatted && !isMixed) 201 { 202 addLine(); 203 } 204 lastElementIsStart = false; 205 } 206 207 public void saveDataValueElement(String name, String content) 208 { 209 if (lastElementIsStart) 210 { 211 closeStartElement(); 212 } 213 saveDoctype(name); 214 215 ++depth; 216 if (!isMixed) 217 { 218 add(getElementIndent()); 219 } 220 add("<"); 221 add(name); 222 if (firstElementMark == null) 223 { 224 firstElementMark = mark(); 225 } 226 227 add(">"); 228 add(content); 229 add("</"); 230 231 --depth; 232 add(name); 233 add(">"); 234 if (!isUnformatted && !isMixed) 235 { 236 addLine(); 237 } 238 lastElementIsStart = false; 239 } 240 241 final protected void saveDoctype(String name) 242 { 243 if (!seenRoot) 244 { 245 seenRoot = true; 246 if (saveDoctype) 248 { 249 add("<!DOCTYPE "); 250 add(name); 251 if (publicId != null) 252 { 253 add(" PUBLIC \""); 254 add(publicId); 255 add("\" "); 256 add("\""); 257 add(systemId); 258 add("\">"); 259 } 260 else if (systemId != null) 261 { 262 add(" SYSTEM \""); 263 add(systemId); 264 add("\">"); 265 } 266 else 267 { 268 add(">"); 269 } 270 271 addLine(); 272 } 273 } 274 } 275 276 public void setMixed(boolean isMixed) 277 { 278 this.isMixed = isMixed; 279 } 280 281 public void setUnformatted(boolean isUnformatted) 282 { 283 this.isUnformatted = isUnformatted; 284 } 285 286 public void addAttribute(String name, String value) 287 { 288 if (currentLineWidth > lineWidth) 289 { 290 addLine(); 291 add(getAttributeIndent()); 292 } 293 else 294 { 295 add(" "); 296 } 297 add(name); 298 add("=\""); 299 add(value); 300 add("\""); 301 } 302 303 public void addAttributeNS(String prefix, String localName, String value) 304 { 305 if (currentLineWidth > lineWidth) 306 { 307 addLine(); 308 add(getAttributeIndent()); 309 } 310 else 311 { 312 add(" "); 313 } 314 add(prefix); 315 add(":"); 316 add(localName); 317 add("=\""); 318 add(value); 319 add("\""); 320 } 321 322 public void startAttribute(String name) 323 { 324 if (currentLineWidth > lineWidth) 325 { 326 addLine(); 327 add(getAttributeIndent()); 328 } 329 else 330 { 331 add(" "); 332 } 333 add(name); 334 add("=\""); 335 } 336 337 public void addAttributeContent(String content) 338 { 339 add(content); 340 } 341 342 public void endAttribute() 343 { 344 add("\""); 345 } 346 347 protected void closeStartElement() 348 { 349 add(">"); 350 if (!isMixed) 351 { 352 addLine(); 353 } 354 lastElementIsStart = false; 355 } 356 357 public void endEmptyElement() 358 { 359 removeLast(); 360 add("/>"); 361 if (!isMixed) 362 { 363 addLine(); 364 } 365 lastElementIsStart = false; 366 } 367 368 public void endContentElement(String content) 369 { 370 add(">"); 371 add(content); 372 add("</"); 373 String name = removeLast(); 374 add(name); 375 add(">"); 376 if (!isMixed) 377 { 378 addLine(); 379 } 380 lastElementIsStart = false; 381 } 382 383 public void endElement() 384 { 385 if (lastElementIsStart) 386 { 387 endEmptyElement(); 388 } 389 else 390 { 391 boolean wasMixed = isMixed; 392 String name = removeLast(); 393 if (name != null) 394 { 395 if (!wasMixed) 396 { 397 add(getElementIndent(1)); 398 } 399 add("</"); 400 add(name); 401 add(">"); 402 403 if (!isMixed) 404 { 405 addLine(); 406 } 407 } 408 } 409 } 410 411 protected String removeLast() 412 { 413 int end = elementNames.size(); 414 isMixed = ((Boolean )mixed.remove(end - 1)).booleanValue(); 415 String result = (String )elementNames.remove(end - 1); 416 if (result != null) 417 { 418 --depth; 419 } 420 return result; 421 } 422 423 protected String getElementIndent() 424 { 425 return getElementIndent(0); 426 } 427 428 protected String getElementIndent(int extra) 429 { 430 int nesting = depth + extra - 1; 431 for (int i = indents.size() - 1; i < nesting; ++i) 432 { 433 indents.add(indents.get(i) + " "); 434 } 435 return (String )indents.get(nesting); 436 } 437 438 protected String getAttributeIndent() 439 { 440 int nesting = depth + 1; 441 for (int i = indents.size() - 1; i < nesting; ++i) 442 { 443 indents.add(indents.get(i) + " "); 444 } 445 return (String )indents.get(nesting); 446 } 447 448 public void addText(String newString) 449 { 450 if (lastElementIsStart) 451 { 452 closeStartElement(); 453 } 454 455 if (lineWidth != Integer.MAX_VALUE) 456 { 457 currentLineWidth += newString.length(); 458 LOOP: for (int i = newString.length() - 1; i >= 0; --i) 459 { 460 switch (newString.charAt(i)) 461 { 462 case '\n': 463 case '\r': 464 { 465 currentLineWidth = newString.length() - i; 466 break LOOP; 467 } 468 } 469 } 470 } 471 472 super.add(newString); 473 } 474 475 public void addCDATA(String newString) 476 { 477 if (lastElementIsStart) 478 { 479 closeStartElement(); 480 } 481 482 add("<![CDATA["); 483 if (lineWidth != Integer.MAX_VALUE) 484 { 485 currentLineWidth += newString.length(); 486 LOOP: for (int i = newString.length() - 1; i >= 0; --i) 487 { 488 switch (newString.charAt(i)) 489 { 490 case '\n': 491 case '\r': 492 { 493 currentLineWidth = newString.length() - i; 494 break LOOP; 495 } 496 } 497 } 498 } 499 500 super.add(newString); 501 add("]]>"); 502 } 503 504 public void addComment(String newString) 505 { 506 if (lastElementIsStart) 507 { 508 closeStartElement(); 509 } 510 511 if (firstElementMark != null && elementNames.isEmpty()) 512 { 513 addLine(); 514 } 515 add("<!--"); 516 if (lineWidth != Integer.MAX_VALUE) 517 { 518 currentLineWidth += newString.length(); 519 LOOP: for (int i = newString.length() - 1; i >= 0; --i) 520 { 521 switch (newString.charAt(i)) 522 { 523 case '\n': 524 case '\r': 525 { 526 currentLineWidth = newString.length() - i; 527 break LOOP; 528 } 529 } 530 } 531 } 532 533 super.add(newString); 534 add("-->"); 535 if (firstElementMark == null) 536 { 537 addLine(); 538 } 539 } 540 541 public void add(String newString) 542 { 543 if (lineWidth != Integer.MAX_VALUE) 546 { 547 currentLineWidth += newString.length(); 548 } 549 super.add(newString); 550 } 551 552 public void addLine() 553 { 554 super.addLine(); 555 currentLineWidth = 0; 556 } 557 558 public Object mark() 559 { 560 markedLineWidth = currentLineWidth; 561 currentLineWidth = lineWidth - 2; 562 return super.mark(); 563 } 564 565 public void resetToMark(Object mark) 566 { 567 if (mark == null) 568 { 569 mark = firstElementMark; 570 } 571 572 super.resetToMark(mark); 573 574 ++depth; 578 579 currentLineWidth = markedLineWidth; 580 } 581 } 582 | Popular Tags |