001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.data;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.io.BufferedInputStream;
007import java.io.ByteArrayInputStream;
008import java.io.CharArrayReader;
009import java.io.CharArrayWriter;
010import java.io.File;
011import java.io.FileInputStream;
012import java.io.InputStream;
013import java.util.ArrayList;
014import java.util.Collection;
015import java.util.Collections;
016import java.util.HashMap;
017import java.util.HashSet;
018import java.util.Iterator;
019import java.util.List;
020import java.util.Map;
021import java.util.Map.Entry;
022import java.util.SortedMap;
023import java.util.TreeMap;
024import java.util.regex.Matcher;
025import java.util.regex.Pattern;
026
027import javax.script.ScriptEngine;
028import javax.script.ScriptEngineManager;
029import javax.script.ScriptException;
030import javax.swing.JOptionPane;
031import javax.swing.SwingUtilities;
032import javax.xml.parsers.DocumentBuilder;
033import javax.xml.parsers.DocumentBuilderFactory;
034import javax.xml.transform.OutputKeys;
035import javax.xml.transform.Transformer;
036import javax.xml.transform.TransformerFactory;
037import javax.xml.transform.dom.DOMSource;
038import javax.xml.transform.stream.StreamResult;
039
040import org.openstreetmap.josm.Main;
041import org.openstreetmap.josm.data.Preferences.Setting;
042import org.openstreetmap.josm.gui.io.DownloadFileTask;
043import org.openstreetmap.josm.plugins.PluginDownloadTask;
044import org.openstreetmap.josm.plugins.PluginInformation;
045import org.openstreetmap.josm.plugins.ReadLocalPluginInformationTask;
046import org.openstreetmap.josm.tools.LanguageInfo;
047import org.openstreetmap.josm.tools.Utils;
048import org.w3c.dom.Document;
049import org.w3c.dom.Element;
050import org.w3c.dom.Node;
051import org.w3c.dom.NodeList;
052
053/**
054 * Class to process configuration changes stored in XML
055 * can be used to modify preferences, store/delete files in .josm folders etc
056 */
057public final class CustomConfigurator {
058    
059    private CustomConfigurator() {
060        // Hide default constructor for utils classes
061    }
062    
063    private static StringBuilder summary = new StringBuilder();
064
065    public static void log(String fmt, Object... vars) {
066        summary.append(String.format(fmt, vars));
067    }
068
069    public static void log(String s) {
070        summary.append(s);
071        summary.append("\n");
072    }
073
074    public static String getLog() {
075        return summary.toString();
076    }
077
078    public static void readXML(String dir, String fileName) {
079        readXML(new File(dir, fileName));
080    }
081
082    /**
083     * Read configuration script from XML file, modifying given preferences object
084     * @param file - file to open for reading XML
085     * @param prefs - arbitrary Preferences object to modify by script
086     */
087    public static void readXML(final File file, final Preferences prefs) {
088        synchronized(CustomConfigurator.class) {
089            busy=true;
090        }
091        new XMLCommandProcessor(prefs).openAndReadXML(file);
092        synchronized(CustomConfigurator.class) {
093            CustomConfigurator.class.notifyAll();
094            busy=false;
095        }
096    }
097
098    /**
099     * Read configuration script from XML file, modifying main preferences
100     * @param file - file to open for reading XML
101     */
102    public static void readXML(File file) {
103        readXML(file, Main.pref);
104    }
105
106    /**
107     * Downloads file to one of JOSM standard folders
108     * @param address - URL to download
109     * @param path - file path relative to base where to put downloaded file
110     * @param base - only "prefs", "cache" and "plugins" allowed for standard folders
111     */
112    public static void downloadFile(String address, String path, String base) {
113        processDownloadOperation(address, path, getDirectoryByAbbr(base), true, false);
114    }
115
116    /**
117     * Downloads file to one of JOSM standard folders nad unpack it as ZIP/JAR file
118     * @param address - URL to download
119     * @param path - file path relative to base where to put downloaded file
120     * @param base - only "prefs", "cache" and "plugins" allowed for standard folders
121     */
122    public static void downloadAndUnpackFile(String address, String path, String base) {
123        processDownloadOperation(address, path, getDirectoryByAbbr(base), true, true);
124    }
125
126    /**
127     * Downloads file to arbitrary folder
128     * @param address - URL to download
129     * @param path - file path relative to parentDir where to put downloaded file
130     * @param parentDir - folder where to put file
131     * @param mkdir - if true, non-existing directories will be created
132     * @param unzip - if true file wil be unzipped and deleted after download
133     */
134    public static void processDownloadOperation(String address, String path, String parentDir, boolean mkdir, boolean unzip) {
135        String dir = parentDir;
136        if (path.contains("..") || path.startsWith("/") || path.contains(":")) {
137            return; // some basic protection
138        }
139        File fOut = new File(dir, path);
140        DownloadFileTask downloadFileTask = new DownloadFileTask(Main.parent, address, fOut, mkdir, unzip);
141
142        Main.worker.submit(downloadFileTask);
143        log("Info: downloading file from %s to %s in background ", parentDir, fOut.getAbsolutePath());
144        if (unzip) log("and unpacking it"); else log("");
145
146    }
147
148    /**
149     * Simple function to show messageBox, may be used from JS API and from other code
150     * @param type - 'i','w','e','q','p' for Information, Warning, Error, Question, Message
151     * @param text - message to display, HTML allowed
152     */
153    public static void messageBox(String type, String text) {
154        if (type==null || type.length()==0) type="plain";
155
156        switch (type.charAt(0)) {
157            case 'i': JOptionPane.showMessageDialog(Main.parent, text, tr("Information"), JOptionPane.INFORMATION_MESSAGE); break;
158            case 'w': JOptionPane.showMessageDialog(Main.parent, text, tr("Warning"), JOptionPane.WARNING_MESSAGE); break;
159            case 'e': JOptionPane.showMessageDialog(Main.parent, text, tr("Error"), JOptionPane.ERROR_MESSAGE); break;
160            case 'q': JOptionPane.showMessageDialog(Main.parent, text, tr("Question"), JOptionPane.QUESTION_MESSAGE); break;
161            case 'p': JOptionPane.showMessageDialog(Main.parent, text, tr("Message"), JOptionPane.PLAIN_MESSAGE); break;
162        }
163    }
164
165    /**
166     * Simple function for choose window, may be used from JS API and from other code
167     * @param text - message to show, HTML allowed
168     * @param opts -
169     * @return number of pressed button, -1 if cancelled
170     */
171    public static int askForOption(String text, String opts) {
172        Integer answer;
173        if (opts.length()>0) {
174            String[] options = opts.split(";");
175            answer = JOptionPane.showOptionDialog(Main.parent, text, "Question", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, 0);
176        } else {
177            answer = JOptionPane.showOptionDialog(Main.parent, text, "Question", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, 2);
178        }
179        if (answer==null) return -1; else return answer;
180    }
181
182    public static String askForText(String text) {
183        String s = JOptionPane.showInputDialog(Main.parent, text, tr("Enter text"), JOptionPane.QUESTION_MESSAGE);
184        if (s!=null && (s=s.trim()).length()>0) {
185            return s;
186        } else {
187            return "";
188        }
189    }
190
191    /**
192     * This function exports part of user preferences to specified file.
193     * Default values are not saved.
194     * @param filename - where to export
195     * @param append - if true, resulting file cause appending to exuisting preferences
196     * @param keys - which preferences keys you need to export ("imagery.entries", for example)
197     */
198    public static void exportPreferencesKeysToFile(String filename, boolean append, String... keys) {
199        HashSet<String> keySet = new HashSet<String>();
200        Collections.addAll(keySet, keys);
201        exportPreferencesKeysToFile(filename, append, keySet);
202    }
203
204    /**
205     * This function exports part of user preferences to specified file.
206     * Default values are not saved.
207     * Preference keys matching specified pattern are saved
208     * @param fileName - where to export
209     * @param append - if true, resulting file cause appending to exuisting preferences
210     * @param pattern - Regexp pattern forh preferences keys you need to export (".*imagery.*", for example)
211     */
212    public static void exportPreferencesKeysByPatternToFile(String fileName, boolean append, String pattern) {
213        List<String> keySet = new ArrayList<String>();
214        Map<String, Setting> allSettings = Main.pref.getAllSettings();
215        for (String key: allSettings.keySet()) {
216            if (key.matches(pattern)) keySet.add(key);
217        }
218        exportPreferencesKeysToFile(fileName, append, keySet);
219    }
220
221    /**
222     * Export specified preferences keys to configuration file
223     * @param filename - name of file
224     * @param append - will the preferences be appended to existing ones when file is imported later. Elsewhere preferences from file will replace existing keys.
225     * @param keys - collection of preferences key names to save
226     */
227    public static void exportPreferencesKeysToFile(String filename, boolean append, Collection<String> keys) {
228        Element root = null;
229        Document document = null;
230        Document exportDocument = null;
231
232        try {
233            String toXML = Main.pref.toXML(true);
234            InputStream is = new ByteArrayInputStream(toXML.getBytes("UTF-8"));
235            DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
236            builderFactory.setValidating(false);
237            builderFactory.setNamespaceAware(false);
238            DocumentBuilder builder = builderFactory.newDocumentBuilder();
239            document = builder.parse(is);
240            exportDocument = builder.newDocument();
241            root = document.getDocumentElement();
242        } catch (Exception ex) {
243            Main.warn("Error getting preferences to save:" +ex.getMessage());
244        }
245        if (root==null) return;
246        try {
247
248            Element newRoot = exportDocument.createElement("config");
249            exportDocument.appendChild(newRoot);
250
251            Element prefElem = exportDocument.createElement("preferences");
252            prefElem.setAttribute("operation", append?"append":"replace");
253            newRoot.appendChild(prefElem);
254
255            NodeList childNodes = root.getChildNodes();
256            int n = childNodes.getLength();
257            for (int i = 0; i < n ; i++) {
258                Node item = childNodes.item(i);
259                if (item.getNodeType() == Node.ELEMENT_NODE) {
260                    String currentKey = ((Element) item).getAttribute("key");
261                    if (keys.contains(currentKey)) {
262                        Node imported = exportDocument.importNode(item, true);
263                        prefElem.appendChild(imported);
264                    }
265                }
266            }
267            File f = new File(filename);
268            Transformer ts = TransformerFactory.newInstance().newTransformer();
269            ts.setOutputProperty(OutputKeys.INDENT, "yes");
270            ts.transform(new DOMSource(exportDocument), new StreamResult(f.toURI().getPath()));
271        } catch (Exception ex) {
272            Main.warn("Error saving preferences part: " +ex.getMessage());
273            ex.printStackTrace();
274        }
275    }
276
277
278        public static void deleteFile(String path, String base) {
279        String dir = getDirectoryByAbbr(base);
280        if (dir==null) {
281            log("Error: Can not find base, use base=cache, base=prefs or base=plugins attribute.");
282            return;
283        }
284        log("Delete file: %s\n", path);
285        if (path.contains("..") || path.startsWith("/") || path.contains(":")) {
286            return; // some basic protection
287        }
288        File fOut = new File(dir, path);
289        if (fOut.exists()) {
290            deleteFileOrDirectory(fOut);
291        }
292        return;
293    }
294
295    public static void deleteFileOrDirectory(String path) {
296        deleteFileOrDirectory(new File(path));
297    }
298
299    public static void deleteFileOrDirectory(File f) {
300        if (f.isDirectory()) {
301            for (File f1: f.listFiles()) {
302                deleteFileOrDirectory(f1);
303            }
304        }
305        try {
306            f.delete();
307        } catch (Exception e) {
308            log("Warning: Can not delete file "+f.getPath());
309        }
310    }
311
312    private static boolean busy=false;
313
314
315    public static void pluginOperation(String install, String uninstall, String delete)  {
316        final List<String> installList = new ArrayList<String>();
317        final List<String> removeList = new ArrayList<String>();
318        final List<String> deleteList = new ArrayList<String>();
319        Collections.addAll(installList, install.toLowerCase().split(";"));
320        Collections.addAll(removeList, uninstall.toLowerCase().split(";"));
321        Collections.addAll(deleteList, delete.toLowerCase().split(";"));
322        installList.remove("");removeList.remove("");deleteList.remove("");
323
324        if (!installList.isEmpty()) {
325            log("Plugins install: "+installList);
326        }
327        if (!removeList.isEmpty()) {
328            log("Plugins turn off: "+removeList);
329        }
330        if (!deleteList.isEmpty()) {
331            log("Plugins delete: "+deleteList);
332        }
333
334        final ReadLocalPluginInformationTask task = new ReadLocalPluginInformationTask();
335        Runnable r = new Runnable() {
336            @Override
337            public void run() {
338                if (task.isCanceled()) return;
339                synchronized (CustomConfigurator.class) {
340                try { // proceed only after all other tasks were finished
341                    while (busy) CustomConfigurator.class.wait();
342                } catch (InterruptedException ex) {
343                    Main.warn("InterruptedException while reading local plugin information");
344                }
345
346                SwingUtilities.invokeLater(new Runnable() {
347                    @Override
348                    public void run() {
349                        List<PluginInformation> availablePlugins = task.getAvailablePlugins();
350                        List<PluginInformation> toInstallPlugins = new ArrayList<PluginInformation>();
351                        List<PluginInformation> toRemovePlugins = new ArrayList<PluginInformation>();
352                        List<PluginInformation> toDeletePlugins = new ArrayList<PluginInformation>();
353                        for (PluginInformation pi: availablePlugins) {
354                            String name = pi.name.toLowerCase();
355                            if (installList.contains(name)) toInstallPlugins.add(pi);
356                            if (removeList.contains(name)) toRemovePlugins.add(pi);
357                            if (deleteList.contains(name)) toDeletePlugins.add(pi);
358                        }
359                        if (!installList.isEmpty()) {
360                            PluginDownloadTask pluginDownloadTask = new PluginDownloadTask(Main.parent, toInstallPlugins, tr ("Installing plugins"));
361                            Main.worker.submit(pluginDownloadTask);
362                        }
363                        Collection<String> pls = new ArrayList<String>(Main.pref.getCollection("plugins"));
364                        for (PluginInformation pi: toInstallPlugins) {
365                            if (!pls.contains(pi.name)) {
366                                pls.add(pi.name);
367                            }
368                        }
369                        for (PluginInformation pi: toRemovePlugins) {
370                            pls.remove(pi.name);
371                        }
372                        for (PluginInformation pi: toDeletePlugins) {
373                            pls.remove(pi.name);
374                            new File(Main.pref.getPluginsDirectory(), pi.name+".jar").deleteOnExit();
375                        }
376                        Main.pref.putCollection("plugins",pls);
377                    }
378                });
379            }
380            }
381
382        };
383        Main.worker.submit(task);
384        Main.worker.submit(r);
385    }
386
387    private static String getDirectoryByAbbr(String base) {
388            String dir;
389            if ("prefs".equals(base) || base.length()==0) {
390                dir = Main.pref.getPreferencesDir();
391            } else if ("cache".equals(base)) {
392                dir = Main.pref.getCacheDirectory().getAbsolutePath();
393            } else if ("plugins".equals(base)) {
394                dir = Main.pref.getPluginsDirectory().getAbsolutePath();
395            } else {
396                dir = null;
397            }
398            return dir;
399    }
400
401    public static Preferences clonePreferences(Preferences pref) {
402        Preferences tmp = new Preferences();
403        tmp.defaults.putAll(   pref.defaults );
404        tmp.properties.putAll( pref.properties );
405        tmp.arrayDefaults.putAll(   pref.arrayDefaults );
406        tmp.arrayProperties.putAll( pref.arrayProperties );
407        tmp.collectionDefaults.putAll(   pref.collectionDefaults );
408        tmp.collectionProperties.putAll( pref.collectionProperties );
409        tmp.listOfStructsDefaults.putAll(   pref.listOfStructsDefaults );
410        tmp.listOfStructsProperties.putAll( pref.listOfStructsProperties );
411        tmp.colornames.putAll( pref.colornames );
412
413        return tmp;
414    }
415
416
417    public static class XMLCommandProcessor {
418
419        Preferences mainPrefs;
420        Map<String,Element> tasksMap = new HashMap<String,Element>();
421
422        private boolean lastV; // last If condition result
423
424
425        ScriptEngine engine ;
426
427        public void openAndReadXML(File file) {
428            log("-- Reading custom preferences from " + file.getAbsolutePath() + " --");
429            try {
430                String fileDir = file.getParentFile().getAbsolutePath();
431                if (fileDir!=null) engine.eval("scriptDir='"+normalizeDirName(fileDir) +"';");
432                openAndReadXML(new BufferedInputStream(new FileInputStream(file)));
433            } catch (Exception ex) {
434                log("Error reading custom preferences: " + ex.getMessage());
435            }
436        }
437
438        public void openAndReadXML(InputStream is) {
439            try {
440                DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
441                builderFactory.setValidating(false);
442                builderFactory.setNamespaceAware(true);
443                DocumentBuilder builder = builderFactory.newDocumentBuilder();
444                Document document = builder.parse(is);
445                synchronized (CustomConfigurator.class) {
446                    processXML(document);
447                }
448            } catch (Exception ex) {
449                log("Error reading custom preferences: "+ex.getMessage());
450            } finally {
451                Utils.close(is);
452            }
453            log("-- Reading complete --");
454        }
455
456        public XMLCommandProcessor(Preferences mainPrefs) {
457            try {
458                this.mainPrefs = mainPrefs;
459                CustomConfigurator.summary = new StringBuilder();
460                engine = new ScriptEngineManager().getEngineByName("rhino");
461                engine.eval("API={}; API.pref={}; API.fragments={};");
462
463                engine.eval("homeDir='"+normalizeDirName(Main.pref.getPreferencesDir()) +"';");
464                engine.eval("josmVersion="+Version.getInstance().getVersion()+";");
465                String className =  CustomConfigurator.class.getName();
466                engine.eval("API.messageBox="+className+".messageBox");
467                engine.eval("API.askText=function(text) { return String("+className+".askForText(text));}");
468                engine.eval("API.askOption="+className+".askForOption");
469                engine.eval("API.downloadFile="+className+".downloadFile");
470                engine.eval("API.downloadAndUnpackFile="+className+".downloadAndUnpackFile");
471                engine.eval("API.deleteFile="+className+".deleteFile");
472                engine.eval("API.plugin ="+className+".pluginOperation");
473                engine.eval("API.pluginInstall = function(names) { "+className+".pluginOperation(names,'','');}");
474                engine.eval("API.pluginUninstall = function(names) { "+className+".pluginOperation('',names,'');}");
475                engine.eval("API.pluginDelete = function(names) { "+className+".pluginOperation('','',names);}");
476            } catch (Exception ex) {
477                log("Error: initializing script engine: "+ex.getMessage());
478            }
479        }
480
481        private void processXML(Document document) {
482            Element root = document.getDocumentElement();
483            processXmlFragment(root);
484        }
485
486        private void processXmlFragment(Element root) {
487            NodeList childNodes = root.getChildNodes();
488            int nops = childNodes.getLength();
489            for (int i = 0; i < nops; i++) {
490                Node item = childNodes.item(i);
491                if (item.getNodeType() != Node.ELEMENT_NODE) continue;
492                String elementName = item.getNodeName();
493                Element elem = (Element) item;
494
495                if ("var".equals(elementName)) {
496                    setVar(elem.getAttribute("name"), evalVars(elem.getAttribute("value")));
497                } else if ("task".equals(elementName)) {
498                    tasksMap.put(elem.getAttribute("name"), elem);
499                } else if ("runtask".equals(elementName)) {
500                    if (processRunTaskElement(elem)) return;
501                } else if ("ask".equals(elementName)) {
502                    processAskElement(elem);
503                } else if ("if".equals(elementName)) {
504                    processIfElement(elem);
505                } else if ("else".equals(elementName)) {
506                    processElseElement(elem);
507                } else if ("break".equals(elementName)) {
508                    return;
509                } else if ("plugin".equals(elementName)) {
510                    processPluginInstallElement(elem);
511                } else if ("messagebox".equals(elementName)){
512                    processMsgBoxElement(elem);
513                } else if ("preferences".equals(elementName)) {
514                    processPreferencesElement(elem);
515                } else if ("download".equals(elementName)) {
516                    processDownloadElement(elem);
517                } else if ("delete".equals(elementName)) {
518                    processDeleteElement(elem);
519                } else if ("script".equals(elementName)) {
520                    processScriptElement(elem);
521                } else {
522                    log("Error: Unknown element " + elementName);
523                }
524
525            }
526        }
527
528
529
530        private void processPreferencesElement(Element item) {
531            String oper = evalVars(item.getAttribute("operation"));
532            String id = evalVars(item.getAttribute("id"));
533
534
535            if ("delete-keys".equals(oper)) {
536                String pattern = evalVars(item.getAttribute("pattern"));
537                String key = evalVars(item.getAttribute("key"));
538                if (key != null) {
539                    PreferencesUtils.deletePreferenceKey(key, mainPrefs);
540                }
541                if (pattern != null) {
542                    PreferencesUtils.deletePreferenceKeyByPattern(pattern, mainPrefs);
543                }
544                return;
545            }
546
547            Preferences tmpPref = readPreferencesFromDOMElement(item);
548            PreferencesUtils.showPrefs(tmpPref);
549
550            if (id.length()>0) {
551                try {
552                    String fragmentVar = "API.fragments['"+id+"']";
553                    engine.eval(fragmentVar+"={};");
554                    PreferencesUtils.loadPrefsToJS(engine, tmpPref, fragmentVar, false);
555                    // we store this fragment as API.fragments['id']
556                } catch (ScriptException ex) {
557                    log("Error: can not load preferences fragment : "+ex.getMessage());
558                }
559            }
560
561            if ("replace".equals(oper)) {
562                log("Preferences replace: %d keys: %s\n",
563                   tmpPref.getAllSettings().size(), tmpPref.getAllSettings().keySet().toString());
564                PreferencesUtils.replacePreferences(tmpPref, mainPrefs);
565            } else if ("append".equals(oper)) {
566                log("Preferences append: %d keys: %s\n",
567                   tmpPref.getAllSettings().size(), tmpPref.getAllSettings().keySet().toString());
568                PreferencesUtils.appendPreferences(tmpPref, mainPrefs);
569            }  else if ("delete-values".equals(oper)) {
570                PreferencesUtils.deletePreferenceValues(tmpPref, mainPrefs);
571            }
572        }
573
574         private void processDeleteElement(Element item) {
575            String path = evalVars(item.getAttribute("path"));
576            String base = evalVars(item.getAttribute("base"));
577            deleteFile(base, path);
578        }
579
580        private void processDownloadElement(Element item) {
581            String address = evalVars(item.getAttribute("url"));
582            String path = evalVars(item.getAttribute("path"));
583            String unzip = evalVars(item.getAttribute("unzip"));
584            String mkdir = evalVars(item.getAttribute("mkdir"));
585
586            String base = evalVars(item.getAttribute("base"));
587            String dir = getDirectoryByAbbr(base);
588            if (dir==null) {
589                log("Error: Can not find directory to place file, use base=cache, base=prefs or base=plugins attribute.");
590                return;
591            }
592
593            if (path.contains("..") || path.startsWith("/") || path.contains(":")) {
594                return; // some basic protection
595            }
596            if (address == null || path == null || address.length() == 0 || path.length() == 0) {
597                log("Error: Please specify url=\"where to get file\" and path=\"where to place it\"");
598                return;
599            }
600            processDownloadOperation(address, path, dir, "true".equals(mkdir), "true".equals(unzip));
601        }
602
603        private void processPluginInstallElement(Element elem) {
604            String install = elem.getAttribute("install");
605            String uninstall = elem.getAttribute("remove");
606            String delete = elem.getAttribute("delete");
607            pluginOperation(install, uninstall, delete);
608        }
609
610        private void processMsgBoxElement(Element elem) {
611            String text = evalVars(elem.getAttribute("text"));
612            String locText = evalVars(elem.getAttribute(LanguageInfo.getJOSMLocaleCode()+".text"));
613            if (locText!=null && locText.length()>0) text=locText;
614
615            String type = evalVars(elem.getAttribute("type"));
616            messageBox(type, text);
617        }
618
619
620        private void processAskElement(Element elem) {
621            String text = evalVars(elem.getAttribute("text"));
622            String locText = evalVars(elem.getAttribute(LanguageInfo.getJOSMLocaleCode()+".text"));
623            if (locText.length()>0) text=locText;
624            String var = elem.getAttribute("var");
625            if (var.length()==0) var="result";
626
627            String input = evalVars(elem.getAttribute("input"));
628            if ("true".equals(input)) {
629                setVar(var, askForText(text));
630            } else {
631                String opts = evalVars(elem.getAttribute("options"));
632                String locOpts = evalVars(elem.getAttribute(LanguageInfo.getJOSMLocaleCode()+".options"));
633                if (locOpts.length()>0) opts=locOpts;
634                setVar(var, String.valueOf(askForOption(text, opts)));
635            }
636        }
637
638        public void setVar(String name, String value) {
639            try {
640                engine.eval(name+"='"+value+"';");
641            } catch (ScriptException ex) {
642                log("Error: Can not assign variable: %s=%s  : %s\n", name, value, ex.getMessage());
643            }
644        }
645
646        private void processIfElement(Element elem) {
647            String realValue = evalVars(elem.getAttribute("test"));
648            boolean v=false;
649            if ("true".equals(realValue)) v=true; else
650            if ("fales".equals(realValue)) v=true; else
651            {
652                log("Error: Illegal test expression in if: %s=%s\n", elem.getAttribute("test"), realValue);
653            }
654
655            if (v) processXmlFragment(elem);
656            lastV = v;
657        }
658
659        private void processElseElement(Element elem) {
660            if (!lastV) {
661                processXmlFragment(elem);
662            }
663        }
664
665        private boolean processRunTaskElement(Element elem) {
666            String taskName = elem.getAttribute("name");
667            Element task = tasksMap.get(taskName);
668            if (task!=null) {
669                log("EXECUTING TASK "+taskName);
670                processXmlFragment(task); // process task recursively
671            } else {
672                log("Error: Can not execute task "+taskName);
673                return true;
674            }
675            return false;
676        }
677
678
679        private void processScriptElement(Element elem) {
680            String js = elem.getChildNodes().item(0).getTextContent();
681            log("Processing script...");
682            try {
683                PreferencesUtils.modifyPreferencesByScript(engine, mainPrefs, js);
684            } catch (ScriptException ex) {
685                messageBox("e", ex.getMessage());
686                log("JS error: "+ex.getMessage());
687            }
688            log("Script finished");
689        }
690
691        /**
692         * substitute ${expression} = expression evaluated by JavaScript
693         */
694        private String evalVars(String s) {
695            Pattern p = Pattern.compile("\\$\\{([^\\}]*)\\}");
696            Matcher mr =  p.matcher(s);
697            StringBuffer sb = new StringBuffer();
698            while (mr.find()) {
699                try {
700                    String result = engine.eval(mr.group(1)).toString();
701                    mr.appendReplacement(sb, result);
702                } catch (ScriptException ex) {
703                    log("Error: Can not evaluate expression %s : %s",  mr.group(1), ex.getMessage());
704                }
705            }
706            mr.appendTail(sb);
707            return sb.toString();
708        }
709
710        private Preferences readPreferencesFromDOMElement(Element item) {
711            Preferences tmpPref = new Preferences();
712            try {
713                Transformer xformer = TransformerFactory.newInstance().newTransformer();
714                CharArrayWriter outputWriter = new CharArrayWriter(8192);
715                StreamResult out = new StreamResult(outputWriter);
716
717                xformer.transform(new DOMSource(item), out);
718
719                String fragmentWithReplacedVars= evalVars(outputWriter.toString());
720
721                CharArrayReader reader = new CharArrayReader(fragmentWithReplacedVars.toCharArray());
722                tmpPref.fromXML(reader);
723            } catch (Exception ex) {
724                log("Error: can not read XML fragment :" + ex.getMessage());
725            }
726
727            return tmpPref;
728        }
729
730        private String normalizeDirName(String dir) {
731            String s = dir.replace("\\", "/");
732            if (s.endsWith("/")) s=s.substring(0,s.length()-1);
733            return s;
734        }
735
736
737    }
738
739    /**
740     * Helper class to do specific Prefrences operation - appending, replacing,
741     * deletion by key and by value
742     * Also contains functions that convert preferences object to JavaScript object and back
743     */
744    public static class PreferencesUtils {
745
746        private static void replacePreferences(Preferences fragment, Preferences mainpref) {
747            // normal prefs
748            for (Entry<String, String> entry : fragment.properties.entrySet()) {
749                mainpref.put(entry.getKey(), entry.getValue());
750            }
751            // "list"
752            for (Entry<String, List<String>> entry : fragment.collectionProperties.entrySet()) {
753                mainpref.putCollection(entry.getKey(), entry.getValue());
754            }
755            // "lists"
756            for (Entry<String, List<List<String>>> entry : fragment.arrayProperties.entrySet()) {
757                List<Collection<String>> array = new ArrayList<Collection<String>>();
758                array.addAll(entry.getValue());
759                mainpref.putArray(entry.getKey(), array);
760            }
761            /// "maps"
762            for (Entry<String, List<Map<String, String>>> entry : fragment.listOfStructsProperties.entrySet()) {
763                mainpref.putListOfStructs(entry.getKey(), entry.getValue());
764            }
765
766        }
767
768        private static void appendPreferences(Preferences fragment, Preferences mainpref) {
769            // normal prefs
770            for (Entry<String, String> entry : fragment.properties.entrySet()) {
771                mainpref.put(entry.getKey(), entry.getValue());
772            }
773
774            // "list"
775            for (Entry<String, List<String>> entry : fragment.collectionProperties.entrySet()) {
776                String key = entry.getKey();
777
778                Collection<String> newItems = getCollection(mainpref, key, true);
779                if (newItems == null) continue;
780
781                for (String item : entry.getValue()) {
782                    // add nonexisting elements to then list
783                    if (!newItems.contains(item)) {
784                        newItems.add(item);
785                    }
786                }
787                mainpref.putCollection(entry.getKey(), newItems);
788            }
789
790            // "lists"
791            for (Entry<String, List<List<String>>> entry : fragment.arrayProperties.entrySet()) {
792                String key = entry.getKey();
793
794                Collection<Collection<String>> newLists = getArray(mainpref, key, true);
795                if (newLists == null) continue;
796
797                for (Collection<String> list : entry.getValue()) {
798                    // add nonexisting list (equals comparison for lists is used implicitly)
799                    if (!newLists.contains(list)) {
800                        newLists.add(list);
801                    }
802                }
803                mainpref.putArray(entry.getKey(), newLists);
804            }
805
806            /// "maps"
807            for (Entry<String, List<Map<String, String>>> entry : fragment.listOfStructsProperties.entrySet()) {
808                String key = entry.getKey();
809
810                List<Map<String, String>> newMaps = getListOfStructs(mainpref, key, true);
811                if (newMaps == null) continue;
812
813                // get existing properties as list of maps
814
815                for (Map<String, String> map : entry.getValue()) {
816                    // add nonexisting map (equals comparison for maps is used implicitly)
817                    if (!newMaps.contains(map)) {
818                        newMaps.add(map);
819                    }
820                }
821                mainpref.putListOfStructs(entry.getKey(), newMaps);
822            }
823        }
824
825        /**
826     * Delete items from @param mainpref collections that match items from @param fragment collections
827     */
828    private static void deletePreferenceValues(Preferences fragment, Preferences mainpref) {
829
830
831        // normal prefs
832        for (Entry<String, String> entry : fragment.properties.entrySet()) {
833            // if mentioned value found, delete it
834            if (entry.getValue().equals(mainpref.properties.get(entry.getKey()))) {
835                mainpref.put(entry.getKey(), null);
836            }
837        }
838
839        // "list"
840        for (Entry<String, List<String>> entry : fragment.collectionProperties.entrySet()) {
841            String key = entry.getKey();
842
843            Collection<String> newItems = getCollection(mainpref, key, true);
844            if (newItems == null) continue;
845
846            // remove mentioned items from collection
847            for (String item : entry.getValue()) {
848                log("Deleting preferences: from list %s: %s\n", key, item);
849                newItems.remove(item);
850            }
851            mainpref.putCollection(entry.getKey(), newItems);
852        }
853
854        // "lists"
855        for (Entry<String, List<List<String>>> entry : fragment.arrayProperties.entrySet()) {
856            String key = entry.getKey();
857
858
859            Collection<Collection<String>> newLists = getArray(mainpref, key, true);
860            if (newLists == null) continue;
861
862            // if items are found in one of lists, remove that list!
863            Iterator<Collection<String>> listIterator = newLists.iterator();
864            while (listIterator.hasNext()) {
865                Collection<String> list = listIterator.next();
866                for (Collection<String> removeList : entry.getValue()) {
867                    if (list.containsAll(removeList)) {
868                        // remove current list, because it matches search criteria
869                        log("Deleting preferences: list from lists %s: %s\n", key, list);
870                        listIterator.remove();
871                    }
872                }
873            }
874
875            mainpref.putArray(entry.getKey(), newLists);
876        }
877
878        /// "maps"
879        for (Entry<String, List<Map<String, String>>> entry : fragment.listOfStructsProperties.entrySet()) {
880            String key = entry.getKey();
881
882            List<Map<String, String>> newMaps = getListOfStructs(mainpref, key, true);
883            if (newMaps == null) continue;
884
885            Iterator<Map<String, String>> mapIterator = newMaps.iterator();
886            while (mapIterator.hasNext()) {
887                Map<String, String> map = mapIterator.next();
888                for (Map<String, String> removeMap : entry.getValue()) {
889                    if (map.entrySet().containsAll(removeMap.entrySet())) {
890                        // the map contain all mentioned key-value pair, so it should be deleted from "maps"
891                        log("Deleting preferences: deleting map from maps %s: %s\n", key, map);
892                        mapIterator.remove();
893                    }
894                }
895            }
896            mainpref.putListOfStructs(entry.getKey(), newMaps);
897        }
898    }
899
900    private static void deletePreferenceKeyByPattern(String pattern, Preferences pref) {
901        Map<String, Setting> allSettings = pref.getAllSettings();
902        for (Entry<String, Setting> entry : allSettings.entrySet()) {
903            String key = entry.getKey();
904            if (key.matches(pattern)) {
905                log("Deleting preferences: deleting key from preferences: " + key);
906                pref.putSetting(key, entry.getValue().getNullInstance());
907            }
908        }
909    }
910
911    private static void deletePreferenceKey(String key, Preferences pref) {
912        Map<String, Setting> allSettings = pref.getAllSettings();
913        if (allSettings.containsKey(key)) {
914            log("Deleting preferences: deleting key from preferences: " + key);
915            pref.putSetting(key, allSettings.get(key).getNullInstance());
916        }
917    }
918
919    private static Collection<String> getCollection(Preferences mainpref, String key, boolean warnUnknownDefault)  {
920        Collection<String> existing = mainpref.collectionProperties.get(key);
921        Collection<String> defaults = mainpref.collectionDefaults.get(key);
922
923        if (existing == null && defaults == null) {
924            if (warnUnknownDefault) defaultUnknownWarning(key);
925            return null;
926        }
927        return  (existing != null)
928                ? new ArrayList<String>(existing) : new ArrayList<String>(defaults);
929    }
930
931    private static Collection<Collection<String>> getArray(Preferences mainpref, String key, boolean warnUnknownDefault)  {
932        Collection<List<String>> existing = mainpref.arrayProperties.get(key);
933        Collection<List<String>> defaults = mainpref.arrayDefaults.get(key);
934
935        if (existing == null && defaults == null) {
936            if (warnUnknownDefault) defaultUnknownWarning(key);
937            return null;
938        }
939
940        return  (existing != null)
941                ? new ArrayList<Collection<String>>(existing) : new ArrayList<Collection<String>>(defaults);
942    }
943
944    private static List<Map<String, String>> getListOfStructs(Preferences mainpref, String key, boolean warnUnknownDefault)  {
945        Collection<Map<String, String>> existing = mainpref.listOfStructsProperties.get(key);
946        Collection<Map<String, String>> defaults = mainpref.listOfStructsDefaults.get(key);
947
948        if (existing == null && defaults == null) {
949            if (warnUnknownDefault) defaultUnknownWarning(key);
950            return null;
951        }
952
953        return (existing != null)
954                ? new ArrayList<Map<String, String>>(existing) : new ArrayList<Map<String, String>>(defaults);
955    }
956
957    private static void defaultUnknownWarning(String key) {
958        log("Warning: Unknown default value of %s , skipped\n", key);
959        JOptionPane.showMessageDialog(
960                Main.parent,
961                tr("<html>Settings file asks to append preferences to <b>{0}</b>,<br/> but its default value is unknown at this moment.<br/> Please activate corresponding function manually and retry importing.", key),
962                tr("Warning"),
963                JOptionPane.WARNING_MESSAGE);
964    }
965
966    private static void showPrefs(Preferences tmpPref) {
967        Main.info("properties: " + tmpPref.properties);
968        Main.info("collections: " + tmpPref.collectionProperties);
969        Main.info("arrays: " + tmpPref.arrayProperties);
970        Main.info("maps: " + tmpPref.listOfStructsProperties);
971    }
972
973    private static void modifyPreferencesByScript(ScriptEngine engine, Preferences tmpPref, String js) throws ScriptException {
974        loadPrefsToJS(engine, tmpPref, "API.pref", true);
975        engine.eval(js);
976        readPrefsFromJS(engine, tmpPref, "API.pref");
977    }
978
979    /**
980     * Convert JavaScript preferences object to preferences data structures
981     * @param engine - JS engine to put object
982     * @param tmpPref - preferences to fill from JS
983     * @param varInJS - JS variable name, where preferences are stored
984     * @throws ScriptException
985     */
986    public static void readPrefsFromJS(ScriptEngine engine, Preferences tmpPref, String varInJS) throws ScriptException {
987        String finish =
988            "stringMap = new java.util.TreeMap ;"+
989            "listMap =  new java.util.TreeMap ;"+
990            "listlistMap = new java.util.TreeMap ;"+
991            "listmapMap =  new java.util.TreeMap ;"+
992            "for (key in "+varInJS+") {"+
993            "  val = "+varInJS+"[key];"+
994            "  type = typeof val == 'string' ? 'string' : val.type;"+
995            "  if (type == 'string') {"+
996            "    stringMap.put(key, val);"+
997            "  } else if (type == 'list') {"+
998            "    l = new java.util.ArrayList;"+
999            "    for (i=0; i<val.length; i++) {"+
1000            "      l.add(java.lang.String.valueOf(val[i]));"+
1001            "    }"+
1002            "    listMap.put(key, l);"+
1003            "  } else if (type == 'listlist') {"+
1004            "    l = new java.util.ArrayList;"+
1005            "    for (i=0; i<val.length; i++) {"+
1006            "      list=val[i];"+
1007            "      jlist=new java.util.ArrayList;"+
1008            "      for (j=0; j<list.length; j++) {"+
1009            "         jlist.add(java.lang.String.valueOf(list[j]));"+
1010            "      }"+
1011            "      l.add(jlist);"+
1012            "    }"+
1013            "    listlistMap.put(key, l);"+
1014            "  } else if (type == 'listmap') {"+
1015            "    l = new java.util.ArrayList;"+
1016            "    for (i=0; i<val.length; i++) {"+
1017            "      map=val[i];"+
1018            "      jmap=new java.util.TreeMap;"+
1019            "      for (var key2 in map) {"+
1020            "         jmap.put(key2,java.lang.String.valueOf(map[key2]));"+
1021            "      }"+
1022            "      l.add(jmap);"+
1023            "    }"+
1024            "    listmapMap.put(key, l);"+
1025            "  }  else {" +
1026            "   org.openstreetmap.josm.data.CustomConfigurator.log('Unknown type:'+val.type+ '- use list, listlist or listmap'); }"+
1027            "  }";
1028        engine.eval(finish);
1029
1030        @SuppressWarnings("unchecked")
1031        Map<String, String> stringMap =  (Map<String, String>) engine.get("stringMap");
1032        @SuppressWarnings("unchecked")
1033        Map<String, List<String>> listMap = (SortedMap<String, List<String>> ) engine.get("listMap");
1034        @SuppressWarnings("unchecked")
1035        Map<String, List<Collection<String>>> listlistMap = (SortedMap<String, List<Collection<String>>>) engine.get("listlistMap");
1036        @SuppressWarnings("unchecked")
1037        Map<String, List<Map<String, String>>> listmapMap = (SortedMap<String, List<Map<String,String>>>) engine.get("listmapMap");
1038
1039        tmpPref.properties.clear();
1040        tmpPref.collectionProperties.clear();
1041        tmpPref.arrayProperties.clear();
1042        tmpPref.listOfStructsProperties.clear();
1043
1044        for (Entry<String, String> e : stringMap.entrySet()) {
1045            if (e.getValue().equals( tmpPref.defaults.get(e.getKey())) ) continue;
1046            tmpPref.properties.put(e.getKey(), e.getValue());
1047        }
1048
1049        for (Entry<String, List<String>> e : listMap.entrySet()) {
1050            if (Preferences.equalCollection(e.getValue(), tmpPref.collectionDefaults.get(e.getKey()))) continue;
1051            tmpPref.collectionProperties.put(e.getKey(), e.getValue());
1052        }
1053
1054        for (Entry<String, List<Collection<String>>> e : listlistMap.entrySet()) {
1055            if (Preferences.equalArray(e.getValue(), tmpPref.arrayDefaults.get(e.getKey()))) continue;
1056            @SuppressWarnings("unchecked") List<List<String>> value = (List)e.getValue();
1057            tmpPref.arrayProperties.put(e.getKey(), value);
1058        }
1059
1060        for (Entry<String, List<Map<String, String>>> e : listmapMap.entrySet()) {
1061            if (Preferences.equalListOfStructs(e.getValue(), tmpPref.listOfStructsDefaults.get(e.getKey()))) continue;
1062            tmpPref.listOfStructsProperties.put(e.getKey(), e.getValue());
1063        }
1064    }
1065
1066    /**
1067     * Convert preferences data structures to JavaScript object
1068     * @param engine - JS engine to put object
1069     * @param tmpPref - preferences to convert
1070     * @param whereToPutInJS - variable name to store preferences in JS
1071     * @param includeDefaults - include known default values to JS objects
1072     * @throws ScriptException
1073     */
1074    public static void loadPrefsToJS(ScriptEngine engine, Preferences tmpPref, String whereToPutInJS, boolean includeDefaults) throws ScriptException {
1075        Map<String, String> stringMap =  new TreeMap<String, String>();
1076        Map<String, List<String>> listMap = new TreeMap<String, List<String>>();
1077        Map<String, List<List<String>>> listlistMap = new TreeMap<String, List<List<String>>>();
1078        Map<String, List<Map<String, String>>> listmapMap = new TreeMap<String, List<Map<String, String>>>();
1079
1080        if (includeDefaults) {
1081            stringMap.putAll(tmpPref.defaults);
1082            listMap.putAll(tmpPref.collectionDefaults);
1083            listlistMap.putAll(tmpPref.arrayDefaults);
1084            listmapMap.putAll(tmpPref.listOfStructsDefaults);
1085        }
1086
1087        while (stringMap.values().remove(null));
1088        while (listMap.values().remove(null));
1089        while (listlistMap.values().remove(null));
1090        while (listmapMap.values().remove(null));
1091
1092        stringMap.putAll(tmpPref.properties);
1093        listMap.putAll(tmpPref.collectionProperties);
1094        listlistMap.putAll(tmpPref.arrayProperties);
1095        listmapMap.putAll(tmpPref.listOfStructsProperties);
1096
1097        engine.put("stringMap", stringMap);
1098        engine.put("listMap", listMap);
1099        engine.put("listlistMap", listlistMap);
1100        engine.put("listmapMap", listmapMap);
1101
1102        String init =
1103            "function getJSList( javaList ) {"+
1104            " var jsList; var i; "+
1105            " if (javaList == null) return null;"+
1106            "jsList = [];"+
1107            "  for (i = 0; i < javaList.size(); i++) {"+
1108            "    jsList.push(String(list.get(i)));"+
1109            "  }"+
1110            "return jsList;"+
1111            "}"+
1112            "function getJSMap( javaMap ) {"+
1113            " var jsMap; var it; var e; "+
1114            " if (javaMap == null) return null;"+
1115            " jsMap = {};"+
1116            " for (it = javaMap.entrySet().iterator(); it.hasNext();) {"+
1117            "    e = it.next();"+
1118            "    jsMap[ String(e.getKey()) ] = String(e.getValue()); "+
1119            "  }"+
1120            "  return jsMap;"+
1121            "}"+
1122            "for (it = stringMap.entrySet().iterator(); it.hasNext();) {"+
1123            "  e = it.next();"+
1124            whereToPutInJS+"[String(e.getKey())] = String(e.getValue());"+
1125            "}\n"+
1126            "for (it = listMap.entrySet().iterator(); it.hasNext();) {"+
1127            "  e = it.next();"+
1128            "  list = e.getValue();"+
1129            "  jslist = getJSList(list);"+
1130            "  jslist.type = 'list';"+
1131            whereToPutInJS+"[String(e.getKey())] = jslist;"+
1132            "}\n"+
1133            "for (it = listlistMap.entrySet().iterator(); it.hasNext(); ) {"+
1134            "  e = it.next();"+
1135            "  listlist = e.getValue();"+
1136            "  jslistlist = [];"+
1137            "  for (it2 = listlist.iterator(); it2.hasNext(); ) {"+
1138            "    list = it2.next(); "+
1139            "    jslistlist.push(getJSList(list));"+
1140            "    }"+
1141            "  jslistlist.type = 'listlist';"+
1142            whereToPutInJS+"[String(e.getKey())] = jslistlist;"+
1143            "}\n"+
1144            "for (it = listmapMap.entrySet().iterator(); it.hasNext();) {"+
1145            "  e = it.next();"+
1146            "  listmap = e.getValue();"+
1147            "  jslistmap = [];"+
1148            "  for (it2 = listmap.iterator(); it2.hasNext();) {"+
1149            "    map = it2.next();"+
1150            "    jslistmap.push(getJSMap(map));"+
1151            "    }"+
1152            "  jslistmap.type = 'listmap';"+
1153            whereToPutInJS+"[String(e.getKey())] = jslistmap;"+
1154            "}\n";
1155
1156        // Execute conversion script
1157        engine.eval(init);
1158    }
1159    }
1160}