/* * Geotools 2 - OpenSource mapping toolkit * (C) 2003, Geotools Project Managment Committee (PMC) * (C) 2002, Institut de Recherche pour le Développement * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ package org.geotools.ct; // J2SE dependencies import java.text.FieldPosition; import java.text.ParseException; import java.text.ParsePosition; import java.util.HashSet; import java.util.Locale; import java.util.Set; import javax.media.jai.ParameterList; import javax.media.jai.ParameterListDescriptor; import javax.media.jai.util.CaselessStringKey; import org.geotools.resources.WKTElement; import org.geotools.resources.WKTFormat; import org.geotools.resources.i18n.ErrorKeys; import org.geotools.resources.i18n.Errors; import org.opengis.referencing.FactoryException; import org.opengis.referencing.operation.NoninvertibleTransformException; /** * Parser for Well Know Text (WKT). * Instances of this class are thread-safe. * * @source $URL$ * @version $Id$ * @author Remi Eve * @author Martin Desruisseaux * * @deprecated Replaced by {@link org.geotools.referencing.wkt.Parser}. */ final class WKTParser extends WKTFormat { /** * The factory to use for creating math transform. */ private MathTransformFactory factory; /** * Construct a parser for the specified locale. * * @param local The locale for parsing and formatting numbers. * @param factory The factory for constructing coordinate systems. */ public WKTParser(final Locale locale, final MathTransformFactory factory) { super(locale); this.factory = factory; } /** * Parses a "PARAM_MT" element. This element has the following pattern: * *
* PARAM_MT["" {,}* ] *
* * @param parent The parent element. * @return The "PARAM_MT" element as an {@link MathTransform} object. * @throws ParseException if the "PARAM_MT" element can't be parsed. */ private MathTransform parseParamMT(final WKTElement parent) throws ParseException { final WKTElement element = parent.pullElement("PARAM_MT"); final String classification = element.pullString("classification"); ParameterList parameters; try { parameters = factory.getMathTransformProvider(classification).getParameterList(); } catch (FactoryException exception) { throw element.parseFailed(exception, null); } /* * Gets the list of parameters expecting an integer value. * All other parameters will be given a double value. */ final Set integers = new HashSet(); final ParameterListDescriptor descriptor = parameters.getParameterListDescriptor(); final Class[] classes = descriptor.getParamClasses(); if (classes != null) { String[] names = null; for (int i=0; i * INVERSE_MT[] * * * @param parent The parent element. * @return The "INVERSE_MT" element as an {@link MathTransform} object. * @throws ParseException if the "INVERSE_MT" element can't be parsed. */ private MathTransform parseInverseMT(final WKTElement parent) throws ParseException { final WKTElement element = parent.pullElement("INVERSE_MT"); try { final MathTransform transform = parseMathTransform(element, true).inverse(); element.close(); return transform; } catch (NoninvertibleTransformException exception) { throw element.parseFailed(exception, null); } } /** * Parses a "PASSTHROUGH_MT" element. This element has the following pattern: * *
* PASSTHROUGH_MT[, ] *
* * @param parent The parent element. * @return The "PASSTHROUGH_MT" element as an {@link MathTransform} object. * @throws ParseException if the "PASSTHROUGH_MT" element can't be parsed. */ private MathTransform parsePassThroughMT(final WKTElement parent) throws ParseException { final WKTElement element = parent.pullElement("PASSTHROUGH_MT"); final int firstAffectedOrdinate = parent.pullInteger("firstAffectedOrdinate"); final MathTransform transform = parseMathTransform(element, true); element.close(); return factory.createPassThroughTransform(firstAffectedOrdinate, transform, 0); } /** * Parses a "CONCAT_MT" element. This element has the following pattern: * *
* CONCAT_MT[ {,}*] *
* * @param parent The parent element. * @return The "CONCAT_MT" element as an {@link MathTransform} object. * @throws ParseException if the "CONCAT_MT" element can't be parsed. */ private MathTransform parseConcatMT(final WKTElement parent) throws ParseException { final WKTElement element = parent.pullElement("CONCAT_MT"); MathTransform transform = parseMathTransform(element, true); MathTransform optionalTransform; while ((optionalTransform = parseMathTransform(element, false)) != null) { transform = factory.createConcatenatedTransform(transform, optionalTransform); } element.close(); return transform; } /** * Parses a mathTransform element. * * @param parent The parent element. * @param required True if parameter is required and false in other case. * @return The next element as a {@link MathTransform} object. * @throws ParseException if the next element can't be parsed. */ private MathTransform parseMathTransform(final WKTElement element, final boolean required) throws ParseException { final Object key = element.peek(); if (key instanceof WKTElement) { final String keyword = ((WKTElement) key).keyword.trim().toUpperCase(locale); if ("PARAM_MT" .equals(keyword)) return parseParamMT (element); if ("CONCAT_MT" .equals(keyword)) return parseConcatMT (element); if ("INVERSE_MT" .equals(keyword)) return parseInverseMT (element); if ("PASSTHROUGH_MT".equals(keyword)) return parsePassThroughMT(element); } if (required) { throw element.parseFailed(null, Errors.format(ErrorKeys.UNKNOW_TYPE_$1, key)); } return null; } /** * Parses a MathTransform element. * * @param text The text to be parsed. * @return The transformation. * @throws ParseException if the string can't be parsed. */ public MathTransform parseMathTransform(final String text) throws ParseException { final WKTElement element = getTree(text, new ParsePosition(0)); final MathTransform mt = parseMathTransform(element, true); element.close(); return mt; } /** * Parses the next element in the specified Well Know Text (WKT) tree. * * @param element The element to be parsed. * @return The parsed object. * @throws ParseException if the element can't be parsed. */ protected Object parse(final WKTElement element) throws ParseException { return parseMathTransform(element, true); } /** * Format the specified object. Current implementation just append {@link Object#toString}, * since the toString() implementation for most {@link org.geotools.cs.Info} * objects is to returns a WKT. * * @task TODO: Provides pacakge private Info.toString(WKTFormat) implementations. * It would allows us to invoke ((Info)obj).toString(this) here. */ public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) { return toAppendTo.append(obj); } }