@Override public void run(List<PrjElement> list) { if (!isHorizontalCRS) { parseProjcs(list, false); isHorizontalCRS = true; } else { throw new PrjParserException("Failed to parse PRJ, because of multiple horizontal CRS definition."); } } };
/** * Return the children of the PrjElement in parameter if it is an instance * of PrjNodeElement and if its name is the same as the name given in * parameter,else it throws a PrjParserException. * * @param e the PrjElement to match * @param name the name of the desired PrjElement */ private List<PrjElement> matchNode(PrjElement e, String name) { return matchNode(e, name, true); }
/** * Transform the PrjElement in parameter into a set of parameters. * * @param el the PrjElement to transform */ static Map<String, String> match(PrjElement el) { PrjMatcher m = new PrjMatcher(); return m.doMatch(el); }
List<PrjElement> ll = matchNode(el, PrjKeyParameters.COMPDCS, false); if (ll == null) { ll = matchNode(el, PrjKeyParameters.PROJCS, false); if (ll == null) { ll = matchNode(el, PrjKeyParameters.GEOGCS, false); if (ll == null) { ll = matchNode(el, PrjKeyParameters.GEOCCS, false); if (ll == null) { ll = matchNode(el, PrjKeyParameters.VERTCS); parseVertcs(ll, true); } else { parseGeoccs(ll); parseGeogcs(ll, true); parseProjcs(ll, true); parseCompdcs(ll); cleanUnits(); return params;
/** * Read the informations contains in the PrjElement in parameter using one * of the PrjNodeMatcher in parameter and put it into the set of parameters. * * @param e the PrjElement to parse * @param nn the PrjNodeMatcher to use to parse the given PrjElement * @param strict if true, the method throw a PrjParserException if it does * not manage to parse the PrjElement in parameter */ private void matchAnyNode(PrjElement e, PrjNodeMatcher[] nn, boolean strict) { if (e instanceof PrjNodeElement) { PrjNodeElement ne = (PrjNodeElement) e; for (PrjNodeMatcher m : nn) { if (ne.getName().equalsIgnoreCase(m.getName())) { m.run(ne.getChildren()); return; } } } if (strict) { throw new PrjParserException("Failed to parse PRJ. Found '" + e + "', completely unexpected!"); } }
/** * Parses a WKT PRJ String into a set of parameters. * * This is the main entry point of the parser. * * @param prjString a WKT string * @return a list of parameters * @throws PrjParserException if the PRJ cannot be parsed into a CRS for any * reason */ public Map<String, String> getParameters(String prjString) { CharBuffer s = CharBuffer.wrap(prjString); PrjElement e; try { e = parseNode(s); } catch (BufferUnderflowException ex) { throw new PrjParserException("Failed to read PRJ.", ex); } Map<String, String> prjParameters = PrjMatcher.match(e); return prjParameters; }
/** * Return the children of the PrjElement in parameter if it is an instance * of PrjNodeElement and if its name is the same as the name given in * parameter,else it throws a PrjParserException unless strict is false, it * then returns null. * * @param e the PrjElement to match * @param name the name of the desired PrjElement * @param strict true if the method must throw an exception instead of * return null */ private List<PrjElement> matchNode(PrjElement e, String name, boolean strict) { if (e instanceof PrjNodeElement) { PrjNodeElement n = (PrjNodeElement) e; if (n.getName().equalsIgnoreCase(name)) { return n.getChildren(); } } if (strict) { throw new PrjParserException("Failed to parse PRJ. Found '" + e + "', expected PrjNodeElement[" + name + "]."); } else { return null; } }
return new PrjNodeElement(name, parseNodeChildren(s)); } else { s.position(s.position() - 1); return new PrjStringElement(name);
/** * Return the informations contains in the PrjElement in parameter if it is * a PrjNumberParameter. * * @param e the PrjElement to parse */ private double getNumber(PrjElement e) { if (e instanceof PrjNumberElement) { PrjNumberElement n = (PrjNumberElement) e; return n.getValue(); } else { throw new PrjParserException("Failed to parse PRJ. Found '" + e + "', expected PrjNumberElement."); } } }
/** * Return the informations contains in the PrjElement in parameter if it is * a PrjStringParameter. * * @param e the PrjElement to parse */ private String getString(PrjElement e) { if (e instanceof PrjStringElement) { PrjStringElement s = (PrjStringElement) e; return s.getValue().trim(); } throw new PrjParserException("Failed to parse PRJ. Found '" + e + "', expected some PrjStringElement."); }
/** * Read the informations contains in a PARAMETER node and put it into the * set of parameters. * * @param ll the children of the PARAMETER node */ private void parseParameter(List<PrjElement> ll) { String param = getString(ll.get(0)); String parm = PrjValueParameters.PARAMNAMES.get(param.toLowerCase().replaceAll("[^a-zA-Z0-9]", "")); if (parm != null) { parseNumber(ll.get(1), parm); } }
/** * Read the informations contains in the PrjElement in parameter using one * of the PrjNodeMatcher in parameter and put it into the set of parameters. * * @param e the PrjElement to parse * @param nn the PrjNodeMatcher to use to parse the given PrjElement */ private void matchAnyNode(PrjElement e, PrjNodeMatcher[] nn) { matchAnyNode(e, nn, false); }
/** * Read the informations contains in the PROJECTION node and put it into the * set of parameters. * * @param ll the children of the PROJECTION node */ private void parseProjection(List<PrjElement> ll) { String proj = getString(ll.get(0)); String prj = PrjValueParameters.PROJNAMES.get(proj.replaceAll("[^a-zA-Z0-9]", "").toLowerCase()); proj = prj != null ? prj : proj; params.put(ProjKeyParameters.proj, proj); }
/** * Read the informations contains in the TOWGS84 node and put it into the * set of parameters. * * @param ll the children of the TOWGS84 node */ private void parseToWGS84(List<PrjElement> ll) { StringBuilder b = new StringBuilder(); b.append(getNumber(ll.get(0))); for (int i = 1; i < ll.size(); i++) { b.append(',').append(getNumber(ll.get(i))); } params.put(ProjKeyParameters.towgs84, b.toString()); }
/** * Parses a String information into its PrjStringElement representation. * * @param s the Buffer to parse */ private PrjStringElement parseString(CharBuffer s) { int start = s.position(); int ll = 0; while (s.hasRemaining()) { char c = s.get(); if (c == '"') { break; } else { ll++; } } s.position(start); String str = s.subSequence(0, ll).toString(); s.position(start + ll + 1); return new PrjStringElement(str); }
/** * Read the informations contains in the PrjElement in parameter if it is a * PrjNumberParameter and put it into the set of parameters. * * @param e the PrjElement to parse * @param name the key to use to put the string in the Map of parameters */ private void parseNumber(PrjElement e, String name) { if (e instanceof PrjNumberElement) { PrjNumberElement n = (PrjNumberElement) e; params.put(name, String.valueOf(n.getValue())); } else { throw new PrjParserException("Failed to parse PRJ. Found '" + e + "', expected PrjNumberElement with " + name + " in it."); } }
/** * Read the informations contains in the PrjElement in parameter if it is a * PrjStringParameter and put it into the set of parameters. * * @param e the PrjElement to parse * @param name the key to use to put the string in the Map of parameters */ private void parseString(PrjElement e, String name) { if (e instanceof PrjStringElement) { PrjStringElement s = (PrjStringElement) e; params.put(name, s.getValue().trim()); } else { throw new PrjParserException("Failed to parse PRJ. Found '" + e + "', expected PrjStringElement with " + name + " in it."); } }
@Override public void run(List<PrjElement> list) { if (!isHorizontalCRS) { parseGeogcs(list, false); isHorizontalCRS = true; } else { throw new PrjParserException("Failed to parse PRJ, because of multiple horizontal CRS definition."); } } };
@Override public void run(List<PrjElement> list) { if (!isVerticalCRS) { parseVertcs(list, false); isVerticalCRS = true; } else { throw new PrjParserException("Failed to parse PRJ, because of multiple vertical CRS definition."); } } };
@Override public void run(List<PrjElement> list) { List<String> info = getAxis(list); switch (indexAxis) { case 0: params.put(PrjKeyParameters.VERTAXIS, info.get(0)); params.put(PrjKeyParameters.VERTAXISTYPE, info.get(1)); indexAxis++; break; default: throw new PrjParserException("Failed to parse PRJ. Found '" + list + "', completely unexpected!"); } } };