Debian Codegraph @

bundles/org.eclipse.equinox.p2.touchpoint.eclipse/src/org/eclipse/equinox/internal/p2/update/XMLWriter.java

raw
1 /*******************************************************************************
2 * Copyright (c) 2008, 2017 IBM Corporation and others.
3 *
4 * This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
8 *
9 * SPDX-License-Identifier: EPL-2.0
10 *
11 * Contributors:
12 * IBM Corporation - initial API and implementation
13 *******************************************************************************/
14 package org.eclipse.equinox.internal.p2.update;
15
16 import java.io.*;
17 import java.nio.charset.StandardCharsets;
18 import java.util.Map;
19
20 /**
21 * A simple XML writer.
22 *
23 * Copied from the org.eclipse.core.resources bundle.
24 */
25 public class XMLWriter extends PrintWriter {
26 protected int tab;
27
28 static final boolean ignoreWhitespace = Boolean.getBoolean("p2.ignoreWhitespace"); //$NON-NLS-1$
29
30 /* constants */
31 protected static final String XML_VERSION = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; //$NON-NLS-1$
32
33 public XMLWriter(OutputStream output) {
34 super(new OutputStreamWriter(output, StandardCharsets.UTF_8));
35 tab = 0;
36 println(XML_VERSION);
37 }
38
39 public void endTag(String name) {
40 tab--;
41 printTag('/' + name, null);
42 }
43
44 public void printSimpleTag(String name, Object value) {
45 if (value != null) {
46 printTag(name, null, true, false);
47 print(getEscaped(String.valueOf(value)));
48 printTag('/' + name, null, false, true);
49 }
50 }
51
52 public void printTabulation() {
53 for (int i = 0; i < tab; i++)
54 super.print('\t');
55 }
56
57 public void printTag(String name, Map<String, String> parameters) {
58 printTag(name, parameters, true, true);
59 }
60
61 public void printTag(String name, Map<String, String> parameters, boolean shouldTab, boolean newLine) {
62 StringBuilder sb = new StringBuilder();
63 sb.append("<"); //$NON-NLS-1$
64 sb.append(name);
65 if (parameters != null)
66 for (String key : parameters.keySet()) {
67 sb.append(" "); //$NON-NLS-1$
68 sb.append(key);
69 sb.append("=\""); //$NON-NLS-1$
70 sb.append(getEscaped(String.valueOf(parameters.get(key))));
71 sb.append("\""); //$NON-NLS-1$
72 }
73 sb.append(">"); //$NON-NLS-1$
74 if (shouldTab && !ignoreWhitespace)
75 printTabulation();
76 if (newLine && !ignoreWhitespace)
77 println(sb.toString());
78 else
79 print(sb.toString());
80 }
81
82 public void startTag(String name, Map<String, String> parameters) {
83 startTag(name, parameters, true);
84 }
85
86 public void startTag(String name, Map<String, String> parameters, boolean newLine) {
87 printTag(name, parameters, true, newLine);
88 tab++;
89 }
90
91 private static void appendEscapedChar(StringBuffer buffer, char c) {
92 String replacement = getReplacement(c);
93 if (replacement != null) {
94 buffer.append('&');
95 buffer.append(replacement);
96 buffer.append(';');
97 } else {
98 buffer.append(c);
99 }
100 }
101
102 public static String getEscaped(String s) {
103 StringBuffer result = new StringBuffer(s.length() + 10);
104 for (int i = 0; i < s.length(); ++i)
105 appendEscapedChar(result, s.charAt(i));
106 return result.toString();
107 }
108
109 private static String getReplacement(char c) {
110 // Encode special XML characters into the equivalent character references.
111 // These five are defined by default for all XML documents.
112 switch (c) {
113 case '<':
114 return "lt"; //$NON-NLS-1$
115 case '>':
116 return "gt"; //$NON-NLS-1$
117 case '"':
118 return "quot"; //$NON-NLS-1$
119 case '\'':
120 return "apos"; //$NON-NLS-1$
121 case '&':
122 return "amp"; //$NON-NLS-1$
123 }
124 return null;
125 }
126 }