Changeset 33493 in osm for applications/editors/josm/plugins/imagery-xml-bounds/src
- Timestamp:
- 2017-08-13T15:33:19+02:00 (7 years ago)
- Location:
- applications/editors/josm/plugins/imagery-xml-bounds/src
- Files:
-
- 5 added
- 13 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/imagery-xml-bounds/src/net/boplicity/xmleditor/XmlEditorKit.java
r30735 r33493 26 26 27 27 private static final long serialVersionUID = 2969169649596107757L; 28 private ViewFactory xmlViewFactory;28 private final ViewFactory xmlViewFactory; 29 29 30 30 /** -
applications/editors/josm/plugins/imagery-xml-bounds/src/org/openstreetmap/josm/plugins/imageryxmlbounds/ImageryXmlBoundsPlugin.java
r32287 r33493 43 43 44 44 /** 45 * Action showing bounds of the current selection46 */47 private final ShowBoundsSelectionAction selectionAction = new ShowBoundsSelectionAction();48 49 /**50 45 * Class modifying the Imagery preferences panel 51 46 */ … … 54 49 /** 55 50 * Initializes the plugin. 56 * @param info 51 * @param info plugin information 57 52 */ 58 53 public ImageryXmlBoundsPlugin(PluginInformation info) { … … 63 58 ExtensionFileFilter.addExporterFirst(new XmlBoundsExporter()); 64 59 // Initialize the selection action 60 ShowBoundsSelectionAction selectionAction = new ShowBoundsSelectionAction(); 65 61 DataSet.addSelectionListener(selectionAction); 66 62 Main.toolbar.register(selectionAction); -
applications/editors/josm/plugins/imagery-xml-bounds/src/org/openstreetmap/josm/plugins/imageryxmlbounds/XmlBoundsConstants.java
r33272 r33493 3 3 4 4 import static org.openstreetmap.josm.tools.I18n.tr; 5 6 import javax.swing.ImageIcon;7 5 8 6 import org.openstreetmap.josm.actions.ExtensionFileFilter; -
applications/editors/josm/plugins/imagery-xml-bounds/src/org/openstreetmap/josm/plugins/imageryxmlbounds/XmlBoundsLayer.java
r33272 r33493 22 22 import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeListener; 23 23 import org.openstreetmap.josm.gui.layer.OsmDataLayer; 24 import org.openstreetmap.josm.tools.ImageProvider;25 24 import org.openstreetmap.josm.plugins.imageryxmlbounds.actions.BoundsLayerSaveAction; 26 25 import org.openstreetmap.josm.plugins.imageryxmlbounds.actions.BoundsLayerSaveAsAction; 27 26 import org.openstreetmap.josm.plugins.imageryxmlbounds.actions.ShowBoundsAction; 27 import org.openstreetmap.josm.tools.ImageProvider; 28 28 29 29 /** … … 52 52 } 53 53 result.add(new ShowBoundsAction(this)); 54 return result.toArray(new Action[ 0]);54 return result.toArray(new Action[result.size()]); 55 55 } 56 56 … … 71 71 /** 72 72 * Constructs a new {@code XmlBoundsLayer}. 73 * @param data data 73 * @param data data set 74 74 */ 75 75 public XmlBoundsLayer(DataSet data) { … … 79 79 /** 80 80 * Constructs a new {@code XmlBoundsLayer}. 81 * @param data data 81 * @param data data set 82 82 * @param name Layer name 83 83 * @param associatedFile Associated file (can be null) -
applications/editors/josm/plugins/imagery-xml-bounds/src/org/openstreetmap/josm/plugins/imageryxmlbounds/actions/BoundsLayerSaveAction.java
r30735 r33493 35 35 public File getFile(Layer layer) { 36 36 File f = layer.getAssociatedFile(); 37 if (f != null && ! 37 if (f != null && !f.exists()) { 38 38 f = null; 39 39 } -
applications/editors/josm/plugins/imagery-xml-bounds/src/org/openstreetmap/josm/plugins/imageryxmlbounds/actions/BoundsLayerSaveAsAction.java
r30735 r33493 31 31 /** 32 32 * Opens a "File/Save as" dialog if the given layer is an XML bounds layer 33 * @param layer layer33 * @param layer current layer 34 34 * @return chosen destination file, or {@code null} 35 35 */ -
applications/editors/josm/plugins/imagery-xml-bounds/src/org/openstreetmap/josm/plugins/imageryxmlbounds/actions/ComputeBoundsAction.java
r33272 r33493 5 5 6 6 import java.awt.event.ActionEvent; 7 import java.io.IOException; 7 8 import java.io.InputStream; 8 import java.lang.Exception;9 9 import java.math.RoundingMode; 10 10 import java.text.DecimalFormat; … … 97 97 } 98 98 // Remove closed ways already inside a selected multipolygon 99 for (Iterator<Way> it = closedWays.iterator(); it.hasNext(); 99 for (Iterator<Way> it = closedWays.iterator(); it.hasNext();) { 100 100 processIterator(it); 101 101 } … … 124 124 primitives.addAll(multipolygons); 125 125 primitives.addAll(closedWays); 126 return getXml(primitives.toArray(new OsmPrimitive[ 0]));126 return getXml(primitives.toArray(new OsmPrimitive[primitives.size()])); 127 127 } 128 128 … … 175 175 } 176 176 177 protected static final String getXml(OsmPrimitive 177 protected static final String getXml(OsmPrimitive... primitives) { 178 178 List<String> entries = new ArrayList<>(); 179 179 for (OsmPrimitive p : primitives) { … … 184 184 } 185 185 } 186 return getImagery(entries.toArray(new String[ 0]));187 } 188 189 protected static final String getImagery(String 186 return getImagery(entries.toArray(new String[entries.size()])); 187 } 188 189 protected static final String getImagery(String... entries) { 190 190 String version = "UNKNOWN"; 191 191 try { 192 Properties p = new Properties(); 193 p.load(ComputeBoundsAction.class.getResourceAsStream("/REVISION")); 194 version = p.getProperty("Revision"); 195 } catch(Exception e) { 196 } 197 StringBuilder result = new StringBuilder(); 198 result.append("<?xml version=\"1.0\" encoding=\"").append(ENCODING).append("\" ?>\n"); 199 result.append("<!-- Generated with JOSM Imagery XML Plugin version ").append(version).append(" -->\n"); 200 result.append("<imagery xmlns=\"").append(XML_NAMESPACE).append("\">\n"); 192 InputStream revision = ComputeBoundsAction.class.getResourceAsStream("/REVISION"); 193 if (revision != null) { 194 Properties p = new Properties(); 195 p.load(revision); 196 version = p.getProperty("Revision"); 197 } 198 } catch(IOException e) { 199 Main.warn(e); 200 } 201 StringBuilder result = new StringBuilder(256); 202 result.append("<?xml version=\"1.0\" encoding=\"").append(ENCODING).append("\" ?>\n") 203 .append("<!-- Generated with JOSM Imagery XML Plugin version ").append(version).append(" -->\n") 204 .append("<imagery xmlns=\"").append(XML_NAMESPACE).append("\">\n"); 201 205 for (String entry : entries) { 202 result.append(entry).append( "\n");206 result.append(entry).append('\n'); 203 207 } 204 208 result.append("</imagery>"); … … 208 212 protected static final String getEntry(OsmPrimitive p, String bounds) { 209 213 return getEntry(p.get(KEY_NAME), p.get(KEY_TYPE), p.get(KEY_DEFAULT), p.get(KEY_URL), bounds, p.get(KEY_PROJECTIONS), 210 p.get(KEY_LOGO_URL), p.get(KEY_EULA), p.get(KEY_ATTR_TEXT), p.get(KEY_ATTR_URL), 214 p.get(KEY_LOGO_URL), p.get(KEY_EULA), p.get(KEY_ATTR_TEXT), p.get(KEY_ATTR_URL), p.get(KEY_TERMS_TEXT), 211 215 p.get(KEY_TERMS_URL), p.get(KEY_COUNTRY_CODE), p.get(KEY_MAX_ZOOM), p.get(KEY_MIN_ZOOM), p.get(KEY_ID), 212 216 p.get(KEY_DATE)); … … 220 224 String logoURL, String eula, String attributionText, String attributionUrl, String termsText, String termsUrl, 221 225 String countryCode, String maxZoom, String minZoom, String id, String date) { 222 StringBuilder result = new StringBuilder( );226 StringBuilder result = new StringBuilder(128); 223 227 result.append(" <entry>\n"+ 224 EIGHT_SP + simpleTag(XML_NAME, name) + "\n"+225 EIGHT_SP + simpleTag(XML_TYPE, type, "wms") + "\n"+226 EIGHT_SP + simpleTag(XML_URL, url != null ? encodeUrl(url) : "", false) + "\n"+227 bounds+ "\n");228 EIGHT_SP + simpleTag(XML_NAME, name) + '\n'+ 229 EIGHT_SP + simpleTag(XML_TYPE, type, "wms") + '\n'+ 230 EIGHT_SP + simpleTag(XML_URL, url != null ? encodeUrl(url) : "", false) + '\n'+ 231 bounds+'\n'); 228 232 if (projections != null && !projections.isEmpty()) { 229 result.append(EIGHT_SP+startTag(XML_PROJECTIONS)+ "\n");233 result.append(EIGHT_SP+startTag(XML_PROJECTIONS)+'\n'); 230 234 int i = 0; 231 235 String[] codes = projections.split(";"); … … 235 239 } 236 240 result.append(simpleTag("code", code.trim())); 237 if (i%6 == 5 || i == codes.length-1 238 result.append( "\n");241 if (i%6 == 5 || i == codes.length-1) { 242 result.append('\n'); 239 243 } 240 244 i++; 241 245 } 242 result.append(EIGHT_SP+endTag(XML_PROJECTIONS)+ "\n");246 result.append(EIGHT_SP+endTag(XML_PROJECTIONS)+'\n'); 243 247 } 244 248 if (isSet(def) && "true".equals(def)) { 245 result.append(EIGHT_SP + simpleTag(XML_DEFAULT, def) + "\n");249 result.append(EIGHT_SP + simpleTag(XML_DEFAULT, def) + '\n'); 246 250 } 247 251 if (isSet(id)) { 248 result.append(EIGHT_SP + simpleTag(XML_ID, id, false) + "\n");252 result.append(EIGHT_SP + simpleTag(XML_ID, id, false) + '\n'); 249 253 } 250 254 if (isSet(date)) { 251 result.append(EIGHT_SP + simpleTag(XML_DATE, date, false) + "\n");255 result.append(EIGHT_SP + simpleTag(XML_DATE, date, false) + '\n'); 252 256 } 253 257 if (isSet(eula)) { 254 result.append(EIGHT_SP + mandatoryTag(XML_EULA, encodeUrl(eula), false) + "\n");258 result.append(EIGHT_SP + mandatoryTag(XML_EULA, encodeUrl(eula), false) + '\n'); 255 259 } 256 260 if (isSet(attributionText)) { 257 result.append(EIGHT_SP + mandatoryTag(XML_ATTR_TEXT, attributionText) + "\n");261 result.append(EIGHT_SP + mandatoryTag(XML_ATTR_TEXT, attributionText) + '\n'); 258 262 } 259 263 if (isSet(attributionUrl)) { 260 result.append(EIGHT_SP + simpleTag(XML_ATTR_URL, encodeUrl(attributionUrl), false) + "\n");264 result.append(EIGHT_SP + simpleTag(XML_ATTR_URL, encodeUrl(attributionUrl), false) + '\n'); 261 265 } 262 266 if (isSet(termsText)) { 263 result.append(EIGHT_SP + simpleTag(XML_TERMS_TEXT, termsText) + "\n");267 result.append(EIGHT_SP + simpleTag(XML_TERMS_TEXT, termsText) + '\n'); 264 268 } 265 269 if (isSet(termsUrl)) { 266 result.append(EIGHT_SP + simpleTag(XML_TERMS_URL, encodeUrl(termsUrl), false) + "\n");270 result.append(EIGHT_SP + simpleTag(XML_TERMS_URL, encodeUrl(termsUrl), false) + '\n'); 267 271 } 268 272 if (isSet(logoURL)) { 269 result.append(EIGHT_SP + simpleTag(XML_LOGO_URL, encodeUrl(logoURL), false) + "\n");273 result.append(EIGHT_SP + simpleTag(XML_LOGO_URL, encodeUrl(logoURL), false) + '\n'); 270 274 } 271 275 if (isSet(countryCode)) { 272 result.append(EIGHT_SP + simpleTag(XML_COUNTRY_CODE, countryCode) + "\n");276 result.append(EIGHT_SP + simpleTag(XML_COUNTRY_CODE, countryCode) + '\n'); 273 277 } 274 278 if ("tms".equals(type)) { 275 279 if (isSet(maxZoom)) { 276 result.append(EIGHT_SP + simpleTag(XML_MAX_ZOOM, maxZoom) + "\n");280 result.append(EIGHT_SP + simpleTag(XML_MAX_ZOOM, maxZoom) + '\n'); 277 281 } 278 282 if (isSet(minZoom)) { 279 result.append(EIGHT_SP + simpleTag(XML_MIN_ZOOM, minZoom) + "\n");283 result.append(EIGHT_SP + simpleTag(XML_MIN_ZOOM, minZoom) + '\n'); 280 284 } 281 285 } … … 333 337 if (i > 1) { 334 338 LatLon b = way.getNode(i-1).getCoor(); 335 if (b.lat() != c.lat() && b.lon() != 339 if (b.lat() != c.lat() && b.lon() != c.lon()) { 336 340 return false; 337 341 } … … 375 379 if(j == cw.getNodesCount()) 376 380 j = 0; 377 result.append("<point ");378 result.append("lat='").append(DF.format(cw.getNode(i).getCoor().lat())).append("' ");379 result.append("lon='").append(DF.format(cw.getNode(i).getCoor().lon())).append("'/>");380 if (i%3 == 2 || i == cw.getNodesCount()-1 381 result.append( "\n");381 result.append("<point lat='") 382 .append(DF.format(cw.getNode(i).getCoor().lat())).append("' lon='") 383 .append(DF.format(cw.getNode(i).getCoor().lon())).append("'/>"); 384 if (i%3 == 2 || i == cw.getNodesCount()-1) { 385 result.append('\n'); 382 386 } 383 387 } -
applications/editors/josm/plugins/imagery-xml-bounds/src/org/openstreetmap/josm/plugins/imageryxmlbounds/actions/EditEntriesAction.java
r33272 r33493 36 36 /** 37 37 * Constructs a new {@code EditEntriesAction}. 38 * @param defaultTable table38 * @param defaultTable parent table 39 39 * @param defaultModel table model 40 40 */ … … 54 54 final XmlBoundsLayer layer = new XmlBoundsLayer( 55 55 XmlBoundsConverter.convertImageryEntries(entries)); 56 final Runnable uiStuff = new Runnable() { 57 @Override 58 public void run() { 59 Main.getLayerManager().addLayer(layer); 60 layer.onPostLoadFromFile(); 61 } 62 }; 63 GuiHelper.runInEDT(uiStuff); 56 GuiHelper.runInEDT(() -> { 57 Main.getLayerManager().addLayer(layer); 58 layer.onPostLoadFromFile(); 59 }); 64 60 } 65 61 -
applications/editors/josm/plugins/imagery-xml-bounds/src/org/openstreetmap/josm/plugins/imageryxmlbounds/actions/downloadtask/DownloadXmlBoundsTask.java
r32329 r33493 31 31 public Future<?> loadUrl(boolean newLayer, String url, 32 32 ProgressMonitor progressMonitor) { 33 downloadTask = new DownloadTask(newLayer,33 downloadTask = new InternalDownloadTask(newLayer, 34 34 new JosmServerLocationReader(url), progressMonitor); 35 35 // We need submit instead of execute so we can wait for it to finish and get the error … … 51 51 } 52 52 53 protected class DownloadTask extends DownloadOsmTask.DownloadTask {53 protected class InternalDownloadTask extends DownloadTask { 54 54 55 55 /** … … 59 59 * @param progressMonitor progress monitor 60 60 */ 61 public DownloadTask(boolean newLayer, OsmServerReader reader, ProgressMonitor progressMonitor) {61 public InternalDownloadTask(boolean newLayer, OsmServerReader reader, ProgressMonitor progressMonitor) { 62 62 super(newLayer, reader, progressMonitor); 63 63 } -
applications/editors/josm/plugins/imagery-xml-bounds/src/org/openstreetmap/josm/plugins/imageryxmlbounds/data/XmlBoundsConverter.java
r33272 r33493 24 24 * @author Don-vip 25 25 */ 26 public class XmlBoundsConverter implements XmlBoundsConstants { 26 public final class XmlBoundsConverter implements XmlBoundsConstants { 27 28 private XmlBoundsConverter() { 29 // Hide default constructor for utilities classes 30 } 27 31 28 32 /** … … 48 52 if (value != null) { 49 53 if (value instanceof Collection) { 50 StringBuilder s = new StringBuilder( "");54 StringBuilder s = new StringBuilder(); 51 55 for (Object elt : (Collection<?>)value) { 52 56 if (elt != null && elt.toString() != null && !elt.toString().isEmpty()) { 53 57 if (s.length() > 0) { 54 s.append( ";");58 s.append(';'); 55 59 } 56 s.append(elt .toString());60 s.append(elt); 57 61 } 58 62 } -
applications/editors/josm/plugins/imagery-xml-bounds/src/org/openstreetmap/josm/plugins/imageryxmlbounds/io/JosmServerLocationReader.java
r30735 r33493 4 4 import static org.openstreetmap.josm.tools.I18n.tr; 5 5 6 import java.io.IOException; 7 6 8 import org.openstreetmap.josm.data.osm.DataSet; 7 9 import org.openstreetmap.josm.gui.progress.ProgressMonitor; 8 10 import org.openstreetmap.josm.io.OsmServerReader; 9 11 import org.openstreetmap.josm.io.OsmTransferException; 12 import org.xml.sax.SAXException; 10 13 11 14 /** … … 14 17 public class JosmServerLocationReader extends OsmServerReader { 15 18 16 private String url;19 private final String url; 17 20 18 21 /** … … 30 33 progressMonitor.beginTask(tr("Contacting Server...", 10)); 31 34 return new XmlBoundsImporter().parseDataSet(url); 32 } catch ( Exception e) {35 } catch (IOException | SAXException e) { 33 36 throw new OsmTransferException(e); 34 37 } finally { -
applications/editors/josm/plugins/imagery-xml-bounds/src/org/openstreetmap/josm/plugins/imageryxmlbounds/io/ValidatingImageryReader.java
r30735 r33493 52 52 */ 53 53 public static void validate(String source) throws SAXException, IOException { 54 SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 55 Schema schema = factory.newSchema(new StreamSource(new CachedFile(XML_SCHEMA).getInputStream())); 56 schema.newValidator().validate(new StreamSource(source)); 54 SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 55 try (CachedFile xmlSchema = new CachedFile(XML_SCHEMA)) { 56 Schema schema = factory.newSchema(new StreamSource(xmlSchema.getInputStream())); 57 schema.newValidator().validate(new StreamSource(source)); 58 } 57 59 } 58 60 } -
applications/editors/josm/plugins/imagery-xml-bounds/src/org/openstreetmap/josm/plugins/imageryxmlbounds/io/XmlBoundsImporter.java
r32489 r33493 11 11 12 12 import org.openstreetmap.josm.Main; 13 import org.openstreetmap.josm.data.imagery.ImageryInfo; 13 14 import org.openstreetmap.josm.data.osm.DataSet; 14 15 import org.openstreetmap.josm.gui.progress.ProgressMonitor; … … 23 24 24 25 /** 26 * Imports a JOSM "Maps" file into an OSM data set, to allow editing it. 25 27 * @author Don-vip 26 *27 28 */ 28 29 public class XmlBoundsImporter extends FileImporter implements XmlBoundsConstants { 29 30 31 /** 32 * Constructs a new {@code XmlBoundsImporter}. 33 */ 30 34 public XmlBoundsImporter() { 31 35 super(FILE_FILTER); 32 36 } 33 37 38 /** 39 * Loads the given input source URL and returns corresponding data set. 40 * @param source input source URL 41 * @return OSM data set 42 * @throws IOException if any I/O error occurs 43 * @throws SAXException if any SAX error occurs 44 */ 34 45 public DataSet parseDataSet(final String source) throws IOException, SAXException { 35 46 return parseDataSet(source, null); 36 47 } 37 48 49 /** 50 * Loads the given input file and returns corresponding data set. 51 * @param file input file 52 * @return OSM data set 53 * @throws IOException if any I/O error occurs 54 * @throws SAXException if any SAX error occurs 55 */ 38 56 public DataSet parseDataSet(final File file) throws IOException, SAXException { 39 57 return parseDataSet(null, file); … … 41 59 42 60 protected DataSet parseDataSet(final String source, final File file) throws IOException, SAXException { 43 ImageryReader reader = null; 44 45 try { 46 reader = new ValidatingImageryReader(source != null ? source : file.getAbsolutePath()); 47 } catch (SAXException e) { 61 List<ImageryInfo> entries; 62 try (ImageryReader reader = new ValidatingImageryReader(source != null ? source : file.getAbsolutePath())) { 63 entries = reader.parse(); 64 } catch (SAXException e) { 65 if (Main.isTraceEnabled()) { 66 Main.trace(e); 67 } 48 68 if (JOptionPane.showConfirmDialog( 49 69 Main.parent, … … 55 75 } 56 76 57 reader = new ImageryReader(source != null ? source : file.getAbsolutePath()); 77 try (ImageryReader reader = new ImageryReader(source != null ? source : file.getAbsolutePath())) { 78 entries = reader.parse(); 79 } 58 80 } 59 81 60 return XmlBoundsConverter.convertImageryEntries( reader.parse());82 return XmlBoundsConverter.convertImageryEntries(entries); 61 83 } 62 84 63 85 protected void importData(final String source, final String layerName, final File file, ProgressMonitor progressMonitor) 64 throws IOException , IllegalDataException{86 throws IOException { 65 87 try { 66 88 final DataSet dataSet = parseDataSet(source, file); 67 89 final XmlBoundsLayer layer = new XmlBoundsLayer(dataSet, source != null ? layerName : file.getName(), file); 68 Runnable uiStuff = new Runnable() { 69 @Override 70 public void run() { 71 if (dataSet.allPrimitives().isEmpty()) { 72 JOptionPane.showMessageDialog( 73 Main.parent, tr("No data found in file {0}.", source != null ? source : file.getPath()), 74 tr("Open Imagery XML file"), JOptionPane.INFORMATION_MESSAGE); 75 } 76 Main.getLayerManager().addLayer(layer); 77 layer.onPostLoadFromFile(); 78 } 79 }; 80 GuiHelper.runInEDT(uiStuff); 90 GuiHelper.runInEDT(() -> { 91 if (dataSet.allPrimitives().isEmpty()) { 92 JOptionPane.showMessageDialog( 93 Main.parent, tr("No data found in file {0}.", source != null ? source : file.getPath()), 94 tr("Open Imagery XML file"), JOptionPane.INFORMATION_MESSAGE); 95 } 96 Main.getLayerManager().addLayer(layer); 97 layer.onPostLoadFromFile(); 98 }); 81 99 } catch (SAXException e) { 82 e.printStackTrace();100 Main.error(e); 83 101 } 84 102 } … … 88 106 throws IOException, IllegalDataException { 89 107 importData(null, null, file, progressMonitor); 90 }91 92 public void importData(final String source, final String layerName, ProgressMonitor progressMonitor)93 throws IOException, IllegalDataException {94 importData(source, layerName, null, progressMonitor);95 108 } 96 109
Note:
See TracChangeset
for help on using the changeset viewer.