KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > tools > ant > wsdl > NamespaceMapping


1 /*
2  * Copyright 2002,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.axis.tools.ant.wsdl;
18
19 import org.apache.tools.ant.BuildException;
20 import org.apache.tools.ant.Project;
21 import org.apache.tools.ant.ProjectComponent;
22
23 import java.io.BufferedInputStream JavaDoc;
24 import java.io.File JavaDoc;
25 import java.io.FileInputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.util.Enumeration JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Properties JavaDoc;
30
31 /**
32  * Used for nested package definitions.
33  * The file format used for storing mappings is a list of package=namespace
34  */

35 public class NamespaceMapping implements Mapper {
36
37
38     private String JavaDoc namespace = null;
39     private String JavaDoc packageName = null;
40     private File JavaDoc mappingFile;
41
42     /**
43      * pass in the namespace to map to
44      */

45     public NamespaceMapping() {
46     }
47
48     /**
49      * the namespace in the WSDL. Required.
50      * @param value new uri of the mapping
51      */

52     public void setNamespace(String JavaDoc value) {
53         namespace = value;
54     }
55
56     /**
57      * the Java package to bind to. Required.
58      * @param value java package name
59      */

60     public void setPackage(String JavaDoc value) {
61         packageName = value;
62     }
63
64     /**
65      * name of a property file that contains mappings in
66      * package=namespace format
67      * @param file file to load
68      */

69     public void setFile(File JavaDoc file) {
70         mappingFile = file;
71     }
72
73     /**
74      * map a namespace to a package
75      * @param owner owning project component (For logging)
76      * @param map map to assign to
77      * @param packName package name
78      * @param nspace namespace
79      * @param packageIsKey if the package is to be the key for the map
80      */

81     protected void map(ProjectComponent owner,
82                        HashMap JavaDoc map,
83                        String JavaDoc packName,
84                        String JavaDoc nspace,
85                        boolean packageIsKey) {
86         owner.log("mapping "+nspace+" to "+packName, Project.MSG_VERBOSE);
87         if(packageIsKey) {
88             map.put(packName,nspace);
89         } else {
90             map.put(nspace, packName);
91         }
92     }
93
94     /**
95      * validate the option set
96      */

97     private void validate() {
98         if (mappingFile != null) {
99             if (namespace != null || packageName != null) {
100                 throw new BuildException(
101                         "Namespace or Package cannot be used with a File attribute");
102             }
103         } else {
104             if (namespace == null) {
105                 throw new BuildException("namespace must be defined");
106             }
107             if (packageName == null) {
108                 throw new BuildException("package must be defined");
109             }
110         }
111     }
112
113     /**
114      * Load a mapping file and save it to the map
115      * @param owner owner component
116      * @param map target map file
117      * @param packageIsKey if the package is to be the key for the map
118      * @throws BuildException if an IOException needed swallowing
119      */

120     protected void mapFile(ProjectComponent owner, HashMap JavaDoc map, boolean packageIsKey) throws BuildException {
121         Properties JavaDoc props = loadMappingPropertiesFile();
122         Enumeration JavaDoc keys = props.keys();
123         while (keys.hasMoreElements()) {
124             String JavaDoc packageName = (String JavaDoc) keys.nextElement();
125             String JavaDoc namespace = props.getProperty(packageName);
126             map(owner, map, packageName, namespace, packageIsKey);
127         }
128     }
129
130     /**
131      * load a file containing properties
132      * @return a properties file with zero or more mappings
133      * @throws BuildException if the load failed
134      */

135     private Properties JavaDoc loadMappingPropertiesFile() throws BuildException {
136         Properties JavaDoc props = new Properties JavaDoc();
137         FileInputStream JavaDoc instr = null;
138         try {
139             instr = new FileInputStream JavaDoc(mappingFile);
140             props.load(new BufferedInputStream JavaDoc(instr));
141         } catch (IOException JavaDoc e) {
142             throw new BuildException("Failed to load " + mappingFile, e);
143         } finally {
144             if (instr != null) {
145                 try {
146                     instr.close();
147                 } catch (IOException JavaDoc e) {
148                 }
149             }
150         }
151         return props;
152     }
153
154
155     /**
156      * execute the mapping
157      * @param owner owner object
158      * @param map map to map to
159      * @param packageIsKey if the package is to be the key for the map
160      * @throws BuildException in case of emergency
161      */

162     public void execute(ProjectComponent owner, HashMap JavaDoc map, boolean packageIsKey) throws BuildException {
163         validate();
164         if (mappingFile != null) {
165             mapFile(owner, map,packageIsKey);
166         } else {
167             map(owner, map, packageName, namespace, packageIsKey);
168         }
169     }
170
171
172 }
Popular Tags