1 | // License: GPL. v2 and later. Copyright 2008-2009 by Pieren <pieren3@gmail.com> and others
|
---|
2 | package cadastre_fr;
|
---|
3 |
|
---|
4 | import 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 | */
|
---|
12 | public 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 | return dbox;
|
---|
36 | } catch (Exception ex) {
|
---|
37 | return null;
|
---|
38 | }
|
---|
39 | }
|
---|
40 | return null;
|
---|
41 | }
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Closed SVG paths are finishing with a "Z" at the end of the moves list.
|
---|
45 | * @param svg
|
---|
46 | * @return
|
---|
47 | */
|
---|
48 | public String [] getClosedPaths(String svg) {
|
---|
49 | ArrayList<String> path = new ArrayList<String>();
|
---|
50 | int i = 0;
|
---|
51 | while (svg.indexOf(cPathStart, i) != -1) {
|
---|
52 | int s = svg.indexOf(cPathStart, i) + cViewBoxStart.length();
|
---|
53 | int e = svg.indexOf(cClosedPathEnd, s);
|
---|
54 | if (s != -1 && e != -1) {
|
---|
55 | String onePath = svg.substring(s, e);
|
---|
56 | if (onePath.indexOf("Z") != -1) // only closed SVG path
|
---|
57 | path.add(onePath);
|
---|
58 | } else
|
---|
59 | break;
|
---|
60 | i = e;
|
---|
61 | }
|
---|
62 | return path.toArray(new String[ path.size() ]);
|
---|
63 | }
|
---|
64 |
|
---|
65 | }
|
---|