KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > modules > input > DateMetaInputModule


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.components.modules.input;
17
18
19 import org.apache.avalon.framework.configuration.Configuration;
20 import org.apache.avalon.framework.configuration.ConfigurationException;
21 import org.apache.avalon.framework.thread.ThreadSafe;
22
23 import java.text.DateFormat JavaDoc;
24 import java.text.SimpleDateFormat JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Map JavaDoc;
27
28 /**
29  * Parses a date string according to a given format and returns a date
30  * object. Configuration options: element "format" to hold a {@link
31  * java.text.SimpleDateFormat} format string, child element
32  * "input-module" holds InputModule to obtain the string from.
33  *
34  * @author <a HREF="mailto:haul@apache.org">Christian Haul</a>
35  * @version CVS $Id: DateMetaInputModule.java 156964 2005-03-10 17:21:25Z cziegeler $
36  */

37 public class DateMetaInputModule extends AbstractMetaModule implements ThreadSafe {
38
39     private String JavaDoc defaultFormat = "yyyy-MM-dd";
40     private DateFormat JavaDoc defaultFormatter = null;
41
42     
43     public void configure(Configuration config) throws ConfigurationException {
44
45         this.inputConf = config.getChild("input-module");
46         this.defaultInput = this.inputConf.getAttribute("name",this.defaultInput);
47         this.defaultFormat = this.inputConf.getAttribute("format",this.defaultFormat);
48         if (this.defaultFormat != null) {
49             this.defaultFormatter = new SimpleDateFormat JavaDoc(this.defaultFormat);
50         }
51     }
52
53
54     public Object JavaDoc[] getAttributeValues( String JavaDoc name, Configuration modeConf, Map JavaDoc objectModel )
55         throws ConfigurationException {
56
57         if (!this.initialized) {
58             this.lazy_initialize();
59         }
60         if (this.defaultInput == null) {
61             if (getLogger().isWarnEnabled())
62                 getLogger().warn("No input module given");
63         }
64
65         // obtain correct configuration objects
66
// default vs dynamic
67
Configuration mConf = null;
68         String JavaDoc inputName=null;
69         String JavaDoc parameter=name;
70         String JavaDoc format=this.defaultFormat;
71         DateFormat JavaDoc formatter=null;
72         if (modeConf!=null) {
73             mConf = modeConf.getChild("input-module");
74             inputName = modeConf.getChild("input-module").getAttribute("name",null);
75             parameter = modeConf.getAttribute("parameter",parameter);
76             format = modeConf.getAttribute("format",format);
77             // preferred:
78
parameter = modeConf.getChild("parameter").getValue(parameter);
79             format = modeConf.getChild("format").getValue(format);
80         }
81         if (this.defaultFormat.equals(format)) {
82             formatter = this.defaultFormatter;
83         } else {
84             formatter = new SimpleDateFormat JavaDoc(format);
85         }
86         
87         Object JavaDoc[] values = getValues(parameter, objectModel,
88                                     this.input, this.defaultInput, this.inputConf,
89                                     null, inputName, mConf);
90         
91         Object JavaDoc[] dates = null;
92         if (values != null) {
93             dates = new Object JavaDoc[values.length];
94             for (int i=0; i<values.length; i++)
95                 try {
96                     dates[i] = formatter.parse(String.valueOf(values[i]));
97                 } catch (Exception JavaDoc e) {
98                     if(getLogger().isWarnEnabled())
99                         getLogger().warn("Problem: Aquired '"+values[i]+"' from '" + inputName + "' for '"
100                                          +name+"' using format '"+format+"' : "+e.getMessage());
101                 }
102         }
103         return dates;
104     }
105
106
107
108
109
110     public Iterator JavaDoc getAttributeNames( Configuration modeConf, Map JavaDoc objectModel )
111         throws ConfigurationException {
112
113         if (!this.initialized) {
114             this.lazy_initialize();
115         }
116         if (this.defaultInput == null) {
117             if (getLogger().isWarnEnabled())
118                 getLogger().warn("No input module given");
119         }
120
121         // obtain correct configuration objects
122
// default vs dynamic
123
Configuration inputConfig = this.inputConf;
124         Configuration mConf = null;
125         String JavaDoc inputName=null;
126         if (modeConf!=null) {
127             mConf = modeConf.getChild("input-module");
128             inputName = modeConf.getChild("input-module").getAttribute("name",null);
129             if (inputName != null) {
130                 inputConfig = modeConf.getChild("input-module");
131             }
132         }
133
134         // done reading configuration
135
// setup modules and read values
136
Iterator JavaDoc enumeration = getNames(objectModel,
137                                     this.input, this.defaultInput, inputConfig,
138                                     null, inputName, mConf);
139         return enumeration;
140      }
141
142
143
144
145     public Object JavaDoc getAttribute( String JavaDoc name, Configuration modeConf, Map JavaDoc objectModel )
146         throws ConfigurationException {
147
148         Object JavaDoc[] values = this.getAttributeValues(name,modeConf,objectModel);
149         return (values != null ? values[0] : null);
150     }
151
152 }
153
Popular Tags