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: package org.apache.ws.java2wsdl;
020:
021: import javax.xml.stream.XMLStreamConstants;
022: import javax.xml.stream.XMLStreamConstants;
023:
024: import java.io.File;
025: import java.io.FileInputStream;
026: import java.io.InputStream;
027: import java.util.HashMap;
028:
029: import javax.xml.namespace.QName;
030: import javax.xml.stream.XMLStreamReader;
031:
032: import org.apache.axiom.om.util.StAXUtils;
033: import org.apache.tools.ant.BuildException;
034:
035: /**
036: * Used for nested package definitions.
037: * The file format used for storing mappings is a list of package=namespace
038: */
039: public class NamespaceMapping implements Mapper {
040: public static final QName PKG2NS_MAPPINGS = new QName(
041: "http://ws.apache.org/axis2", "pkg2ns_mapping");
042: public static final QName MAPPING = new QName(
043: "http://ws.apache.org/axis2", "mapping");
044: public static final String NAMESPACE = "namespace";
045: public static final String PACKAGE = "package";
046:
047: private String namespace = null;
048: private String packageName = null;
049: private File mappingFile;
050: private InputStream mapInputStream = null;
051: private XMLStreamReader mapXmlReader = null;
052:
053: /**
054: * pass in the namespace to map to
055: */
056: public NamespaceMapping() {
057: }
058:
059: /**
060: * the namespace in the WSDL. Required.
061: * @param value new uri of the mapping
062: */
063: public void setNamespace(String value) {
064: namespace = value;
065: }
066:
067: /**
068: * the Java package to bind to. Required.
069: * @param value java package name
070: */
071: public void setPackage(String value) {
072: packageName = value;
073: }
074:
075: /**
076: * name of a property file that contains mappings in
077: * package=namespace format
078: * @param file file to load
079: */
080: public void setFile(File file) {
081: mappingFile = file;
082: }
083:
084: /**
085: * map a namespace to a package
086: * @param map map to assign to
087: * @param packName package name
088: * @param nspace namespace
089: * @param packageIsKey if the package is to be the key for the map
090: */
091: protected void map(HashMap map, String packName, String nspace,
092: boolean packageIsKey) {
093: if (packageIsKey) {
094: map.put(packName, nspace);
095: } else {
096: map.put(nspace, packName);
097: }
098: }
099:
100: /**
101: * validate the option set
102: */
103: private void validate() {
104: if (mappingFile != null || mapInputStream != null
105: || mapXmlReader != null) {
106: if (namespace != null || packageName != null) {
107: throw new BuildException(
108: "Namespace or Package cannot be used with a File attribute");
109: }
110: } else {
111: if (namespace == null) {
112: throw new BuildException("namespace must be defined");
113: }
114: if (packageName == null) {
115: throw new BuildException("package must be defined");
116: }
117: }
118: }
119:
120: /**
121: * Load a mapping xml reader and save it to the map
122: * @param map target map file
123: * @param packageIsKey if the package is to be the key for the map
124: * @throws BuildException if an IOException needed swallowing
125: */
126: protected void mapXmlReader(HashMap map, boolean packageIsKey)
127: throws BuildException {
128: try {
129: loadMappingFromXMLReader(map, packageIsKey);
130: } catch (Exception e) {
131: throw new BuildException(e);
132: }
133: }
134:
135: /**
136: * Load a mapping input stream and save it to the map
137: * @param map target map file
138: * @param packageIsKey if the package is to be the key for the map
139: * @throws BuildException if an IOException needed swallowing
140: */
141: protected void mapXmlStream(HashMap map, boolean packageIsKey)
142: throws BuildException {
143: try {
144: mapXmlReader = StAXUtils
145: .createXMLStreamReader(mapInputStream);
146: mapXmlReader(map, packageIsKey);
147: //ensure this clean up so that the next invocation does not have any stale state
148: mapXmlReader = null;
149: } catch (Exception e) {
150: throw new BuildException(e);
151: }
152: }
153:
154: /**
155: * Load a mapping file and save it to the map
156: * @param map target map file
157: * @param packageIsKey if the package is to be the key for the map
158: * @throws BuildException if an IOException needed swallowing
159: */
160: protected void mapFile(HashMap map, boolean packageIsKey)
161: throws BuildException {
162: try {
163: mapInputStream = new FileInputStream(mappingFile);
164: mapXmlStream(map, packageIsKey);
165: //need to do this since the file was opened here
166: mapInputStream.close();
167: //ensure this clean up so that the next invocation does not have any stale state
168: mapInputStream = null;
169: } catch (Exception e) {
170: throw new BuildException(e);
171: }
172: }
173:
174: /**
175: * load from an xml reader containing mapping info
176: * @return a properties file with zero or more mappings
177: * @throws BuildException if the load failed
178: */
179: private void loadMappingFromXMLReader(HashMap map,
180: boolean packageIsKey) throws BuildException {
181: try {
182: QName parentElement = null;
183: //if the reader is in the fragment that deals with package to namespace mappings
184: while (mapXmlReader.hasNext()) {
185: // mapXmlReader.next();mapXmlReader.next();
186: // if ( mapXmlReader.getName().equals(PKG2NS_MAPPINGS) ) {
187: switch (mapXmlReader.next()) {
188: case XMLStreamConstants.START_ELEMENT:
189: QName qname = mapXmlReader.getName();
190: if (MAPPING.equals(qname)
191: && parentElement.equals(PKG2NS_MAPPINGS)) {
192: map(map, mapXmlReader.getAttributeValue(null,
193: PACKAGE), mapXmlReader
194: .getAttributeValue(null, NAMESPACE),
195: packageIsKey);
196: } else {
197: parentElement = qname;
198: }
199:
200: mapXmlReader.next();
201: break;
202: case XMLStreamConstants.END_ELEMENT:
203: if (mapXmlReader.getName().equals(PKG2NS_MAPPINGS)) {
204: //ensure this clean up
205: return;
206: }
207: break;
208: // }
209: }
210: }
211: } catch (Exception e) {
212: System.out
213: .println("Exception while loading package to namespace mappings... "
214: + e);
215: }
216: }
217:
218: /**
219: * execute the mapping
220: * @param map map to map to
221: * @param packageIsKey if the package is to be the key for the map
222: * @throws BuildException in case of emergency
223: */
224: public void execute(HashMap map, boolean packageIsKey)
225: throws BuildException {
226: validate();
227: if (mappingFile != null) {
228: mapFile(map, packageIsKey);
229: //ensure this clean up so that the next invocation does not have any stale state
230: mappingFile = null;
231: } else if (mapInputStream != null) {
232: mapXmlStream(map, packageIsKey);
233: } else if (mapXmlReader != null) {
234: mapXmlReader(map, packageIsKey);
235: } else {
236: map(map, packageName, namespace, packageIsKey);
237: }
238: }
239:
240: public InputStream getMapInputStream() {
241: return mapInputStream;
242: }
243:
244: public void setMapInputStream(InputStream mapInputStream) {
245: this .mapInputStream = mapInputStream;
246: }
247:
248: public File getMappingFile() {
249: return mappingFile;
250: }
251:
252: public void setMappingFile(File mappingFile) {
253: this .mappingFile = mappingFile;
254: }
255:
256: public XMLStreamReader getXmlReader() {
257: return mapXmlReader;
258: }
259:
260: public void setXmlReader(XMLStreamReader xmlReader) {
261: this.mapXmlReader = xmlReader;
262: }
263:
264: }
|