KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > encoding > ConstructorTarget


1 /*
2  * Copyright 2001-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.axis.encoding;
17
18 import java.lang.reflect.Constructor JavaDoc;
19 import java.util.ArrayList JavaDoc;
20 import java.util.List JavaDoc;
21
22 import org.xml.sax.SAXException JavaDoc;
23 import org.apache.axis.i18n.Messages;
24
25
26 /**
27  * Used when the class need a specific Constructor (not default one)
28  * @author Florent Benoit
29  */

30 public class ConstructorTarget implements Target {
31
32     /**
33      * Constructor to use
34      */

35     private Constructor JavaDoc constructor = null;
36     
37     /**
38      * Deserializer on which set value
39      */

40     private Deserializer deSerializer = null;
41     
42     
43     /**
44      * List of values
45      */

46     private List JavaDoc values = null;
47     
48     public ConstructorTarget(Constructor JavaDoc constructor, Deserializer deSerializer) {
49         this.deSerializer = deSerializer;
50         this.constructor = constructor;
51         values = new ArrayList JavaDoc();
52     }
53     
54     
55     /**
56      * Instantiate a new class with right constructor
57      * @param value value to use on Constructor
58      * @throws SAXException on error
59      */

60     public void set(Object JavaDoc value) throws SAXException JavaDoc {
61         try {
62             // store received value
63
values.add(value);
64
65             // got right parameter length
66
if (constructor.getParameterTypes().length == values.size()) {
67                 // type of parameters
68
Class JavaDoc[] classes = constructor.getParameterTypes();
69                 
70                 // args array
71
Object JavaDoc[] args = new Object JavaDoc[constructor.getParameterTypes().length];
72                 
73                 // Get arg for the type of the class
74
for (int c = 0; c < classes.length; c++) {
75                     boolean found = false;
76                     int i = 0;
77                     while (!found && i < values.size()) {
78                          // got right class arg
79
if (values.get(i).getClass().getName().toLowerCase().indexOf(classes[c].getName().toLowerCase()) != -1) {
80                             found = true;
81                             args[c] = values.get(i);
82                         }
83                         i++;
84
85                     }
86                     // no suitable object for class required
87
if (!found) {
88                         throw new SAXException JavaDoc(Messages.getMessage("cannotFindObjectForClass00", classes[c].toString()));
89                     }
90                 }
91                 
92                 // then build object
93
Object JavaDoc o = constructor.newInstance(args);
94                 deSerializer.setValue(o);
95             }
96         } catch (Exception JavaDoc e) {
97             throw new SAXException JavaDoc(e);
98         }
99
100     }
101
102 }
103
Popular Tags