|
Lines 11-43
Link Here
|
| 11 |
|
11 |
|
| 12 |
package org.eclipse.ui; |
12 |
package org.eclipse.ui; |
| 13 |
|
13 |
|
| 14 |
import java.io.IOException; |
14 |
import java.io.*; |
| 15 |
import java.io.Reader; |
15 |
import java.util.*; |
| 16 |
import java.io.Writer; |
16 |
import javax.xml.parsers.*; |
| 17 |
import java.util.ArrayList; |
|
|
| 18 |
|
| 19 |
import javax.xml.parsers.DocumentBuilder; |
| 20 |
import javax.xml.parsers.DocumentBuilderFactory; |
| 21 |
import javax.xml.parsers.ParserConfigurationException; |
| 22 |
import javax.xml.transform.OutputKeys; |
| 23 |
import javax.xml.transform.Result; |
| 24 |
import javax.xml.transform.Source; |
| 25 |
import javax.xml.transform.Transformer; |
| 26 |
import javax.xml.transform.TransformerConfigurationException; |
| 27 |
import javax.xml.transform.TransformerException; |
| 28 |
import javax.xml.transform.TransformerFactory; |
| 29 |
import javax.xml.transform.dom.DOMSource; |
| 30 |
import javax.xml.transform.stream.StreamResult; |
| 31 |
|
| 32 |
import org.eclipse.ui.internal.WorkbenchMessages; |
17 |
import org.eclipse.ui.internal.WorkbenchMessages; |
| 33 |
import org.eclipse.ui.internal.WorkbenchPlugin; |
18 |
import org.eclipse.ui.internal.WorkbenchPlugin; |
| 34 |
import org.w3c.dom.Attr; |
19 |
import org.w3c.dom.*; |
| 35 |
import org.w3c.dom.Document; |
|
|
| 36 |
import org.w3c.dom.Element; |
| 37 |
import org.w3c.dom.NamedNodeMap; |
| 38 |
import org.w3c.dom.Node; |
| 39 |
import org.w3c.dom.NodeList; |
| 40 |
import org.w3c.dom.Text; |
| 41 |
import org.xml.sax.InputSource; |
20 |
import org.xml.sax.InputSource; |
| 42 |
import org.xml.sax.SAXException; |
21 |
import org.xml.sax.SAXException; |
| 43 |
|
22 |
|
|
Lines 140-146
Link Here
|
| 140 |
document.appendChild(element); |
119 |
document.appendChild(element); |
| 141 |
return new XMLMemento(document, element); |
120 |
return new XMLMemento(document, element); |
| 142 |
} catch (ParserConfigurationException e) { |
121 |
} catch (ParserConfigurationException e) { |
| 143 |
throw new Error(e); |
122 |
// throw new Error(e); |
|
|
123 |
throw new Error(e.getMessage()); |
| 144 |
} |
124 |
} |
| 145 |
} |
125 |
} |
| 146 |
|
126 |
|
|
Lines 406-423
Link Here
|
| 406 |
* @throws IOException if there is a problem serializing the document to the stream. |
386 |
* @throws IOException if there is a problem serializing the document to the stream. |
| 407 |
*/ |
387 |
*/ |
| 408 |
public void save(Writer writer) throws IOException { |
388 |
public void save(Writer writer) throws IOException { |
| 409 |
Result result = new StreamResult(writer); |
389 |
DOMWriter out = new DOMWriter(writer); |
| 410 |
Source source = new DOMSource(factory); |
|
|
| 411 |
try { |
390 |
try { |
| 412 |
Transformer transformer = TransformerFactory.newInstance() |
391 |
save(element, out); |
| 413 |
.newTransformer(); |
392 |
} finally { |
| 414 |
transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$ |
393 |
out.close(); |
| 415 |
transformer.setOutputProperty(OutputKeys.METHOD, "xml"); //$NON-NLS-1$ |
394 |
} |
| 416 |
transformer.transform(source, result); |
395 |
} |
| 417 |
} catch (TransformerConfigurationException e) { |
396 |
|
| 418 |
throw (IOException) (new IOException().initCause(e)); |
397 |
private void save(Element element, DOMWriter out) { |
| 419 |
} catch (TransformerException e) { |
398 |
out.startTag(element); |
| 420 |
throw (IOException) (new IOException().initCause(e)); |
399 |
NodeList children = element.getChildNodes(); |
| 421 |
} |
400 |
for (int i = 0; i < children.getLength(); i++) |
|
|
401 |
save ((Element)children.item(i), out); |
| 402 |
out.endTag(element); |
| 403 |
} |
| 404 |
|
| 405 |
/** |
| 406 |
* A simple XML writer. |
| 407 |
*/ |
| 408 |
private static class DOMWriter extends PrintWriter { |
| 409 |
protected int tab; |
| 410 |
|
| 411 |
/* constants */ |
| 412 |
protected static final String XML_VERSION = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; //$NON-NLS-1$ |
| 413 |
|
| 414 |
public DOMWriter(Writer output) throws UnsupportedEncodingException { |
| 415 |
super(output); |
| 416 |
tab = 0; |
| 417 |
println(XML_VERSION); |
| 418 |
} |
| 419 |
|
| 420 |
public void endTag(Element element) { |
| 421 |
tab--; |
| 422 |
if (!element.hasChildNodes()) |
| 423 |
return; |
| 424 |
StringBuffer sb = new StringBuffer(); |
| 425 |
sb.append("</"); //$NON-NLS-1$ |
| 426 |
sb.append(element.getNodeName()); |
| 427 |
sb.append(">"); //$NON-NLS-1$ |
| 428 |
printTabulation(); |
| 429 |
println(sb.toString()); |
| 430 |
} |
| 431 |
|
| 432 |
public void printTabulation() { |
| 433 |
for (int i = 0; i < tab; i++) |
| 434 |
super.print(" "); //$NON-NLS-1$ |
| 435 |
} |
| 436 |
|
| 437 |
public void printTag(Element element, boolean shouldTab, boolean newLine) { |
| 438 |
StringBuffer sb = new StringBuffer(); |
| 439 |
sb.append("<"); //$NON-NLS-1$ |
| 440 |
sb.append(element.getTagName()); |
| 441 |
NamedNodeMap attributes = element.getAttributes(); |
| 442 |
for (int i = 0; i < attributes.getLength(); i++) { |
| 443 |
Attr attribute = (Attr)attributes.item(i); |
| 444 |
sb.append(" "); //$NON-NLS-1$ |
| 445 |
sb.append(attribute.getName()); |
| 446 |
sb.append("=\""); //$NON-NLS-1$ |
| 447 |
sb.append(getEscaped(String.valueOf(attribute.getValue()))); |
| 448 |
sb.append("\""); //$NON-NLS-1$ |
| 449 |
} |
| 450 |
sb.append(element.hasChildNodes() ? ">" : "/>"); //$NON-NLS-1$ //$NON-NLS-2$ |
| 451 |
if (shouldTab) |
| 452 |
printTabulation(); |
| 453 |
if (newLine) |
| 454 |
println(sb.toString()); |
| 455 |
else |
| 456 |
print(sb.toString()); |
| 457 |
} |
| 458 |
|
| 459 |
public void startTag(Element element) { |
| 460 |
printTag(element, true, true); |
| 461 |
tab++; |
| 462 |
} |
| 463 |
|
| 464 |
private static void appendEscapedChar(StringBuffer buffer, char c) { |
| 465 |
String replacement = getReplacement(c); |
| 466 |
if (replacement != null) { |
| 467 |
buffer.append('&'); |
| 468 |
buffer.append(replacement); |
| 469 |
buffer.append(';'); |
| 470 |
} else { |
| 471 |
buffer.append(c); |
| 472 |
} |
| 473 |
} |
| 474 |
|
| 475 |
public static String getEscaped(String s) { |
| 476 |
StringBuffer result = new StringBuffer(s.length() + 10); |
| 477 |
for (int i = 0; i < s.length(); ++i) |
| 478 |
appendEscapedChar(result, s.charAt(i)); |
| 479 |
return result.toString(); |
| 480 |
} |
| 481 |
|
| 482 |
private static String getReplacement(char c) { |
| 483 |
// Encode special XML characters into the equivalent character references. |
| 484 |
// These five are defined by default for all XML documents. |
| 485 |
switch (c) { |
| 486 |
case '<' : |
| 487 |
return "lt"; //$NON-NLS-1$ |
| 488 |
case '>' : |
| 489 |
return "gt"; //$NON-NLS-1$ |
| 490 |
case '"' : |
| 491 |
return "quot"; //$NON-NLS-1$ |
| 492 |
case '\'' : |
| 493 |
return "apos"; //$NON-NLS-1$ |
| 494 |
case '&' : |
| 495 |
return "amp"; //$NON-NLS-1$ |
| 496 |
} |
| 497 |
return null; |
| 498 |
} |
| 422 |
} |
499 |
} |
| 423 |
} |
500 |
} |