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: */
019:
020: package org.apache.axis2.wsdl.codegen.writer;
021:
022: import org.apache.axis2.i18n.Messages;
023: import org.apache.axis2.util.XSLTTemplateProcessor;
024: import org.apache.axis2.wsdl.codegen.CodeGenerationException;
025: import org.apache.axis2.wsdl.i18n.CodegenMessages;
026: import org.apache.axis2.wsdl.util.ConfigPropertyFileLoader;
027: import org.apache.commons.logging.Log;
028: import org.apache.commons.logging.LogFactory;
029: import org.w3c.dom.Document;
030:
031: import javax.xml.transform.URIResolver;
032: import java.io.File;
033: import java.io.FileOutputStream;
034: import java.io.InputStream;
035: import java.util.Iterator;
036: import java.util.Map;
037:
038: public abstract class FileWriter {
039:
040: private static final Log log = LogFactory.getLog(FileWriter.class);
041:
042: protected File outputFileLocation = null;
043: protected File outputFile = null;
044: protected FileOutputStream stream = null;
045: protected InputStream xsltStream = null;
046: protected String language = ConfigPropertyFileLoader
047: .getDefaultLanguage(); //default would java
048:
049: protected static final String TEMPLATE_SUFFIX = ".template";
050: protected static final String EXTENSION_SUFFIX = ".extension";
051: protected static final String SEPARATOR_STRING = ",";
052:
053: protected boolean fileExists = false;//a flag saying the file is existing
054: protected boolean isOverride = false;
055:
056: /**
057: * Sets the language.
058: *
059: * @param language
060: */
061: public void setLanguage(String language) {
062: this .language = language;
063: }
064:
065: /** Loads the template. */
066: public void loadTemplate() throws CodeGenerationException {
067: // the default behavior for the class writers is to use the property map from the languge specific types
068: // The properties are arranged in the following order
069: // <lang-name>.* .template=<write-class>,<template-name>
070:
071: //first get the language specific property map
072: Class clazz = this .getClass();
073: Map languageSpecificPropertyMap = (Map) ConfigPropertyFileLoader
074: .getLanguageSpecificPropertiesMap().get(this .language);
075: if (languageSpecificPropertyMap == null) {
076: throw new CodeGenerationException(CodegenMessages
077: .getMessage("writer.noLangPropertiesExtension"));
078: }
079:
080: String templateName = findTemplate(languageSpecificPropertyMap);
081: if (templateName != null) {
082: this .xsltStream = clazz.getResourceAsStream(templateName);
083: } else {
084: throw new CodeGenerationException(CodegenMessages
085: .getMessage("writer.templateMissing"));
086: }
087:
088: }
089:
090: protected String findTemplate(Map languageSpecificPropertyMap) {
091: //search through the proprty names to find the template relevant to this class
092:
093: String ownClazzName = this .getClass().getName();
094: String key;
095: String propertyValue;
096: String templateName = null;
097: Iterator keys = languageSpecificPropertyMap.keySet().iterator();
098:
099: while (keys.hasNext()) {
100: //check for template entries
101: key = keys.next().toString();
102: if (key.endsWith(TEMPLATE_SUFFIX)) {
103: // check if the class name is there
104: propertyValue = languageSpecificPropertyMap.get(key)
105: .toString();
106: if (propertyValue.startsWith(ownClazzName)) {
107: //bingo! we found the right template
108: templateName = propertyValue
109: .substring(propertyValue
110: .indexOf(SEPARATOR_STRING) + 1);
111: break;
112: }
113: }
114:
115: }
116: return templateName;
117: }
118:
119: /**
120: * Creates the output file.
121: *
122: * @param packageName
123: * @param fileName
124: * @throws Exception
125: */
126: public void createOutFile(String packageName, String fileName)
127: throws Exception {
128: outputFile = org.apache.axis2.util.FileWriter.createClassFile(
129: outputFileLocation, packageName, fileName,
130: getFileExtensionForLanguage(language));
131: //set the existing flag
132: if (this .isOverride) {
133: this .stream = new FileOutputStream(outputFile);
134: } else {
135: fileExists = outputFile.exists();
136: if (!fileExists) {
137: this .stream = new FileOutputStream(outputFile);
138: } else {
139: log.info(Messages.getMessage("fileExistsNoOverwrite",
140: outputFile.toString()));
141: }
142: }
143:
144: }
145:
146: /**
147: * Finds the file name extension.
148: *
149: * @param language
150: * @return Returns the file extension.
151: */
152: protected String getFileExtensionForLanguage(String language) {
153: Map languageSpecificPropertyMap = (Map) ConfigPropertyFileLoader
154: .getLanguageSpecificPropertiesMap().get(this .language);
155: Iterator keys = languageSpecificPropertyMap.keySet().iterator();
156: String key;
157: String extension = null;
158: while (keys.hasNext()) {
159: //check for template entries
160: key = keys.next().toString();
161: if (key.endsWith(EXTENSION_SUFFIX)) {
162: extension = languageSpecificPropertyMap.get(key)
163: .toString();
164: //add a . to the front
165: extension = "." + extension;
166: }
167: }
168:
169: return extension;
170: }
171:
172: /**
173: * Writes the output file.
174: *
175: * @param doc
176: * @throws Exception
177: */
178: public void parse(Document doc, URIResolver resolver)
179: throws Exception {
180: if (!fileExists) {
181: XSLTTemplateProcessor.parse(this .stream, doc,
182: this .xsltStream, resolver);
183: this .stream.flush();
184: this .stream.close();
185: }
186: }
187:
188: public boolean isOverride() {
189: return isOverride;
190: }
191:
192: public void setOverride(boolean override) {
193: isOverride = override;
194: }
195:
196: public File getOutputFile() {
197: return outputFile;
198: }
199:
200: public void setOutputFile(File outputFile) {
201: this.outputFile = outputFile;
202: }
203: }
|