/* * 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.resources; // Formatting import java.text.DecimalFormat; import java.text.Format; import java.text.NumberFormat; import java.text.ParseException; import java.text.ParsePosition; import java.util.Locale; /** * The base class for Well Know Text (WKT) parser and formatter. This base class * contains information about the symbols to use (opening and closing bracket, element separator, * etc.). This is a relatively light object compared to their subclasses and can be used when * parsing are not needed. * * @source $URL$ * @version $Id$ * @author Remi Eve * @author Martin Desruisseaux * * @deprecated Rempaced by {@link org.geotools.referencing.wkt.AbstractFormat}. */ public abstract class WKTFormat extends Format { /** * The locale for number parsing and formatting. */ public final Locale locale; /** * The object to use for parsing and formatting numbers. * Note: {@link NumberFormat} object are usually not thread safe. * Consider using this format in a synchronized block if thread safe * behavior is wanted. */ public final NumberFormat number; /** * The character to use as an element separator. * This is usually the coma ','. */ public final char elementSeparator; /** * The character to use as text delimitor. * This is usually the quote '"'. */ public final char textDelimitor = '"'; /** * List of caracters acceptable as opening bracket. The closing bracket must * be the character in the closingBrackets array at the same index * than the opening bracket. */ final char[] openingBrackets = {'[', '('}; /** * List of caracters acceptable as closing bracket. */ final char[] closingBrackets = {']', ')'}; /** * The character to use for openining element's parameters. * This is usually '[' or '('. * This character is used for formatting WKT. */ public final char openingBracket = '['; /** * The character to use for closing element's parameters. * This is usually ']' or ')'. * This character is used for formatting WKT. */ public final char closingBracket = ']'; /** * Construct a format for the specified locale. * * @param locale The locale for parsing and formatting numbers. */ public WKTFormat(final Locale locale) { this.locale = locale; this.number = NumberFormat.getNumberInstance(locale); char decimalSeparator = '.'; if (number instanceof DecimalFormat) { final DecimalFormat df = (DecimalFormat) number; decimalSeparator = df.getDecimalFormatSymbols().getDecimalSeparator(); } elementSeparator = (decimalSeparator==',') ? ';' : ','; number.setGroupingUsed(false); } /** * Returns a tree of {@link WKTElement} for the specified text. * * @param text The text to parse. * @param position In input, the position where to start parsing from. * In output, the first character after the separator. */ protected final WKTElement getTree(final String text, final ParsePosition position) throws ParseException { return new WKTElement(new WKTElement(this, text, position)); } /** * Parses the next element in the specified Well Know Text (WKT) tree. * * @param element The element to be parsed. * @return The object. * @throws ParseException if the element can't be parsed. */ protected abstract Object parse(final WKTElement element) throws ParseException; /** * Parses a Well Know Text (WKT). * * @param text The text to be parsed. * @return The object. * @throws ParseException if the string can't be parsed. */ public final Object parseObject(final String text) throws ParseException { final WKTElement element = getTree(text, new ParsePosition(0)); final Object object = parse(element); element.close(); return object; } /** * Parses a Well Know Text (WKT). * * @param text The text to be parsed. * @param position The position to start parsing from. * @return The object. */ public final Object parseObject(final String text, final ParsePosition position) { final int origin = position.getIndex(); try { return parse(getTree(text, position)); } catch (ParseException exception) { position.setIndex(origin); if (position.getErrorIndex() < origin) { position.setErrorIndex(exception.getErrorOffset()); } return null; } } }