001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.tools.misc.processor;
019:
020: import java.io.IOException;
021: import java.io.Writer;
022: import java.util.ArrayList;
023: import java.util.HashMap;
024: import java.util.List;
025: import java.util.Map;
026: import java.util.logging.Level;
027: import java.util.logging.Logger;
028: import javax.wsdl.Definition;
029: import javax.wsdl.extensions.schema.Schema;
030: import javax.wsdl.factory.WSDLFactory;
031: import javax.wsdl.xml.WSDLReader;
032:
033: import org.xml.sax.SAXParseException;
034:
035: import org.apache.cxf.common.i18n.Message;
036: import org.apache.cxf.common.logging.LogUtils;
037: import org.apache.cxf.tools.common.Processor;
038: import org.apache.cxf.tools.common.ToolConstants;
039: import org.apache.cxf.tools.common.ToolContext;
040: import org.apache.cxf.tools.common.ToolException;
041: import org.apache.cxf.tools.util.ClassCollector;
042: import org.apache.cxf.tools.util.FileWriterUtil;
043: import org.apache.cxf.tools.validator.internal.WSDL11Validator;
044: import org.apache.cxf.wsdl.WSDLExtensibilityPlugin;
045: import org.apache.cxf.wsdl11.WSDLDefinitionBuilder;
046:
047: public class AbstractWSDLToProcessor implements Processor,
048: com.sun.tools.xjc.api.ErrorListener {
049: protected static final Logger LOG = LogUtils
050: .getL7dLogger(AbstractWSDLToProcessor.class);
051: protected static final String WSDL_FILE_NAME_EXT = ".wsdl";
052:
053: protected Definition wsdlDefinition;
054: protected ToolContext env;
055: protected WSDLFactory wsdlFactory;
056: protected WSDLReader wsdlReader;
057:
058: protected ClassCollector classColletor;
059: List<Schema> schemaList = new ArrayList<Schema>();
060:
061: private final Map<String, WSDLExtensibilityPlugin> wsdlPlugins = new HashMap<String, WSDLExtensibilityPlugin>();
062:
063: protected Writer getOutputWriter(String newNameExt)
064: throws ToolException {
065: Writer writer = null;
066: String newName = null;
067: String outputDir;
068:
069: if (env.get(ToolConstants.CFG_OUTPUTFILE) != null) {
070: newName = (String) env.get(ToolConstants.CFG_OUTPUTFILE);
071: } else {
072: String oldName = (String) env
073: .get(ToolConstants.CFG_WSDLURL);
074: int position = oldName.lastIndexOf("/");
075: if (position < 0) {
076: position = oldName.lastIndexOf("\\");
077: }
078: if (position >= 0) {
079: oldName = oldName.substring(position + 1, oldName
080: .length());
081: }
082: if (oldName.toLowerCase().indexOf(WSDL_FILE_NAME_EXT) >= 0) {
083: newName = oldName.substring(0, oldName.length() - 5)
084: + newNameExt + WSDL_FILE_NAME_EXT;
085: } else {
086: newName = oldName + newNameExt;
087: }
088: }
089: if (env.get(ToolConstants.CFG_OUTPUTDIR) != null) {
090: outputDir = (String) env.get(ToolConstants.CFG_OUTPUTDIR);
091: if (!("/".equals(outputDir
092: .substring(outputDir.length() - 1)) || "\\"
093: .equals(outputDir.substring(outputDir.length() - 1)))) {
094: outputDir = outputDir + "/";
095: }
096: } else {
097: outputDir = "./";
098: }
099: FileWriterUtil fw = new FileWriterUtil(outputDir);
100: try {
101: writer = fw.getWriter("", newName);
102: } catch (IOException ioe) {
103: Message msg = new Message("FAIL_TO_WRITE_FILE", LOG, env
104: .get(ToolConstants.CFG_OUTPUTDIR)
105: + System.getProperty("file.seperator") + newName);
106: throw new ToolException(msg, ioe);
107: }
108: return writer;
109: }
110:
111: protected void parseWSDL(String wsdlURL) throws ToolException {
112: WSDLDefinitionBuilder builder = new WSDLDefinitionBuilder(true);
113: wsdlDefinition = builder.build(wsdlURL);
114:
115: wsdlReader = builder.getWSDLReader();
116: wsdlFactory = builder.getWSDLFactory();
117:
118: wsdlPlugins.putAll(builder.getWSDLPlugins());
119: LOG.log(Level.INFO, "FOUND_WSDL_PLUGINS", wsdlPlugins.keySet());
120: }
121:
122: public WSDLExtensibilityPlugin getWSDLPlugin(final String key,
123: final Class clz) {
124: StringBuffer sb = new StringBuffer();
125: sb.append(key);
126: sb.append("-");
127: sb.append(clz.getName());
128: WSDLExtensibilityPlugin plugin = wsdlPlugins.get(sb.toString());
129: if (plugin == null) {
130: throw new ToolException(new Message("FOUND_NO_WSDL_PLUGIN",
131: LOG, sb.toString(), clz));
132: }
133: return plugin;
134: }
135:
136: protected void init() throws ToolException {
137:
138: }
139:
140: public Definition getWSDLDefinition() {
141: return this .wsdlDefinition;
142: }
143:
144: public void process() throws ToolException {
145: }
146:
147: public void validateWSDL() throws ToolException {
148: if (env.validateWSDL()) {
149: WSDL11Validator validator = new WSDL11Validator(
150: this .wsdlDefinition, this .env);
151: validator.isValid();
152: }
153: }
154:
155: public void setEnvironment(ToolContext penv) {
156: this .env = penv;
157: }
158:
159: public ToolContext getEnvironment() {
160: return this .env;
161: }
162:
163: public void error(SAXParseException exception) {
164: if (this .env.isVerbose()) {
165: exception.printStackTrace();
166: } else {
167: System.err.println("Parsing schema error: \n"
168: + exception.toString());
169: }
170: }
171:
172: public void fatalError(SAXParseException exception) {
173: if (this .env.isVerbose()) {
174: exception.printStackTrace();
175: } else {
176: System.err.println("Parsing schema fatal error: \n"
177: + exception.toString());
178: }
179: }
180:
181: public void info(SAXParseException exception) {
182: if (this .env.isVerbose()) {
183: System.err.println("Parsing schema info: "
184: + exception.toString());
185: }
186: }
187:
188: public void warning(SAXParseException exception) {
189: if (this .env.isVerbose()) {
190: System.err.println("Parsing schema warning "
191: + exception.toString());
192: }
193: }
194:
195: }
|