KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > websvc > design > schema2java > JavaGenerator


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.websvc.design.schema2java;
21
22 import com.sun.tools.xjc.Driver;
23 import java.io.*;
24 import java.util.ArrayList JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.List JavaDoc;
27 import javax.xml.parsers.ParserConfigurationException JavaDoc;
28 import javax.xml.parsers.SAXParser JavaDoc;
29 import javax.xml.parsers.SAXParserFactory JavaDoc;
30 import org.xml.sax.SAXException JavaDoc;
31 import org.xml.sax.SAXException JavaDoc;
32 import org.xml.sax.helpers.DefaultHandler JavaDoc;
33
34 /**
35  *
36  * @author mkuchtiak
37  */

38 public class JavaGenerator {
39     
40     private String JavaDoc primaryFileJavaName;
41     private List JavaDoc<String JavaDoc> generatedFiles;
42     
43     /** Creates a new instance of JavaGenerator */
44     public JavaGenerator() {
45     }
46     
47     /** generates java artifacts for selected schema element
48      */

49     public XJCResult generateJavaArtifacts(File schemaFile, String JavaDoc elementName, File srcRoot, String JavaDoc packageName) {
50
51         Runtime JavaDoc rt = Runtime.getRuntime();
52         XJCResult result = new XJCResult();
53          
54         String JavaDoc d = srcRoot.getAbsolutePath();
55         String JavaDoc schema = schemaFile.getAbsolutePath();
56         try {
57             ElementHandler handler = ElementHandler.parse(schemaFile.toURI().toASCIIString(), elementName);
58             primaryFileJavaName = handler.getJavaName();
59         } catch (IOException ex) {
60             ex.printStackTrace();
61         } catch (ParserConfigurationException JavaDoc ex) {
62             ex.printStackTrace();
63         } catch (SAXException JavaDoc ex) {
64             ex.printStackTrace();
65         }
66         // XJC generator
67
generatedFiles = new ArrayList JavaDoc<String JavaDoc>();
68         XJCClassGenerationListener xjcListener = new XJCClassGenerationListener(this);
69         String JavaDoc[] arguments = new String JavaDoc[] {
70             "-d",d,"-verbose","-p",packageName,"-npa","-nv", schema //NOI18N
71
};
72         try {
73             Driver.run(arguments,xjcListener);
74         } catch (Throwable JavaDoc ex) {
75             ex.printStackTrace();
76         }
77
78         List JavaDoc<File> secondaryFiles = new ArrayList JavaDoc<File>();
79         File primaryFile = null;
80         String JavaDoc packagePath = packageName.replace('.', '/');
81         int index = packageName.length()+1;
82         if (generatedFiles.size()>0) {
83             for (String JavaDoc generatedFile:generatedFiles) {
84                 if (generatedFile.startsWith(packagePath+"/")) {
85                     File file = new File(srcRoot.getAbsolutePath()+"/"+generatedFile);
86                     String JavaDoc fileName = generatedFile.substring(index);
87                     if (primaryFileJavaName!=null && fileName.equals(primaryFileJavaName+".java")) { //NOI18N
88
primaryFile = file;
89                     } else {
90                         secondaryFiles.add(file);
91                     }
92                 }
93             }
94         }
95         result.setPrimaryFile(primaryFile);
96         result.setSecondaryFiles(secondaryFiles);
97         
98         return result;
99     }
100     
101     void addGeneratedFile(String JavaDoc generatedFile) {
102         generatedFiles.add(generatedFile);
103     }
104
105     public static class XJCResult {
106         
107         private File primaryFile;
108         private List JavaDoc<File> secondaryFiles;
109         private int exitValue;
110                 
111         public XJCResult() {
112         }
113
114         public void setPrimaryFile(File primaryFile) {
115            this.primaryFile=primaryFile;
116         }
117         
118         public File getPrimaryFile() {
119             return primaryFile;
120         }
121         
122         public void setSecondaryFiles(List JavaDoc<File> secondaryFiles) {
123            this.secondaryFiles=secondaryFiles;
124         }
125         
126         public List JavaDoc<File> getSecondaryFiles() {
127             return secondaryFiles;
128         }
129     }
130     
131     static class ElementHandler extends DefaultHandler JavaDoc{
132
133         public static final String JavaDoc SCHEMA_NAMESPACE = "http://www.w3.org/2001/XMLSchema"; //NOI18N
134

135         private int elementLevel;
136         private String JavaDoc elementName;
137         private String JavaDoc elementType;
138         
139         
140         public static ElementHandler parse(String JavaDoc wsdlUrl, String JavaDoc elementName) throws ParserConfigurationException JavaDoc, SAXException JavaDoc, IOException {
141             ElementHandler handler = new ElementHandler(elementName);
142             SAXParserFactory JavaDoc factory = SAXParserFactory.newInstance();
143             factory.setNamespaceAware(true);
144             SAXParser JavaDoc saxParser = factory.newSAXParser();
145             saxParser.parse(wsdlUrl, handler);
146             return handler;
147         }
148         
149         /** Creates a new instance of WsdlWrapperHandler */
150         private ElementHandler(String JavaDoc elementName) {
151             this.elementName=elementName;
152         }
153
154         public void startElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qname, org.xml.sax.Attributes JavaDoc attributes) throws org.xml.sax.SAXException JavaDoc {
155             if (SCHEMA_NAMESPACE.equals(uri) && "element".equals(localName)) { // NOI18N
156
if (elementLevel==0 && elementName.equalsIgnoreCase(attributes.getValue("name"))) {
157                     elementName = attributes.getValue("name");
158                     String JavaDoc type = attributes.getValue("type");// NOI18N
159
if (type!=null) elementType=type;
160                 }
161                 elementLevel++;
162             }
163         }
164         public void endElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName) throws SAXException JavaDoc {
165             if (SCHEMA_NAMESPACE.equals(uri) && "element".equals(localName)) { // NOI18N
166
elementLevel--;
167             }
168         }
169         
170         public String JavaDoc getJavaName() {
171             String JavaDoc javaName = elementType==null?elementName:elementType;
172             if (javaName!=null && javaName.length()>0)
173                 return javaName.substring(0,1).toUpperCase()+javaName.substring(1);
174             else return javaName;
175         }
176     }
177     
178 }
179
Popular Tags