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