1 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 ; 24 import java.text.SimpleDateFormat ; 25 import java.util.Iterator ; 26 import java.util.Map ; 27 28 37 public class DateMetaInputModule extends AbstractMetaModule implements ThreadSafe { 38 39 private String defaultFormat = "yyyy-MM-dd"; 40 private DateFormat 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 (this.defaultFormat); 50 } 51 } 52 53 54 public Object [] getAttributeValues( String name, Configuration modeConf, Map 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 Configuration mConf = null; 68 String inputName=null; 69 String parameter=name; 70 String format=this.defaultFormat; 71 DateFormat 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 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 (format); 85 } 86 87 Object [] values = getValues(parameter, objectModel, 88 this.input, this.defaultInput, this.inputConf, 89 null, inputName, mConf); 90 91 Object [] dates = null; 92 if (values != null) { 93 dates = new Object [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 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 getAttributeNames( Configuration modeConf, Map 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 Configuration inputConfig = this.inputConf; 124 Configuration mConf = null; 125 String 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 Iterator enumeration = getNames(objectModel, 137 this.input, this.defaultInput, inputConfig, 138 null, inputName, mConf); 139 return enumeration; 140 } 141 142 143 144 145 public Object getAttribute( String name, Configuration modeConf, Map objectModel ) 146 throws ConfigurationException { 147 148 Object [] values = this.getAttributeValues(name,modeConf,objectModel); 149 return (values != null ? values[0] : null); 150 } 151 152 } 153 | Popular Tags |