| 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 cClosedPathEnd = "\"/>";
|
|---|
| 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 | /**
|
|---|
| 43 | * Closed SVG paths are finishing with a "Z" at the end of the moves list.
|
|---|
| 44 | * @param svg
|
|---|
| 45 | * @return
|
|---|
| 46 | */
|
|---|
| 47 | public String [] getClosedPaths(String svg) {
|
|---|
| 48 | ArrayList<String> path = new ArrayList<String>();
|
|---|
| 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 | }
|
|---|