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