comparison src/org/ssdt_ohio/tools/ant/WsdlSetup.java @ 2:09f9f3d5c507

CM-127: Move wsdlsetup and wsd2html.xsl from Tools
author smith@nwoca.org
date Tue, 07 Jun 2011 18:06:07 -0400
parents
children
comparison
equal deleted inserted replaced
1:82fc5e17cc59 2:09f9f3d5c507
1 /* Copyright 2003 Ohio Department of Education, Office of Information Technology,
2 * 25 South Front St, Columbus, Ohio 43215, U.S.A., All Rights Reserved.
3 */
4 package org.ssdt_ohio.tools.ant;
5
6 import java.io.File;
7
8 import java.util.ArrayList;
9 import java.util.Iterator;
10 import java.util.List;
11 import javax.xml.parsers.DocumentBuilderFactory;
12 import javax.xml.transform.Transformer;
13 import javax.xml.transform.TransformerFactory;
14 import javax.xml.transform.dom.DOMSource;
15 import javax.xml.transform.stream.StreamResult;
16
17 import org.apache.tools.ant.BuildException;
18 import org.apache.tools.ant.Task;
19
20 import org.w3c.dom.Document;
21 import org.w3c.dom.Element;
22 import org.w3c.dom.Node;
23 import org.w3c.dom.NodeList;
24
25 import java.util.regex.Pattern;
26
27 /** Simple ant task for maniuplating WSDL settings.
28 *
29 *
30 * @author smith
31 * @since 2007-8-1
32 */
33 public class WsdlSetup
34 extends Task {
35
36 private static final String SCHEMA_NS = "http://www.w3.org/2001/XMLSchema";
37 private static final String OECNRPC_NS = "http://xml.ssdt.nwoca.org/OECN-RPC/10";
38 private final List locations = new ArrayList(); /* Store locations */
39
40 private String file;
41 private String destfile;
42 private String typesFile;
43 private String typesPattern = ".*Fault$";
44 private boolean elementFormDefaultQualified = true;
45
46 public void execute() {
47 if (file == null) {
48 throw new BuildException("must specify 'file' as input file");
49 }
50
51 if (destfile == null) {
52 throw new BuildException("must specify 'destfile' as output file");
53 }
54
55 try {
56 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
57 dbf.setNamespaceAware(true);
58
59 Document wsdl = dbf.newDocumentBuilder().parse(new File(getFile()));
60 NodeList imports = wsdl.getElementsByTagNameNS(SCHEMA_NS, "import");
61
62 for (int i = 0; i < imports.getLength(); i++) {
63 replaceLocations((Element) imports.item(i));
64 }
65
66 TransformerFactory tFactory = TransformerFactory.newInstance();
67 Transformer transformer = tFactory.newTransformer();
68 DOMSource source = new DOMSource(wsdl);
69 StreamResult result = new StreamResult(new File(getDestfile()));
70 transformer.transform(source, result);
71
72 if (typesFile != null) {
73 generateTypesSchema(wsdl);
74 }
75 } catch (javax.xml.parsers.ParserConfigurationException e) {
76 throw new BuildException(e);
77 } catch (org.xml.sax.SAXException e) {
78 throw new BuildException(e);
79 } catch (java.io.IOException e) {
80 throw new BuildException(e);
81 } catch (javax.xml.transform.TransformerException e) {
82 throw new BuildException(e);
83 }
84 }
85
86 private void replaceLocations(Element imp) {
87 String ns = imp.getAttribute("namespace");
88
89 for (Iterator it = locations.iterator(); it.hasNext();) {
90 SchemaLocation loc = (SchemaLocation) it.next();
91
92 if (ns.equals(loc.getNamespace())) {
93 imp.setAttribute("schemaLocation", loc.getLocation());
94 }
95 }
96 }
97
98 private void generateTypesSchema(Document wsdl)
99 throws BuildException {
100 try {
101 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
102 dbf.setNamespaceAware(true);
103
104 Document schema = dbf.newDocumentBuilder().newDocument();
105 Element root = (Element) wsdl.getElementsByTagNameNS(SCHEMA_NS,
106 "schema").item(0);
107 String targetNamespace = root.getAttribute("targetNamespace");
108
109 schema.appendChild(schema.importNode(root, true));
110 root = schema.getDocumentElement();
111 root.setAttribute("xmlns:xsd", SCHEMA_NS);
112 if (isElementFormDefaultQualified()) {
113 root.setAttribute("elementFormDefault", "qualified");
114 }
115 root.setAttribute("xmlns", targetNamespace);
116 root.setAttribute("xmlns:oecnrpc", OECNRPC_NS);
117
118 for (Iterator it = locations.iterator(); it.hasNext();) {
119 SchemaLocation loc = (SchemaLocation) it.next();
120
121 if (loc.getPrefix() != null) {
122 root.setAttribute("xmlns:" + loc.getPrefix(), loc.getNamespace());
123 }
124 }
125
126 // Remove everything except the included types.
127 boolean done = false;
128
129 Pattern typesPat = Pattern.compile(getTypesPattern());
130
131 while (!done) {
132 done = true;
133
134 NodeList children = root.getChildNodes();
135
136 for (int i = 0; i < children.getLength(); i++) {
137 if (children.item(i).getNodeType() == Node.ELEMENT_NODE) {
138 Element e = (Element) children.item(i);
139
140 if (!e.getLocalName().equals("import") && !typesPat.matcher(e.
141 getAttribute("name")).matches()) {
142 root.removeChild(e);
143 done = false;
144 }
145 }
146 }
147 }
148 TransformerFactory tFactory = TransformerFactory.newInstance();
149 Transformer transformer = tFactory.newTransformer();
150
151 DOMSource source = new DOMSource(schema);
152 StreamResult result = new StreamResult(new File(getTypesFile()));
153 transformer.transform(source, result);
154 } catch (Exception e) {
155 throw new BuildException(e);
156 }
157 }
158
159 public SchemaLocation createSchemaLocation() {
160 SchemaLocation loc = new SchemaLocation();
161 locations.add(loc);
162
163 return loc;
164 }
165
166 /**
167 * Getter for property file.
168 * @return Value of property file.
169 */
170 public java.lang.String getFile() {
171 return file;
172 }
173
174 /**
175 * Setter for property file.
176 * @param file New value of property file.
177 */
178 public void setFile(java.lang.String file) {
179 this.file = file;
180 }
181
182 /**
183 * Getter for property destfile.
184 * @return Value of property destfile.
185 */
186 public java.lang.String getDestfile() {
187 return destfile;
188 }
189
190 /**
191 * Setter for property destfile.
192 * @param destfile New value of property destfile.
193 */
194 public void setDestfile(java.lang.String destfile) {
195 this.destfile = destfile;
196 }
197
198 /**
199 * Getter for property typesFile.
200 * @return Value of property typesFile.
201 */
202 public java.lang.String getTypesFile() {
203 return typesFile;
204 }
205
206 /**
207 * Setter for property typesFile.
208 * @param typesFile New value of property typesFile.
209 */
210 public void setTypesFile(java.lang.String typesFile) {
211 this.typesFile = typesFile;
212 }
213
214 /**
215 * @return the elementFormDefaultQualified
216 */
217 public boolean isElementFormDefaultQualified() {
218 return elementFormDefaultQualified;
219 }
220
221 /**
222 * @param elementFormDefaultQualified the elementFormDefaultQualified to set
223 */
224 public void setElementFormDefaultQualified(boolean elementFormDefaultQualified) {
225 this.elementFormDefaultQualified = elementFormDefaultQualified;
226 }
227
228 /** A nested 'schemaLocation' object. */
229 public class SchemaLocation {
230
231 private String namespace;
232 private String location;
233 private String prefix;
234
235 public SchemaLocation() {
236 }
237
238 public java.lang.String getNamespace() {
239 return namespace;
240 }
241
242 public void setNamespace(java.lang.String namespace) {
243 this.namespace = namespace;
244 }
245
246 public java.lang.String getLocation() {
247 return location;
248 }
249
250 public void setLocation(java.lang.String location) {
251 this.location = location;
252 }
253
254 public String getPrefix() {
255 return prefix;
256 }
257
258 public void setPrefix(String prefix) {
259 this.prefix = prefix;
260 }
261 }
262
263 public String getTypesPattern() {
264 return typesPattern;
265 }
266
267 public void setTypesPattern(String typesPattern) {
268 this.typesPattern = typesPattern;
269 }
270 }