1 19 20 package org.netbeans.modules.websvc.dev.wizard; 21 22 import java.io.BufferedInputStream ; 23 import java.io.BufferedOutputStream ; 24 import java.io.ByteArrayInputStream ; 25 import java.io.ByteArrayOutputStream ; 26 import java.io.IOException ; 27 import java.io.InputStream ; 28 import java.io.OutputStream ; 29 import java.io.OutputStreamWriter ; 30 import java.io.StringReader ; 31 import java.io.StringWriter ; 32 import java.io.Writer ; 33 import java.text.DateFormat ; 34 import java.util.*; 35 import javax.swing.text.BadLocationException ; 36 import javax.swing.text.Document ; 37 import javax.xml.parsers.ParserConfigurationException ; 38 import javax.xml.parsers.SAXParser ; 39 import javax.xml.parsers.SAXParserFactory ; 40 import javax.xml.transform.Source ; 41 import javax.xml.transform.Templates ; 42 import javax.xml.transform.Transformer ; 43 import javax.xml.transform.TransformerConfigurationException ; 44 import javax.xml.transform.TransformerException ; 45 import javax.xml.transform.TransformerFactory ; 46 import javax.xml.transform.URIResolver ; 47 import javax.xml.transform.stream.StreamResult ; 48 import javax.xml.transform.stream.StreamSource ; 49 import org.netbeans.api.java.project.JavaProjectConstants; 50 import org.netbeans.api.project.Project; 51 import org.netbeans.api.project.ProjectUtils; 52 import org.netbeans.api.project.SourceGroup; 53 import org.netbeans.api.project.Sources; 54 import org.netbeans.modules.websvc.dev.dd.gen.wscreation.Bean; 55 import org.openide.ErrorManager; 56 import org.openide.cookies.EditorCookie; 57 import org.openide.filesystems.FileLock; 58 import org.openide.filesystems.FileObject; 59 import org.openide.filesystems.FileUtil; 60 import org.openide.loaders.DataObject; 61 import org.openide.text.IndentEngine; 62 import org.openide.util.RequestProcessor; 63 import org.xml.sax.Attributes ; 64 import org.xml.sax.InputSource ; 65 import org.xml.sax.SAXException ; 66 import org.xml.sax.helpers.DefaultHandler ; 67 68 public class WSGenerationUtil { 69 public static final String TEMPLATE_BASE = "/org/netbeans/modules/websvc/dev/wizard/xsl/"; private String genDate; 71 private String genAuthor; 72 private Map templateCache = new HashMap(); 73 74 public static String getSelectedPackageName(FileObject targetFolder, Project p) { 75 Sources sources = ProjectUtils.getSources(p); 76 SourceGroup[] groups = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA); 77 String packageName = null; 78 for (int i = 0; i < groups.length && packageName == null; i++) { 79 packageName = FileUtil.getRelativePath(groups [i].getRootFolder(), targetFolder); 80 } 81 if (packageName != null) { 82 packageName = packageName.replaceAll("/", "."); 83 } 84 return packageName+""; 85 } 86 87 public String getBeanClassName(String wsName) { 88 return wsName + "Impl"; } 93 94 public String getSEIName(String wsName) { 95 return wsName + "SEI"; } 97 98 public Bean getDefaultBean() { 99 Bean b = new Bean(); 100 b.setCommentData(true); 101 if (genDate == null) { 102 genDate = DateFormat.getDateTimeInstance().format(new Date()); 103 genAuthor = System.getProperty("user.name"); 104 } 105 b.setCommentDataAuthor(genAuthor); b.setCommentDataDate(genDate); 107 return b; 108 } 109 110 public String getFullClassName(String pkg, String className) { 111 return (pkg==null||pkg.length()==0)?className:pkg+"."+className; } 113 114 public String generateClass(String template, Bean genData, FileObject pkg, boolean open) throws IOException { 115 String clsName = genData.getClassnameName(); 116 clsName = FileUtil.findFreeFileName(pkg, clsName, "java"); genData.setClassnameName(clsName); 118 generateClass(template, pkg, clsName, getStreamSource(genData), open); 119 return clsName; 120 } 121 122 public String generateClass(String template, FileObject pkg, String clsName, String inputXml, boolean open) 123 throws IOException { 124 clsName = FileUtil.findFreeFileName(pkg, clsName, "java"); generateClass(template, pkg, clsName, getStreamSource(inputXml), open); 126 return clsName; 127 } 128 129 public FileObject generateWSDL(String template, String wsName, String soapBinding, String portTypeName, FileObject folder, String wsdlName, StreamSource source) throws IOException { 130 return generateWSDL(template, wsName, soapBinding, portTypeName, folder, null, wsdlName, source); 131 } 132 133 public FileObject generateWSDL(String template, String wsName, String soapBinding, String portTypeName, FileObject folder, FileObject originalFolder, String wsdlName, StreamSource source) throws IOException 134 { 135 FileObject wsdlFile = folder.createData(FileUtil.findFreeFileName(folder, wsdlName, "wsdl"), "wsdl"); FileLock fl = null; 137 OutputStream os = null; 138 try { 139 fl = wsdlFile.lock(); 140 os = new BufferedOutputStream (wsdlFile.getOutputStream(fl)); 141 Transformer transformer = getTransformer(template); 142 transformer.setParameter("WSNAME", wsName); 143 transformer.setParameter("SOAPBINDING", soapBinding); 144 if (portTypeName != null) { 145 transformer.setParameter("PORTTYPENAME", portTypeName); 146 } else { 147 return wsdlFile; 148 } 149 transformer.transform(source, new StreamResult (os)); 150 os.close(); 151 } 152 catch(TransformerConfigurationException tce) { 153 IOException ioe = new IOException (); 154 ioe.initCause(tce); 155 throw ioe; 156 } 157 catch(TransformerException te) { 158 IOException ioe = new IOException (); 159 ioe.initCause(te); 160 throw ioe; 161 } 162 finally { 163 if(os != null) { 164 os.close(); 165 } 166 if(fl != null) { 167 fl.releaseLock(); 168 } 169 } 170 copyImportedSchemas(originalFolder,folder,wsdlFile); 172 return wsdlFile; 173 } 174 175 public void generateClass(String template, FileObject pkg, String clsName, StreamSource source, boolean open) throws IOException { 176 FileObject cFile = pkg.createData(clsName,"java"); FileLock fl = null; 178 OutputStream os = null; 179 Writer w = null; 180 try { 181 fl = cFile.lock(); 182 os = new BufferedOutputStream (cFile.getOutputStream(fl)); 183 getTransformer(template).transform(source, new StreamResult (new OutputStreamWriter (os))); 184 os.close(); 185 fl.releaseLock(); 186 DataObject dobj = DataObject.find(cFile); 187 final EditorCookie ec = (EditorCookie) dobj.getCookie(EditorCookie.class); 188 Document d = ec.openDocument(); 189 try { 190 String fullText = d.getText(0,d.getLength()); 191 IndentEngine javaIndent = IndentEngine.find(d); 192 StringWriter writer = new StringWriter (d.getLength()); 193 w = javaIndent.createWriter(d, 0, writer); 194 w.write(fullText); 195 w.close(); 196 d.remove(0, d.getLength()); 197 d.insertString(0, writer.getBuffer().toString(), null); 198 ec.saveDocument(); 199 } catch (BadLocationException ble) { 200 ErrorManager.getDefault().notify(ble); 201 } 202 if (open) { 203 RequestProcessor.getDefault().post(new Runnable () { 204 public void run() { 205 ec.open(); 206 } 207 },1000); 208 } 209 } catch (TransformerConfigurationException tce) { 210 IOException ioe = new IOException (); 211 ioe.initCause(tce); 212 throw ioe; 213 } catch (TransformerException te) { 214 IOException ioe = new IOException (); 215 ioe.initCause(te); 216 throw ioe; 217 } finally { 218 if (os != null) { 219 os.close(); 220 } 221 if (w != null) { 222 w.close(); 223 } 224 if(fl != null) { 225 fl.releaseLock(); 226 } 227 } 228 } 229 230 public String getBaseName(String fullClassName) { 231 return fullClassName.substring(fullClassName.lastIndexOf('.')+1); } 233 234 private Transformer getTransformer(String template) throws TransformerConfigurationException { 235 Templates t = (Templates ) templateCache.get(template); 236 if (t != null) { 237 return t.newTransformer(); 238 } 239 InputStream is = new BufferedInputStream (getClass().getResourceAsStream(template)); 240 TransformerFactory transFactory = TransformerFactory.newInstance(); 241 transFactory.setURIResolver(new URIResolver () { 242 public Source resolve(String href, String base) 243 throws TransformerException { 244 InputStream is = getClass().getResourceAsStream( 245 TEMPLATE_BASE + href.substring(href.lastIndexOf('/')+1)); 246 if (is == null) { 247 return null; 248 } 249 250 return new StreamSource (is); 251 } 252 }); 253 t = transFactory.newTemplates(new StreamSource (is)); 254 templateCache.put(template, t); 255 return t.newTransformer(); 256 } 257 258 private StreamSource getStreamSource(Bean genData) throws IOException { 259 ByteArrayOutputStream bos = new ByteArrayOutputStream (); 260 try { 261 genData.write(bos); 262 } finally { 263 bos.close(); 264 } 265 return new StreamSource (new ByteArrayInputStream (bos.toByteArray())); 266 } 267 268 private StreamSource getStreamSource(String xml) throws IOException { 269 StringReader sr = new StringReader (xml); 270 return new StreamSource (sr); 271 } 272 273 275 static List getSchemaNames(FileObject fo, boolean fromWsdl) { 276 List result = null; 277 try { 278 SAXParserFactory factory = SAXParserFactory.newInstance(); 279 factory.setNamespaceAware(true); 280 SAXParser saxParser = factory.newSAXParser(); 281 ImportsHandler handler= (fromWsdl?(ImportsHandler)new WsdlImportsHandler():(ImportsHandler)new SchemaImportsHandler()); 282 saxParser.parse(new InputSource (fo.getInputStream()), (DefaultHandler )handler); 283 result = handler.getSchemaNames(); 284 } catch(ParserConfigurationException ex) { 285 } catch(SAXException ex) { 287 } catch(IOException ex) { 289 } 291 292 return result; 293 } 294 295 private static interface ImportsHandler { 296 public List getSchemaNames(); 297 } 298 299 private static class WsdlImportsHandler extends DefaultHandler implements ImportsHandler { 300 301 private static final String W3C_WSDL_SCHEMA = "http://schemas.xmlsoap.org/wsdl"; private static final String W3C_WSDL_SCHEMA_SLASH = "http://schemas.xmlsoap.org/wsdl/"; 304 private List schemaNames; 305 306 private boolean insideSchema; 307 308 WsdlImportsHandler() { 309 schemaNames = new ArrayList(); 310 } 311 312 public void startElement(String uri, String localname, String qname, Attributes attributes) throws SAXException { 313 if(W3C_WSDL_SCHEMA.equals(uri) || W3C_WSDL_SCHEMA_SLASH.equals(uri)) { 314 if("types".equals(localname)) { insideSchema=true; 316 } 317 if("import".equals(localname)) { String wsdlLocation = attributes.getValue("location"); if (wsdlLocation!=null && wsdlLocation.indexOf("/")<0 && wsdlLocation.endsWith(".wsdl")) { schemaNames.add(wsdlLocation); 321 } 322 } 323 } 324 if(insideSchema && "import".equals(localname)) { String schemaLocation = attributes.getValue("schemaLocation"); if (schemaLocation!=null && schemaLocation.indexOf("/")<0 && schemaLocation.endsWith(".xsd")) { schemaNames.add(schemaLocation); 328 } 329 } 330 } 331 332 public void endElement(String uri, String localname, String qname) throws SAXException { 333 if(W3C_WSDL_SCHEMA.equals(uri) || W3C_WSDL_SCHEMA_SLASH.equals(uri)) { 334 if("types".equals(localname)) { insideSchema=false; 336 } 337 } 338 } 339 340 public List getSchemaNames() { 341 return schemaNames; 342 } 343 } 344 345 private static class SchemaImportsHandler extends DefaultHandler implements ImportsHandler { 346 347 private List schemaNames; 348 349 SchemaImportsHandler() { 350 schemaNames = new ArrayList(); 351 } 352 353 public void startElement(String uri, String localname, String qname, Attributes attributes) throws SAXException { 354 if("import".equals(localname)) { String schemaLocation = attributes.getValue("schemaLocation"); if (schemaLocation!=null && schemaLocation.indexOf("/")<0 && schemaLocation.endsWith(".xsd")) { schemaNames.add(schemaLocation); 358 } 359 } 360 } 361 362 public List getSchemaNames() { 363 return schemaNames; 364 } 365 } 366 367 369 private synchronized void copyImportedSchemas(FileObject resourceFolder, FileObject targetFolder, FileObject fo) throws IOException { 370 List schemaNames = getSchemaNames(fo,"wsdl".equals(fo.getExt())); Iterator it = schemaNames.iterator(); 372 while (it.hasNext()) { 373 String schemaName = (String )it.next(); 374 FileObject schemaFile = resourceFolder.getFileObject(schemaName); 375 if (schemaFile!=null) { 376 FileObject target = targetFolder.getFileObject(schemaFile.getName(),schemaFile.getExt()); 377 if(target != null) { 378 target.delete(); 379 } 380 FileObject copy = schemaFile.copy(targetFolder,schemaFile.getName(),schemaFile.getExt()); 382 copyImportedSchemas(resourceFolder, targetFolder, copy); 383 } 384 } 385 } 386 387 } 388 389 | Popular Tags |