Changeset 7276 in josm
- Timestamp:
- 2014-06-30T01:06:11+02:00 (9 years ago)
- 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 45 45 import org.openstreetmap.josm.gui.mappaint.mapcss.parsergen.MapCSSParser; 46 46 import org.openstreetmap.josm.gui.mappaint.mapcss.parsergen.ParseException; 47 import org.openstreetmap.josm.gui.preferences.SourceEntry; 47 48 import org.openstreetmap.josm.gui.preferences.validator.ValidatorPreference; 48 49 import org.openstreetmap.josm.gui.preferences.validator.ValidatorTagCheckerRulesPreference; … … 539 540 * @throws ParseException if the config file does not match MapCSS syntax 540 541 * @throws IOException if any I/O error occurs 541 * @since 542 * @since 7275 542 543 */ 543 public void addMapCSS(String url) throws ParseException, IOException {544 public synchronized void addMapCSS(String url) throws ParseException, IOException { 544 545 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); 547 551 } 548 552 } … … 551 555 public synchronized void initialize() throws Exception { 552 556 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; 554 562 try { 555 563 if (i.startsWith("resource:")) { … … 559 567 } 560 568 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 } 561 576 } catch (IOException ex) { 562 577 Main.warn(tr("Failed to add {0} to tag checker", i)); -
trunk/src/org/openstreetmap/josm/gui/mappaint/MapPaintStyles.java
r7256 r7276 4 4 import static org.openstreetmap.josm.tools.I18n.tr; 5 5 6 import java.io.File; 6 7 import java.io.IOException; 7 8 import java.io.InputStreamReader; … … 164 165 List<String> dirs = new LinkedList<>(); 165 166 166 StringsourceDir = source.getLocalSourceDir();167 File sourceDir = source.getLocalSourceDir(); 167 168 if (sourceDir != null) { 168 dirs.add(sourceDir );169 dirs.add(sourceDir.getPath()); 169 170 } 170 171 171 172 Collection<String> prefIconDirs = Main.pref.getCollection("mappaint.icon.sources"); 172 for(String fileset : prefIconDirs) 173 { 173 for (String fileset : prefIconDirs) { 174 174 String[] a; 175 175 if(fileset.indexOf('=') >= 0) { -
trunk/src/org/openstreetmap/josm/gui/preferences/SourceEntry.java
r7083 r7276 11 11 /** 12 12 * 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 14 15 */ 15 16 public class SourceEntry { 16 17 17 18 /** 18 * A URL can be anything that MirroredInputStreamunderstands, i.e.19 * A URL can be anything that CachedFile understands, i.e. 19 20 * a local file, http://, or a file from the current jar 20 21 */ … … 53 54 public boolean active; 54 55 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 */ 55 71 public SourceEntry(String url, boolean isZip, String zipEntryPath, String name, String title, boolean active) { 56 72 this.url = url; … … 62 78 } 63 79 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 */ 64 91 public SourceEntry(String url, String name, String title, Boolean active) { 65 92 this(url, false, null, name, title, active); 66 93 } 67 94 95 /** 96 * Constructs a new {@code SourceEntry}. 97 * @param e existing source entry to copy 98 */ 68 99 public SourceEntry(SourceEntry e) { 69 100 this.url = e.url; … … 118 149 119 150 /** 120 * extract file part from url, e.g.: 121 * http://www.test.com/file.xml?format=text --> file.xml 151 * Extracts file part from url, e.g.: 152 * <code>http://www.test.com/file.xml?format=text --> file.xml</code> 153 * @return The filename part of the URL 122 154 */ 123 155 public String getFileNamePart() { … … 141 173 } 142 174 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 */ 143 179 public boolean isLocal() { 144 180 if (url.startsWith("http://") || url.startsWith("https://") || url.startsWith("resource://")) … … 147 183 } 148 184 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() { 150 191 if (!isLocal()) 151 192 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(); 157 194 } 158 195 -
trunk/src/org/openstreetmap/josm/io/FileWatcher.java
r7248 r7276 16 16 17 17 import org.openstreetmap.josm.Main; 18 import org.openstreetmap.josm.data.validation.OsmValidator; 19 import org.openstreetmap.josm.data.validation.tests.MapCSSTagChecker; 18 20 import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.MapPaintStyleLoader; 19 21 import org.openstreetmap.josm.gui.mappaint.StyleSource; 22 import org.openstreetmap.josm.gui.mappaint.mapcss.parsergen.ParseException; 23 import org.openstreetmap.josm.gui.preferences.SourceEntry; 20 24 import org.openstreetmap.josm.tools.CheckParameterUtil; 21 25 … … 29 33 30 34 private final Map<Path, StyleSource> styleMap = new HashMap<>(); 35 private final Map<Path, SourceEntry> ruleMap = new HashMap<>(); 31 36 32 37 /** … … 55 60 */ 56 61 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"); 58 79 if (watcher == null) { 59 80 throw new IllegalStateException("File watcher is not available"); 60 81 } 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); 67 84 // Get parent directory as WatchService allows only to monitor directories, not single files 68 85 File dir = file.getParentFile(); 69 86 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"); 71 88 } 72 89 synchronized(this) { … … 74 91 // (it returns the same key so it should not send events several times) 75 92 dir.toPath().register(watcher, StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_CREATE); 76 styleMap.put(file.toPath(), style);93 map.put(file.toPath(), obj); 77 94 } 78 95 } … … 115 132 synchronized(this) { 116 133 StyleSource style = styleMap.get(fullPath); 134 SourceEntry rule = ruleMap.get(fullPath); 117 135 if (style != null) { 118 136 Main.info("Map style "+style.getDisplayString()+" has been modified. Reloading style..."); 119 //style.loadStyleSource();120 137 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 } 121 148 } else if (Main.isDebugEnabled()) { 122 149 Main.debug("Received "+kind.name()+" event for unregistered file: "+fullPath); -
trunk/src/org/openstreetmap/josm/tools/MultiMap.java
r7275 r7276 213 213 214 214 @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 215 231 public String toString() { 216 232 List<String> entries = new ArrayList<>(map.size());
Note: See TracChangeset
for help on using the changeset viewer.