01: /**
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */package org.apache.cocoon.components.search.fieldmodel;
17:
18: import java.text.ParseException;
19: import java.text.SimpleDateFormat;
20: import java.util.Date;
21:
22: import org.apache.lucene.document.DateField;
23: import org.apache.lucene.document.Field;
24:
25: /**
26: * Field Definition for Date type
27: *
28: * @author Nicolas Maisonneuve
29: */
30: public class DateFieldDefinition extends FieldDefinition {
31:
32: private SimpleDateFormat df;
33:
34: /**
35: * @param name
36: * name of the field
37: */
38: public DateFieldDefinition(String name) {
39: super (name, DATE);
40: }
41:
42: /**
43: * Set the date format to parse string date in the
44: *
45: * @see #createLField(String) method
46: * @param df
47: */
48: public void setDateFormat(SimpleDateFormat df) {
49: this .df = df;
50: }
51:
52: /**
53: * @return the dateformat
54: */
55: public SimpleDateFormat getDateFormat() {
56: return df;
57: }
58:
59: /**
60: * Create a Lucene Field
61: *
62: * @param dateString
63: * String date in string format
64: * @return A field.
65: * @see org.apache.lucene.document.Field
66: *
67: */
68: public final Field createLField(String dateString)
69: throws IllegalArgumentException {
70: Date date = null;
71: try {
72: date = df.parse(dateString);
73: } catch (ParseException ex) {
74: throw new IllegalArgumentException(ex.getMessage());
75: }
76: return createLField(date);
77: }
78:
79: /**
80: * Create Lucene Field
81: *
82: * @param date
83: * the date
84: * @return A field.
85: * @see org.apache.lucene.document.Field
86: *
87: */
88: public final Field createLField(Date date) {
89: return new Field(name, DateField.dateToString(date), store,
90: true, index);
91: }
92:
93: }
|