001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2003-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.jdbc.attributeio;
017:
018: import java.io.IOException;
019: import java.sql.PreparedStatement;
020: import java.sql.ResultSet;
021: import java.sql.SQLException;
022: import java.sql.Types;
023:
024: import org.geotools.data.DataSourceException;
025:
026: import com.vividsolutions.jts.geom.Geometry;
027: import com.vividsolutions.jts.io.ParseException;
028: import com.vividsolutions.jts.io.WKTReader;
029: import com.vividsolutions.jts.io.WKTWriter;
030:
031: /**
032: * An attribute IO object that can read and write geometries encoded into
033: * WKT format.
034: *
035: * @author wolf
036: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/jdbc/src/main/java/org/geotools/data/jdbc/attributeio/WKTAttributeIO.java $
037: */
038: public class WKTAttributeIO implements AttributeIO {
039: WKTReader reader;
040: WKTWriter writer;
041:
042: /**
043: * Lazily initialize the WKTReader
044: */
045: private WKTReader getWKTReader() {
046: if (reader == null) {
047: reader = new WKTReader();
048: }
049: return reader;
050: }
051:
052: /**
053: * Lazily initialize the WKTWriter
054: *
055: */
056: private WKTWriter getWKTWriter() {
057: if (writer == null) {
058: writer = new WKTWriter();
059: }
060:
061: return writer;
062: }
063:
064: /**
065: * @see org.geotools.data.jdbc.attributeio.AttributeIO#read(java.sql.ResultSet,
066: * int)
067: */
068: public Object read(ResultSet rs, int position) throws IOException {
069: try {
070: String wkt = rs.getString(position);
071: if (wkt == null || wkt.equals("")) {
072: return null;
073: }
074: return getWKTReader().read(wkt);
075: } catch (SQLException e) {
076: throw new DataSourceException("Sql reading problem", e);
077: } catch (ParseException e) {
078: throw new DataSourceException("Could not parse WKT", e);
079: }
080: }
081:
082: /**
083: * @see org.geotools.data.jdbc.attributeio.AttributeIO#write(java.sql.ResultSet,
084: * int, java.lang.Object)
085: */
086: public void write(ResultSet rs, int position, Object value)
087: throws IOException {
088: try {
089: if (value == null) {
090: rs.updateNull(position);
091: } else {
092: Geometry g = (Geometry) value;
093: String wkt = getWKTWriter().write(g);
094: rs.updateString(position, wkt);
095: }
096: } catch (Exception e) {
097: throw new DataSourceException("Sql writing problem", e);
098: }
099: }
100:
101: /**
102: * @see org.geotools.data.jdbc.attributeio.AttributeIO#write(java.sql.ResultSet,
103: * int, java.lang.Object)
104: */
105: public void write(PreparedStatement ps, int position, Object value)
106: throws IOException {
107: try {
108: if (value == null) {
109: ps.setNull(position, Types.VARCHAR);
110: } else {
111: Geometry g = (Geometry) value;
112: String wkt = getWKTWriter().write(g);
113: ps.setString(position, wkt);
114: }
115: } catch (Exception e) {
116: throw new DataSourceException("Sql writing problem", e);
117: }
118: }
119: }
|