KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > myeis > j2c > formathandler > MyEISFormatHandlerGenerator


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2002 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "WSIF" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 2001, 2002, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57
58 package com.myeis.j2c.formathandler;
59
60 import java.beans.BeanInfo JavaDoc;
61 import java.beans.Introspector JavaDoc;
62 import java.beans.PropertyDescriptor JavaDoc;
63 import java.io.ByteArrayInputStream JavaDoc;
64 import java.io.IOException JavaDoc;
65 import java.io.InputStream JavaDoc;
66 import java.io.InputStreamReader JavaDoc;
67 import java.lang.reflect.Method JavaDoc;
68 import java.util.Hashtable JavaDoc;
69
70 import javax.wsdl.Binding;
71 import javax.wsdl.Definition;
72 import javax.xml.namespace.QName JavaDoc;
73 /*
74 * The class implementing FormatHandlerGenerator interface that
75 * provides view for generating a Custom Format Handler for a particular XSD Type or Element.
76 * @param <code>generationPackage</code> The fully qualified package that the generator should use to generate its classes in.
77 * @param <code>beanClass</code> The fully qualified name of the Java bean class that represents the XSD type (or element) for which a format handler is being generated.
78 * @param <code>aDefinition</code> The WSDL definition from which the XSD type is derived.
79 * @param <code>aBinding</code> The WSDL binding element that contains the type mapping for the XSD type that is being used.
80 * @param <code>anEncoding</code> The <i>encoding</i> attribute off the TypeMapping element associated with the format handler to be generated
81 * @param <code>aStyle</code> The <i>style</i> attribute off the TypeMapping element associated with the format handler to be generated
82 * @param <code>xsdQname</code> The QName of the XSD element(or type) for which the format handler is being generated
83 * @param <code>elementType</code> The type of element represented by the <code>xsdQname</code> parameter. This is either <code>CustomFormatHandlerGenerator.XSD_TYPE</code> or <code>CustomFormatHandlerGenerator.XSD_ELEMENT</code>
84 * @return a <code>HashTable</code> that contains a set of <code>java.io.InputStream</code> s. Each input stream contains the generated code for a particular
85 * class generated by the format handler generator. The number of input streams returned depends on the number of classes that are generated by the format handler
86 * generator. The keys of the HashTable must be the name of each class (unqualified) that is represented by its corresponding InputStream value. The class name keys
87 * are unqualified because they must all be in the <code>generationPackage</code> that is passed in as an input parameter.
88 */

89
90 import org.apache.wsif.WSIFException;
91 import org.apache.wsif.providers.jca.toolplugin.FormatHandlerGenerator;
92 import org.apache.wsif.util.WSIFUtils;
93
94 public class MyEISFormatHandlerGenerator implements FormatHandlerGenerator {
95
96     /**
97      * @see org.apache.wsif.providers.jca.toolplugin.FormatHandlerGenerator#generate(String, String, Definition, Binding, String, String, QName, int, Object)
98      */

99     public Hashtable JavaDoc generate(
100         String JavaDoc generationPackage,
101         String JavaDoc beanClass,
102         javax.wsdl.Definition aDefinition,
103         javax.wsdl.Binding aBinding,
104         String JavaDoc anEncoding,
105         String JavaDoc aStyle,
106         javax.xml.namespace.QName JavaDoc xsdQname,
107         int elementType,
108         Object JavaDoc generationContext) throws WSIFException
109         {
110
111         if (!anEncoding.equals("myeis")) {
112             System.err.println("Encoding: " + anEncoding);
113             return null;
114         }
115
116         ClassLoader JavaDoc threadClassLoader = Thread.currentThread().getContextClassLoader();
117         String JavaDoc definitionPackageName = WSIFUtils.getPackageNameFromNamespaceURI(aDefinition.getTargetNamespace());
118         definitionPackageName = definitionPackageName.replace('.','/');
119         String JavaDoc metaDataFile = "com/myeis/services/MyEISFormatHandlerGenMetaData.eis";
120         InputStream JavaDoc metaDataStream = threadClassLoader.getResourceAsStream(metaDataFile);
121         String JavaDoc metaDatafield = null;
122         
123         if (metaDataStream != null) {
124             try {
125                 //Read in the contents of the stream
126
InputStreamReader JavaDoc reader = new InputStreamReader JavaDoc(metaDataStream);
127                 char buffer[] = new char[1024];
128                 StringBuffer JavaDoc data = new StringBuffer JavaDoc();
129                 int length;
130                 length = reader.read(buffer);
131                 while (length > 0) {
132                     data.append(buffer, 0, length);
133                     length = reader.read(buffer);
134                 }
135                 metaDatafield = data.toString();
136             } catch (IOException JavaDoc e) {
137                 //Error occured reading metadata
138
throw new WSIFException("Error occurred reading metadata needed for format handler generator!",e);
139             }
140
141         }
142
143         String JavaDoc beanClassName = beanClass.substring(beanClass.lastIndexOf(".") + 1, beanClass.length());
144         String JavaDoc beanClassField = "field" + beanClassName;
145         String JavaDoc formatHandlerClassName = beanClassName + "FormatHandler";
146
147         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
148
149         buffer.append("package " + generationPackage + ";\n");
150         buffer.append(importsGen);
151         buffer.append("public class " + beanClassName + "FormatHandler implements WSIFFormatHandler_JCA {\n");
152
153         buffer.append("private " + beanClassName + " " + beanClassField + " = null;\n");
154         
155         if(metaDatafield!=null && metaDatafield.length()>0){
156             buffer.append("private static String META_DATA_A = \""+ metaDatafield+"\";\n");
157         }
158         
159
160         buffer.append("public void read(InputStream inputStream) throws IOException {\n");
161         buffer.append(" try {\n");
162         buffer.append(" ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);\n");
163         buffer.append(" Object obj = (Object)objectInputStream.readObject();\n");
164         buffer.append(" if(obj instanceof "+beanClassName+"){\n");
165         buffer.append(" this."+ beanClassField +" = ("+ beanClassName + ")obj;\n");
166         buffer.append(" }else if (obj instanceof com.myeis.services.internal."+beanClassName+"DataObject){\n");
167         buffer.append(" com.myeis.services.internal."+ beanClassName + "DataObject a"+ beanClassName + " = (com.myeis.services.internal."+ beanClassName + "DataObject)obj;\n");
168         buffer.append(" this."+ beanClassField +" = new "+ beanClassName +"();\n");
169         
170         try {
171             Class JavaDoc bean =
172                 Class.forName("com.myeis.services.internal." + beanClassName+"DataObject");
173             BeanInfo JavaDoc bInfo = Introspector.getBeanInfo(bean);
174             if (bInfo != null) {
175                 PropertyDescriptor JavaDoc properties[] =
176                     bInfo.getPropertyDescriptors();
177                 for (int i = 0; i < properties.length; i++) {
178                     Method JavaDoc writer = properties[i].getWriteMethod();
179                     Method JavaDoc reader = properties[i].getReadMethod();
180                     if (reader != null && writer != null) {
181                         String JavaDoc setter = writer.getName();
182                         String JavaDoc getter = reader.getName();
183                         buffer.append(
184                             " this."
185                                 + beanClassField
186                                 + "."
187                                 + setter
188                                 + "(a"
189                                 + beanClassName
190                                 + "."
191                                 + getter
192                                 + "());\n");
193                     }
194                 }
195             }
196         } catch (Exception JavaDoc e) {
197 // throw new WSIFException(
198
// "Error occurred introspecting bean properties in format handler generator!",
199
// e);
200

201             buffer.append(" String ex ="+e+";\n");
202         }
203     
204         buffer.append(" }\n");
205         buffer.append(" } catch (ClassNotFoundException e) {\n");
206         buffer.append(" e.printStackTrace();\n");
207         buffer.append(" throw new IOException(\"class not found !\");\n");
208         buffer.append(" } \n");
209         buffer.append("}\n\n");
210         buffer.append("public void write(OutputStream outputStream) throws IOException {\n");
211
212         buffer.append(" ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);\n");
213         buffer.append(" objectOutputStream.writeObject(this." + beanClassField + ");\n");
214         buffer.append(" objectOutputStream.flush();\n");
215         buffer.append("}\n\n");
216
217         buffer.append("public void setObjectPart(Object objectPart) {\n");
218         buffer.append(" this." + beanClassField + " = (" + beanClassName + ")objectPart;\n");
219         buffer.append("}\n\n");
220         buffer.append("public Object getObjectPart() {\n");
221         buffer.append(" return this." + beanClassField + ";\n");
222         buffer.append("}\n\n");
223
224         buffer.append("public Object getObjectPart(Class sourceClass) {\n");
225         buffer.append(" try{\n");
226         buffer.append(" if (sourceClass.isAssignableFrom(Class.forName(\"javax.xml.transform.sax.SAXSource\"))){\n");
227         buffer.append(" SAXSource saxSource = new SAXSource(new InputSource());\n");
228         buffer.append(" XMLReader reader = new ContentReader();\n");
229         buffer.append(" saxSource.setXMLReader(reader);\n");
230         buffer.append(" return saxSource;\n");
231         buffer.append(" }\n");
232         buffer.append(" }\n");
233         buffer.append(" catch (Throwable e) {\n");
234         buffer.append(" }\n");
235
236         buffer.append(" return null;\n");
237         buffer.append("}\n\n");
238
239         buffer.append(defaultMethodsGen);
240
241         writeContentReader(buffer, beanClassField);
242         buffer.append("}"); // end of class
243

244         ByteArrayInputStream JavaDoc inputStream = new ByteArrayInputStream JavaDoc(buffer.toString().getBytes());
245         Hashtable JavaDoc returnTable = new Hashtable JavaDoc();
246         returnTable.put(formatHandlerClassName, inputStream);
247         return returnTable;
248
249     }
250
251     private void writeContentReader(StringBuffer JavaDoc buffer, String JavaDoc beanClassField) {
252         buffer.append("public class ContentReader implements org.xml.sax.XMLReader {\n");
253         buffer.append(" private ContentHandler fieldContentHandler = null;\n");
254
255         buffer.append(" public org.xml.sax.ContentHandler getContentHandler() {return null;}\n");
256         buffer.append(" public org.xml.sax.DTDHandler getDTDHandler() {return null;}\n");
257         buffer.append(" public org.xml.sax.EntityResolver getEntityResolver() {return null;}\n");
258         buffer.append(" public org.xml.sax.ErrorHandler getErrorHandler() {return null;}\n");
259         buffer.append(" public boolean getFeature(String name) throws org.xml.sax.SAXNotSupportedException, org.xml.sax.SAXNotRecognizedException {return false;}\n");
260         buffer.append(" public Object getProperty(String name) throws org.xml.sax.SAXNotSupportedException, org.xml.sax.SAXNotRecognizedException {return null;}\n");
261         buffer.append(" public void setDTDHandler(org.xml.sax.DTDHandler handler) {}\n");
262         buffer.append(" public void setEntityResolver(org.xml.sax.EntityResolver resolver) {}\n");
263         buffer.append(" public void setErrorHandler(org.xml.sax.ErrorHandler handler) {}\n");
264         buffer.append(" public void setFeature(String name, boolean value) throws org.xml.sax.SAXNotSupportedException, org.xml.sax.SAXNotRecognizedException {}\n");
265         buffer.append(" public void setProperty(String name, Object value) throws org.xml.sax.SAXNotSupportedException, org.xml.sax.SAXNotRecognizedException {}\n");
266         buffer.append(" public void parse(String systemId) throws java.io.IOException, org.xml.sax.SAXException {}\n");
267         buffer.append(" public void parse(org.xml.sax.InputSource input) throws java.io.IOException, org.xml.sax.SAXException {\n");
268         buffer.append(" try {\n");
269         buffer.append(" Attributes attributes = new AttributesImpl();\n");
270         buffer.append(" this.fieldContentHandler.startDocument();\n");
271
272         if (beanClassField.equals("fieldCustomer")) {
273
274             buffer.append(" this.fieldContentHandler.startElement(\"\", \"number\", \"number\", attributes);\n");
275             buffer.append(" this.fieldContentHandler.characters(" + beanClassField + ".getNumber().toCharArray(), 0, " + beanClassField + ".getNumber().length());\n");
276             buffer.append(" this.fieldContentHandler.endElement(\"\", \"number\", \"number\");\n");
277             buffer.append(" this.fieldContentHandler.startElement(\"\", \"firstName\", \"firstName\", attributes);\n");
278             buffer.append(" this.fieldContentHandler.characters(" + beanClassField + ".getFirstName().toCharArray(), 0, " + beanClassField + ".getFirstName().length());\n");
279             buffer.append(" this.fieldContentHandler.endElement(\"\", \"firstName\", \"firstName\");\n");
280             buffer.append(" this.fieldContentHandler.startElement(\"\", \"lastName\", \"lastName\", attributes);\n");
281             buffer.append(" this.fieldContentHandler.characters(" + beanClassField + ".getLastName().toCharArray(), 0, " + beanClassField + ".getLastName().length());\n");
282             buffer.append(" this.fieldContentHandler.endElement(\"\", \"lastName\", \"lastName\");\n");
283         } else if (beanClassField.equals("fieldPurchaseOrder")) {
284             buffer.append(" this.fieldContentHandler.startElement(\"\", \"orderNumber\", \"orderNumber\", attributes);\n");
285             buffer.append(" this.fieldContentHandler.characters(" + beanClassField + ".getOrderNumber().toCharArray(), 0, " + beanClassField + ".getOrderNumber().length());\n");
286             buffer.append(" this.fieldContentHandler.endElement(\"\", \"orderNumber\", \"orderNumber\");\n");
287
288             buffer.append(" this.fieldContentHandler.startElement(\"\", \"description\", \"description\", attributes);\n");
289             buffer.append(" this.fieldContentHandler.characters(" + beanClassField + ".getDescription().toCharArray(), 0, " + beanClassField + ".getDescription().length());\n");
290             buffer.append(" this.fieldContentHandler.endElement(\"\", \"description\", \"description\");\n");
291
292             buffer.append(" this.fieldContentHandler.startElement(\"\", \"partNumber\", \"partNumber\", attributes);\n");
293             buffer.append(" this.fieldContentHandler.characters(" + beanClassField + ".getPartNumber().toCharArray(), 0, " + beanClassField + ".getPartNumber().length());\n");
294             buffer.append(" this.fieldContentHandler.endElement(\"\", \"partNumber\", \"partNumber\");\n");
295
296         } else if (beanClassField.equals("fieldAddress")) {
297             buffer.append(" this.fieldContentHandler.startElement(\"\", \"customerNumber\", \"customerNumber\", attributes);\n");
298             buffer.append(" this.fieldContentHandler.characters(" + beanClassField + ".getCustomerNumber().toCharArray(), 0, " + beanClassField + ".getCustomerNumber().length());\n");
299             buffer.append(" this.fieldContentHandler.endElement(\"\", \"customerNumber\", \"customerNumber\");\n");
300
301             buffer.append(" this.fieldContentHandler.startElement(\"\", \"city\", \"city\", attributes);\n");
302             buffer.append(" this.fieldContentHandler.characters(" + beanClassField + ".getCity().toCharArray(), 0, " + beanClassField + ".getCity().length());\n");
303             buffer.append(" this.fieldContentHandler.endElement(\"\", \"city\", \"city\");\n");
304
305             buffer.append(" this.fieldContentHandler.startElement(\"\", \"state\", \"state\", attributes);\n");
306             buffer.append(" this.fieldContentHandler.characters(" + beanClassField + ".getState().toCharArray(), 0, " + beanClassField + ".getState().length());\n");
307             buffer.append(" this.fieldContentHandler.endElement(\"\", \"state\", \"state\");\n");
308
309             buffer.append(" this.fieldContentHandler.startElement(\"\", \"street\", \"street\", attributes);\n");
310             buffer.append(" this.fieldContentHandler.characters(" + beanClassField + ".getStreet().toCharArray(), 0, " + beanClassField + ".getStreet().length());\n");
311             buffer.append(" this.fieldContentHandler.endElement(\"\", \"street\", \"street\");\n");
312
313             buffer.append(" this.fieldContentHandler.startElement(\"\", \"zipCode\", \"zipCode\", attributes);\n");
314             buffer.append(" this.fieldContentHandler.characters(" + beanClassField + ".getZipCode().toCharArray(), 0, " + beanClassField + ".getZipCode().length());\n");
315             buffer.append(" this.fieldContentHandler.endElement(\"\", \"zipCode\", \"zipCode\");\n");
316
317         }
318
319         buffer.append(" this.fieldContentHandler.endDocument();\n");
320         buffer.append(" } catch (Exception e) {\n");
321         buffer.append(" e.printStackTrace();\n");
322         buffer.append(" } \n");
323         buffer.append(" }\n");
324         buffer.append(" public void setContentHandler(org.xml.sax.ContentHandler handler) {\n");
325         buffer.append(" this.fieldContentHandler = handler;\n");
326         buffer.append(" }\n");
327         buffer.append("} \n");
328
329     }
330
331     private static final String JavaDoc importsGen = "import java.io.*;\n" + "import com.myeis.services.*;\n" + "import org.apache.wsif.providers.jca.WSIFFormatHandler_JCA;\n" +
332     "import javax.xml.transform.Transformer;\n" + "import javax.xml.transform.TransformerFactory;\n" + "import javax.xml.transform.sax.SAXSource;\n" +
333     "import org.xml.sax.XMLReader;\n" +
334     "import org.xml.sax.Attributes;\n" + "import org.xml.sax.ContentHandler;\n" + "import org.xml.sax.InputSource;\n" + "import org.xml.sax.helpers.AttributesImpl;\n\n\n\n";
335
336     private static final String JavaDoc defaultMethodsGen =
337         "public Object getElement(String elementName) {return null;}\n"
338             + "public void setElement(String elementName, Object element) {}\n"
339             + "public Object getElement(String name, int index) {return null;}\n"
340             + "public void setElement(String name, int index, Object value) {}\n"
341             + "public javax.xml.namespace.QName getPartQName(){\n"
342             + " return null;\n"
343             + " }\n"
344             + " \n"
345             + " public void setPartQName(javax.xml.namespace.QName qname){\n"
346             + " }\n"
347             + "public void setInteractionSpec(javax.resource.cci.InteractionSpec arg0) {\n"
348             + " return;\n"
349             + " }\n";
350     /**
351      * @see org.apache.wsif.providers.jca.toolplugin.FormatHandlerGenerator#createGenerationContext()
352      */

353     public Object JavaDoc createGenerationContext() {
354         return null;
355     }
356 }
Popular Tags