/** Scans the expected string, or throws {@code ParseException} */ private void scan(String expected) throws ParseException { if (upto + expected.length() > input.length()) { throw newParseException("expected \"" + expected + "\" but hit EOF"); } String subString = input.substring(upto, upto+expected.length()); if (subString.equals(expected) == false) { throw newParseException("expected \"" + expected + "\" but got \"" + subString + "\""); } upto += expected.length(); }
private void readEnd() throws ParseException { while (upto < input.length()) { char ch = input.charAt(upto); if (isJSONWhitespace(ch) == false) { throw newParseException("unexpected character '" + ch + "' after end of GeoJSON object"); } upto++; } }
/** Scans across whitespace and consumes the expected character, or throws {@code ParseException} if the character is wrong */ private void scan(char expected) throws ParseException { while (upto < input.length()) { char ch = input.charAt(upto); if (isJSONWhitespace(ch)) { upto++; continue; } if (ch != expected) { throw newParseException("expected '" + expected + "' but got '" + ch + "'"); } upto++; return; } throw newParseException("expected '" + expected + "' but got EOF"); }
private char peek() throws ParseException { while (upto < input.length()) { char ch = input.charAt(upto); if (isJSONWhitespace(ch)) { upto++; continue; } return ch; } throw newParseException("unexpected EOF"); }
/** Parses [[lat, lon], [lat, lon] ...] into 2d double array */ private double[][] parsePoints(List<Object> o) throws ParseException { double[] lats = new double[o.size()]; double[] lons = new double[o.size()]; for(int i=0;i<o.size();i++) { Object point = o.get(i); if (point instanceof List == false) { throw newParseException("elements of coordinates array must [lat, lon] array, but got: " + point); } List<Object> pointList = (List<Object>) point; if (pointList.size() != 2) { throw newParseException("elements of coordinates array must [lat, lon] array, but got wrong element count: " + pointList); } if (pointList.get(0) instanceof Double == false) { throw newParseException("elements of coordinates array must [lat, lon] array, but first element is not a Double: " + pointList.get(0)); } if (pointList.get(1) instanceof Double == false) { throw newParseException("elements of coordinates array must [lat, lon] array, but second element is not a Double: " + pointList.get(1)); } // lon, lat ordering in GeoJSON! lons[i] = ((Double) pointList.get(0)).doubleValue(); lats[i] = ((Double) pointList.get(1)).doubleValue(); } return new double[][] {lats, lons}; }
private Number parseNumber() throws ParseException { StringBuilder b = new StringBuilder(); int uptoStart = upto; while (upto < input.length()) { char ch = input.charAt(upto); if (ch == '-' || ch == '.' || (ch >= '0' && ch <= '9') || ch == 'e' || ch == 'E') { upto++; b.append(ch); } else { break; } } // we only handle doubles try { return Double.parseDouble(b.toString()); } catch (NumberFormatException nfe) { upto = uptoStart; throw newParseException("could not parse number as double"); } }
throw newParseException("hit EOF inside string literal"); throw newParseException("hit EOF inside string literal"); } else { throw newParseException("unsupported string escape character \\" + ch); throw newParseException("hit EOF inside string literal");
private Polygon parsePolygon(List<Object> coordinates) throws ParseException { List<Polygon> holes = new ArrayList<>(); Object o = coordinates.get(0); if (o instanceof List == false) { throw newParseException("first element of polygon array must be an array [[lat, lon], [lat, lon] ...] but got: " + o); } double[][] polyPoints = parsePoints((List<Object>) o); for(int i=1;i<coordinates.size();i++) { o = coordinates.get(i); if (o instanceof List == false) { throw newParseException("elements of coordinates array must be an array [[lat, lon], [lat, lon] ...] but got: " + o); } double[][] holePoints = parsePoints((List<Object>) o); holes.add(new Polygon(holePoints[0], holePoints[1])); } return new Polygon(polyPoints[0], polyPoints[1], holes.toArray(new Polygon[holes.size()])); }
public Polygon[] parse() throws ParseException { // parse entire object parseObject(""); // make sure there's nothing left: readEnd(); // The order of JSON object keys (type, geometry, coordinates in our case) can be arbitrary, so we wait until we are done parsing to // put the pieces together here: if (coordinates == null) { throw newParseException("did not see any polygon coordinates"); } if (polyType == null) { throw newParseException("did not see type: Polygon or MultiPolygon"); } if (polyType.equals("Polygon")) { return new Polygon[] {parsePolygon(coordinates)}; } else { List<Polygon> polygons = new ArrayList<>(); for(int i=0;i<coordinates.size();i++) { Object o = coordinates.get(i); if (o instanceof List == false) { throw newParseException("elements of coordinates array should be an array, but got: " + o.getClass()); } polygons.add(parsePolygon((List<Object>) o)); } return polygons.toArray(new Polygon[polygons.size()]); } }
throw newParseException("expected ',' separating list items, but got '" + ch + "'"); throw newParseException("hit EOF while parsing array"); o = parseNumber(); } else { throw newParseException("expected another array or number while parsing array, not '" + ch + "'"); throw newParseException("hit EOF while reading array");
throw newParseException("expected , but got " + ch); throw newParseException("cannot handle linked crs"); break; } else { throw newParseException("expected array, object, string or literal value, but got: " + ch); if (o instanceof String == false) { upto = uptoStart; throw newParseException("crs.properties.name should be a string, but saw: " + o); throw newParseException("crs must be CRS84 from OGC, but saw: " + o); if (o instanceof String == false) { upto = uptoStart; throw newParseException("type should be a string, but got: " + o); throw newParseException("can only handle type FeatureCollection (if it has a single polygon geometry), Feature, Polygon or MutiPolygon, but got " + type); throw newParseException("coordinates should be an array, but got: " + o.getClass()); throw newParseException("only one Polygon or MultiPolygon is supported");
/** Scans the expected string, or throws {@code ParseException} */ private void scan(String expected) throws ParseException { if (upto + expected.length() > input.length()) { throw newParseException("expected \"" + expected + "\" but hit EOF"); } String subString = input.substring(upto, upto+expected.length()); if (subString.equals(expected) == false) { throw newParseException("expected \"" + expected + "\" but got \"" + subString + "\""); } upto += expected.length(); }
/** Scans across whitespace and consumes the expected character, or throws {@code ParseException} if the character is wrong */ private void scan(char expected) throws ParseException { while (upto < input.length()) { char ch = input.charAt(upto); if (isJSONWhitespace(ch)) { upto++; continue; } if (ch != expected) { throw newParseException("expected '" + expected + "' but got '" + ch + "'"); } upto++; return; } throw newParseException("expected '" + expected + "' but got EOF"); }
private void readEnd() throws ParseException { while (upto < input.length()) { char ch = input.charAt(upto); if (isJSONWhitespace(ch) == false) { throw newParseException("unexpected character '" + ch + "' after end of GeoJSON object"); } upto++; } }
private char peek() throws ParseException { while (upto < input.length()) { char ch = input.charAt(upto); if (isJSONWhitespace(ch)) { upto++; continue; } return ch; } throw newParseException("unexpected EOF"); }
private Number parseNumber() throws ParseException { StringBuilder b = new StringBuilder(); int uptoStart = upto; while (upto < input.length()) { char ch = input.charAt(upto); if (ch == '-' || ch == '.' || (ch >= '0' && ch <= '9') || ch == 'e' || ch == 'E') { upto++; b.append(ch); } else { break; } } // we only handle doubles try { return Double.parseDouble(b.toString()); } catch (NumberFormatException nfe) { upto = uptoStart; throw newParseException("could not parse number as double"); } }
/** Parses [[lat, lon], [lat, lon] ...] into 2d double array */ private double[][] parsePoints(List<Object> o) throws ParseException { double[] lats = new double[o.size()]; double[] lons = new double[o.size()]; for(int i=0;i<o.size();i++) { Object point = o.get(i); if (point instanceof List == false) { throw newParseException("elements of coordinates array must [lat, lon] array, but got: " + point); } List<Object> pointList = (List<Object>) point; if (pointList.size() != 2) { throw newParseException("elements of coordinates array must [lat, lon] array, but got wrong element count: " + pointList); } if (pointList.get(0) instanceof Double == false) { throw newParseException("elements of coordinates array must [lat, lon] array, but first element is not a Double: " + pointList.get(0)); } if (pointList.get(1) instanceof Double == false) { throw newParseException("elements of coordinates array must [lat, lon] array, but second element is not a Double: " + pointList.get(1)); } // lon, lat ordering in GeoJSON! lons[i] = ((Double) pointList.get(0)).doubleValue(); lats[i] = ((Double) pointList.get(1)).doubleValue(); } return new double[][] {lats, lons}; }
throw newParseException("hit EOF inside string literal"); throw newParseException("hit EOF inside string literal"); } else { throw newParseException("unsupported string escape character \\" + ch); throw newParseException("hit EOF inside string literal");
private Polygon parsePolygon(List<Object> coordinates) throws ParseException { List<Polygon> holes = new ArrayList<>(); Object o = coordinates.get(0); if (o instanceof List == false) { throw newParseException("first element of polygon array must be an array [[lat, lon], [lat, lon] ...] but got: " + o); } double[][] polyPoints = parsePoints((List<Object>) o); for(int i=1;i<coordinates.size();i++) { o = coordinates.get(i); if (o instanceof List == false) { throw newParseException("elements of coordinates array must be an array [[lat, lon], [lat, lon] ...] but got: " + o); } double[][] holePoints = parsePoints((List<Object>) o); holes.add(new Polygon(holePoints[0], holePoints[1])); } return new Polygon(polyPoints[0], polyPoints[1], holes.toArray(new Polygon[holes.size()])); }
public Polygon[] parse() throws ParseException { // parse entire object parseObject(""); // make sure there's nothing left: readEnd(); // The order of JSON object keys (type, geometry, coordinates in our case) can be arbitrary, so we wait until we are done parsing to // put the pieces together here: if (coordinates == null) { throw newParseException("did not see any polygon coordinates"); } if (polyType == null) { throw newParseException("did not see type: Polygon or MultiPolygon"); } if (polyType.equals("Polygon")) { return new Polygon[] {parsePolygon(coordinates)}; } else { List<Polygon> polygons = new ArrayList<>(); for(int i=0;i<coordinates.size();i++) { Object o = coordinates.get(i); if (o instanceof List == false) { throw newParseException("elements of coordinates array should be an array, but got: " + o.getClass()); } polygons.add(parsePolygon((List<Object>) o)); } return polygons.toArray(new Polygon[polygons.size()]); } }