source: josm/src/org/openstreetmap/josm/tools/XmlWriter.java@ 74

Last change on this file since 74 was 74, checked in by imi, 18 years ago

changed Preferences system to more flexible one.

File size: 1.2 KB
Line 
1package org.openstreetmap.josm.tools;
2
3import java.util.HashMap;
4
5/**
6 * Helper class to use for xml outputting classes.
7 *
8 * @author imi
9 */
10public class XmlWriter {
11
12 /**
13 * Encode the given string in XML1.0 format.
14 * Optimized to fast pass strings that don't need encoding (normal case).
15 */
16 public static String encode(String unencoded) {
17 StringBuilder buffer = null;
18 for (int i = 0; i < unencoded.length(); ++i) {
19 String encS = XmlWriter.encoding.get(unencoded.charAt(i));
20 if (encS != null) {
21 if (buffer == null)
22 buffer = new StringBuilder(unencoded.substring(0,i));
23 buffer.append(encS);
24 } else if (buffer != null)
25 buffer.append(unencoded.charAt(i));
26 }
27 return (buffer == null) ? unencoded : buffer.toString();
28 }
29
30 /**
31 * @return The standard XML1.0 header. Encoding is utf-8
32 */
33 public static String header() {
34 return "<?xml version='1.0' encoding='UTF-8'?>";
35 }
36
37
38
39 final private static HashMap<Character, String> encoding = new HashMap<Character, String>();
40 static {
41 encoding.put('<', "&lt;");
42 encoding.put('>', "&gt;");
43 encoding.put('"', "&quot;");
44 encoding.put('\'', "&apos;");
45 encoding.put('&', "&amp;");
46 encoding.put('\n', "&#xA;");
47 encoding.put('\r', "&#xD;");
48 encoding.put('\t', "&#x9;");
49 }
50}
Note: See TracBrowser for help on using the repository browser.