src/share/jaxws_classes/com/sun/tools/internal/ws/processor/generator/GeneratorBase.java

changeset 0
373ffda63c9a
child 637
9c07ef4934dd
     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	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,244 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2013, 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 +import javax.annotation.processing.Filer;
    1.67 +import javax.tools.FileObject;
    1.68 +
    1.69 +import javax.tools.StandardLocation;
    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 +    @Override
   1.103 +    public void visit(Model model) throws Exception {
   1.104 +        for (Service service : model.getServices()) {
   1.105 +            service.accept(this);
   1.106 +        }
   1.107 +    }
   1.108 +
   1.109 +    @Override
   1.110 +    public void visit(Service service) throws Exception {
   1.111 +        for (Port port : service.getPorts()) {
   1.112 +            port.accept(this);
   1.113 +        }
   1.114 +    }
   1.115 +
   1.116 +    @Override
   1.117 +    public void visit(Port port) throws Exception {
   1.118 +        for (Operation operation : port.getOperations()) {
   1.119 +            operation.accept(this);
   1.120 +        }
   1.121 +    }
   1.122 +
   1.123 +    @Override
   1.124 +    public void visit(Operation operation) throws Exception {
   1.125 +        operation.getRequest().accept(this);
   1.126 +        if (operation.getResponse() != null) {
   1.127 +            operation.getResponse().accept(this);
   1.128 +        }
   1.129 +        Iterator faults = operation.getFaultsSet().iterator();
   1.130 +        if (faults != null) {
   1.131 +            Fault fault;
   1.132 +            while (faults.hasNext()) {
   1.133 +                fault = (Fault) faults.next();
   1.134 +                fault.accept(this);
   1.135 +            }
   1.136 +        }
   1.137 +    }
   1.138 +
   1.139 +    @Override
   1.140 +    public void visit(Parameter param) throws Exception {}
   1.141 +
   1.142 +    @Override
   1.143 +    public void visit(Block block) throws Exception {}
   1.144 +
   1.145 +    @Override
   1.146 +    public void visit(Response response) throws Exception {}
   1.147 +
   1.148 +    @Override
   1.149 +    public void visit(Request request) throws Exception {}
   1.150 +
   1.151 +    @Override
   1.152 +    public void visit(Fault fault) throws Exception {}
   1.153 +
   1.154 +    public List<String> getJAXWSClassComment(){
   1.155 +        return getJAXWSClassComment(targetVersion);
   1.156 +    }
   1.157 +
   1.158 +    public static List<String> getJAXWSClassComment(String targetVersion) {
   1.159 +        List<String> comments = new ArrayList<String>();
   1.160 +        comments.add("This class was generated by the JAX-WS RI.\n");
   1.161 +        comments.add(ToolVersion.VERSION.BUILD_VERSION+"\n");
   1.162 +        comments.add("Generated source version: " + targetVersion);
   1.163 +        return comments;
   1.164 +    }
   1.165 +
   1.166 +    protected JDefinedClass getClass(String className, ClassType type) throws JClassAlreadyExistsException {
   1.167 +        JDefinedClass cls;
   1.168 +        try {
   1.169 +            cls = cm._class(className, type);
   1.170 +        } catch (JClassAlreadyExistsException e){
   1.171 +            cls = cm._getClass(className);
   1.172 +            if (cls == null) {
   1.173 +                throw e;
   1.174 +            }
   1.175 +        }
   1.176 +        return cls;
   1.177 +    }
   1.178 +
   1.179 +    protected void log(String msg) {
   1.180 +        if (options.verbose) {
   1.181 +            System.out.println(
   1.182 +                "["
   1.183 +                    + Names.stripQualifier(this.getClass().getName())
   1.184 +                    + ": "
   1.185 +                    + msg
   1.186 +                    + "]");
   1.187 +        }
   1.188 +    }
   1.189 +
   1.190 +    protected void writeHandlerConfig(String className, JDefinedClass cls, WsimportOptions options) {
   1.191 +        Element e = options.getHandlerChainConfiguration();
   1.192 +        if (e == null) {
   1.193 +            return;
   1.194 +        }
   1.195 +        JAnnotationUse handlerChainAnn = cls.annotate(cm.ref(HandlerChain.class));
   1.196 +        NodeList nl = e.getElementsByTagNameNS(
   1.197 +            "http://java.sun.com/xml/ns/javaee", "handler-chain");
   1.198 +        if(nl.getLength() > 0){
   1.199 +            String fName = getHandlerConfigFileName(className);
   1.200 +            handlerChainAnn.param("file", fName);
   1.201 +            generateHandlerChainFile(e, className);
   1.202 +        }
   1.203 +    }
   1.204 +
   1.205 +     private String getHandlerConfigFileName(String fullName){
   1.206 +        String name = Names.stripQualifier(fullName);
   1.207 +        return name+"_handler.xml";
   1.208 +    }
   1.209 +
   1.210 +    private void generateHandlerChainFile(Element hChains, String name) {
   1.211 +
   1.212 +        Filer filer = options.filer;
   1.213 +
   1.214 +        try {
   1.215 +            IndentingWriter p;
   1.216 +            FileObject jfo;
   1.217 +            if (filer != null) {
   1.218 +                jfo = filer.createResource(StandardLocation.SOURCE_OUTPUT,
   1.219 +                        Names.getPackageName(name), getHandlerConfigFileName(name));
   1.220 +                options.addGeneratedFile(new File(jfo.toUri()));
   1.221 +                p = new IndentingWriter(new OutputStreamWriter(jfo.openOutputStream()));
   1.222 +            } else { // leave for backw. compatibility now
   1.223 +                String hcName = getHandlerConfigFileName(name);
   1.224 +                File packageDir = DirectoryUtil.getOutputDirectoryFor(name, destDir);
   1.225 +                File hcFile = new File(packageDir, hcName);
   1.226 +                options.addGeneratedFile(hcFile);
   1.227 +                p = new IndentingWriter(new OutputStreamWriter(new FileOutputStream(hcFile)));
   1.228 +            }
   1.229 +
   1.230 +            Transformer it = XmlUtil.newTransformer();
   1.231 +
   1.232 +            it.setOutputProperty(OutputKeys.METHOD, "xml");
   1.233 +            it.setOutputProperty(OutputKeys.INDENT, "yes");
   1.234 +            it.setOutputProperty(
   1.235 +                "{http://xml.apache.org/xslt}indent-amount",
   1.236 +                "2");
   1.237 +            it.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
   1.238 +            it.transform( new DOMSource(hChains), new StreamResult(p) );
   1.239 +            p.close();
   1.240 +        } catch (Exception e) {
   1.241 +            throw new GeneratorException(
   1.242 +                    "generator.nestedGeneratorError",
   1.243 +                    e);
   1.244 +        }
   1.245 +    }
   1.246 +
   1.247 +}

mercurial