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