1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/tools/internal/ws/processor/generator/GeneratorBase.java Tue Mar 06 16:09:35 2012 -0800 1.3 @@ -0,0 +1,230 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package com.sun.tools.internal.ws.processor.generator; 1.30 + 1.31 +import com.sun.codemodel.internal.ClassType; 1.32 +import com.sun.codemodel.internal.JAnnotationUse; 1.33 +import com.sun.codemodel.internal.JClassAlreadyExistsException; 1.34 +import com.sun.codemodel.internal.JCodeModel; 1.35 +import com.sun.codemodel.internal.JDefinedClass; 1.36 +import com.sun.tools.internal.ws.ToolVersion; 1.37 +import com.sun.tools.internal.ws.processor.model.Block; 1.38 +import com.sun.tools.internal.ws.processor.model.Fault; 1.39 +import com.sun.tools.internal.ws.processor.model.Model; 1.40 +import com.sun.tools.internal.ws.processor.model.ModelVisitor; 1.41 +import com.sun.tools.internal.ws.processor.model.Operation; 1.42 +import com.sun.tools.internal.ws.processor.model.Parameter; 1.43 +import com.sun.tools.internal.ws.processor.model.Port; 1.44 +import com.sun.tools.internal.ws.processor.model.Request; 1.45 +import com.sun.tools.internal.ws.processor.model.Response; 1.46 +import com.sun.tools.internal.ws.processor.model.Service; 1.47 +import com.sun.tools.internal.ws.processor.util.DirectoryUtil; 1.48 +import com.sun.tools.internal.ws.processor.util.IndentingWriter; 1.49 +import com.sun.tools.internal.ws.wscompile.ErrorReceiver; 1.50 +import com.sun.tools.internal.ws.wscompile.WsimportOptions; 1.51 +import com.sun.xml.internal.ws.util.xml.XmlUtil; 1.52 +import org.w3c.dom.Element; 1.53 +import org.w3c.dom.NodeList; 1.54 + 1.55 +import javax.jws.HandlerChain; 1.56 +import javax.xml.transform.OutputKeys; 1.57 +import javax.xml.transform.Transformer; 1.58 +import javax.xml.transform.dom.DOMSource; 1.59 +import javax.xml.transform.stream.StreamResult; 1.60 +import java.io.File; 1.61 +import java.io.FileOutputStream; 1.62 +import java.io.OutputStreamWriter; 1.63 +import java.util.ArrayList; 1.64 +import java.util.Iterator; 1.65 +import java.util.List; 1.66 + 1.67 +/** 1.68 + * 1.69 + * @author WS Development Team 1.70 + */ 1.71 +public abstract class GeneratorBase implements ModelVisitor { 1.72 + private File destDir; 1.73 + private String targetVersion; 1.74 + protected boolean donotOverride; 1.75 + protected JCodeModel cm; 1.76 + protected Model model; 1.77 + protected String wsdlLocation; 1.78 + protected ErrorReceiver receiver; 1.79 + protected WsimportOptions options; 1.80 + 1.81 + protected GeneratorBase() { 1.82 + } 1.83 + 1.84 + public void init(Model model, WsimportOptions options, ErrorReceiver receiver){ 1.85 + this.model = model; 1.86 + this.options = options; 1.87 + this.destDir = options.destDir; 1.88 + this.receiver = receiver; 1.89 + this.wsdlLocation = options.wsdlLocation; 1.90 + this.targetVersion = options.target.getVersion(); 1.91 + this.cm = options.getCodeModel(); 1.92 + } 1.93 + 1.94 + public void doGeneration() { 1.95 + try { 1.96 + model.accept(this); 1.97 + } catch (Exception e) { 1.98 + receiver.error(e); 1.99 + } 1.100 + } 1.101 + 1.102 + public void visit(Model model) throws Exception { 1.103 + for (Service service : model.getServices()) { 1.104 + service.accept(this); 1.105 + } 1.106 + } 1.107 + 1.108 + public void visit(Service service) throws Exception { 1.109 + for (Port port : service.getPorts()) { 1.110 + port.accept(this); 1.111 + } 1.112 + } 1.113 + 1.114 + public void visit(Port port) throws Exception { 1.115 + for (Operation operation : port.getOperations()) { 1.116 + operation.accept(this); 1.117 + } 1.118 + } 1.119 + 1.120 + public void visit(Operation operation) throws Exception { 1.121 + operation.getRequest().accept(this); 1.122 + if (operation.getResponse() != null) 1.123 + operation.getResponse().accept(this); 1.124 + Iterator faults = operation.getFaultsSet().iterator(); 1.125 + if (faults != null) { 1.126 + Fault fault; 1.127 + while (faults.hasNext()) { 1.128 + fault = (Fault) faults.next(); 1.129 + fault.accept(this); 1.130 + } 1.131 + } 1.132 + } 1.133 + 1.134 + public void visit(Parameter param) throws Exception { 1.135 + } 1.136 + 1.137 + public void visit(Block block) throws Exception { 1.138 + } 1.139 + 1.140 + public void visit(Response response) throws Exception { 1.141 + } 1.142 + 1.143 + 1.144 + public void visit(Request request) throws Exception { 1.145 + } 1.146 + 1.147 + public void visit(Fault fault) throws Exception { 1.148 + } 1.149 + 1.150 + public List<String> getJAXWSClassComment(){ 1.151 + return getJAXWSClassComment(targetVersion); 1.152 + } 1.153 + 1.154 + public static List<String> getJAXWSClassComment(String targetVersion) { 1.155 + List<String> comments = new ArrayList<String>(); 1.156 + comments.add("This class was generated by the JAX-WS RI.\n"); 1.157 + comments.add(ToolVersion.VERSION.BUILD_VERSION+"\n"); 1.158 + comments.add("Generated source version: " + targetVersion); 1.159 + return comments; 1.160 + } 1.161 + 1.162 + protected JDefinedClass getClass(String className, ClassType type) throws JClassAlreadyExistsException { 1.163 + JDefinedClass cls; 1.164 + try { 1.165 + cls = cm._class(className, type); 1.166 + } catch (JClassAlreadyExistsException e){ 1.167 + cls = cm._getClass(className); 1.168 + if(cls == null) 1.169 + throw e; 1.170 + } 1.171 + return cls; 1.172 + } 1.173 + 1.174 + protected void log(String msg) { 1.175 + if (options.verbose) { 1.176 + System.out.println( 1.177 + "[" 1.178 + + Names.stripQualifier(this.getClass().getName()) 1.179 + + ": " 1.180 + + msg 1.181 + + "]"); 1.182 + } 1.183 + } 1.184 + 1.185 + protected void writeHandlerConfig(String className, JDefinedClass cls, WsimportOptions options) { 1.186 + Element e = options.getHandlerChainConfiguration(); 1.187 + if(e == null) 1.188 + return; 1.189 + JAnnotationUse handlerChainAnn = cls.annotate(cm.ref(HandlerChain.class)); 1.190 + NodeList nl = e.getElementsByTagNameNS( 1.191 + "http://java.sun.com/xml/ns/javaee", "handler-chain"); 1.192 + if(nl.getLength() > 0){ 1.193 + String fName = getHandlerConfigFileName(className); 1.194 + handlerChainAnn.param("file", fName); 1.195 + generateHandlerChainFile(e, className); 1.196 + } 1.197 + } 1.198 + 1.199 + private String getHandlerConfigFileName(String fullName){ 1.200 + String name = Names.stripQualifier(fullName); 1.201 + return name+"_handler.xml"; 1.202 + } 1.203 + 1.204 + private void generateHandlerChainFile(Element hChains, String name) { 1.205 + String hcName = getHandlerConfigFileName(name); 1.206 + 1.207 + File packageDir = DirectoryUtil.getOutputDirectoryFor(name, destDir); 1.208 + File hcFile = new File(packageDir, hcName); 1.209 + 1.210 + options.addGeneratedFile(hcFile); 1.211 + 1.212 + try { 1.213 + IndentingWriter p = 1.214 + new IndentingWriter( 1.215 + new OutputStreamWriter(new FileOutputStream(hcFile))); 1.216 + Transformer it = XmlUtil.newTransformer(); 1.217 + 1.218 + it.setOutputProperty(OutputKeys.METHOD, "xml"); 1.219 + it.setOutputProperty(OutputKeys.INDENT, "yes"); 1.220 + it.setOutputProperty( 1.221 + "{http://xml.apache.org/xslt}indent-amount", 1.222 + "2"); 1.223 + it.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); 1.224 + it.transform( new DOMSource(hChains), new StreamResult(p) ); 1.225 + p.close(); 1.226 + } catch (Exception e) { 1.227 + throw new GeneratorException( 1.228 + "generator.nestedGeneratorError", 1.229 + e); 1.230 + } 1.231 + } 1.232 + 1.233 +}