Changeset 10408 in osm
- Timestamp:
- 2008-09-02T18:23:45+02:00 (16 years ago)
- Location:
- applications/editors/josm/plugins/validator/src/org/openstreetmap/josm/plugins/validator
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/validator/src/org/openstreetmap/josm/plugins/validator/ErrorTreePanel.java
r9755 r10408 48 48 this.getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION); 49 49 setErrorList(errors); 50 50 } 51 51 52 52 public String getToolTipText(MouseEvent e) { -
applications/editors/josm/plugins/validator/src/org/openstreetmap/josm/plugins/validator/tests/TagChecker.java
r9909 r10408 8 8 import java.awt.GridBagLayout; 9 9 import java.io.BufferedReader; 10 import java.io.File;11 import java.io.FileInputStream;12 import java.io.FileNotFoundException;13 import java.io.FileReader;14 10 import java.io.InputStream; 15 11 import java.io.InputStreamReader; … … 48 44 import org.openstreetmap.josm.gui.preferences.TaggingPresetPreference; 49 45 import org.openstreetmap.josm.gui.tagging.TaggingPreset; 46 import org.openstreetmap.josm.io.MirroredInputStream; 50 47 import org.openstreetmap.josm.Main; 51 48 import org.openstreetmap.josm.plugins.validator.OSMValidatorPlugin; … … 153 150 * @throws IOException 154 151 */ 155 private static void initializeData() throws FileNotFoundException,IOException152 private static void initializeData() throws IOException 156 153 { 157 154 spellCheckKeyData = new HashMap<String, String>(); … … 177 174 for(String source: sources.split(";")) 178 175 { 179 File sourceFile = null;180 176 try 181 177 { 182 sourceFile = Util.mirror(new URL(source), Util.getPluginDir(), -1); 183 } 184 catch(java.net.MalformedURLException e) {} 185 if( sourceFile == null || !sourceFile.exists() ) 186 { 187 sourceFile = new File(source); 188 if( sourceFile == null || !sourceFile.exists() ) 189 { 190 errorSources += source + "\n"; 191 continue; 192 } 193 } 194 195 BufferedReader reader = new BufferedReader(new FileReader(sourceFile)); 196 197 String okValue = null; 198 Boolean tagcheckerfile = false; 199 do 200 { 201 String line = reader.readLine(); 202 if( line == null || (!tagcheckerfile && line.length() == 0) ) 203 break; 204 if( line.startsWith("#") ) 205 { 206 if(line.startsWith("# JOSM TagChecker")) 207 tagcheckerfile = true; 208 } 209 else if(tagcheckerfile) 210 { 211 if(line.length() > 0) 212 { 213 CheckerData d = new CheckerData(); 214 String err = d.getData(line); 215 216 if(err == null) 217 checkerData.add(d); 218 else 219 System.err.println(tr("Invalid tagchecker line - {0}: {1}", err, line)); 220 } 221 } 222 else if( line.charAt(0) == '+' ) 223 { 224 okValue = line.substring(1); 225 } 226 else if( line.charAt(0) == '-' && okValue != null ) 227 { 228 spellCheckKeyData.put(line.substring(1), okValue); 229 } 230 else 231 { 232 System.err.println(tr("Invalid spellcheck line: {0}", line)); 233 } 234 } 235 while( true ); 178 MirroredInputStream s = new MirroredInputStream(source, Util.getPluginDir(), -1); 179 InputStreamReader r; 180 try 181 { 182 r = new InputStreamReader(s, "UTF-8"); 183 } 184 catch (UnsupportedEncodingException e) 185 { 186 r = new InputStreamReader(s); 187 } 188 BufferedReader reader = new BufferedReader(r); 189 190 String okValue = null; 191 Boolean tagcheckerfile = false; 192 String line; 193 while((line = reader.readLine()) != null && (tagcheckerfile || line.length() != 0)) 194 { 195 if(line.startsWith("#")) 196 { 197 if(line.startsWith("# JOSM TagChecker")) 198 tagcheckerfile = true; 199 } 200 else if(tagcheckerfile) 201 { 202 if(line.length() > 0) 203 { 204 CheckerData d = new CheckerData(); 205 String err = d.getData(line); 206 207 if(err == null) 208 checkerData.add(d); 209 else 210 System.err.println(tr("Invalid tagchecker line - {0}: {1}", err, line)); 211 } 212 } 213 else if(line.charAt(0) == '+') 214 { 215 okValue = line.substring(1); 216 } 217 else if(line.charAt(0) == '-' && okValue != null) 218 { 219 spellCheckKeyData.put(line.substring(1), okValue); 220 } 221 else 222 { 223 System.err.println(tr("Invalid spellcheck line: {0}", line)); 224 } 225 } 226 } 227 catch (IOException e) 228 { 229 errorSources += source + "\n"; 230 } 236 231 } 237 232 238 233 if( errorSources.length() > 0 ) 239 throw new IOException( tr("Could not downloaddata file(s):\n{0}", errorSources) );234 throw new IOException( tr("Could not access data file(s):\n{0}", errorSources) ); 240 235 } 241 236 … … 251 246 252 247 Collection<TaggingPreset> presets = TaggingPresetPreference.taggingPresets; 253 if( presets == null || presets.isEmpty() ) 254 { 255 // Skip re-reading presets if there are none available 256 return; 257 } 258 259 presetsValueData = new Bag<String, String>(); 260 readPresetFromPreferences(); 261 } 262 248 if(presets != null) 249 { 250 presetsValueData = new Bag<String, String>(); 251 for(TaggingPreset p : presets) 252 { 253 for(TaggingPreset.Item i : p.data) 254 { 255 if(i instanceof TaggingPreset.Combo) 256 { 257 TaggingPreset.Combo combo = (TaggingPreset.Combo) i; 258 for(String value : combo.values.split(",")) 259 presetsValueData.add(combo.key, value); 260 } 261 else if(i instanceof TaggingPreset.Key) 262 { 263 TaggingPreset.Key k = (TaggingPreset.Key) i; 264 presetsValueData.add(k.key, k.value); 265 } 266 } 267 } 268 } 269 } 263 270 264 271 @Override … … 345 352 withErrors.add(p, "FIXME"); 346 353 } 347 }348 }349 }350 351 /**352 * Parse an anotation preset from a stream353 *354 * @param inStream The stream of the anotstion preset355 * @throws SAXException356 */357 public static void readPresets(InputStream inStream) throws SAXException358 {359 BufferedReader in = null;360 try361 {362 in = new BufferedReader(new InputStreamReader(inStream, "UTF-8"));363 }364 catch (UnsupportedEncodingException e)365 {366 e.printStackTrace();367 in = new BufferedReader(new InputStreamReader(inStream));368 }369 370 XmlObjectParser parser = new XmlObjectParser();371 parser.mapOnStart("item", TaggingPreset.class);372 parser.map("text", TaggingPreset.Text.class);373 parser.map("check", TaggingPreset.Check.class);374 parser.map("combo", TaggingPreset.Combo.class);375 parser.map("label", TaggingPreset.Label.class);376 parser.map("key", TaggingPreset.Key.class);377 parser.start(in);378 379 while(parser.hasNext())380 {381 Object obj = parser.next();382 if (obj instanceof TaggingPreset.Combo) {383 TaggingPreset.Combo combo = (TaggingPreset.Combo)obj;384 for(String value : combo.values.split(",") )385 presetsValueData.add(combo.key, value);386 }387 }388 }389 390 /**391 * Reads the tagging presets392 */393 public static void readPresetFromPreferences()394 {395 String allAnnotations = Main.pref.get("taggingpreset.sources");396 if(allAnnotations == null | allAnnotations.length() == 0)397 return;398 for(String source : allAnnotations.split(";"))399 {400 InputStream in = null;401 try402 {403 if (source.startsWith("http") || source.startsWith("ftp") || source.startsWith("file"))404 in = new URL(source).openStream();405 else if (source.startsWith("resource://"))406 in = Main.class.getResourceAsStream(source.substring("resource:/".length()));407 else408 in = new FileInputStream(source);409 readPresets(in);410 in.close();411 }412 catch (IOException e)413 {414 // Error already reported by JOSM415 }416 catch (SAXException e)417 {418 // Error already reported by JOSM419 354 } 420 355 } -
applications/editors/josm/plugins/validator/src/org/openstreetmap/josm/plugins/validator/util/Util.java
r9891 r10408 6 6 import java.awt.event.ActionListener; 7 7 import java.awt.geom.Point2D; 8 import java.io.*;9 import java.net.URL;10 import java.net.URLConnection;11 8 import java.util.*; 12 9 … … 73 70 this.time = time; 74 71 } 75 }76 77 78 /**79 * Loads a text file in a String80 *81 * @param resource The URL of the file82 * @return A String with the file contents83 * @throws IOException when error reading the file84 */85 public static String loadFile(URL resource) throws IOException86 {87 BufferedReader in = null;88 try89 {90 in = new BufferedReader(new InputStreamReader(resource.openStream()));91 StringBuilder sb = new StringBuilder();92 for (String line = in.readLine(); line != null; line = in.readLine())93 {94 sb.append(line);95 sb.append('\n');96 }97 return sb.toString();98 }99 finally100 {101 if( in != null )102 {103 try {104 in.close();105 } catch (IOException e) {106 e.printStackTrace();107 }108 }109 }110 }111 112 /**113 * Mirrors a file to a local file.114 * <p>115 * The file mirrored is only downloaded if it has been more than one day since last download116 *117 * @param url The URL of the remote file118 * @param destDir The destionation dir of the mirrored file119 * @param maxTime The time interval, in seconds, to check if the file changed. If less than 0, it defaults to 1 week120 * @return The local file121 */122 public static File mirror(URL url, String destDir, long maxTime)123 {124 if( url.getProtocol().equals("file") )125 return new File(url.toString() ) ;126 127 String localPath = Main.pref.get( PreferenceEditor.PREFIX + ".mirror." + url);128 File oldFile = null;129 if( localPath != null && localPath.length() > 0)130 {131 StringTokenizer st = new StringTokenizer(localPath, ";");132 long checkDate = Long.parseLong(st.nextToken());133 localPath = st.nextToken();134 oldFile = new File(localPath);135 maxTime = (maxTime <= 0) ? 7 * 24 * 60 * 60 * 1000 : maxTime * 1000;136 if( System.currentTimeMillis() - checkDate < maxTime )137 {138 if( oldFile.exists() )139 return oldFile;140 }141 }142 143 File destDirFile = new File(destDir);144 if( !destDirFile.exists() )145 destDirFile.mkdirs();146 147 localPath = destDir + System.currentTimeMillis() + "-" + new File(url.getPath()).getName();148 BufferedOutputStream bos = null;149 BufferedInputStream bis = null;150 try151 {152 URLConnection conn = url.openConnection();153 conn.setConnectTimeout(5000);154 bis = new BufferedInputStream(conn.getInputStream());155 bos = new BufferedOutputStream( new FileOutputStream(localPath) );156 byte[] buffer = new byte[4096];157 int length;158 while( (length = bis.read( buffer )) > -1 )159 {160 bos.write( buffer, 0, length );161 }162 }163 catch(IOException ioe)164 {165 if( oldFile != null )166 return oldFile;167 else168 return null;169 }170 finally171 {172 if( bis != null )173 {174 try {175 bis.close();176 } catch (IOException e) {177 e.printStackTrace();178 }179 }180 if( bos != null )181 {182 try {183 bos.close();184 } catch (IOException e) {185 e.printStackTrace();186 }187 }188 }189 190 Main.pref.put( PreferenceEditor.PREFIX + ".mirror." + url, System.currentTimeMillis() + ";" + localPath);191 192 if( oldFile != null )193 oldFile.delete();194 195 return new File(localPath);196 72 } 197 73
Note:
See TracChangeset
for help on using the changeset viewer.