source: osm/applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr/SVGParser.java@ 33279

Last change on this file since 33279 was 32556, checked in by donvip, 9 years ago

checkstyle

File size: 2.1 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package cadastre_fr;
3
4import java.util.ArrayList;
5
6/**
7 * This class is not intended to be a real SVG parser. It's also not using existing
8 * xml parsers. It's just extracting the required strings from an SVG file coming
9 * from the French land registry cadastre.gouv.fr
10 *
11 */
12public class SVGParser {
13
14 private String cViewBoxStart = "viewBox=\"";
15 private String cViewBoxEnd = "\"";
16 private String cPathStart = "<path d=\"";
17 private String cClosedPathEnd = "\"/>";
18
19 /**
20 * The SVG viewBox looks like this:
21 * viewBox="969780.0 320377.11 5466.130000000005 2846.429999999993"
22 * @param svg the SVG XML data
23 * @return double [x,y,dx,dy] of viewBox; null if parsing failed
24 */
25 public double[] getViewBox(String svg) {
26 int s = svg.indexOf(cViewBoxStart)+cViewBoxStart.length();
27 int e = svg.indexOf(cViewBoxEnd, s);
28 if (s != -1 && e != -1) {
29 try {
30 String str = svg.substring(s, e);
31 String[] viewBox = str.split(" ");
32 double[] dbox = new double[4];
33 for (int i = 0; i < 4; i++) {
34 dbox[i] = Double.parseDouble(viewBox[i]);
35 }
36 return dbox;
37 } catch (Exception ex) {
38 return null;
39 }
40 }
41 return null;
42 }
43
44 /**
45 * Closed SVG paths are finishing with a "Z" at the end of the moves list.
46 */
47 public String[] getClosedPaths(String svg) {
48 ArrayList<String> path = new ArrayList<>();
49 int i = 0;
50 while (svg.indexOf(cPathStart, i) != -1) {
51 int s = svg.indexOf(cPathStart, i) + cViewBoxStart.length();
52 int e = svg.indexOf(cClosedPathEnd, s);
53 if (s != -1 && e != -1) {
54 String onePath = svg.substring(s, e);
55 if (onePath.indexOf("Z") != -1) // only closed SVG path
56 path.add(onePath);
57 } else
58 break;
59 i = e;
60 }
61 return path.toArray(new String[ path.size() ]);
62 }
63
64}
Note: See TracBrowser for help on using the repository browser.