001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.tools.ws.processor.generator;
038:
039: import com.sun.codemodel.ClassType;
040: import com.sun.codemodel.JAnnotationUse;
041: import com.sun.codemodel.JClassAlreadyExistsException;
042: import com.sun.codemodel.JCodeModel;
043: import com.sun.codemodel.JDefinedClass;
044: import com.sun.tools.ws.ToolVersion;
045: import com.sun.tools.ws.processor.model.Block;
046: import com.sun.tools.ws.processor.model.Fault;
047: import com.sun.tools.ws.processor.model.Model;
048: import com.sun.tools.ws.processor.model.ModelVisitor;
049: import com.sun.tools.ws.processor.model.Operation;
050: import com.sun.tools.ws.processor.model.Parameter;
051: import com.sun.tools.ws.processor.model.Port;
052: import com.sun.tools.ws.processor.model.Request;
053: import com.sun.tools.ws.processor.model.Response;
054: import com.sun.tools.ws.processor.model.Service;
055: import com.sun.tools.ws.processor.util.DirectoryUtil;
056: import com.sun.tools.ws.processor.util.IndentingWriter;
057: import com.sun.tools.ws.wscompile.ErrorReceiver;
058: import com.sun.tools.ws.wscompile.WsimportOptions;
059: import com.sun.xml.ws.util.xml.XmlUtil;
060: import org.w3c.dom.Element;
061: import org.w3c.dom.NodeList;
062:
063: import javax.jws.HandlerChain;
064: import javax.xml.transform.OutputKeys;
065: import javax.xml.transform.Transformer;
066: import javax.xml.transform.dom.DOMSource;
067: import javax.xml.transform.stream.StreamResult;
068: import java.io.File;
069: import java.io.FileOutputStream;
070: import java.io.OutputStreamWriter;
071: import java.util.ArrayList;
072: import java.util.Iterator;
073: import java.util.List;
074:
075: /**
076: *
077: * @author WS Development Team
078: */
079: public abstract class GeneratorBase implements GeneratorConstants,
080: ModelVisitor {
081: private File destDir;
082: private String targetVersion;
083: protected boolean donotOverride;
084: protected JCodeModel cm;
085: protected final Model model;
086: protected final String wsdlLocation;
087: protected final ErrorReceiver receiver;
088: protected final WsimportOptions options;
089:
090: protected GeneratorBase(Model model, WsimportOptions options,
091: ErrorReceiver receiver) {
092: this .model = model;
093: this .options = options;
094: this .destDir = options.destDir;
095: this .receiver = receiver;
096: this .wsdlLocation = options.wsdlLocation;
097: this .targetVersion = options.target.getVersion();
098: this .cm = options.getCodeModel();
099: }
100:
101: protected void doGeneration() {
102: try {
103: model.accept(this );
104: } catch (Exception e) {
105: receiver.error(e);
106: }
107: }
108:
109: public void visit(Model model) throws Exception {
110: for (Service service : model.getServices()) {
111: service.accept(this );
112: }
113: }
114:
115: public void visit(Service service) throws Exception {
116: for (Port port : service.getPorts()) {
117: port.accept(this );
118: }
119: }
120:
121: public void visit(Port port) throws Exception {
122: for (Operation operation : port.getOperations()) {
123: operation.accept(this );
124: }
125: }
126:
127: public void visit(Operation operation) throws Exception {
128: operation.getRequest().accept(this );
129: if (operation.getResponse() != null)
130: operation.getResponse().accept(this );
131: Iterator faults = operation.getFaultsSet().iterator();
132: if (faults != null) {
133: Fault fault;
134: while (faults.hasNext()) {
135: fault = (Fault) faults.next();
136: fault.accept(this );
137: }
138: }
139: }
140:
141: public void visit(Parameter param) throws Exception {
142: }
143:
144: public void visit(Block block) throws Exception {
145: }
146:
147: public void visit(Response response) throws Exception {
148: }
149:
150: public void visit(Request request) throws Exception {
151: }
152:
153: public void visit(Fault fault) throws Exception {
154: }
155:
156: public List<String> getJAXWSClassComment() {
157: return getJAXWSClassComment(targetVersion);
158: }
159:
160: public static List<String> getJAXWSClassComment(String targetVersion) {
161: List<String> comments = new ArrayList<String>();
162: comments.add("This class was generated by the JAX-WS RI.\n");
163: comments.add(ToolVersion.VERSION.BUILD_VERSION + "\n");
164: comments.add("Generated source version: " + targetVersion);
165: return comments;
166: }
167:
168: protected JDefinedClass getClass(String className, ClassType type)
169: throws JClassAlreadyExistsException {
170: JDefinedClass cls;
171: try {
172: cls = cm._class(className, type);
173: } catch (JClassAlreadyExistsException e) {
174: cls = cm._getClass(className);
175: if (cls == null)
176: throw e;
177: }
178: return cls;
179: }
180:
181: protected void log(String msg) {
182: if (options.verbose) {
183: System.out.println("["
184: + Names.stripQualifier(this .getClass().getName())
185: + ": " + msg + "]");
186: }
187: }
188:
189: protected void writeHandlerConfig(String className,
190: JDefinedClass cls, WsimportOptions options) {
191: Element e = options.getHandlerChainConfiguration();
192: if (e == null)
193: return;
194: JAnnotationUse handlerChainAnn = cls.annotate(cm
195: .ref(HandlerChain.class));
196: NodeList nl = e.getElementsByTagNameNS(
197: "http://java.sun.com/xml/ns/javaee", "handler-chain");
198: if (nl.getLength() > 0) {
199: String fName = getHandlerConfigFileName(className);
200: handlerChainAnn.param("file", fName);
201: generateHandlerChainFile(e, className);
202: }
203: }
204:
205: private String getHandlerConfigFileName(String fullName) {
206: String name = Names.stripQualifier(fullName);
207: return name + "_handler.xml";
208: }
209:
210: private void generateHandlerChainFile(Element hChains, String name) {
211: String hcName = getHandlerConfigFileName(name);
212:
213: File packageDir = DirectoryUtil.getOutputDirectoryFor(name,
214: destDir);
215: File hcFile = new File(packageDir, hcName);
216:
217: options.addGeneratedFile(hcFile);
218:
219: try {
220: IndentingWriter p = new IndentingWriter(
221: new OutputStreamWriter(new FileOutputStream(hcFile)));
222: Transformer it = XmlUtil.newTransformer();
223:
224: it.setOutputProperty(OutputKeys.METHOD, "xml");
225: it.setOutputProperty(OutputKeys.INDENT, "yes");
226: it.setOutputProperty(
227: "{http://xml.apache.org/xslt}indent-amount", "2");
228: it.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
229: it.transform(new DOMSource(hChains), new StreamResult(p));
230: p.close();
231: } catch (Exception e) {
232: throw new GeneratorException(
233: "generator.nestedGeneratorError", e);
234: }
235: }
236:
237: }
|