001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2005-2006, GeoTools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: */
016: package org.geotools.data.mif;
017:
018: /**
019: * <p>Utility class for setting object values from strings and vice-versa.</p>
020: * <p>The main use of this class is building a schema-dependent array of "parsers" which speed up the process of reading
021: * text lines and converting them into features.</p>
022: *
023: * @author Luca S. Percich, AMA-MI
024: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/mif/src/main/java/org/geotools/data/mif/MIFValueSetter.java $
025: * @version $Id: MIFValueSetter.java 22266 2006-10-19 11:30:55Z acuster $
026: */
027: public abstract class MIFValueSetter {
028: protected String strValue = null; // The object value as string
029: protected Object objValue = null; // the object value
030: private String defaultValue = ""; // String representation of the default value (must be correctly converted into object by the stringToValue method!!!)
031: private String errorMessage = "";
032:
033: /**
034: * <p>The constructor accepts a default value for the ValueSetter.</p>
035: *
036: * @param defa String representation of the default value
037: */
038: public MIFValueSetter(String defa) {
039: defaultValue = defa;
040: }
041:
042: /**
043: * <p>Sets the value as a String. After a setString call, getValue() can be
044: * used to access the converted value.</p>
045: *
046: * @param value String representation of the object value
047: *
048: * @return true if conversion was successful
049: */
050: public final boolean setString(String value) {
051: strValue = value;
052:
053: try {
054: stringToValue();
055:
056: return true;
057: } catch (Exception e) {
058: errorMessage = e.getMessage();
059: strValue = defaultValue;
060:
061: try {
062: stringToValue();
063: } catch (Exception ex) {
064: // Should never reach this place!!!!
065: objValue = null;
066: errorMessage += ". Bad default string value";
067: }
068:
069: return false;
070: }
071: }
072:
073: /**
074: * <p>Returns the string value.</p>
075: *
076: */
077: public final String getString() {
078: return strValue;
079: }
080:
081: /**
082: * <p>Sets the object value, and calculates the String value.</p>
083: *
084: * @param value The Object value
085: */
086: public final void setValue(Object value) {
087: objValue = value;
088: valueToString();
089: }
090:
091: /**
092: * <p>Gets the object value.</p>
093: *
094: */
095: public final Object getValue() {
096: return objValue;
097: }
098:
099: /**
100: * <p>Gets and resets the current error message.</p>
101: *
102: * @return The current error message, "" if none
103: */
104: public final String getError() {
105: String tmp = errorMessage;
106: errorMessage = "";
107:
108: return tmp;
109: }
110:
111: /**
112: * <p>Converts an object value to string - the default implementation uses
113: * toString for non-null values.</p>
114: */
115: protected void valueToString() {
116: // String.valueOf() would yeld "null"
117: if (objValue == null) {
118: strValue = "";
119: } else {
120: strValue = objValue.toString();
121: }
122: }
123:
124: /**
125: * <p>This method must be overridden by descendants in order to implement the
126: * correct conversion between strings and object values. <br>
127: * Must throw an exception if conversion failed</p>.
128: *
129: * @throws Exception if value conversion failed
130: */
131: protected abstract void stringToValue() throws Exception;
132: }
|