001: /*
002: * The Apache Software License, Version 1.1
003: *
004: *
005: * Copyright (c) 2002 The Apache Software Foundation. All rights
006: * reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: *
012: * 1. Redistributions of source code must retain the above copyright
013: * notice, this list of conditions and the following disclaimer.
014: *
015: * 2. Redistributions in binary form must reproduce the above copyright
016: * notice, this list of conditions and the following disclaimer in
017: * the documentation and/or other materials provided with the
018: * distribution.
019: *
020: * 3. The end-user documentation included with the redistribution,
021: * if any, must include the following acknowledgment:
022: * "This product includes software developed by the
023: * Apache Software Foundation (http://www.apache.org/)."
024: * Alternately, this acknowledgment may appear in the software itself,
025: * if and wherever such third-party acknowledgments normally appear.
026: *
027: * 4. The names "WSIF" and "Apache Software Foundation" must
028: * not be used to endorse or promote products derived from this
029: * software without prior written permission. For written
030: * permission, please contact apache@apache.org.
031: *
032: * 5. Products derived from this software may not be called "Apache",
033: * nor may "Apache" appear in their name, without prior written
034: * permission of the Apache Software Foundation.
035: *
036: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
040: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: * ====================================================================
049: *
050: * This software consists of voluntary contributions made by many
051: * individuals on behalf of the Apache Software Foundation and was
052: * originally based on software copyright (c) 2001, 2002, International
053: * Business Machines, Inc., http://www.apache.org. For more
054: * information on the Apache Software Foundation, please see
055: * <http://www.apache.org/>.
056: */
057:
058: package com.myeis.j2c.formathandler;
059:
060: import java.beans.BeanInfo;
061: import java.beans.Introspector;
062: import java.beans.PropertyDescriptor;
063: import java.io.ByteArrayInputStream;
064: import java.io.IOException;
065: import java.io.InputStream;
066: import java.io.InputStreamReader;
067: import java.lang.reflect.Method;
068: import java.util.Hashtable;
069:
070: import javax.wsdl.Binding;
071: import javax.wsdl.Definition;
072: import javax.xml.namespace.QName; /*
073: * The class implementing FormatHandlerGenerator interface that
074: * provides view for generating a Custom Format Handler for a particular XSD Type or Element.
075: * @param <code>generationPackage</code> The fully qualified package that the generator should use to generate its classes in.
076: * @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.
077: * @param <code>aDefinition</code> The WSDL definition from which the XSD type is derived.
078: * @param <code>aBinding</code> The WSDL binding element that contains the type mapping for the XSD type that is being used.
079: * @param <code>anEncoding</code> The <i>encoding</i> attribute off the TypeMapping element associated with the format handler to be generated
080: * @param <code>aStyle</code> The <i>style</i> attribute off the TypeMapping element associated with the format handler to be generated
081: * @param <code>xsdQname</code> The QName of the XSD element(or type) for which the format handler is being generated
082: * @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>
083: * @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
084: * 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
085: * 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
086: * are unqualified because they must all be in the <code>generationPackage</code> that is passed in as an input parameter.
087: */
088:
089: import org.apache.wsif.WSIFException;
090: import org.apache.wsif.providers.jca.toolplugin.FormatHandlerGenerator;
091: import org.apache.wsif.util.WSIFUtils;
092:
093: public class MyEISFormatHandlerGenerator implements
094: FormatHandlerGenerator {
095:
096: /**
097: * @see org.apache.wsif.providers.jca.toolplugin.FormatHandlerGenerator#generate(String, String, Definition, Binding, String, String, QName, int, Object)
098: */
099: public Hashtable generate(String generationPackage,
100: String beanClass, javax.wsdl.Definition aDefinition,
101: javax.wsdl.Binding aBinding, String anEncoding,
102: String aStyle, javax.xml.namespace.QName xsdQname,
103: int elementType, Object generationContext)
104: throws WSIFException {
105:
106: if (!anEncoding.equals("myeis")) {
107: System.err.println("Encoding: " + anEncoding);
108: return null;
109: }
110:
111: ClassLoader threadClassLoader = Thread.currentThread()
112: .getContextClassLoader();
113: String definitionPackageName = WSIFUtils
114: .getPackageNameFromNamespaceURI(aDefinition
115: .getTargetNamespace());
116: definitionPackageName = definitionPackageName.replace('.', '/');
117: String metaDataFile = "com/myeis/services/MyEISFormatHandlerGenMetaData.eis";
118: InputStream metaDataStream = threadClassLoader
119: .getResourceAsStream(metaDataFile);
120: String metaDatafield = null;
121:
122: if (metaDataStream != null) {
123: try {
124: //Read in the contents of the stream
125: InputStreamReader reader = new InputStreamReader(
126: metaDataStream);
127: char buffer[] = new char[1024];
128: StringBuffer data = new StringBuffer();
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 e) {
137: //Error occured reading metadata
138: throw new WSIFException(
139: "Error occurred reading metadata needed for format handler generator!",
140: e);
141: }
142:
143: }
144:
145: String beanClassName = beanClass.substring(beanClass
146: .lastIndexOf(".") + 1, beanClass.length());
147: String beanClassField = "field" + beanClassName;
148: String formatHandlerClassName = beanClassName + "FormatHandler";
149:
150: StringBuffer buffer = new StringBuffer();
151:
152: buffer.append("package " + generationPackage + ";\n");
153: buffer.append(importsGen);
154: buffer.append("public class " + beanClassName
155: + "FormatHandler implements WSIFFormatHandler_JCA {\n");
156:
157: buffer.append("private " + beanClassName + " " + beanClassField
158: + " = null;\n");
159:
160: if (metaDatafield != null && metaDatafield.length() > 0) {
161: buffer.append("private static String META_DATA_A = \""
162: + metaDatafield + "\";\n");
163: }
164:
165: buffer
166: .append("public void read(InputStream inputStream) throws IOException {\n");
167: buffer.append(" try {\n");
168: buffer
169: .append(" ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);\n");
170: buffer
171: .append(" Object obj = (Object)objectInputStream.readObject();\n");
172: buffer.append(" if(obj instanceof " + beanClassName + "){\n");
173: buffer.append(" this." + beanClassField + " = ("
174: + beanClassName + ")obj;\n");
175: buffer
176: .append(" }else if (obj instanceof com.myeis.services.internal."
177: + beanClassName + "DataObject){\n");
178: buffer.append(" com.myeis.services.internal." + beanClassName
179: + "DataObject a" + beanClassName
180: + " = (com.myeis.services.internal." + beanClassName
181: + "DataObject)obj;\n");
182: buffer.append(" this." + beanClassField + " = new "
183: + beanClassName + "();\n");
184:
185: try {
186: Class bean = Class.forName("com.myeis.services.internal."
187: + beanClassName + "DataObject");
188: BeanInfo bInfo = Introspector.getBeanInfo(bean);
189: if (bInfo != null) {
190: PropertyDescriptor properties[] = bInfo
191: .getPropertyDescriptors();
192: for (int i = 0; i < properties.length; i++) {
193: Method writer = properties[i].getWriteMethod();
194: Method reader = properties[i].getReadMethod();
195: if (reader != null && writer != null) {
196: String setter = writer.getName();
197: String getter = reader.getName();
198: buffer.append(" this." + beanClassField + "."
199: + setter + "(a" + beanClassName + "."
200: + getter + "());\n");
201: }
202: }
203: }
204: } catch (Exception e) {
205: // throw new WSIFException(
206: // "Error occurred introspecting bean properties in format handler generator!",
207: // e);
208:
209: buffer.append(" String ex =" + e + ";\n");
210: }
211:
212: buffer.append(" }\n");
213: buffer.append(" } catch (ClassNotFoundException e) {\n");
214: buffer.append(" e.printStackTrace();\n");
215: buffer
216: .append(" throw new IOException(\"class not found !\");\n");
217: buffer.append(" } \n");
218: buffer.append("}\n\n");
219: buffer
220: .append("public void write(OutputStream outputStream) throws IOException {\n");
221:
222: buffer
223: .append(" ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);\n");
224: buffer.append(" objectOutputStream.writeObject(this."
225: + beanClassField + ");\n");
226: buffer.append(" objectOutputStream.flush();\n");
227: buffer.append("}\n\n");
228:
229: buffer
230: .append("public void setObjectPart(Object objectPart) {\n");
231: buffer.append(" this." + beanClassField + " = ("
232: + beanClassName + ")objectPart;\n");
233: buffer.append("}\n\n");
234: buffer.append("public Object getObjectPart() {\n");
235: buffer.append(" return this." + beanClassField + ";\n");
236: buffer.append("}\n\n");
237:
238: buffer
239: .append("public Object getObjectPart(Class sourceClass) {\n");
240: buffer.append(" try{\n");
241: buffer
242: .append(" if (sourceClass.isAssignableFrom(Class.forName(\"javax.xml.transform.sax.SAXSource\"))){\n");
243: buffer
244: .append(" SAXSource saxSource = new SAXSource(new InputSource());\n");
245: buffer.append(" XMLReader reader = new ContentReader();\n");
246: buffer.append(" saxSource.setXMLReader(reader);\n");
247: buffer.append(" return saxSource;\n");
248: buffer.append(" }\n");
249: buffer.append(" }\n");
250: buffer.append(" catch (Throwable e) {\n");
251: buffer.append(" }\n");
252:
253: buffer.append(" return null;\n");
254: buffer.append("}\n\n");
255:
256: buffer.append(defaultMethodsGen);
257:
258: writeContentReader(buffer, beanClassField);
259: buffer.append("}"); // end of class
260:
261: ByteArrayInputStream inputStream = new ByteArrayInputStream(
262: buffer.toString().getBytes());
263: Hashtable returnTable = new Hashtable();
264: returnTable.put(formatHandlerClassName, inputStream);
265: return returnTable;
266:
267: }
268:
269: private void writeContentReader(StringBuffer buffer,
270: String beanClassField) {
271: buffer
272: .append("public class ContentReader implements org.xml.sax.XMLReader {\n");
273: buffer
274: .append(" private ContentHandler fieldContentHandler = null;\n");
275:
276: buffer
277: .append(" public org.xml.sax.ContentHandler getContentHandler() {return null;}\n");
278: buffer
279: .append(" public org.xml.sax.DTDHandler getDTDHandler() {return null;}\n");
280: buffer
281: .append(" public org.xml.sax.EntityResolver getEntityResolver() {return null;}\n");
282: buffer
283: .append(" public org.xml.sax.ErrorHandler getErrorHandler() {return null;}\n");
284: buffer
285: .append(" public boolean getFeature(String name) throws org.xml.sax.SAXNotSupportedException, org.xml.sax.SAXNotRecognizedException {return false;}\n");
286: buffer
287: .append(" public Object getProperty(String name) throws org.xml.sax.SAXNotSupportedException, org.xml.sax.SAXNotRecognizedException {return null;}\n");
288: buffer
289: .append(" public void setDTDHandler(org.xml.sax.DTDHandler handler) {}\n");
290: buffer
291: .append(" public void setEntityResolver(org.xml.sax.EntityResolver resolver) {}\n");
292: buffer
293: .append(" public void setErrorHandler(org.xml.sax.ErrorHandler handler) {}\n");
294: buffer
295: .append(" public void setFeature(String name, boolean value) throws org.xml.sax.SAXNotSupportedException, org.xml.sax.SAXNotRecognizedException {}\n");
296: buffer
297: .append(" public void setProperty(String name, Object value) throws org.xml.sax.SAXNotSupportedException, org.xml.sax.SAXNotRecognizedException {}\n");
298: buffer
299: .append(" public void parse(String systemId) throws java.io.IOException, org.xml.sax.SAXException {}\n");
300: buffer
301: .append(" public void parse(org.xml.sax.InputSource input) throws java.io.IOException, org.xml.sax.SAXException {\n");
302: buffer.append(" try {\n");
303: buffer
304: .append(" Attributes attributes = new AttributesImpl();\n");
305: buffer.append(" this.fieldContentHandler.startDocument();\n");
306:
307: if (beanClassField.equals("fieldCustomer")) {
308:
309: buffer
310: .append(" this.fieldContentHandler.startElement(\"\", \"number\", \"number\", attributes);\n");
311: buffer.append(" this.fieldContentHandler.characters("
312: + beanClassField
313: + ".getNumber().toCharArray(), 0, "
314: + beanClassField + ".getNumber().length());\n");
315: buffer
316: .append(" this.fieldContentHandler.endElement(\"\", \"number\", \"number\");\n");
317: buffer
318: .append(" this.fieldContentHandler.startElement(\"\", \"firstName\", \"firstName\", attributes);\n");
319: buffer.append(" this.fieldContentHandler.characters("
320: + beanClassField
321: + ".getFirstName().toCharArray(), 0, "
322: + beanClassField + ".getFirstName().length());\n");
323: buffer
324: .append(" this.fieldContentHandler.endElement(\"\", \"firstName\", \"firstName\");\n");
325: buffer
326: .append(" this.fieldContentHandler.startElement(\"\", \"lastName\", \"lastName\", attributes);\n");
327: buffer.append(" this.fieldContentHandler.characters("
328: + beanClassField
329: + ".getLastName().toCharArray(), 0, "
330: + beanClassField + ".getLastName().length());\n");
331: buffer
332: .append(" this.fieldContentHandler.endElement(\"\", \"lastName\", \"lastName\");\n");
333: } else if (beanClassField.equals("fieldPurchaseOrder")) {
334: buffer
335: .append(" this.fieldContentHandler.startElement(\"\", \"orderNumber\", \"orderNumber\", attributes);\n");
336: buffer
337: .append(" this.fieldContentHandler.characters("
338: + beanClassField
339: + ".getOrderNumber().toCharArray(), 0, "
340: + beanClassField
341: + ".getOrderNumber().length());\n");
342: buffer
343: .append(" this.fieldContentHandler.endElement(\"\", \"orderNumber\", \"orderNumber\");\n");
344:
345: buffer
346: .append(" this.fieldContentHandler.startElement(\"\", \"description\", \"description\", attributes);\n");
347: buffer
348: .append(" this.fieldContentHandler.characters("
349: + beanClassField
350: + ".getDescription().toCharArray(), 0, "
351: + beanClassField
352: + ".getDescription().length());\n");
353: buffer
354: .append(" this.fieldContentHandler.endElement(\"\", \"description\", \"description\");\n");
355:
356: buffer
357: .append(" this.fieldContentHandler.startElement(\"\", \"partNumber\", \"partNumber\", attributes);\n");
358: buffer.append(" this.fieldContentHandler.characters("
359: + beanClassField
360: + ".getPartNumber().toCharArray(), 0, "
361: + beanClassField + ".getPartNumber().length());\n");
362: buffer
363: .append(" this.fieldContentHandler.endElement(\"\", \"partNumber\", \"partNumber\");\n");
364:
365: } else if (beanClassField.equals("fieldAddress")) {
366: buffer
367: .append(" this.fieldContentHandler.startElement(\"\", \"customerNumber\", \"customerNumber\", attributes);\n");
368: buffer.append(" this.fieldContentHandler.characters("
369: + beanClassField
370: + ".getCustomerNumber().toCharArray(), 0, "
371: + beanClassField
372: + ".getCustomerNumber().length());\n");
373: buffer
374: .append(" this.fieldContentHandler.endElement(\"\", \"customerNumber\", \"customerNumber\");\n");
375:
376: buffer
377: .append(" this.fieldContentHandler.startElement(\"\", \"city\", \"city\", attributes);\n");
378: buffer.append(" this.fieldContentHandler.characters("
379: + beanClassField + ".getCity().toCharArray(), 0, "
380: + beanClassField + ".getCity().length());\n");
381: buffer
382: .append(" this.fieldContentHandler.endElement(\"\", \"city\", \"city\");\n");
383:
384: buffer
385: .append(" this.fieldContentHandler.startElement(\"\", \"state\", \"state\", attributes);\n");
386: buffer.append(" this.fieldContentHandler.characters("
387: + beanClassField + ".getState().toCharArray(), 0, "
388: + beanClassField + ".getState().length());\n");
389: buffer
390: .append(" this.fieldContentHandler.endElement(\"\", \"state\", \"state\");\n");
391:
392: buffer
393: .append(" this.fieldContentHandler.startElement(\"\", \"street\", \"street\", attributes);\n");
394: buffer.append(" this.fieldContentHandler.characters("
395: + beanClassField
396: + ".getStreet().toCharArray(), 0, "
397: + beanClassField + ".getStreet().length());\n");
398: buffer
399: .append(" this.fieldContentHandler.endElement(\"\", \"street\", \"street\");\n");
400:
401: buffer
402: .append(" this.fieldContentHandler.startElement(\"\", \"zipCode\", \"zipCode\", attributes);\n");
403: buffer.append(" this.fieldContentHandler.characters("
404: + beanClassField
405: + ".getZipCode().toCharArray(), 0, "
406: + beanClassField + ".getZipCode().length());\n");
407: buffer
408: .append(" this.fieldContentHandler.endElement(\"\", \"zipCode\", \"zipCode\");\n");
409:
410: }
411:
412: buffer.append(" this.fieldContentHandler.endDocument();\n");
413: buffer.append(" } catch (Exception e) {\n");
414: buffer.append(" e.printStackTrace();\n");
415: buffer.append(" } \n");
416: buffer.append(" }\n");
417: buffer
418: .append(" public void setContentHandler(org.xml.sax.ContentHandler handler) {\n");
419: buffer.append(" this.fieldContentHandler = handler;\n");
420: buffer.append(" }\n");
421: buffer.append("} \n");
422:
423: }
424:
425: private static final String importsGen = "import java.io.*;\n"
426: + "import com.myeis.services.*;\n"
427: + "import org.apache.wsif.providers.jca.WSIFFormatHandler_JCA;\n"
428: + "import javax.xml.transform.Transformer;\n"
429: + "import javax.xml.transform.TransformerFactory;\n"
430: + "import javax.xml.transform.sax.SAXSource;\n"
431: + "import org.xml.sax.XMLReader;\n"
432: + "import org.xml.sax.Attributes;\n"
433: + "import org.xml.sax.ContentHandler;\n"
434: + "import org.xml.sax.InputSource;\n"
435: + "import org.xml.sax.helpers.AttributesImpl;\n\n\n\n";
436:
437: private static final String defaultMethodsGen = "public Object getElement(String elementName) {return null;}\n"
438: + "public void setElement(String elementName, Object element) {}\n"
439: + "public Object getElement(String name, int index) {return null;}\n"
440: + "public void setElement(String name, int index, Object value) {}\n"
441: + "public javax.xml.namespace.QName getPartQName(){\n"
442: + " return null;\n"
443: + " }\n"
444: + " \n"
445: + " public void setPartQName(javax.xml.namespace.QName qname){\n"
446: + " }\n"
447: + "public void setInteractionSpec(javax.resource.cci.InteractionSpec arg0) {\n"
448: + " return;\n" + " }\n";
449:
450: /**
451: * @see org.apache.wsif.providers.jca.toolplugin.FormatHandlerGenerator#createGenerationContext()
452: */
453: public Object createGenerationContext() {
454: return null;
455: }
456: }
|