1 16 package org.apache.cocoon.components.modules.input; 17 18 import org.apache.avalon.framework.configuration.Configuration; 19 import org.apache.avalon.framework.configuration.ConfigurationException; 20 import org.apache.avalon.framework.thread.ThreadSafe; 21 22 import org.apache.cocoon.util.JDBCTypeConversions; 23 24 import java.util.ArrayList ; 25 import java.util.Map ; 26 import java.util.Iterator ; 27 import java.util.SortedSet ; 28 import java.util.TreeSet ; 29 30 50 public class CollectionMetaModule extends AbstractMetaModule implements ThreadSafe { 51 52 protected Configuration memberConf = null; 53 54 55 public void configure(Configuration config) throws ConfigurationException { 56 57 this.memberConf = config.getChild("member"); 58 this.inputConf = config.getChild("input-module"); 59 this.defaultInput = this.inputConf.getAttribute("name",this.defaultInput); 60 } 61 62 63 public Object getAttribute( String name, Configuration modeConf, Map objectModel ) 64 throws ConfigurationException { 65 66 if (!this.initialized) { 67 this.lazy_initialize(); 68 } 69 if (this.defaultInput == null) { 70 if (getLogger().isWarnEnabled()) 71 getLogger().warn("No input module given. FAILING"); 72 return null; 73 } 74 75 Configuration mConf = this.memberConf; 78 Configuration inputConfig = null; 79 String inputName=null; 80 if (modeConf!=null) { 81 mConf = modeConf.getChild("member"); 82 inputName = modeConf.getChild("input-module").getAttribute("name",null); 83 if (inputName != null) { 84 inputConfig = modeConf.getChild("input-module"); 85 } 86 } 87 88 String jType = mConf.getAttribute("type","string"); 93 String pName = mConf.getAttribute("name"); 94 int index = pName.indexOf("*"); 95 if (index>-1) { 96 String prefix = (index > 0 ? pName.substring(0,index) : null); 97 String suffix = (index < (pName.length() -1) ? pName.substring(index+1,pName.length()) : null); 98 pName = prefix+name+suffix; 99 } 100 101 getLogger().debug("jType "+jType); 102 103 Object [] values = getValues(pName, objectModel, 104 this.input, this.defaultInput, this.inputConf, 105 null, inputName, inputConfig); 106 Object [] objects = null; 107 108 if (values != null) { 109 110 objects = new Object [values.length]; 111 112 for (int i = 0; i<values.length; i++) { 113 Object value = values[i]; 114 objects[i] = JDBCTypeConversions.convert(value, jType); 115 } 116 } 117 118 return objects; 119 } 120 121 122 public Iterator getAttributeNames( Configuration modeConf, Map objectModel ) 123 throws ConfigurationException { 124 125 if (!this.initialized) { 126 this.lazy_initialize(); 127 } 128 if (this.defaultInput == null) { 129 if (getLogger().isWarnEnabled()) 130 getLogger().warn("No input module given. FAILING"); 131 return null; 132 } 133 134 137 Configuration mConf = this.memberConf; 138 Configuration inputConfig = null; 139 String inputName=null; 140 if (modeConf!=null) { 141 mConf = modeConf.getChild("member"); 142 inputName = modeConf.getChild("input-module").getAttribute("name",null); 143 if (inputName != null) { 144 inputConfig = modeConf.getChild("input-module"); 145 } 146 } 147 148 Iterator names = getNames(objectModel, 149 this.input, this.defaultInput, this.inputConf, 150 null, inputName, inputConfig); 151 152 if (names != null) { 153 SortedSet matchset = new TreeSet (); 154 String pName = mConf.getAttribute("name"); 155 int index = pName.indexOf("*"); 156 157 if (index>-1) { 158 162 String prefix = (index > 0 ? pName.substring(0,index) : null); 163 String suffix = (index < (pName.length() -1) ? pName.substring(index+1,pName.length()) : null); 164 165 while (names.hasNext()) { 166 String name = (String )names.next(); 167 if (name.startsWith(prefix) && name.endsWith(suffix)) { 168 String wildcard = name.substring(prefix.length()); 169 wildcard = wildcard.substring(0,wildcard.length()-suffix.length()); 170 matchset.add(wildcard); 171 } 172 } 173 } else { 174 boolean done=false; 178 while (!done && names.hasNext()) { 179 String name = (String )names.next(); 180 if (name.equals(pName)) { 181 matchset.add(pName); 182 done = true; 183 } 184 } 185 } 186 return matchset.iterator(); 187 } else { 188 return null; 189 } 190 } 191 192 193 public Object [] getAttributeValues( String name, Configuration modeConf, Map objectModel ) 194 throws ConfigurationException { 195 196 Iterator names = this.getAttributeNames( modeConf, objectModel ); 197 ArrayList values = new ArrayList (); 198 while (names.hasNext()) { 199 values.add(this.getAttribute((String ) names.next(),modeConf,objectModel)); 200 } 201 202 return values.toArray(); 203 204 } 205 } 206 | Popular Tags |