1 25 package org.objectweb.jonas.ws; 26 27 import java.io.File ; 28 import java.io.FileOutputStream ; 29 import java.io.IOException ; 30 import java.io.OutputStreamWriter ; 31 import java.io.Writer ; 32 import java.nio.charset.Charset ; 33 import java.util.Iterator ; 34 import java.util.List ; 35 import java.util.Map ; 36 import javax.wsdl.Definition; 37 import javax.wsdl.Import; 38 import javax.wsdl.Types; 39 import javax.wsdl.WSDLException; 40 import javax.wsdl.extensions.ExtensibilityElement; 41 import javax.wsdl.extensions.UnknownExtensibilityElement; 42 import javax.wsdl.extensions.schema.Schema; 43 import javax.wsdl.factory.WSDLFactory; 44 import javax.wsdl.xml.WSDLWriter; 45 import org.w3c.dom.Document ; 46 import org.objectweb.jonas_lib.xml.XMLSerializer; 47 import org.objectweb.jonas.common.Log; 48 import org.objectweb.util.monolog.api.BasicLevel; 49 import org.objectweb.util.monolog.api.Logger; 50 51 55 public class JDefinitionWriter { 56 57 58 private File base; 59 60 61 private Charset cs; 62 63 64 private Definition definition; 65 66 67 private String filename; 68 69 70 private static Logger logger = Log.getLogger(Log.JONAS_WS_PREFIX); 71 72 80 public JDefinitionWriter(Definition def, File context, Charset cs, String filename) { 81 if (!context.exists()) { 82 throw new IllegalArgumentException ("Context MUST exists : " + context); 83 } 84 this.definition = def; 85 if (context.isDirectory()) { 86 this.base = context; 87 } else { 88 this.base = context.getParentFile(); 89 } 90 this.cs = cs; 91 this.filename = filename; 92 } 93 94 99 public void write() throws IOException , WSDLException { 100 writeDefinition(definition, base, filename); 101 Map imports = definition.getImports(); 102 for (Iterator ns = imports.keySet().iterator(); ns.hasNext();) { 103 String uri = (String ) ns.next(); 105 List importeds = (List ) imports.get(uri); 106 for (Iterator imp = importeds.iterator(); imp.hasNext();) { 108 Import imported = (Import) imp.next(); 109 110 String locURI = imported.getLocationURI(); 113 if (!locURI.startsWith("http://")) { 114 Definition impDef = imported.getDefinition(); 118 if (locURI.toLowerCase().endsWith("wsdl")) { 119 JDefinitionWriter jdw = new JDefinitionWriter(impDef, new File (base, filename).getCanonicalFile(), cs, locURI); 121 jdw.write(); 122 } else { 123 Types types = impDef.getTypes(); 125 if (types != null) { 126 List extList = types.getExtensibilityElements(); 127 for (Iterator extIt = extList.iterator(); extIt.hasNext();) { 128 ExtensibilityElement ext = (ExtensibilityElement) extIt.next(); 129 Document doc = null; 130 131 if (ext instanceof Schema) { 132 Schema schema = (Schema) ext; 134 doc = schema.getElement().getOwnerDocument(); 135 } else if (ext instanceof UnknownExtensibilityElement) { 136 UnknownExtensibilityElement unknownExtElem = (UnknownExtensibilityElement) ext; 138 doc = unknownExtElem.getElement().getOwnerDocument(); 139 } 140 141 if (doc != null) { 142 File dir = new File (base, filename).getCanonicalFile().getParentFile(); 144 writeDocument(doc, dir, locURI); 145 } 146 } 147 } 148 } 149 } 150 } 151 } 152 } 153 154 160 private void writeDocument(Document doc, File base, String locURI) throws IOException { 161 XMLSerializer ser = new XMLSerializer(doc); 162 File file = new File (base, locURI).getCanonicalFile(); 163 logger.log(BasicLevel.DEBUG, "Writing XML Document in " + file); 164 createParentIfNeeded(file); 165 Writer writer = new OutputStreamWriter (new FileOutputStream (file), cs); 166 ser.serialize(writer); 167 writer.close(); 168 } 169 170 177 private void writeDefinition(Definition def, File base, String filename) throws WSDLException, IOException { 178 WSDLFactory factory = WSDLFactory.newInstance(); 179 WSDLWriter writer = factory.newWSDLWriter(); 180 File wsdl = new File (base, filename).getCanonicalFile(); 181 logger.log(BasicLevel.DEBUG, "Writing WSDL Definition in " + wsdl); 182 createParentIfNeeded(wsdl); 183 Writer w = new OutputStreamWriter (new FileOutputStream (wsdl), cs); 184 writer.writeWSDL(def, w); 185 w.close(); 186 } 187 188 193 private void createParentIfNeeded(File file) throws IOException { 194 File parent = file.getParentFile(); 195 if (!parent.exists()) { 196 if (!parent.mkdirs()) { 197 throw new IOException ("Cannot create directory " + parent.getCanonicalPath()); 199 } 200 } else if (!parent.isDirectory()) { 201 throw new IOException ("Parent " + parent.getCanonicalPath() + " already exists but is not a directory."); 203 } 204 } 205 } | Popular Tags |