Ignore:
Timestamp:
2012-08-15T14:01:54+02:00 (12 years ago)
Author:
donvip
Message:

[josm_tag2link] fix indentation, project settings, update javadoc and copyright

Location:
applications/editors/josm/plugins/tag2link
Files:
1 added
19 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/tag2link

    • Property svn:ignore
      •  

        old new  
        11build
        2 .settings
        32.classpath
        43.project
  • applications/editors/josm/plugins/tag2link/src/org/openstreetmap/josm/plugins/tag2link/Tag2LinkConstants.java

    r26964 r28547  
    11//    JOSM tag2link plugin.
    2 //    Copyright (C) 2011 Don-vip & FrViPofm
     2//    Copyright (C) 2011-2012 Don-vip & FrViPofm
    33//
    44//    This program is free software: you can redistribute it and/or modify
     
    2323public interface Tag2LinkConstants {
    2424
    25         /**
    26          * XML Schema
    27          */
    28         public static final String XML_LOCATION = "/resources/tag2link_sources.xml";
    29        
    30         /**
    31          * File encoding.
    32          */
    33         public static final String UTF8_ENCODING = "UTF-8";
    34        
    35         /**
    36          * Plugin icons.
    37          */
    38         public static String ICON_16 = "tag2linkv2_16x16.png";
    39         public static String ICON_24 = "tag2linkv2_24x24.png";
     25    /**
     26     * XML Schema
     27     */
     28    public static final String XML_LOCATION = "/resources/tag2link_sources.xml";
     29   
     30    /**
     31     * File encoding.
     32     */
     33    public static final String UTF8_ENCODING = "UTF-8";
     34   
     35    /**
     36     * Plugin icon, 16x16 pixels.
     37     */
     38    public static String ICON_16 = "tag2linkv2_16x16.png";
     39   
     40    /**
     41     * Plugin icon, 24x24 pixels.
     42     */
     43    public static String ICON_24 = "tag2linkv2_24x24.png";
     44
     45    /**
     46     * Plugin icon, 48x48 pixels.
     47     */
    4048    public static String ICON_48 = "tag2linkv2_48x48.png";
    4149   
     50    /**
     51     * Mail icon, 24x24 pixels.
     52     */
    4253    public static String MAIL_ICON_24 = "tag2mailv3_24x24.png";
    4354}
  • applications/editors/josm/plugins/tag2link/src/org/openstreetmap/josm/plugins/tag2link/Tag2LinkPlugin.java

    r27857 r28547  
    11//    JOSM tag2link plugin.
    2 //    Copyright (C) 2011 Don-vip & FrViPofm
     2//    Copyright (C) 2011-2012 Don-vip & FrViPofm
    33//
    44//    This program is free software: you can redistribute it and/or modify
     
    1717
    1818import org.openstreetmap.josm.gui.MapFrame;
    19 import org.openstreetmap.josm.gui.preferences.PreferenceSetting;
    2019import org.openstreetmap.josm.plugins.Plugin;
    2120import org.openstreetmap.josm.plugins.PluginInformation;
     
    2928 * @author Don-vip
    3029 * History:
     30 * 0.3d 15-Aug-2012 Add ref:(FR:)?FINESS and osm.fr link for ref:(FR:)?INSEE (josm#7961)
    3131 * 0.3c 22-Dec-2011 Add contact namespace for website
    3232 * 0.3b 29-Oct-2011 Add UAI support
     
    4444    // private Tag2LinkPreferenceSetting preferenceSetting; TODO
    4545
    46         /**
    47         * Initializes the plugin.
    48          * @param info
    49         */
    50         public Tag2LinkPlugin(PluginInformation info) {
    51                 super(info);
    52                 // this.preferenceSetting = new Tag2LinkPreferenceSetting();
    53                 Tag2LinkRuleChecker.init();
    54         }
     46    /**
     47    * Initializes the plugin.
     48     * @param info The plugin info provided by JOSM
     49    */
     50    public Tag2LinkPlugin(PluginInformation info) {
     51        super(info);
     52        // this.preferenceSetting = new Tag2LinkPreferenceSetting();
     53        Tag2LinkRuleChecker.init();
     54    }
    5555
    5656    /* (non-Javadoc)
  • applications/editors/josm/plugins/tag2link/src/org/openstreetmap/josm/plugins/tag2link/Tag2LinkPreferenceSetting.java

    r27857 r28547  
    11//    JOSM tag2link plugin.
    2 //    Copyright (C) 2011 Don-vip & FrViPofm
     2//    Copyright (C) 2011-2012 Don-vip & FrViPofm
    33//
    44//    This program is free software: you can redistribute it and/or modify
     
    2121import org.openstreetmap.josm.gui.preferences.PreferenceTabbedPane;
    2222
     23/**
     24 * Preferences of tag2link plugin. Currently not implemented.
     25 * @author Don-vip
     26 *
     27 */
    2328public class Tag2LinkPreferenceSetting extends DefaultTabPreferenceSetting implements Tag2LinkConstants {
    2429
     30    /**
     31     * Constructs a new {@code Tag2LinkPreferenceSetting}.
     32     */
    2533    public Tag2LinkPreferenceSetting() {
    2634        super(ICON_48, tr("Tag2Link Preferences"), tr("Tag2Link Preferences"));
  • applications/editors/josm/plugins/tag2link/src/org/openstreetmap/josm/plugins/tag2link/Tag2LinkRuleChecker.java

    r26980 r28547  
    11//    JOSM tag2link plugin.
    2 //    Copyright (C) 2011 Don-vip & FrViPofm
     2//    Copyright (C) 2011-2012 Don-vip & FrViPofm
    33//
    44//    This program is free software: you can redistribute it and/or modify
     
    3737import org.openstreetmap.josm.plugins.tag2link.io.SourcesReader;
    3838
     39/**
     40 * Class matching rules against a specified OSM primitive in order to get its relevant links.
     41 * @author Don-vip
     42 *
     43 */
    3944public class Tag2LinkRuleChecker implements Tag2LinkConstants {
    4045
     
    4247   
    4348    private static boolean initialized = false;
    44        
     49   
     50    /**
     51     * Initializes the matching rules mechanism.
     52     */
    4553    public static void init() {
    4654        if (!initialized) {
     
    5159   
    5260    private static String findValue(String arg, Collection<MatchingTag> matchingTags) {
    53                 for (MatchingTag tag : matchingTags) {
    54                         if (tag.params.containsKey(arg)) {
    55                                 return tag.params.get(arg);
    56                         }
    57                 }
    58                 return null;
     61        for (MatchingTag tag : matchingTags) {
     62            if (tag.params.containsKey(arg)) {
     63                return tag.params.get(arg);
     64            }
     65        }
     66        return null;
    5967    }
    6068   
    6169    private static String replaceParams(String s, EvalResult eval) {
    62         String result = s;
    63                 Matcher m = Pattern.compile("%([^%]*)%").matcher(s);
    64                 while (m.find()) {
    65                         String arg = m.group(1);
    66                        
    67                         // Search for a standard value
    68                         String val = findValue(arg, eval.matchingTags);
    69                        
    70                         // No standard value found: test lang() function
    71                         if (val == null) {
    72                                 Matcher lm = Pattern.compile(".*lang(?:\\((\\p{Lower}{2,})(?:,(\\p{Lower}{2,}))*\\))?.*").matcher(arg);
    73                                 if (lm.matches()) {
    74                                         String josmLang = Main.pref.get("language");
    75                                         String jvmLang = (josmLang.isEmpty() ? Locale.getDefault().getLanguage() : josmLang).split("_")[0];
    76                                         if (lm.groupCount() == 0) {
    77                                                 val = jvmLang;
    78                                         } else {
    79                                                 for (int i = 1; i<=lm.groupCount() && val == null; i++) {
    80                                                         if (jvmLang.equals(lm.group(i))) {
    81                                                                 val = jvmLang;
    82                                                         }
    83                                                 }
    84                                         }
    85                                 }
    86                         }
    87                        
    88                         // Find a default value if set after ":"
    89                         if (val == null && arg.contains(":")) {
    90                                 String[] vars = arg.split(":");
    91                                 for (int i = 0; val == null && i < vars.length-1; i++) {
    92                                         val = findValue(vars[i], eval.matchingTags);
    93                                 }
    94                                 if (val == null) {
    95                                         // Default value
    96                                         val = vars[vars.length-1];
    97                                 }
    98                         }
    99                        
    100                         // Has a value been found ?
    101                         if (val != null) {
    102                                 try {
    103                                         // Special hack for Wikipedia that prevents spaces being replaced by "+" characters, but by "_"
    104                                         if (s.contains("wikipedia.")) {
    105                                                 val = val.replaceAll(" ", "_");
    106                                         }
    107                                         // Encode param to be included in the URL, except if it is the URL itself !
    108                                         if (!m.group().equals(s)) {
    109                                                 val = URLEncoder.encode(val, UTF8_ENCODING);
    110                                         }
    111                                         // Finally replace parameter
    112                                         result = result.replaceFirst(Pattern.quote(m.group()), val);
    113                                 } catch (UnsupportedEncodingException e) {
    114                                         e.printStackTrace();
    115                                 }
    116                         } else {
    117                                 System.err.println("Invalid argument: "+arg);
    118                         }
    119                 }
    120         return result;
     70        String result = s;
     71        Matcher m = Pattern.compile("%([^%]*)%").matcher(s);
     72        while (m.find()) {
     73            String arg = m.group(1);
     74           
     75            // Search for a standard value
     76            String val = findValue(arg, eval.matchingTags);
     77           
     78            // No standard value found: test lang() function
     79            if (val == null) {
     80                Matcher lm = Pattern.compile(".*lang(?:\\((\\p{Lower}{2,})(?:,(\\p{Lower}{2,}))*\\))?.*").matcher(arg);
     81                if (lm.matches()) {
     82                    String josmLang = Main.pref.get("language");
     83                    String jvmLang = (josmLang.isEmpty() ? Locale.getDefault().getLanguage() : josmLang).split("_")[0];
     84                    if (lm.groupCount() == 0) {
     85                        val = jvmLang;
     86                    } else {
     87                        for (int i = 1; i<=lm.groupCount() && val == null; i++) {
     88                            if (jvmLang.equals(lm.group(i))) {
     89                                val = jvmLang;
     90                            }
     91                        }
     92                    }
     93                }
     94            }
     95           
     96            // Find a default value if set after ":"
     97            if (val == null && arg.contains(":")) {
     98                String[] vars = arg.split(":");
     99                for (int i = 0; val == null && i < vars.length-1; i++) {
     100                    val = findValue(vars[i], eval.matchingTags);
     101                }
     102                if (val == null) {
     103                    // Default value
     104                    val = vars[vars.length-1];
     105                }
     106            }
     107           
     108            // Has a value been found ?
     109            if (val != null) {
     110                try {
     111                    // Special hack for Wikipedia that prevents spaces being replaced by "+" characters, but by "_"
     112                    if (s.contains("wikipedia.")) {
     113                        val = val.replaceAll(" ", "_");
     114                    }
     115                    // Encode param to be included in the URL, except if it is the URL itself !
     116                    if (!m.group().equals(s)) {
     117                        val = URLEncoder.encode(val, UTF8_ENCODING);
     118                    }
     119                    // Finally replace parameter
     120                    result = result.replaceFirst(Pattern.quote(m.group()), val);
     121                } catch (UnsupportedEncodingException e) {
     122                    e.printStackTrace();
     123                }
     124            } else {
     125                System.err.println("Invalid argument: "+arg);
     126            }
     127        }
     128        return result;
    121129    }
    122130   
    123131    private static void replaceMapParams(Map<String, String> map, EvalResult eval) {
    124                 for (Iterator<String> it = map.keySet().iterator(); it.hasNext(); ) {
    125                         String key = it.next();
    126                         String value = map.get(key);
    127                         String key2 = replaceParams(key, eval);
    128                         String value2 = replaceParams(value, eval);
    129                         if (key.equals(key2) && value.equals(value2)) {
    130                                 // Nothing to do
    131                         } else if (key.equals(key2)) {
    132                                 // Update value
    133                                 map.put(key, value2);
    134                         } else {
    135                                 // Update key, and maybe value
    136                                 map.remove(key);
    137                                 map.put(key2, value2);
    138                         }
    139                 }
     132        for (Iterator<String> it = map.keySet().iterator(); it.hasNext(); ) {
     133            String key = it.next();
     134            String value = map.get(key);
     135            String key2 = replaceParams(key, eval);
     136            String value2 = replaceParams(value, eval);
     137            if (key.equals(key2) && value.equals(value2)) {
     138                // Nothing to do
     139            } else if (key.equals(key2)) {
     140                // Update value
     141                map.put(key, value2);
     142            } else {
     143                // Update key, and maybe value
     144                map.remove(key);
     145                map.put(key2, value2);
     146            }
     147        }
    140148    }
    141149   
    142150    private static Collection<Link> processEval(EvalResult eval, Rule rule, Source source) {
    143         Collection<Link> result = new ArrayList<Link>();
     151        Collection<Link> result = new ArrayList<Link>();
    144152        if (eval.matches()) {
    145153            for (Link link : rule.links) {
    146                         try {
    147                                         Link copy = (Link) link.clone();
    148                         copy.name = copy.name.replaceAll("%name%", source.name);
    149                         copy.url = replaceParams(copy.url, eval);
    150                         if (copy instanceof LinkPost) {
    151                                 LinkPost lp = (LinkPost) copy;
    152                                 replaceMapParams(lp.headers, eval);
    153                                 replaceMapParams(lp.params, eval);
    154                         }
    155                         result.add(copy);
    156                         } catch (CloneNotSupportedException e) {
    157                                 System.err.println(e);
    158                         }
    159             }
    160         }
    161         return result;
    162     }
    163    
     154                try {
     155                    Link copy = (Link) link.clone();
     156                    copy.name = copy.name.replaceAll("%name%", source.name);
     157                    copy.url = replaceParams(copy.url, eval);
     158                    if (copy instanceof LinkPost) {
     159                        LinkPost lp = (LinkPost) copy;
     160                        replaceMapParams(lp.headers, eval);
     161                        replaceMapParams(lp.params, eval);
     162                    }
     163                    result.add(copy);
     164                } catch (CloneNotSupportedException e) {
     165                    System.err.println(e);
     166                }
     167            }
     168        }
     169        return result;
     170    }
     171   
     172    /**
     173     * Replies the links relevant to the given OSM primitive.
     174     * @param p The OSM primitive
     175     * @return the links relevant to the {@code p}.
     176     */
    164177    public static Collection<Link> getLinks(IPrimitive p) {
    165178        Collection<Link> result = new ArrayList<Link>();
     
    172185    }
    173186
    174         public static Collection<Link> getLinks(Tag tag) {
    175                 Collection<Link> result = new ArrayList<Link>();
     187    /**
     188     * Replies the links relevant to the given OSM tag.
     189     * @param tag The OSM tag
     190     * @return the links relevant to the {@code tag}.
     191     */
     192    public static Collection<Link> getLinks(Tag tag) {
     193        Collection<Link> result = new ArrayList<Link>();
    176194        for (Source source : sources) {
    177195            for (Rule rule : source.rules) {
     
    179197            }
    180198        }
    181                 return result;
    182         }
     199        return result;
     200    }
    183201}
  • applications/editors/josm/plugins/tag2link/src/org/openstreetmap/josm/plugins/tag2link/action/OpenLinkAction.java

    r28546 r28547  
    11//    JOSM tag2link plugin.
    2 //    Copyright (C) 2011 Don-vip & FrViPofm
     2//    Copyright (C) 2011-2012 Don-vip & FrViPofm
    33//
    44//    This program is free software: you can redistribute it and/or modify
     
    3333import org.openstreetmap.josm.tools.OpenBrowser;
    3434
     35/**
     36 * Action allowing to open a general link.
     37 * @author Don-vip
     38 *
     39 */
    3540@SuppressWarnings("serial")
    3641public class OpenLinkAction extends JosmAction implements Tag2LinkConstants {
     
    3843    private Link link;
    3944   
     45    /**
     46     * Constructs a new {@code OpenLinkAction}.
     47     * @param link The link to open
     48     */
    4049    public OpenLinkAction(Link link) {
    4150        super(tr(link.name), ICON_24, tr("Launch browser with information about the selected object"), null, false);
     
    4554    @Override
    4655    public void actionPerformed(ActionEvent e) {
    47         if (link instanceof LinkPost) {
    48                 try {
    49                         LinkPost lp = (LinkPost) link;
    50                         System.out.println("Sending POST request to "+lp.url);
    51                         HttpURLConnection conn = (HttpURLConnection) new URL(lp.url).openConnection();
    52                                 conn.setDoOutput(true);
    53                                 conn.setRequestMethod("POST");
    54                                 for (String header : lp.headers.keySet()) {
    55                                         conn.setRequestProperty(header, lp.headers.get(header));
    56                                 }
    57                                 String data = "";
    58                                 for (String param : lp.params.keySet()) {
    59                                         if (!data.isEmpty()) {
    60                                                 data += "&";
    61                                         }
    62                                         data += param+"="+lp.params.get(param);
    63                                 }
    64                                 OutputStreamWriter osw = new OutputStreamWriter(conn.getOutputStream());
    65                                 osw.write(data);
    66                                 osw.flush();
    67                                 osw.close();
    68                                
    69                                 String filename = "output.pdf";// FIXME: should work for PDF files only (not even tested)
    70                                 FileOutputStream fos = new FileOutputStream(filename);
    71                                 InputStream is = conn.getInputStream();
    72                                 byte[] buffer = new byte[2048];
    73                                 int n = -1;
    74                                 while ((n = is.read(buffer)) > -1) {
    75                                         fos.write(buffer, 0, n);
    76                                 }
    77                                 is.close();
    78                                 fos.close();
    79                                
    80                         System.out.println("Opening "+filename);
    81                         String result = OpenBrowser.displayUrl("file://"+filename);
    82                         if (result != null) {
    83                                 System.err.println(result);
    84                         }
    85                        
    86                         } catch (MalformedURLException ex) {
    87                                 ex.printStackTrace();
    88                         } catch (IOException ex) {
    89                                 ex.printStackTrace();
    90                         }
    91         } else {
    92                 System.out.println("Opening "+link.url);
    93                 String result = OpenBrowser.displayUrl(link.url);
    94                 if (result != null) {
    95                         System.err.println(result);
    96                 }
    97         }
     56        if (link instanceof LinkPost) {
     57            try {
     58                LinkPost lp = (LinkPost) link;
     59                System.out.println("Sending POST request to "+lp.url);
     60                HttpURLConnection conn = (HttpURLConnection) new URL(lp.url).openConnection();
     61                conn.setDoOutput(true);
     62                conn.setRequestMethod("POST");
     63                for (String header : lp.headers.keySet()) {
     64                    conn.setRequestProperty(header, lp.headers.get(header));
     65                }
     66                String data = "";
     67                for (String param : lp.params.keySet()) {
     68                    if (!data.isEmpty()) {
     69                        data += "&";
     70                    }
     71                    data += param+"="+lp.params.get(param);
     72                }
     73                OutputStreamWriter osw = new OutputStreamWriter(conn.getOutputStream());
     74                osw.write(data);
     75                osw.flush();
     76                osw.close();
     77               
     78                String filename = "output.pdf";// FIXME: should work for PDF files only (not even tested)
     79                FileOutputStream fos = new FileOutputStream(filename);
     80                InputStream is = conn.getInputStream();
     81                byte[] buffer = new byte[2048];
     82                int n = -1;
     83                while ((n = is.read(buffer)) > -1) {
     84                    fos.write(buffer, 0, n);
     85                }
     86                is.close();
     87                fos.close();
     88               
     89                System.out.println("Opening "+filename);
     90                String result = OpenBrowser.displayUrl("file://"+filename);
     91                if (result != null) {
     92                    System.err.println(result);
     93                }
     94               
     95            } catch (MalformedURLException ex) {
     96                ex.printStackTrace();
     97            } catch (IOException ex) {
     98                ex.printStackTrace();
     99            }
     100        } else {
     101            System.out.println("Opening "+link.url);
     102            String result = OpenBrowser.displayUrl(link.url);
     103            if (result != null) {
     104                System.err.println(result);
     105            }
     106        }
    98107    }
    99108}
  • applications/editors/josm/plugins/tag2link/src/org/openstreetmap/josm/plugins/tag2link/action/OpenMailAction.java

    r26980 r28547  
    11//    JOSM tag2link plugin.
    2 //    Copyright (C) 2011 Don-vip & FrViPofm
     2//    Copyright (C) 2011-2012 Don-vip & FrViPofm
    33//
    44//    This program is free software: you can redistribute it and/or modify
     
    2626import org.openstreetmap.josm.plugins.tag2link.data.Link;
    2727
     28/**
     29 * Action allowing to open a mailto:// link.
     30 * @author Don-vip
     31 *
     32 */
    2833@SuppressWarnings("serial")
    2934public class OpenMailAction extends JosmAction implements Tag2LinkConstants {
    30        
     35   
    3136    private String url;
    3237   
     38    /**
     39     * Constructs a new {@code OpenMailAction}.
     40     * @param link The link to open
     41     */
    3342    public OpenMailAction(Link link) {
    3443        super(tr(link.name), MAIL_ICON_24, tr("Launch your default software for sending an email to the selected contact address"), null, false);
     
    3847    @Override
    3948    public void actionPerformed(ActionEvent e) {
    40         if (Desktop.isDesktopSupported()) {
    41                 try {
    42                         System.out.println("Sending "+url);
    43                                 Desktop.getDesktop().mail(new URI(url));
    44                         } catch (Exception ex) {
    45                                 ex.printStackTrace();
    46                         }
    47         }
     49        if (Desktop.isDesktopSupported()) {
     50            try {
     51                System.out.println("Sending "+url);
     52                Desktop.getDesktop().mail(new URI(url));
     53            } catch (Exception ex) {
     54                ex.printStackTrace();
     55            }
     56        }
    4857    }
    4958}
  • applications/editors/josm/plugins/tag2link/src/org/openstreetmap/josm/plugins/tag2link/data/Condition.java

    r26980 r28547  
    11//    JOSM tag2link plugin.
    2 //    Copyright (C) 2011 Don-vip & FrViPofm
     2//    Copyright (C) 2011-2012 Don-vip & FrViPofm
    33//
    44//    This program is free software: you can redistribute it and/or modify
     
    1818import java.util.regex.Pattern;
    1919
     20/**
     21 * A condition allowing to match a rule against an OSM primitive or tag.
     22 * @author Don-vip
     23 *
     24 */
    2025public class Condition {
     26   
     27    /**
     28     * The pattern used against tag key. May be null.
     29     */
    2130    public Pattern keyPattern;
     31
     32    /**
     33     * The pattern used against tag value. May be null.
     34     */
    2235    public Pattern valPattern;
     36
     37    /**
     38     * The condition id. May be null.
     39     */
    2340    public String id;
    2441   
    25         /* (non-Javadoc)
    26         * @see java.lang.Object#toString()
    27         */
    28         @Override
    29         public String toString() {
    30                 return "Condition ["
    31                                 + (keyPattern != null ? "k=" + keyPattern + ", " : "")
    32                                 + (valPattern != null ? "v=" + valPattern + ", " : "")
    33                                 + (id != null ? "id=" + id : "") + "]";
    34         }
     42    /* (non-Javadoc)
     43    * @see java.lang.Object#toString()
     44    */
     45    @Override
     46    public String toString() {
     47        return "Condition ["
     48                + (keyPattern != null ? "k=" + keyPattern + ", " : "")
     49                + (valPattern != null ? "v=" + valPattern + ", " : "")
     50                + (id != null ? "id=" + id : "") + "]";
     51    }
    3552}
  • applications/editors/josm/plugins/tag2link/src/org/openstreetmap/josm/plugins/tag2link/data/Link.java

    r26980 r28547  
    11//    JOSM tag2link plugin.
    2 //    Copyright (C) 2011 Don-vip & FrViPofm
     2//    Copyright (C) 2011-2012 Don-vip & FrViPofm
    33//
    44//    This program is free software: you can redistribute it and/or modify
     
    2121   
    2222    public Link(String name, String url) {
    23         this.name = name;
    24         this.url = url;
     23        this.name = name;
     24        this.url = url;
    2525    }
    2626   
    2727    public Link(Link link) {
    28         this(new String(link.name), new String(link.url));
     28        this(new String(link.name), new String(link.url));
    2929    }
    3030   
     
    4545    }
    4646
    47         /* (non-Javadoc)
    48         * @see java.lang.Object#toString()
    49         */
    50         @Override
    51         public String toString() {
    52                 return "Link [name=" + name + ", url=" + url + "]";
    53         }
     47    /* (non-Javadoc)
     48    * @see java.lang.Object#toString()
     49    */
     50    @Override
     51    public String toString() {
     52        return "Link [name=" + name + ", url=" + url + "]";
     53    }
    5454
    55         /* (non-Javadoc)
    56         * @see java.lang.Object#clone()
    57         */
    58         @Override
    59         public Link clone() throws CloneNotSupportedException {
    60                 return new Link(this);
    61         }
     55    /* (non-Javadoc)
     56    * @see java.lang.Object#clone()
     57    */
     58    @Override
     59    public Link clone() throws CloneNotSupportedException {
     60        return new Link(this);
     61    }
    6262}
  • applications/editors/josm/plugins/tag2link/src/org/openstreetmap/josm/plugins/tag2link/data/LinkPost.java

    r26980 r28547  
    11//    JOSM tag2link plugin.
    2 //    Copyright (C) 2011 Don-vip & FrViPofm
     2//    Copyright (C) 2011-2012 Don-vip & FrViPofm
    33//
    44//    This program is free software: you can redistribute it and/or modify
     
    2121public class LinkPost extends Link {
    2222
    23         public final Map<String, String> headers;
    24         public final Map<String, String> params;
    25        
    26         public LinkPost(LinkPost link) {
    27                 this(new String(link.name), new String(link.url),
    28                                 link.headers == null ? null : new HashMap<String, String>(link.headers),
    29                                 link.params  == null ? null : new HashMap<String, String>(link.params));
    30         }
     23    public final Map<String, String> headers;
     24    public final Map<String, String> params;
     25   
     26    public LinkPost(LinkPost link) {
     27        this(new String(link.name), new String(link.url),
     28                link.headers == null ? null : new HashMap<String, String>(link.headers),
     29                link.params  == null ? null : new HashMap<String, String>(link.params));
     30    }
    3131
    32         public LinkPost(String name, String url) {
    33                 this(name, url, null, null);
    34         }
     32    public LinkPost(String name, String url) {
     33        this(name, url, null, null);
     34    }
    3535
    36         public LinkPost(String name, String url, Map<String, String> headers, Map<String, String> params) {
    37                 super(name, url);
    38                 this.headers = headers;
    39                 this.params = params;
    40         }
    41        
    42         protected final boolean containsParams(Map<String, String> map) {
    43                 if (map != null) {
    44                         for (String key : map.keySet()) {
    45                                 if (containsParams(map.get(key))) {
    46                                         return true;
    47                                 }
    48                         }
    49                 }
    50                 return false;
    51         }
    52        
    53         public boolean headersContainsParams() {
    54                 return containsParams(headers);
    55         }
     36    public LinkPost(String name, String url, Map<String, String> headers, Map<String, String> params) {
     37        super(name, url);
     38        this.headers = headers;
     39        this.params = params;
     40    }
     41   
     42    protected final boolean containsParams(Map<String, String> map) {
     43        if (map != null) {
     44            for (String key : map.keySet()) {
     45                if (containsParams(map.get(key))) {
     46                    return true;
     47                }
     48            }
     49        }
     50        return false;
     51    }
     52   
     53    public boolean headersContainsParams() {
     54        return containsParams(headers);
     55    }
    5656
    57         public boolean paramsContainsParams() {
    58                 return containsParams(params);
    59         }
     57    public boolean paramsContainsParams() {
     58        return containsParams(params);
     59    }
    6060
    61         /* (non-Javadoc)
    62         * @see org.openstreetmap.josm.plugins.tag2link.data.Link#containsParams()
    63         */
    64         @Override
    65         public boolean containsParams() {
    66                 return super.containsParams() || headersContainsParams() || paramsContainsParams();
    67         }
     61    /* (non-Javadoc)
     62    * @see org.openstreetmap.josm.plugins.tag2link.data.Link#containsParams()
     63    */
     64    @Override
     65    public boolean containsParams() {
     66        return super.containsParams() || headersContainsParams() || paramsContainsParams();
     67    }
    6868
    69         /* (non-Javadoc)
    70         * @see org.openstreetmap.josm.plugins.tag2link.data.Link#clone()
    71         */
    72         @Override
    73         public LinkPost clone() throws CloneNotSupportedException {
    74                 return new LinkPost(this);
    75         }
     69    /* (non-Javadoc)
     70    * @see org.openstreetmap.josm.plugins.tag2link.data.Link#clone()
     71    */
     72    @Override
     73    public LinkPost clone() throws CloneNotSupportedException {
     74        return new LinkPost(this);
     75    }
    7676}
  • applications/editors/josm/plugins/tag2link/src/org/openstreetmap/josm/plugins/tag2link/data/Rule.java

    r26980 r28547  
    11//    JOSM tag2link plugin.
    2 //    Copyright (C) 2011 Don-vip & FrViPofm
     2//    Copyright (C) 2011-2012 Don-vip & FrViPofm
    33//
    44//    This program is free software: you can redistribute it and/or modify
     
    3535        private String prefix;
    3636        public MatchingTag(String key, String value, String prefix) {
    37                 this.key = key;
    38                 this.value = value;
    39                 this.params = new HashMap<String, String>();
    40                 this.prefix = prefix;
    41                 addKeyValueParams();
     37            this.key = key;
     38            this.value = value;
     39            this.params = new HashMap<String, String>();
     40            this.prefix = prefix;
     41            addKeyValueParams();
    4242        }
    4343        public void addParams(Matcher m, String paramName) {
    44                 for (int i = 1; i<=m.groupCount(); i++) {
    45                         params.put(prefix+paramName+"."+i, m.group(i));
    46                 }
     44            for (int i = 1; i<=m.groupCount(); i++) {
     45                params.put(prefix+paramName+"."+i, m.group(i));
     46            }
    4747        }
    4848        private void addKeyValueParams() {
    49                 params.put("k", key);
    50                 params.put("v", value);
    51                 if (!prefix.isEmpty()) {
    52                 params.put(prefix+"k", key);
    53                 params.put(prefix+"v", value);
    54                 }
     49            params.put("k", key);
     50            params.put("v", value);
     51            if (!prefix.isEmpty()) {
     52                params.put(prefix+"k", key);
     53                params.put(prefix+"v", value);
     54            }
    5555        }
    56                 /* (non-Javadoc)
    57                 * @see java.lang.Object#toString()
    58                 */
    59                 @Override
    60                 public String toString() {
    61                         return "MatchingTag [" + (key != null ? "key=" + key + ", " : "")
    62                                         + (value != null ? "value=" + value + ", " : "")
    63                                         + (params != null ? "params=" + params + ", " : "")
    64                                         + (prefix != null ? "prefix=" + prefix : "") + "]";
    65                 }
     56        /* (non-Javadoc)
     57        * @see java.lang.Object#toString()
     58        */
     59        @Override
     60        public String toString() {
     61            return "MatchingTag [" + (key != null ? "key=" + key + ", " : "")
     62                    + (value != null ? "value=" + value + ", " : "")
     63                    + (params != null ? "params=" + params + ", " : "")
     64                    + (prefix != null ? "prefix=" + prefix : "") + "]";
     65        }
    6666    }
    6767   
    6868    public static class EvalResult {
    69         private final int conditionsNumber;
    70         public EvalResult(int conditionsNumber) {
    71                 this.conditionsNumber = conditionsNumber;
    72         }
     69        private final int conditionsNumber;
     70        public EvalResult(int conditionsNumber) {
     71            this.conditionsNumber = conditionsNumber;
     72        }
    7373        public final Collection<MatchingTag> matchingTags = new ArrayList<MatchingTag>();
    7474        public boolean matches() {
    7575            return conditionsNumber > 0 && matchingTags.size() >= conditionsNumber;
    7676        }
    77                 /* (non-Javadoc)
    78                 * @see java.lang.Object#toString()
    79                 */
    80                 @Override
    81                 public String toString() {
    82                         return "EvalResult [conditionsNumber=" + conditionsNumber
    83                                         + ", matchingTags=" + matchingTags + "]";
    84                 }
     77        /* (non-Javadoc)
     78        * @see java.lang.Object#toString()
     79        */
     80        @Override
     81        public String toString() {
     82            return "EvalResult [conditionsNumber=" + conditionsNumber
     83                    + ", matchingTags=" + matchingTags + "]";
     84        }
    8585    }
    8686   
     
    9191                Matcher keyMatcher = c.keyPattern.matcher(key);
    9292                if (keyMatcher.matches()) {
    93                         String idPrefix = c.id == null ? "" : c.id+".";
    94                         MatchingTag tag = new MatchingTag(key, tags.get(key), idPrefix);
    95                         tag.addParams(keyMatcher, "k");
    96                         boolean matchingTag = true;
    97                         if (c.valPattern != null) {
    98                                 Matcher valMatcher = c.valPattern.matcher(tag.value);
    99                                 if (valMatcher.matches()) {
    100                                         tag.addParams(valMatcher, "v");
    101                                 } else {
    102                                         matchingTag = false;
    103                                 }
    104                         }
    105                         if (matchingTag) {
    106                                 result.matchingTags.add(tag);
    107                         }
     93                    String idPrefix = c.id == null ? "" : c.id+".";
     94                    MatchingTag tag = new MatchingTag(key, tags.get(key), idPrefix);
     95                    tag.addParams(keyMatcher, "k");
     96                    boolean matchingTag = true;
     97                    if (c.valPattern != null) {
     98                        Matcher valMatcher = c.valPattern.matcher(tag.value);
     99                        if (valMatcher.matches()) {
     100                            tag.addParams(valMatcher, "v");
     101                        } else {
     102                            matchingTag = false;
     103                        }
     104                    }
     105                    if (matchingTag) {
     106                        result.matchingTags.add(tag);
     107                    }
    108108                }
    109109            }
     
    113113   
    114114    public EvalResult evaluates(IPrimitive p) {
    115         return evaluates(p.getKeys());
     115        return evaluates(p.getKeys());
    116116    }
    117117
    118         public EvalResult evaluates(Tag tag) {
    119                 Map<String, String> map = new HashMap<String, String>();
    120                 map.put(tag.getKey(), tag.getValue());
    121                 return evaluates(map);
    122         }
     118    public EvalResult evaluates(Tag tag) {
     119        Map<String, String> map = new HashMap<String, String>();
     120        map.put(tag.getKey(), tag.getValue());
     121        return evaluates(map);
     122    }
    123123
    124         /* (non-Javadoc)
    125         * @see java.lang.Object#toString()
    126         */
    127         @Override
    128         public String toString() {
    129                 return "Rule [conditions=" + conditions + ", links=" + links + "]";
    130         }
     124    /* (non-Javadoc)
     125    * @see java.lang.Object#toString()
     126    */
     127    @Override
     128    public String toString() {
     129        return "Rule [conditions=" + conditions + ", links=" + links + "]";
     130    }
    131131}
  • applications/editors/josm/plugins/tag2link/src/org/openstreetmap/josm/plugins/tag2link/data/Source.java

    r26980 r28547  
    11//    JOSM tag2link plugin.
    2 //    Copyright (C) 2011 Don-vip & FrViPofm
     2//    Copyright (C) 2011-2012 Don-vip & FrViPofm
    33//
    44//    This program is free software: you can redistribute it and/or modify
     
    1919import java.util.Collection;
    2020
     21/**
     22 * A source of links relative to an OSM primitive or tag, depending of the successful match of its conditions against it.
     23 * @author Don-vip
     24 *
     25 */
    2126public class Source {
     27    /**
     28     * The user-friendly source name.
     29     */
    2230    public final String name;
     31   
     32    /**
     33     * The rules applied against an OSM primitive or tag.
     34     */
    2335    public final Collection<Rule> rules = new ArrayList<Rule>();
    2436
     37    /**
     38     * Constructs a new {@code Source}.
     39     * @param name The user-friendly source name
     40     */
    2541    public Source(String name) {
    26                 this.name = name;
    27         }
     42        this.name = name;
     43    }
    2844
    29         /* (non-Javadoc)
    30         * @see java.lang.Object#toString()
    31         */
    32         @Override
    33         public String toString() {
    34                 return "Source [name=" + name + ", rules=" + rules + "]";
    35         }
     45    /* (non-Javadoc)
     46    * @see java.lang.Object#toString()
     47    */
     48    @Override
     49    public String toString() {
     50        return "Source [name=" + name + ", rules=" + rules + "]";
     51    }
    3652}
  • applications/editors/josm/plugins/tag2link/src/org/openstreetmap/josm/plugins/tag2link/io/SourcesReader.java

    r26980 r28547  
    11//    JOSM tag2link plugin.
    2 //    Copyright (C) 2011 Don-vip & FrViPofm
     2//    Copyright (C) 2011-2012 Don-vip & FrViPofm
    33//
    44//    This program is free software: you can redistribute it and/or modify
     
    4242import org.openstreetmap.josm.plugins.tag2link.data.Source;
    4343
     44/**
     45 * Class allowing to read the sources file.
     46 * @author Don-vip
     47 *
     48 */
    4449public class SourcesReader implements Tag2LinkConstants {
    4550   
    4651    XMLStreamReader parser;
    4752   
     53    /**
     54     * Constructs a new {@code SourcesReader}.
     55     * @param parser The XML parser
     56     */
    4857    public SourcesReader(XMLStreamReader parser) {
    4958        this.parser = parser;
    5059    }
    5160
     61    /**
     62     * Reads the sources and replies them as a {@code Collection}.
     63     * @return The colleciton of sources.
     64     */
    5265    public static Collection<Source> readSources() {
    5366        List<Source> result = new ArrayList<Source>();
     
    184197        String v = parser.getAttributeValue(null, "v");
    185198        if (v != null) {
    186                 c.valPattern = Pattern.compile(v);
     199            c.valPattern = Pattern.compile(v);
    187200        }
    188201        c.id = parser.getAttributeValue(null, "id");
     
    197210       
    198211        if ("POST".equals(parser.getAttributeValue(null, "method"))) {
    199                 Map<String, String> headers = null;
    200                 Map<String, String> params = null;
    201                 while (parser.hasNext()) {
     212            Map<String, String> headers = null;
     213            Map<String, String> params = null;
     214            while (parser.hasNext()) {
    202215                int event = parser.next();
    203216                if (event == XMLStreamConstants.START_ELEMENT) {
    204217                    if (parser.getLocalName().equals("header")) {
    205218                        if (headers == null) {
    206                                 headers = new HashMap<String, String>();
     219                            headers = new HashMap<String, String>();
    207220                        }
    208221                        headers.put(parser.getAttributeValue(null, "name"), parser.getAttributeValue(null, "value"));
    209222                    } else if (parser.getLocalName().equals("param")) {
    210223                        if (params == null) {
    211                                 params = new HashMap<String, String>();
     224                            params = new HashMap<String, String>();
    212225                        }
    213226                        params.put(parser.getAttributeValue(null, "name"), parser.getAttributeValue(null, "value"));
     
    217230                } else if (event == XMLStreamConstants.END_ELEMENT) {
    218231                    if (parser.getLocalName().equals("link")) {
    219                         break;
     232                        break;
    220233                    }
    221234                }
    222235            }
    223                 link = new LinkPost(name, href, headers, params);
     236            link = new LinkPost(name, href, headers, params);
    224237        } else {
    225                 link = new Link(name, href);
    226                 jumpToEnd();
     238            link = new Link(name, href);
     239            jumpToEnd();
    227240        }
    228241        return link;
  • applications/editors/josm/plugins/tag2link/src/org/openstreetmap/josm/plugins/tag2link/listeners/AbstractIPrimitivePopupListener.java

    r26921 r28547  
    11//    JOSM tag2link plugin.
    2 //    Copyright (C) 2011 Don-vip & FrViPofm
     2//    Copyright (C) 2011-2012 Don-vip & FrViPofm
    33//
    44//    This program is free software: you can redistribute it and/or modify
  • applications/editors/josm/plugins/tag2link/src/org/openstreetmap/josm/plugins/tag2link/listeners/AbstractPopupListener.java

    r26964 r28547  
    11//    JOSM tag2link plugin.
    2 //    Copyright (C) 2011 Don-vip & FrViPofm
     2//    Copyright (C) 2011-2012 Don-vip & FrViPofm
    33//
    44//    This program is free software: you can redistribute it and/or modify
     
    5454   
    5555    protected void addLink(JPopupMenu popup, Link link) {
    56         JosmAction action = null;
    57         if (link.url.matches("mailto:.*")) {
    58                 action = new OpenMailAction(link);
    59         } else {
    60                 action = new OpenLinkAction(link);
    61         }
     56        JosmAction action = null;
     57        if (link.url.matches("mailto:.*")) {
     58            action = new OpenMailAction(link);
     59        } else {
     60            action = new OpenLinkAction(link);
     61        }
    6262
    6363        itemList.add(popup.add(action));
  • applications/editors/josm/plugins/tag2link/src/org/openstreetmap/josm/plugins/tag2link/listeners/MembershipPopupListener.java

    r26922 r28547  
    11//    JOSM tag2link plugin.
    2 //    Copyright (C) 2011 Don-vip & FrViPofm
     2//    Copyright (C) 2011-2012 Don-vip & FrViPofm
    33//
    44//    This program is free software: you can redistribute it and/or modify
  • applications/editors/josm/plugins/tag2link/src/org/openstreetmap/josm/plugins/tag2link/listeners/PropertyPopupListener.java

    r26922 r28547  
    11//    JOSM tag2link plugin.
    2 //    Copyright (C) 2011 Don-vip & FrViPofm
     2//    Copyright (C) 2011-2012 Don-vip & FrViPofm
    33//
    44//    This program is free software: you can redistribute it and/or modify
  • applications/editors/josm/plugins/tag2link/src/org/openstreetmap/josm/plugins/tag2link/listeners/RelationPopupListener.java

    r26921 r28547  
    11//    JOSM tag2link plugin.
    2 //    Copyright (C) 2011 Don-vip & FrViPofm
     2//    Copyright (C) 2011-2012 Don-vip & FrViPofm
    33//
    44//    This program is free software: you can redistribute it and/or modify
  • applications/editors/josm/plugins/tag2link/src/org/openstreetmap/josm/plugins/tag2link/listeners/SelectionPopupListener.java

    r26921 r28547  
    11//    JOSM tag2link plugin.
    2 //    Copyright (C) 2011 Don-vip & FrViPofm
     2//    Copyright (C) 2011-2012 Don-vip & FrViPofm
    33//
    44//    This program is free software: you can redistribute it and/or modify
Note: See TracChangeset for help on using the changeset viewer.