Changeset 7276 in josm for trunk/src


Ignore:
Timestamp:
2014-06-30T01:06:11+02:00 (6 years ago)
Author:
Don-vip
Message:

fix #9518 - Automatically reload MapCSS tagchecker validator rules (configurable with validator.auto_reload_local_rules property), improve javadoc

Location:
trunk/src/org/openstreetmap/josm
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/validation/tests/MapCSSTagChecker.java

    r7275 r7276  
    4545import org.openstreetmap.josm.gui.mappaint.mapcss.parsergen.MapCSSParser;
    4646import org.openstreetmap.josm.gui.mappaint.mapcss.parsergen.ParseException;
     47import org.openstreetmap.josm.gui.preferences.SourceEntry;
    4748import org.openstreetmap.josm.gui.preferences.validator.ValidatorPreference;
    4849import org.openstreetmap.josm.gui.preferences.validator.ValidatorTagCheckerRulesPreference;
     
    539540     * @throws ParseException if the config file does not match MapCSS syntax
    540541     * @throws IOException if any I/O error occurs
    541      * @since
     542     * @since 7275
    542543     */
    543     public void addMapCSS(String url) throws ParseException, IOException {
     544    public synchronized void addMapCSS(String url) throws ParseException, IOException {
    544545        CheckParameterUtil.ensureParameterNotNull(url, "url");
    545         try (InputStream s = new CachedFile(url).getInputStream()) {
    546             checks.putAll(url, TagCheck.readMapCSS(new BufferedReader(UTFInputStreamReader.create(s))));
     546        CachedFile cache = new CachedFile(url);
     547        try (InputStream s = cache.getInputStream()) {
     548            List<TagCheck> tagchecks = TagCheck.readMapCSS(new BufferedReader(UTFInputStreamReader.create(s)));
     549            checks.remove(url);
     550            checks.putAll(url, tagchecks);
    547551        }
    548552    }
     
    551555    public synchronized void initialize() throws Exception {
    552556        checks.clear();
    553         for (String i : new ValidatorTagCheckerRulesPreference.RulePrefHelper().getActiveUrls()) {
     557        for (SourceEntry source : new ValidatorTagCheckerRulesPreference.RulePrefHelper().get()) {
     558            if (!source.active) {
     559                continue;
     560            }
     561            String i = source.url;
    554562            try {
    555563                if (i.startsWith("resource:")) {
     
    559567                }
    560568                addMapCSS(i);
     569                if (Main.pref.getBoolean("validator.auto_reload_local_rules", true) && source.isLocal()) {
     570                    try {
     571                        Main.fileWatcher.registerValidatorRule(source);
     572                    } catch (IOException e) {
     573                        Main.error(e);
     574                    }
     575                }
    561576            } catch (IOException ex) {
    562577                Main.warn(tr("Failed to add {0} to tag checker", i));
  • trunk/src/org/openstreetmap/josm/gui/mappaint/MapPaintStyles.java

    r7256 r7276  
    44import static org.openstreetmap.josm.tools.I18n.tr;
    55
     6import java.io.File;
    67import java.io.IOException;
    78import java.io.InputStreamReader;
     
    164165        List<String> dirs = new LinkedList<>();
    165166
    166         String sourceDir = source.getLocalSourceDir();
     167        File sourceDir = source.getLocalSourceDir();
    167168        if (sourceDir != null) {
    168             dirs.add(sourceDir);
     169            dirs.add(sourceDir.getPath());
    169170        }
    170171
    171172        Collection<String> prefIconDirs = Main.pref.getCollection("mappaint.icon.sources");
    172         for(String fileset : prefIconDirs)
    173         {
     173        for (String fileset : prefIconDirs) {
    174174            String[] a;
    175175            if(fileset.indexOf('=') >= 0) {
  • trunk/src/org/openstreetmap/josm/gui/preferences/SourceEntry.java

    r7083 r7276  
    1111/**
    1212 * A source entry primarily used to save the user's selection of mappaint styles,
    13  * but also for preset sources.
     13 * but also for preset sources or validator rules.
     14 * @since 3796
    1415 */
    1516public class SourceEntry {
    1617
    1718    /**
    18      *  A URL can be anything that MirroredInputStream understands, i.e.
     19     *  A URL can be anything that CachedFile understands, i.e.
    1920     *  a local file, http://, or a file from the current jar
    2021     */
     
    5354    public boolean active;
    5455
     56    /**
     57     * Constructs a new {@code SourceEntry}.
     58     * @param url URL that {@link org.openstreetmap.josm.io.CachedFile} understands
     59     * @param isZip if url is a zip file and the resource is inside the zip file
     60     * @param zipEntryPath If {@code isZip} is {@code true}, denotes the path inside the zip file
     61     * @param name Source name
     62     * @param title title that can be used as menu entry
     63     * @param active boolean flag that can be used to turn the source on or off at runtime
     64     * @see #url
     65     * @see #isZip
     66     * @see #zipEntryPath
     67     * @see #name
     68     * @see #title
     69     * @see #active
     70     */
    5571    public SourceEntry(String url, boolean isZip, String zipEntryPath, String name, String title, boolean active) {
    5672        this.url = url;
     
    6278    }
    6379
     80    /**
     81     * Constructs a new {@code SourceEntry}.
     82     * @param url URL that {@link org.openstreetmap.josm.io.CachedFile} understands
     83     * @param name Source name
     84     * @param title title that can be used as menu entry
     85     * @param active boolean flag that can be used to turn the source on or off at runtime
     86     * @see #url
     87     * @see #name
     88     * @see #title
     89     * @see #active
     90     */
    6491    public SourceEntry(String url, String name, String title, Boolean active) {
    6592        this(url, false, null, name, title, active);
    6693    }
    6794
     95    /**
     96     * Constructs a new {@code SourceEntry}.
     97     * @param e existing source entry to copy
     98     */
    6899    public SourceEntry(SourceEntry e) {
    69100        this.url = e.url;
     
    118149
    119150    /**
    120      * extract file part from url, e.g.:
    121      * http://www.test.com/file.xml?format=text --&gt; file.xml
     151     * Extracts file part from url, e.g.:
     152     * <code>http://www.test.com/file.xml?format=text --&gt; file.xml</code>
     153     * @return The filename part of the URL
    122154     */
    123155    public String getFileNamePart() {
     
    141173    }
    142174
     175    /**
     176     * Determines if this source denotes a file on a local filesystem.
     177     * @return {@code true} if the source is a local file
     178     */
    143179    public boolean isLocal() {
    144180        if (url.startsWith("http://") || url.startsWith("https://") || url.startsWith("resource://"))
     
    147183    }
    148184
    149     public String getLocalSourceDir() {
     185    /**
     186     * Return the source directory, only for local files.
     187     * @return The source directory, or {@code null} if this file isn't local, or does not have a parent
     188     * @since 7276
     189     */
     190    public File getLocalSourceDir() {
    150191        if (!isLocal())
    151192            return null;
    152         File f = new File(url);
    153         File dir = f.getParentFile();
    154         if (dir == null)
    155             return null;
    156         return dir.getPath();
     193        return new File(url).getParentFile();
    157194    }
    158195
  • trunk/src/org/openstreetmap/josm/io/FileWatcher.java

    r7248 r7276  
    1616
    1717import org.openstreetmap.josm.Main;
     18import org.openstreetmap.josm.data.validation.OsmValidator;
     19import org.openstreetmap.josm.data.validation.tests.MapCSSTagChecker;
    1820import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.MapPaintStyleLoader;
    1921import org.openstreetmap.josm.gui.mappaint.StyleSource;
     22import org.openstreetmap.josm.gui.mappaint.mapcss.parsergen.ParseException;
     23import org.openstreetmap.josm.gui.preferences.SourceEntry;
    2024import org.openstreetmap.josm.tools.CheckParameterUtil;
    2125
     
    2933
    3034    private final Map<Path, StyleSource> styleMap = new HashMap<>();
     35    private final Map<Path, SourceEntry> ruleMap = new HashMap<>();
    3136
    3237    /**
     
    5560     */
    5661    public void registerStyleSource(StyleSource style) throws IOException {
    57         CheckParameterUtil.ensureParameterNotNull(style, "style");
     62        register(style, styleMap);
     63    }
     64
     65    /**
     66     * Registers a validator rule for local file changes, allowing dynamic reloading.
     67     * @param rule The rule to watch
     68     * @throws IllegalArgumentException if {@code rule} is null or if it does not provide a local file
     69     * @throws IllegalStateException if the watcher service failed to start
     70     * @throws IOException if an I/O error occurs
     71     * @since 7276
     72     */
     73    public void registerValidatorRule(SourceEntry rule) throws IOException {
     74        register(rule, ruleMap);
     75    }
     76
     77    private <T extends SourceEntry> void register(T obj, Map<Path, T> map) throws IOException {
     78        CheckParameterUtil.ensureParameterNotNull(obj, "obj");
    5879        if (watcher == null) {
    5980            throw new IllegalStateException("File watcher is not available");
    6081        }
    61         CachedFile cf = style.getCachedFile();
    62         // Get underlying file
    63         File file = cf.getFile();
    64         if (file == null) {
    65             throw new IllegalArgumentException("Style "+style+" does not have a local file");
    66         }
     82        // Get local file, as this method is only called for local style sources
     83        File file = new File(obj.url);
    6784        // Get parent directory as WatchService allows only to monitor directories, not single files
    6885        File dir = file.getParentFile();
    6986        if (dir == null) {
    70             throw new IllegalArgumentException("Style "+style+" does not have a parent directory");
     87            throw new IllegalArgumentException("Resource "+obj+" does not have a parent directory");
    7188        }
    7289        synchronized(this) {
     
    7491            // (it returns the same key so it should not send events several times)
    7592            dir.toPath().register(watcher, StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_CREATE);
    76             styleMap.put(file.toPath(), style);
     93            map.put(file.toPath(), obj);
    7794        }
    7895    }
     
    115132                synchronized(this) {
    116133                    StyleSource style = styleMap.get(fullPath);
     134                    SourceEntry rule = ruleMap.get(fullPath);
    117135                    if (style != null) {
    118136                        Main.info("Map style "+style.getDisplayString()+" has been modified. Reloading style...");
    119                         //style.loadStyleSource();
    120137                        Main.worker.submit(new MapPaintStyleLoader(Collections.singleton(style)));
     138                    } else if (rule != null) {
     139                        Main.info("Validator rule "+rule.getDisplayString()+" has been modified. Reloading rule...");
     140                        MapCSSTagChecker tagChecker = OsmValidator.getTest(MapCSSTagChecker.class);
     141                        if (tagChecker != null) {
     142                            try {
     143                                tagChecker.addMapCSS(rule.url);
     144                            } catch (IOException | ParseException e) {
     145                                Main.warn(e);
     146                            }
     147                        }
    121148                    } else if (Main.isDebugEnabled()) {
    122149                        Main.debug("Received "+kind.name()+" event for unregistered file: "+fullPath);
  • trunk/src/org/openstreetmap/josm/tools/MultiMap.java

    r7275 r7276  
    213213
    214214    @Override
     215    public int hashCode() {
     216        return map.hashCode();
     217    }
     218
     219    @Override
     220    public boolean equals(Object obj) {
     221        if (this == obj)
     222            return true;
     223        if (obj == null)
     224            return false;
     225        if (!(obj instanceof MultiMap))
     226            return false;
     227        return map.equals(((MultiMap<?,?>) obj).map);
     228    }
     229
     230    @Override
    215231    public String toString() {
    216232        List<String> entries = new ArrayList<>(map.size());
Note: See TracChangeset for help on using the changeset viewer.