001: //$HeadURL: svn+ssh://mschneider@svn.wald.intevation.org/deegree/base/trunk/src/org/deegree/io/datastore/sql/wherebuilder/AbstractPropertyNode.java $
002: /*---------------- FILE HEADER ------------------------------------------
003:
004: This file is part of deegree.
005: Copyright (C) 2001-2008 by:
006: EXSE, Department of Geography, University of Bonn
007: http://www.giub.uni-bonn.de/deegree/
008: lat/lon GmbH
009: http://www.lat-lon.de
010:
011: This library is free software; you can redistribute it and/or
012: modify it under the terms of the GNU Lesser General Public
013: License as published by the Free Software Foundation; either
014: version 2.1 of the License, or (at your option) any later version.
015:
016: This library is distributed in the hope that it will be useful,
017: but WITHOUT ANY WARRANTY; without even the implied warranty of
018: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: Lesser General Public License for more details.
020:
021: You should have received a copy of the GNU Lesser General Public
022: License along with this library; if not, write to the Free Software
023: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024:
025: Contact:
026:
027: Andreas Poth
028: lat/lon GmbH
029: Aennchenstraße 19
030: 53177 Bonn
031: Germany
032: E-Mail: poth@lat-lon.de
033:
034: Prof. Dr. Klaus Greve
035: Department of Geography
036: University of Bonn
037: Meckenheimer Allee 166
038: 53115 Bonn
039: Germany
040: E-Mail: greve@giub.uni-bonn.de
041:
042: ---------------------------------------------------------------------------*/
043: package org.deegree.io.datastore.sql.wherebuilder;
044:
045: import java.util.ArrayList;
046: import java.util.List;
047:
048: /**
049: * Represents a <code>String</code> that may contain special symbols (wildCard, singleChar, escape) as a list of its
050: * parts ({@link SpecialCharStringPart}).
051: * <p>
052: * The internal representation needs no escape symbols.
053: *
054: * @author <a href="mailto:schneider@lat-lon.de">Markus Schneider </a>
055: * @author last edited by: $Author: mschneider $
056: *
057: * @version $Revision: 6588 $, $Date: 2007-04-11 17:31:29 +0200 (Mi, 11 Apr 2007) $
058: */
059: public class SpecialCharString {
060:
061: private List<SpecialCharStringPart> parts;
062:
063: private String wildCard;
064:
065: private String singleChar;
066:
067: private String escape;
068:
069: /**
070: * Constructs a new <code>SpecialCharString</code> instance from the given parameters.
071: *
072: * @param encodedString
073: * @param wildCard
074: * @param singleChar
075: * @param escape
076: */
077: public SpecialCharString(String encodedString, String wildCard,
078: String singleChar, String escape) {
079: this .wildCard = wildCard;
080: this .singleChar = singleChar;
081: this .escape = escape;
082: this .parts = decode(encodedString);
083: }
084:
085: /**
086: * Decodes the given <code>String</code> to a representation that contains explicit objects to represent wildCard
087: * and singleChar symbols and has no escape symbols.
088: *
089: * @param text
090: * encoded <code>String</code>, may contain wildCard, singleChar and escape symbols
091: * @return decoded text
092: */
093: private List<SpecialCharStringPart> decode(String text) {
094:
095: List<SpecialCharStringPart> parts = new ArrayList<SpecialCharStringPart>(
096: text.length());
097:
098: boolean escapeMode = false;
099:
100: StringBuffer sb = null;
101: while (text.length() > 0) {
102: if (escapeMode) {
103: if (sb == null) {
104: sb = new StringBuffer();
105: }
106: sb.append(text.charAt(0));
107: text = text.substring(1);
108: escapeMode = false;
109: } else {
110: if (text.startsWith(wildCard)) {
111: if (sb != null) {
112: parts.add(new PlainText(sb.toString()));
113: sb = null;
114: }
115: parts.add(new WildCard());
116: text = text.substring(wildCard.length());
117: } else if (text.startsWith(singleChar)) {
118: if (sb != null) {
119: parts.add(new PlainText(sb.toString()));
120: sb = null;
121: }
122: parts.add(new SingleChar());
123: text = text.substring(singleChar.length());
124: } else if (text.startsWith(escape)) {
125: text = text.substring(escape.length());
126: escapeMode = true;
127: } else {
128: if (sb == null) {
129: sb = new StringBuffer();
130: }
131: sb.append(text.charAt(0));
132: text = text.substring(1);
133: }
134: }
135: }
136:
137: if (sb != null) {
138: parts.add(new PlainText(sb.toString()));
139: }
140: return parts;
141: }
142:
143: /**
144: * Returns an encoding that is suitable for arguments of "IS LIKE"-clauses in SQL.
145: * <p>
146: * This means:
147: * <ul>
148: * <li>wildCard: encoded as the '%'-character</li>
149: * <li>singleChar: encoded as the '_'-character</li>
150: * <li>escape: encoded as the '\'-character</li>
151: * </ul>
152: *
153: * @return encoded string
154: */
155: public String toSQLStyle() {
156: StringBuffer sb = new StringBuffer();
157: for (SpecialCharStringPart part : parts) {
158: sb.append(part.toSQLStyle());
159: }
160: return sb.toString();
161: }
162:
163: /**
164: * Returns an encoding that is suitable for arguments of "IS LIKE"-clauses in SQL.
165: * <p>
166: * This means:
167: * <ul>
168: * <li>wildCard: encoded as the '%'-character</li>
169: * <li>singleChar: encoded as the '_'-character</li>
170: * <li>escape: encoded as the '\'-character</li>
171: * </ul>
172: *
173: * @param toLowerCase
174: * true means: convert to lowercase letters
175: * @return encoded string
176: */
177: public String toSQLStyle(boolean toLowerCase) {
178: StringBuffer sb = new StringBuffer();
179: for (SpecialCharStringPart part : parts) {
180: sb.append(part.toSQLStyle(toLowerCase));
181: }
182: return sb.toString();
183: }
184:
185: @Override
186: public String toString() {
187: StringBuffer sb = new StringBuffer();
188: for (SpecialCharStringPart part : parts) {
189: sb.append(part.toString());
190: sb.append('\n');
191: }
192: return sb.toString();
193: }
194: }
|