325N/A/*
325N/A * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
325N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
325N/A *
325N/A * This code is free software; you can redistribute it and/or modify it
325N/A * under the terms of the GNU General Public License version 2 only, as
325N/A * published by the Free Software Foundation. Oracle designates this
325N/A * particular file as subject to the "Classpath" exception as provided
325N/A * by Oracle in the LICENSE file that accompanied this code.
325N/A *
325N/A * This code is distributed in the hope that it will be useful, but WITHOUT
325N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
325N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
325N/A * version 2 for more details (a copy is included in the LICENSE file that
325N/A * accompanied this code).
325N/A *
325N/A * You should have received a copy of the GNU General Public License version
325N/A * 2 along with this work; if not, write to the Free Software Foundation,
325N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
325N/A *
325N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
325N/A * or visit www.oracle.com if you need additional information or have any
325N/A * questions.
325N/A */
325N/A
325N/Apackage com.sun.tools.internal.ws.processor.generator;
325N/A
325N/Aimport com.sun.codemodel.internal.ClassType;
325N/Aimport com.sun.codemodel.internal.JAnnotationUse;
325N/Aimport com.sun.codemodel.internal.JClassAlreadyExistsException;
325N/Aimport com.sun.codemodel.internal.JCodeModel;
325N/Aimport com.sun.codemodel.internal.JDefinedClass;
325N/Aimport com.sun.tools.internal.ws.ToolVersion;
325N/Aimport com.sun.tools.internal.ws.processor.model.Block;
325N/Aimport com.sun.tools.internal.ws.processor.model.Fault;
325N/Aimport com.sun.tools.internal.ws.processor.model.Model;
325N/Aimport com.sun.tools.internal.ws.processor.model.ModelVisitor;
325N/Aimport com.sun.tools.internal.ws.processor.model.Operation;
325N/Aimport com.sun.tools.internal.ws.processor.model.Parameter;
325N/Aimport com.sun.tools.internal.ws.processor.model.Port;
325N/Aimport com.sun.tools.internal.ws.processor.model.Request;
325N/Aimport com.sun.tools.internal.ws.processor.model.Response;
325N/Aimport com.sun.tools.internal.ws.processor.model.Service;
325N/Aimport com.sun.tools.internal.ws.processor.util.DirectoryUtil;
325N/Aimport com.sun.tools.internal.ws.processor.util.IndentingWriter;
325N/Aimport com.sun.tools.internal.ws.wscompile.ErrorReceiver;
325N/Aimport com.sun.tools.internal.ws.wscompile.WsimportOptions;
325N/Aimport com.sun.xml.internal.ws.util.xml.XmlUtil;
325N/Aimport org.w3c.dom.Element;
325N/Aimport org.w3c.dom.NodeList;
325N/A
325N/Aimport javax.jws.HandlerChain;
325N/Aimport javax.xml.transform.OutputKeys;
325N/Aimport javax.xml.transform.Transformer;
325N/Aimport javax.xml.transform.dom.DOMSource;
325N/Aimport javax.xml.transform.stream.StreamResult;
325N/Aimport java.io.File;
325N/Aimport java.io.FileOutputStream;
325N/Aimport java.io.OutputStreamWriter;
325N/Aimport java.util.ArrayList;
325N/Aimport java.util.Iterator;
325N/Aimport java.util.List;
325N/A
325N/A/**
325N/A *
325N/A * @author WS Development Team
325N/A */
325N/Apublic abstract class GeneratorBase
325N/A implements
325N/A GeneratorConstants,
325N/A ModelVisitor{
325N/A private File destDir;
325N/A private String targetVersion;
325N/A protected boolean donotOverride;
325N/A protected JCodeModel cm;
325N/A protected final Model model;
325N/A protected final String wsdlLocation;
325N/A protected final ErrorReceiver receiver;
325N/A protected final WsimportOptions options;
325N/A
325N/A protected GeneratorBase(Model model, WsimportOptions options, ErrorReceiver receiver){
325N/A this.model = model;
325N/A this.options = options;
325N/A this.destDir = options.destDir;
325N/A this.receiver = receiver;
325N/A this.wsdlLocation = options.wsdlLocation;
325N/A this.targetVersion = options.target.getVersion();
325N/A this.cm = options.getCodeModel();
325N/A }
325N/A
325N/A protected void doGeneration() {
325N/A try {
325N/A model.accept(this);
325N/A } catch (Exception e) {
325N/A receiver.error(e);
325N/A }
325N/A }
325N/A
325N/A public void visit(Model model) throws Exception {
325N/A for (Service service : model.getServices()) {
325N/A service.accept(this);
325N/A }
325N/A }
325N/A
325N/A public void visit(Service service) throws Exception {
325N/A for (Port port : service.getPorts()) {
325N/A port.accept(this);
325N/A }
325N/A }
325N/A
325N/A public void visit(Port port) throws Exception {
325N/A for (Operation operation : port.getOperations()) {
325N/A operation.accept(this);
325N/A }
325N/A }
325N/A
325N/A public void visit(Operation operation) throws Exception {
325N/A operation.getRequest().accept(this);
325N/A if (operation.getResponse() != null)
325N/A operation.getResponse().accept(this);
325N/A Iterator faults = operation.getFaultsSet().iterator();
325N/A if (faults != null) {
325N/A Fault fault;
325N/A while (faults.hasNext()) {
325N/A fault = (Fault) faults.next();
325N/A fault.accept(this);
325N/A }
325N/A }
325N/A }
325N/A
325N/A public void visit(Parameter param) throws Exception {
325N/A }
325N/A
325N/A public void visit(Block block) throws Exception {
325N/A }
325N/A
325N/A public void visit(Response response) throws Exception {
325N/A }
325N/A
325N/A
325N/A public void visit(Request request) throws Exception {
325N/A }
325N/A
325N/A public void visit(Fault fault) throws Exception {
325N/A }
325N/A
325N/A public List<String> getJAXWSClassComment(){
325N/A return getJAXWSClassComment(targetVersion);
325N/A }
325N/A
325N/A public static List<String> getJAXWSClassComment(String targetVersion) {
325N/A List<String> comments = new ArrayList<String>();
325N/A comments.add("This class was generated by the JAX-WS RI.\n");
325N/A comments.add(ToolVersion.VERSION.BUILD_VERSION+"\n");
325N/A comments.add("Generated source version: " + targetVersion);
325N/A return comments;
325N/A }
325N/A
325N/A protected JDefinedClass getClass(String className, ClassType type) throws JClassAlreadyExistsException {
325N/A JDefinedClass cls;
325N/A try {
325N/A cls = cm._class(className, type);
325N/A } catch (JClassAlreadyExistsException e){
325N/A cls = cm._getClass(className);
325N/A if(cls == null)
325N/A throw e;
325N/A }
325N/A return cls;
325N/A }
325N/A
325N/A protected void log(String msg) {
325N/A if (options.verbose) {
325N/A System.out.println(
325N/A "["
325N/A + Names.stripQualifier(this.getClass().getName())
325N/A + ": "
325N/A + msg
325N/A + "]");
325N/A }
325N/A }
325N/A
325N/A protected void writeHandlerConfig(String className, JDefinedClass cls, WsimportOptions options) {
325N/A Element e = options.getHandlerChainConfiguration();
325N/A if(e == null)
325N/A return;
325N/A JAnnotationUse handlerChainAnn = cls.annotate(cm.ref(HandlerChain.class));
325N/A NodeList nl = e.getElementsByTagNameNS(
325N/A "http://java.sun.com/xml/ns/javaee", "handler-chain");
325N/A if(nl.getLength() > 0){
325N/A String fName = getHandlerConfigFileName(className);
325N/A handlerChainAnn.param("file", fName);
325N/A generateHandlerChainFile(e, className);
325N/A }
325N/A }
325N/A
325N/A private String getHandlerConfigFileName(String fullName){
325N/A String name = Names.stripQualifier(fullName);
325N/A return name+"_handler.xml";
325N/A }
325N/A
325N/A private void generateHandlerChainFile(Element hChains, String name) {
325N/A String hcName = getHandlerConfigFileName(name);
325N/A
325N/A File packageDir = DirectoryUtil.getOutputDirectoryFor(name, destDir);
325N/A File hcFile = new File(packageDir, hcName);
325N/A
325N/A options.addGeneratedFile(hcFile);
325N/A
325N/A try {
325N/A IndentingWriter p =
325N/A new IndentingWriter(
325N/A new OutputStreamWriter(new FileOutputStream(hcFile)));
325N/A Transformer it = XmlUtil.newTransformer();
325N/A
325N/A it.setOutputProperty(OutputKeys.METHOD, "xml");
325N/A it.setOutputProperty(OutputKeys.INDENT, "yes");
325N/A it.setOutputProperty(
325N/A "{http://xml.apache.org/xslt}indent-amount",
325N/A "2");
325N/A it.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
325N/A it.transform( new DOMSource(hChains), new StreamResult(p) );
325N/A p.close();
325N/A } catch (Exception e) {
325N/A throw new GeneratorException(
325N/A "generator.nestedGeneratorError",
325N/A e);
325N/A }
325N/A }
325N/A
325N/A}