1 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 ; 25 import java.util.List ; 26 import java.util.List ; 27 import javax.xml.parsers.ParserConfigurationException ; 28 import javax.xml.parsers.SAXParser ; 29 import javax.xml.parsers.SAXParserFactory ; 30 import org.xml.sax.SAXException ; 31 import org.xml.sax.SAXException ; 32 import org.xml.sax.helpers.DefaultHandler ; 33 34 38 public class JavaGenerator { 39 40 private String primaryFileJavaName; 41 private List <String > generatedFiles; 42 43 44 public JavaGenerator() { 45 } 46 47 49 public XJCResult generateJavaArtifacts(File schemaFile, String elementName, File srcRoot, String packageName) { 50 51 Runtime rt = Runtime.getRuntime(); 52 XJCResult result = new XJCResult(); 53 54 String d = srcRoot.getAbsolutePath(); 55 String 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 ex) { 62 ex.printStackTrace(); 63 } catch (SAXException ex) { 64 ex.printStackTrace(); 65 } 66 generatedFiles = new ArrayList <String >(); 68 XJCClassGenerationListener xjcListener = new XJCClassGenerationListener(this); 69 String [] arguments = new String [] { 70 "-d",d,"-verbose","-p",packageName,"-npa","-nv", schema }; 72 try { 73 Driver.run(arguments,xjcListener); 74 } catch (Throwable ex) { 75 ex.printStackTrace(); 76 } 77 78 List <File> secondaryFiles = new ArrayList <File>(); 79 File primaryFile = null; 80 String packagePath = packageName.replace('.', '/'); 81 int index = packageName.length()+1; 82 if (generatedFiles.size()>0) { 83 for (String generatedFile:generatedFiles) { 84 if (generatedFile.startsWith(packagePath+"/")) { 85 File file = new File(srcRoot.getAbsolutePath()+"/"+generatedFile); 86 String fileName = generatedFile.substring(index); 87 if (primaryFileJavaName!=null && fileName.equals(primaryFileJavaName+".java")) { 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 generatedFile) { 102 generatedFiles.add(generatedFile); 103 } 104 105 public static class XJCResult { 106 107 private File primaryFile; 108 private List <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 <File> secondaryFiles) { 123 this.secondaryFiles=secondaryFiles; 124 } 125 126 public List <File> getSecondaryFiles() { 127 return secondaryFiles; 128 } 129 } 130 131 static class ElementHandler extends DefaultHandler { 132 133 public static final String SCHEMA_NAMESPACE = "http://www.w3.org/2001/XMLSchema"; 135 private int elementLevel; 136 private String elementName; 137 private String elementType; 138 139 140 public static ElementHandler parse(String wsdlUrl, String elementName) throws ParserConfigurationException , SAXException , IOException { 141 ElementHandler handler = new ElementHandler(elementName); 142 SAXParserFactory factory = SAXParserFactory.newInstance(); 143 factory.setNamespaceAware(true); 144 SAXParser saxParser = factory.newSAXParser(); 145 saxParser.parse(wsdlUrl, handler); 146 return handler; 147 } 148 149 150 private ElementHandler(String elementName) { 151 this.elementName=elementName; 152 } 153 154 public void startElement(String uri, String localName, String qname, org.xml.sax.Attributes attributes) throws org.xml.sax.SAXException { 155 if (SCHEMA_NAMESPACE.equals(uri) && "element".equals(localName)) { if (elementLevel==0 && elementName.equalsIgnoreCase(attributes.getValue("name"))) { 157 elementName = attributes.getValue("name"); 158 String type = attributes.getValue("type"); if (type!=null) elementType=type; 160 } 161 elementLevel++; 162 } 163 } 164 public void endElement(String uri, String localName, String qName) throws SAXException { 165 if (SCHEMA_NAMESPACE.equals(uri) && "element".equals(localName)) { elementLevel--; 167 } 168 } 169 170 public String getJavaName() { 171 String 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 |