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: */
017: package org.apache.cocoon.components.modules.input;
018:
019: import org.apache.avalon.framework.configuration.Configuration;
020: import org.apache.avalon.framework.configuration.ConfigurationException;
021: import org.apache.avalon.framework.thread.ThreadSafe;
022:
023: import java.text.DateFormat;
024: import java.text.SimpleDateFormat;
025: import java.util.Iterator;
026: import java.util.Map;
027:
028: /**
029: * Parses a date string according to a given format and returns a date
030: * object. Configuration options: element "format" to hold a {@link
031: * java.text.SimpleDateFormat} format string, child element
032: * "input-module" holds InputModule to obtain the string from.
033: *
034: * @author <a href="mailto:haul@apache.org">Christian Haul</a>
035: * @version CVS $Id: DateMetaInputModule.java 433543 2006-08-22 06:22:54Z crossley $
036: */
037: public class DateMetaInputModule extends AbstractMetaModule implements
038: ThreadSafe {
039:
040: private String defaultFormat = "yyyy-MM-dd";
041: private DateFormat defaultFormatter = null;
042:
043: public void configure(Configuration config)
044: throws ConfigurationException {
045:
046: this .inputConf = config.getChild("input-module");
047: this .defaultInput = this .inputConf.getAttribute("name",
048: this .defaultInput);
049: this .defaultFormat = this .inputConf.getAttribute("format",
050: this .defaultFormat);
051: if (this .defaultFormat != null) {
052: this .defaultFormatter = new SimpleDateFormat(
053: this .defaultFormat);
054: }
055: }
056:
057: public Object[] getAttributeValues(String name,
058: Configuration modeConf, Map objectModel)
059: throws ConfigurationException {
060:
061: if (!this .initialized) {
062: this .lazy_initialize();
063: }
064: if (this .defaultInput == null) {
065: if (getLogger().isWarnEnabled())
066: getLogger().warn("No input module given");
067: }
068:
069: // obtain correct configuration objects
070: // default vs dynamic
071: Configuration mConf = null;
072: String inputName = null;
073: String parameter = name;
074: String format = this .defaultFormat;
075: DateFormat formatter = null;
076: if (modeConf != null) {
077: mConf = modeConf.getChild("input-module");
078: inputName = modeConf.getChild("input-module").getAttribute(
079: "name", null);
080: parameter = modeConf.getAttribute("parameter", parameter);
081: format = modeConf.getAttribute("format", format);
082: // preferred:
083: parameter = modeConf.getChild("parameter").getValue(
084: parameter);
085: format = modeConf.getChild("format").getValue(format);
086: }
087: if (this .defaultFormat.equals(format)) {
088: formatter = this .defaultFormatter;
089: } else {
090: formatter = new SimpleDateFormat(format);
091: }
092:
093: Object[] values = getValues(parameter, objectModel, this .input,
094: this .defaultInput, this .inputConf, null, inputName,
095: mConf);
096:
097: Object[] dates = null;
098: if (values != null) {
099: dates = new Object[values.length];
100: for (int i = 0; i < values.length; i++)
101: try {
102: dates[i] = formatter.parse(String
103: .valueOf(values[i]));
104: } catch (Exception e) {
105: if (getLogger().isWarnEnabled())
106: getLogger().warn(
107: "Problem: Aquired '" + values[i]
108: + "' from '" + inputName
109: + "' for '" + name
110: + "' using format '" + format
111: + "' : " + e.getMessage());
112: }
113: }
114: return dates;
115: }
116:
117: public Iterator getAttributeNames(Configuration modeConf,
118: Map objectModel) throws ConfigurationException {
119:
120: if (!this .initialized) {
121: this .lazy_initialize();
122: }
123: if (this .defaultInput == null) {
124: if (getLogger().isWarnEnabled())
125: getLogger().warn("No input module given");
126: }
127:
128: // obtain correct configuration objects
129: // default vs dynamic
130: Configuration inputConfig = this .inputConf;
131: Configuration mConf = null;
132: String inputName = null;
133: if (modeConf != null) {
134: mConf = modeConf.getChild("input-module");
135: inputName = modeConf.getChild("input-module").getAttribute(
136: "name", null);
137: if (inputName != null) {
138: inputConfig = modeConf.getChild("input-module");
139: }
140: }
141:
142: // done reading configuration
143: // setup modules and read values
144: Iterator enumeration = getNames(objectModel, this .input,
145: this .defaultInput, inputConfig, null, inputName, mConf);
146: return enumeration;
147: }
148:
149: public Object getAttribute(String name, Configuration modeConf,
150: Map objectModel) throws ConfigurationException {
151:
152: Object[] values = this .getAttributeValues(name, modeConf,
153: objectModel);
154: return (values != null ? values[0] : null);
155: }
156:
157: }
|