001: //$HeadURL$
002: /*---------------- FILE HEADER ------------------------------------------
003: This file is part of deegree.
004: Copyright (C) 2001-2008 by:
005: Department of Geography, University of Bonn
006: http://www.giub.uni-bonn.de/deegree/
007: lat/lon GmbH
008: http://www.lat-lon.de
009:
010: This library is free software; you can redistribute it and/or
011: modify it under the terms of the GNU Lesser General Public
012: License as published by the Free Software Foundation; either
013: version 2.1 of the License, or (at your option) any later version.
014: This library is distributed in the hope that it will be useful,
015: but WITHOUT ANY WARRANTY; without even the implied warranty of
016: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: Lesser General Public License for more details.
018: You should have received a copy of the GNU Lesser General Public
019: License along with this library; if not, write to the Free Software
020: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
021: Contact:
022:
023: Andreas Poth
024: lat/lon GmbH
025: Aennchenstr. 19
026: 53177 Bonn
027: Germany
028: E-Mail: poth@lat-lon.de
029:
030: Prof. Dr. Klaus Greve
031: Department of Geography
032: University of Bonn
033: Meckenheimer Allee 166
034: 53115 Bonn
035: Germany
036: E-Mail: greve@giub.uni-bonn.de
037: ---------------------------------------------------------------------------*/
038:
039: package org.deegree.model.filterencoding;
040:
041: import java.util.List;
042:
043: import org.deegree.model.feature.Feature;
044:
045: /**
046: *
047: *
048: * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
049: * @author last edited by: $Author: poth $
050: *
051: * @version $Revision: 6251 $, $Date: 2007-03-19 16:59:28 +0100 (Mo, 19 Mrz 2007) $
052: */
053: public class FunctionSoundex extends Function {
054:
055: /**
056: *
057: */
058: FunctionSoundex() {
059: super ();
060: }
061:
062: /**
063: * @param name
064: * @param args
065: */
066: FunctionSoundex(String name, List<Expression> args) {
067: super (name, args);
068: }
069:
070: /*
071: * (non-Javadoc)
072: *
073: * @see org.deegree.model.filterencoding.Function#evaluate(org.deegree.model.feature.Feature)
074: */
075: @Override
076: public Object evaluate(Feature feature)
077: throws FilterEvaluationException {
078: Literal literal = (Literal) args.get(0);
079: String s = literal.getValue();
080:
081: return soundex(s);
082:
083: }
084:
085: /**
086: *
087: * @param s
088: * @return
089: */
090: private String soundex(String s) {
091: int SIZE = 4;
092:
093: char[] x = s.toUpperCase().toCharArray();
094: char firstLetter = x[0];
095:
096: // convert letters to numeric code
097: for (int i = 0; i < x.length; i++) {
098: switch (x[i]) {
099: case 'B':
100: case 'F':
101: case 'P':
102: case 'V': {
103: x[i] = '1';
104: break;
105: }
106:
107: case 'C':
108: case 'G':
109: case 'J':
110: case 'K':
111: case 'Q':
112: case 'S':
113: case 'X':
114: case 'Z': {
115: x[i] = '2';
116: break;
117: }
118:
119: case 'D':
120: case 'T': {
121: x[i] = '3';
122: break;
123: }
124:
125: case 'L': {
126: x[i] = '4';
127: break;
128: }
129:
130: case 'M':
131: case 'N': {
132: x[i] = '5';
133: break;
134: }
135:
136: case 'R': {
137: x[i] = '6';
138: break;
139: }
140:
141: default: {
142: x[i] = '0';
143: break;
144: }
145: }
146: }
147:
148: // remove duplicates
149: String output = "" + firstLetter;
150: char last = x[0];
151: for (int i = 1; i < x.length; i++) {
152: if (x[i] != '0' && x[i] != last) {
153: last = x[i];
154: output += last;
155: }
156: }
157:
158: // pad with 0's or truncate
159: for (int i = output.length(); i < SIZE; i++) {
160: output += '0';
161: }
162: output = output.substring(0, SIZE);
163:
164: return output;
165: }
166:
167: }
|