Ignore:
Timestamp:
2011-03-01T19:29:36+01:00 (13 years ago)
Author:
stoecker
Message:

fix applet preferences, move XML code to Preferences, as we may want to use it as standard format soon

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/Preferences.java

    r3908 r3938  
    1313import java.io.OutputStreamWriter;
    1414import java.io.PrintWriter;
     15import java.io.Reader;
    1516import java.lang.annotation.Retention;
    1617import java.lang.annotation.RetentionPolicy;
     
    3233import javax.swing.JOptionPane;
    3334
     35import org.openstreetmap.josm.io.XmlWriter;
    3436import org.openstreetmap.josm.Main;
    3537import org.openstreetmap.josm.tools.ColorHelper;
    3638import org.openstreetmap.josm.tools.Utils;
     39import org.openstreetmap.josm.tools.XmlObjectParser;
     40import org.xml.sax.SAXException;
    3741
    3842/**
     
    884888            } else
    885889                throw new RuntimeException("unsupported preference primitive type");
    886            
     890
    887891            try {
    888892                f.set(struct, value);
     
    909913     * The default plugin site
    910914     */
    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=>"};
    912917
    913918    /**
     
    928933        putCollection("pluginmanager.sites", sites);
    929934    }
     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    }
    9301001}
Note: See TracChangeset for help on using the changeset viewer.