KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 1999-2004 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 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 JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.SortedSet JavaDoc;
28 import java.util.TreeSet JavaDoc;
29
30 /**
31  * Constructs an array of values suitable for a JDBC collection type
32  * from parameters obtained from another input module. Application is
33  * not limited to JDBC collections but can be used wherever similar
34  * named attributes shall be collected to an array of a given
35  * type. Currently, long, int, and string are known, more to come.
36  *
37  * <p><b>Global and local configuration</b></p>
38  * <table border="1">
39  * <tr><td><code>input-module</code></td><td>Name of the input module used to obtain the value and its configuration</td></tr>
40  * <tr><td><code>member</code></td> <td>Collection member <table
41  * <tr><td>Attribute</td><td></td></tr>
42  * <tr><td>name</td><td>Parameter name, "*" may distinguish multiple collections</td></tr>
43  * <tr><td>type</td><td>JDBC type name of members</td></tr>
44  * </table> </td></tr>
45  * </table>
46  *
47  * @author <a HREF="mailto:haul@apache.org">Christian Haul</a>
48  * @version CVS $Id: CollectionMetaModule.java 30932 2004-07-29 17:35:38Z vgritsenko $
49  */

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 JavaDoc getAttribute( String JavaDoc name, Configuration modeConf, Map JavaDoc 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         // obtain correct configuration objects
76
// default vs dynamic
77
Configuration mConf = this.memberConf;
78         Configuration inputConfig = null;
79         String JavaDoc 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         // read necessary parameters
89
// name is used only if parameter name contains '*'
90
// in that case it replaces '*' otherwise it is
91
// ignored
92
String JavaDoc jType = mConf.getAttribute("type","string");
93         String JavaDoc pName = mConf.getAttribute("name");
94         int index = pName.indexOf("*");
95         if (index>-1) {
96             String JavaDoc prefix = (index > 0 ? pName.substring(0,index) : null);
97             String JavaDoc 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 JavaDoc[] values = getValues(pName, objectModel,
104                                     this.input, this.defaultInput, this.inputConf,
105                                     null, inputName, inputConfig);
106         Object JavaDoc[] objects = null;
107
108         if (values != null) {
109             
110             objects = new Object JavaDoc[values.length];
111             
112             for (int i = 0; i<values.length; i++) {
113                 Object JavaDoc value = values[i];
114                 objects[i] = JDBCTypeConversions.convert(value, jType);
115             }
116         }
117
118         return objects;
119     }
120
121
122     public Iterator JavaDoc getAttributeNames( Configuration modeConf, Map JavaDoc 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         // obtain correct configuration objects
135
// default vs dynamic
136

137         Configuration mConf = this.memberConf;
138         Configuration inputConfig = null;
139         String JavaDoc 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 JavaDoc names = getNames(objectModel,
149                                   this.input, this.defaultInput, this.inputConf,
150                                   null, inputName, inputConfig);
151
152         if (names != null) {
153             SortedSet JavaDoc matchset = new TreeSet JavaDoc();
154             String JavaDoc pName = mConf.getAttribute("name");
155             int index = pName.indexOf("*");
156
157             if (index>-1) {
158                 // parameter name contains '*'
159
// find all strings that match this '*'
160
// return them in an enumeration
161

162                 String JavaDoc prefix = (index > 0 ? pName.substring(0,index) : null);
163                 String JavaDoc suffix = (index < (pName.length() -1) ? pName.substring(index+1,pName.length()) : null);
164                 
165                 while (names.hasNext()) {
166                     String JavaDoc name = (String JavaDoc)names.next();
167                     if (name.startsWith(prefix) && name.endsWith(suffix)) {
168                         String JavaDoc wildcard = name.substring(prefix.length());
169                         wildcard = wildcard.substring(0,wildcard.length()-suffix.length());
170                         matchset.add(wildcard);
171                     }
172                 }
173             } else {
174                 // parameter name without wildcard
175
// check that name is among available names
176
// and return it in that case
177
boolean done=false;
178                 while (!done && names.hasNext()) {
179                     String JavaDoc name = (String JavaDoc)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 JavaDoc[] getAttributeValues( String JavaDoc name, Configuration modeConf, Map JavaDoc objectModel )
194         throws ConfigurationException {
195
196         Iterator JavaDoc names = this.getAttributeNames( modeConf, objectModel );
197         ArrayList JavaDoc values = new ArrayList JavaDoc();
198         while (names.hasNext()) {
199             values.add(this.getAttribute((String JavaDoc) names.next(),modeConf,objectModel));
200         }
201
202         return values.toArray();
203
204     }
205 }
206
Popular Tags