Changeset 9828 in josm
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/preferences/PreferencesWriter.java
r9823 r9828 13 13 /** 14 14 * Write preferences to XML. 15 * @since 9823 15 16 */ 16 17 public class PreferencesWriter extends XmlWriter implements SettingVisitor { … … 23 24 * @param out the {@link PrintWriter} 24 25 * @param noPassword if password must be excluded 25 * @param defaults true, if default values are converted to XML, false for 26 * regular preferences 26 * @param defaults true, if default values are converted to XML, false for regular preferences 27 27 */ 28 28 public PreferencesWriter(PrintWriter out, boolean noPassword, boolean defaults) { … … 38 38 */ 39 39 public void write(Collection<Map.Entry<String, Setting<?>>> settings) { 40 out.write( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");40 out.write(String.format("<?xml version=\"1.0\" encoding=\"UTF-8\"?>%n")); 41 41 String rootElement = defaults ? "preferences-defaults" : "preferences"; 42 42 out.write(String.format("<%s xmlns='%s/preferences-1.0'", rootElement, Main.getXMLBase())); … … 44 44 out.write(" xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'"); 45 45 } 46 out.write(String.format(" version='%d'> \n", Version.getInstance().getVersion()));46 out.write(String.format(" version='%d'>%n", Version.getInstance().getVersion())); 47 47 for (Map.Entry<String, Setting<?>> e : settings) { 48 48 setKey(e.getKey()); 49 49 e.getValue().visit(this); 50 50 } 51 out.write(String.format("</%s> \n", rootElement));51 out.write(String.format("</%s>%n", rootElement)); 52 52 } 53 53 … … 56 56 } 57 57 58 private void addTime(Setting setting) {58 private void addTime(Setting<?> setting) { 59 59 if (defaults) { 60 60 Long time = setting.getTime(); 61 if (time == null) throw new IllegalStateException(); 61 if (time == null) 62 throw new IllegalStateException(); 62 63 out.write("' time='" + time); 63 64 } 65 } 66 67 private void addDefaults() { 68 out.write(String.format("' xsi:nil='true'/>%n")); 64 69 } 65 70 … … 68 73 if (noPassword && "osm-server.password".equals(key)) 69 74 return; // do not store plain password. 70 out.write(" <tag key='"); 71 out.write(XmlWriter.encode(key)); 75 out.write(" <tag key='" + XmlWriter.encode(key)); 72 76 addTime(setting); 73 77 if (setting.getValue() != null) { 74 out.write("' value='"); 75 out.write(XmlWriter.encode(setting.getValue())); 76 out.write("'/>\n"); 78 out.write(String.format("' value='%s'/>%n", XmlWriter.encode(setting.getValue()))); 77 79 } else if (defaults) { 78 out.write("' xsi:nil='true'/>\n");80 addDefaults(); 79 81 } else { 80 82 throw new NullPointerException(); … … 87 89 addTime(setting); 88 90 if (setting.getValue() != null) { 89 out.write( "'>\n");91 out.write(String.format("'>%n")); 90 92 for (String s : setting.getValue()) { 91 out.write( " <entry value='" + XmlWriter.encode(s) + "'/>\n");93 out.write(String.format(" <entry value='%s'/>%n", XmlWriter.encode(s))); 92 94 } 93 out.write( " </list>\n");95 out.write(String.format(" </list>%n")); 94 96 } else if (defaults) { 95 out.write("' xsi:nil='true'/>\n");97 addDefaults(); 96 98 } else { 97 99 throw new NullPointerException(); … … 104 106 addTime(setting); 105 107 if (setting.getValue() != null) { 106 out.write( "'>\n");108 out.write(String.format("'>%n")); 107 109 for (List<String> list : setting.getValue()) { 108 out.write( " <list>\n");110 out.write(String.format(" <list>%n")); 109 111 for (String s : list) { 110 out.write( " <entry value='" + encode(s) + "'/>\n");112 out.write(String.format(" <entry value='%s'/>%n", encode(s))); 111 113 } 112 out.write( " </list>\n");114 out.write(String.format(" </list>%n")); 113 115 } 114 out.write( " </lists>\n");116 out.write(String.format(" </lists>%n")); 115 117 } else if (defaults) { 116 out.write("' xsi:nil='true'/>\n");118 addDefaults(); 117 119 } else { 118 120 throw new NullPointerException(); … … 125 127 addTime(setting); 126 128 if (setting.getValue() != null) { 127 out.write( "'>\n");129 out.write(String.format("'>%n")); 128 130 for (Map<String, String> struct : setting.getValue()) { 129 out.write( " <map>\n");131 out.write(String.format(" <map>%n")); 130 132 for (Map.Entry<String, String> e : struct.entrySet()) { 131 out.write(String.format(" <tag key='%s' value='%s'/> \n", encode(e.getKey()), encode(e.getValue())));133 out.write(String.format(" <tag key='%s' value='%s'/>%n", encode(e.getKey()), encode(e.getValue()))); 132 134 } 133 out.write( " </map>\n");135 out.write(String.format(" </map>%n")); 134 136 } 135 out.write( " </maps>\n");137 out.write(String.format(" </maps>%n")); 136 138 } else if (defaults) { 137 out.write("' xsi:nil='true'/>\n");139 addDefaults(); 138 140 } else { 139 141 throw new NullPointerException(); 140 142 } 141 143 } 142 143 144 } -
trunk/src/org/openstreetmap/josm/io/XmlWriter.java
r9231 r9828 58 58 public static String encode(String unencoded, boolean keepApos) { 59 59 StringBuilder buffer = null; 60 for (int i = 0; i < unencoded.length(); ++i) { 61 String encS = null; 62 if (!keepApos || unencoded.charAt(i) != '\'') { 63 encS = XmlWriter.encoding.get(unencoded.charAt(i)); 64 } 65 if (encS != null) { 66 if (buffer == null) { 67 buffer = new StringBuilder(unencoded.substring(0, i)); 60 if (unencoded != null) { 61 for (int i = 0; i < unencoded.length(); ++i) { 62 String encS = null; 63 if (!keepApos || unencoded.charAt(i) != '\'') { 64 encS = XmlWriter.encoding.get(unencoded.charAt(i)); 68 65 } 69 buffer.append(encS); 70 } else if (buffer != null) { 71 buffer.append(unencoded.charAt(i)); 66 if (encS != null) { 67 if (buffer == null) { 68 buffer = new StringBuilder(unencoded.substring(0, i)); 69 } 70 buffer.append(encS); 71 } else if (buffer != null) { 72 buffer.append(unencoded.charAt(i)); 73 } 72 74 } 73 75 }
Note:
See TracChangeset
for help on using the changeset viewer.