Changeset 3938 in josm
- Timestamp:
- 2011-03-01T19:29:36+01:00 (14 years ago)
- Location:
- trunk
- Files:
-
- 1 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/Preferences.java
r3908 r3938 13 13 import java.io.OutputStreamWriter; 14 14 import java.io.PrintWriter; 15 import java.io.Reader; 15 16 import java.lang.annotation.Retention; 16 17 import java.lang.annotation.RetentionPolicy; … … 32 33 import javax.swing.JOptionPane; 33 34 35 import org.openstreetmap.josm.io.XmlWriter; 34 36 import org.openstreetmap.josm.Main; 35 37 import org.openstreetmap.josm.tools.ColorHelper; 36 38 import org.openstreetmap.josm.tools.Utils; 39 import org.openstreetmap.josm.tools.XmlObjectParser; 40 import org.xml.sax.SAXException; 37 41 38 42 /** … … 884 888 } else 885 889 throw new RuntimeException("unsupported preference primitive type"); 886 890 887 891 try { 888 892 f.set(struct, value); … … 909 913 * The default plugin site 910 914 */ 911 private final static String[] DEFAULT_PLUGIN_SITE = {"http://josm.openstreetmap.de/plugin%<?plugins=>"}; 915 private final static String[] DEFAULT_PLUGIN_SITE = { 916 "http://josm.openstreetmap.de/plugin%<?plugins=>"}; 912 917 913 918 /** … … 928 933 putCollection("pluginmanager.sites", sites); 929 934 } 935 936 public static class XMLTag { 937 public String key; 938 public String value; 939 } 940 public static class XMLCollection { 941 public String key; 942 } 943 public static class XMLEntry { 944 public String value; 945 } 946 public void fromXML(Reader in) throws SAXException { 947 XmlObjectParser parser = new XmlObjectParser(); 948 parser.map("tag", XMLTag.class); 949 parser.map("entry", XMLEntry.class); 950 parser.map("collection", XMLCollection.class); 951 parser.startWithValidation(in, 952 "http://josm.openstreetmap.de/preferences-1.0", "resource://data/preferences.xsd"); 953 LinkedList<String> vals = new LinkedList<String>(); 954 while(parser.hasNext()) { 955 Object o = parser.next(); 956 if(o instanceof XMLTag) { 957 properties.put(((XMLTag)o).key, ((XMLTag)o).value); 958 } else if (o instanceof XMLEntry) { 959 vals.add(((XMLEntry)o).value); 960 } else if (o instanceof XMLCollection) { 961 properties.put(((XMLCollection)o).key, Utils.join("\u001e", vals)); 962 vals = new LinkedList<String>(); 963 } 964 } 965 } 966 967 public String toXML(boolean nopass) { 968 StringBuilder b = new StringBuilder( 969 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + 970 "<preferences xmlns=\"http://josm.openstreetmap.de/preferences-1.0\">\n"); 971 for (Entry<String, String> p : properties.entrySet()) { 972 if (nopass && p.getKey().equals("osm-server.password")) { 973 continue; // do not store plain password. 974 } 975 String r = p.getValue(); 976 if(r.contains("\u001e")) 977 { 978 b.append(" <collection key='"); 979 b.append(XmlWriter.encode(p.getKey())); 980 b.append("'>\n"); 981 for (String val : r.split("\u001e", -1)) 982 { 983 b.append(" <entry value='"); 984 b.append(XmlWriter.encode(val)); 985 b.append("' />\n"); 986 } 987 b.append(" </collection>\n"); 988 } 989 else 990 { 991 b.append(" <tag key='"); 992 b.append(XmlWriter.encode(p.getKey())); 993 b.append("' value='"); 994 b.append(XmlWriter.encode(p.getValue())); 995 b.append("' />\n"); 996 } 997 } 998 b.append("</preferences>"); 999 return b.toString(); 1000 } 930 1001 } -
trunk/src/org/openstreetmap/josm/data/ServerSidePreferences.java
r3845 r3938 27 27 import org.openstreetmap.josm.io.OsmConnection; 28 28 import org.openstreetmap.josm.io.OsmTransferException; 29 import org.openstreetmap.josm.io.XmlWriter;30 29 import org.openstreetmap.josm.tools.Base64; 31 import org. openstreetmap.josm.tools.XmlObjectParser;30 import org.xml.sax.SAXException; 32 31 33 32 /** … … 152 151 } 153 152 154 public static class Prop {155 public String key;156 public String value;157 }158 159 153 public void download(String userName, String password) { 160 154 if (!properties.containsKey("applet.username") && userName != null) { … … 177 171 boolean res = false; 178 172 try { 179 /* TODO: parse collection! */ 180 XmlObjectParser.Uniform<Prop> parser = new XmlObjectParser.Uniform<Prop>(in, "tag", Prop.class); 181 for (Prop p : parser) { 182 res = true; 183 properties.put(p.key, p.value); 184 } 173 fromXML(in); 185 174 } catch (RuntimeException e) { 175 e.printStackTrace(); 176 } catch (SAXException e) { 186 177 e.printStackTrace(); 187 178 } … … 196 187 */ 197 188 public void upload() { 198 StringBuilder b = new StringBuilder("<preferences>\n"); 199 for (Entry<String, String> p : properties.entrySet()) { 200 if (p.getKey().equals("osm-server.password")) { 201 continue; // do not upload password. It would get stored in plain! 202 } 203 String r = p.getValue(); 204 if(r.contains("\u001e")) 205 { 206 b.append("<collection key='"); 207 b.append(XmlWriter.encode(p.getKey())); 208 b.append(">\n"); 209 for (String val : r.split("\u001e", -1)) 210 { 211 b.append(" <entry value='"); 212 b.append(XmlWriter.encode(val)); 213 b.append("' />\n"); 214 } 215 } 216 else 217 { 218 b.append("<tag key='"); 219 b.append(XmlWriter.encode(p.getKey())); 220 b.append("' value='"); 221 b.append(XmlWriter.encode(p.getValue())); 222 b.append("' />\n"); 223 } 224 } 225 b.append("</preferences>"); 226 connection.upload(b.toString()); 189 connection.upload(toXML(true)); 227 190 } 228 191 }
Note:
See TracChangeset
for help on using the changeset viewer.