001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.cocoon.components.search.fieldmodel;
017:
018: import org.apache.lucene.document.Field;
019:
020: /**
021: * Field Definition class
022: *
023: * @author Nicolas Maisonneuve
024: *
025: */
026: public abstract class FieldDefinition {
027: /**
028: * Text type
029: */
030: public static final int TEXT = 0;
031:
032: /**
033: * Keyword type
034: */
035: public static final int KEYWORD = 1;
036:
037: /**
038: * Date type
039: */
040: public static final int DATE = 2;
041:
042: public static final String[] STRING_TYPE = { "text", "keyword",
043: "date" };
044:
045: /**
046: * Name of the field
047: */
048: protected String name;
049:
050: /**
051: * type of the field (text, keyword, date)
052: */
053: protected int type;
054:
055: /**
056: * Lucene Field specification
057: */
058: protected boolean store;
059:
060: protected boolean index;
061:
062: // futur lucene 1.9: protected Field.Store store;
063: // futur lucene 1.9: protected Field.Index index;
064:
065: protected FieldDefinition(String name, String type)
066: throws IllegalArgumentException {
067: this (name, stringTotype(type));
068: }
069:
070: protected FieldDefinition(String name, int type)
071: throws IllegalArgumentException {
072: this (name, type, false);
073: }
074:
075: public static FieldDefinition create(String name, int type) {
076: FieldDefinition field = null;
077:
078: if (name == null || name.trim().equals("")) {
079: throw new IllegalArgumentException("name cannot be empty");
080: }
081: switch (type) {
082: case TEXT:
083: case KEYWORD:
084: field = new StringFieldDefinition(name, type);
085: break;
086: case DATE:
087: field = new DateFieldDefinition(name);
088: break;
089: default:
090: throw new IllegalArgumentException("type not allowed");
091: }
092: return field;
093: }
094:
095: /**
096: *
097: * @param name
098: * String field's name
099: * @param type
100: * int indexation type
101: * @param store
102: * boolean store value in the index
103: * @throws IllegalArgumentException
104: */
105: private FieldDefinition(String name, int type, boolean store)
106: throws IllegalArgumentException {
107:
108: this .name = name.intern();
109: setType(type);
110: setStore(store);
111: }
112:
113: public int hashCode() {
114: return name.hashCode() * this .type;
115: }
116:
117: public void setStore(boolean store) {
118: // for futur lucene1.9
119: // this.store=(store)?Field.Store.YES:Field.Store.NO;
120: this .store = store;
121: }
122:
123: public boolean getStore() {
124: // for futur lucene1.9 return this.store==Field.Store.YES;
125: return store;
126: }
127:
128: public boolean equals(FieldDefinition fielddef) {
129: if (name.equals(fielddef.name())
130: && getType() == fielddef.getType()) {
131: return true;
132: } else {
133: return false;
134: }
135: }
136:
137: public boolean equals(Object object) {
138: if (object instanceof FieldDefinition) {
139: return equals((FieldDefinition) object);
140: } else {
141: return false;
142: }
143: }
144:
145: public String name() {
146: return name;
147: }
148:
149: /**
150: * Create Lucene Field
151: *
152: * @param value
153: * String value to store in the lucene field
154: * @return Field
155: */
156: public abstract Field createLField(String value);
157:
158: public int getType() {
159: return type;
160: }
161:
162: /**
163: * Set the type of the FieldDefinition (DATE,TEXT,KEYWORD)
164: *
165: * @param type
166: * int
167: * @throws IllegalArgumentException
168: */
169: private void setType(int type) throws IllegalArgumentException {
170: switch (type) {
171: case FieldDefinition.TEXT:
172: index = true;
173: break;
174: case FieldDefinition.DATE:
175: index = true;
176: break;
177: case FieldDefinition.KEYWORD:
178: index = false;
179: break;
180: default:
181: throw new IllegalArgumentException("type not allowed");
182: }
183: this .type = type;
184: }
185:
186: public final String toString() {
187: StringBuffer b = new StringBuffer();
188: b.append("name: " + name);
189: b.append(", type: " + FieldDefinition.STRING_TYPE[type]);
190: b.append(", store: " + getStore());
191: return b.toString();
192: }
193:
194: /**
195: * Convert String to type
196: *
197: * @param typename
198: * String
199: * @return int
200: */
201: static final public int stringTotype(String typename)
202: throws IllegalArgumentException {
203: for (int i = 0; i < STRING_TYPE.length; i++) {
204: if (typename.toLowerCase().equals(STRING_TYPE[i])) {
205: return i;
206: }
207: }
208: throw new IllegalArgumentException("type " + typename
209: + " is not allowed");
210: }
211:
212: }
|