File: JBossWSDLReaderImpl.java

package info (click to toggle)
libjboss-web-services-java 0.0%2Bsvn5660%2Bdak2-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 7,276 kB
  • sloc: java: 79,207; xml: 29; makefile: 19; sh: 16
file content (2372 lines) | stat: -rw-r--r-- 61,930 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
/*
 * JBoss, Home of Professional Open Source
 * Copyright 2005, JBoss Inc., and individual contributors as indicated
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jboss.ws.tools.wsdl;

import com.ibm.wsdl.Constants;
import com.ibm.wsdl.extensions.schema.SchemaConstants;
import com.ibm.wsdl.util.StringUtils;
import com.ibm.wsdl.util.xml.DOMUtils;
import com.ibm.wsdl.util.xml.QNameUtils;
import com.ibm.wsdl.util.xml.XPathUtils;
import org.jboss.ws.core.utils.JBossWSEntityResolver;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.xml.sax.InputSource;

import javax.wsdl.*;
import javax.wsdl.extensions.AttributeExtensible;
import javax.wsdl.extensions.ExtensibilityElement;
import javax.wsdl.extensions.ExtensionDeserializer;
import javax.wsdl.extensions.ExtensionRegistry;
import javax.wsdl.extensions.schema.Schema;
import javax.wsdl.extensions.schema.SchemaReference;
import javax.wsdl.factory.WSDLFactory;
import javax.wsdl.xml.WSDLLocator;
import javax.wsdl.xml.WSDLReader;
import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.InputStream;
import java.net.URL;
import java.util.*;


/**
 * A WSDLReader fork of the original wsdl4j 1.6.2 package
 * that delegates to the JBossWSEntityResolver  in
 * <code>private static Document getDocument(InputSource inputSource,String desc)</code>
 * <p>
 * Original authors: Matthew J. Duftler,Nirmal Mukhi
 * 
 * @version $Revision: 3472 $
 */
public class JBossWSDLReaderImpl implements WSDLReader
{
	// Used for determining the style of operations.
	private static final List STYLE_ONE_WAY =
			Arrays.asList(new String[]{Constants.ELEM_INPUT});
	private static final List STYLE_REQUEST_RESPONSE =
			Arrays.asList(new String[]{Constants.ELEM_INPUT, Constants.ELEM_OUTPUT});
	private static final List STYLE_SOLICIT_RESPONSE =
			Arrays.asList(new String[]{Constants.ELEM_OUTPUT, Constants.ELEM_INPUT});
	private static final List STYLE_NOTIFICATION =
			Arrays.asList(new String[]{Constants.ELEM_OUTPUT});

	protected boolean verbose = true;
	protected boolean importDocuments = true;
	protected ExtensionRegistry extReg = null;
	protected String factoryImplName = null;
	protected WSDLLocator loc = null;
	protected WSDLFactory factory = null;

	//Contains all schemas used by this wsdl, either in-line or nested
	//via wsdl imports or schema imports, includes or redefines
	protected Map allSchemas = new Hashtable();


	/**
	 * Sets the specified feature to the specified value.
	 * <p>
	 * The supported features are:
	 * <p>
	 * <table border=1>
	 *   <tr>
	 *     <th>Name</th>
	 *     <th>Description</th>
	 *     <th>Default Value</th>
	 *   </tr>
	 *   <tr>
	 *     <td><center>javax.wsdl.verbose</center></td>
	 *     <td>If set to true, status messages will be displayed.</td>
	 *     <td><center>true</center></td>
	 *   </tr>
	 *   <tr>
	 *     <td><center>javax.wsdl.importDocuments</center></td>
	 *     <td>If set to true, imported WSDL documents will be
	 *         retrieved and processed.</td>
	 *     <td><center>true</center></td>
	 *   </tr>
	 * </table>
	 * <p>
	 * All feature names must be fully-qualified, Java package style. All
	 * names starting with javax.wsdl. are reserved for features defined
	 * by the JWSDL specification. It is recommended that implementation-
	 * specific features be fully-qualified to match the package name
	 * of that implementation. For example: com.abc.featureName
	 *
	 * @param name the name of the feature to be set.
	 * @param value the value to set the feature to.
	 * @throws IllegalArgumentException if the feature name is not recognized.
	 * @see #getFeature(String)
	 */
	public void setFeature(String name, boolean value)
			throws IllegalArgumentException
	{
		if (name == null)
		{
			throw new IllegalArgumentException("Feature name must not be null.");
		}

		if (name.equals(Constants.FEATURE_VERBOSE))
		{
			verbose = value;
		}
		else if (name.equals(Constants.FEATURE_IMPORT_DOCUMENTS))
		{
			importDocuments = value;
		}
		else
		{
			throw new IllegalArgumentException("Feature name '" + name +
					"' not recognized.");
		}
	}

	/**
	 * Gets the value of the specified feature.
	 *
	 * @param name the name of the feature to get the value of.
	 * @return the value of the feature.
	 * @throws IllegalArgumentException if the feature name is not recognized.
	 * @see #setFeature(String, boolean)
	 */
	public boolean getFeature(String name) throws IllegalArgumentException
	{
		if (name == null)
		{
			throw new IllegalArgumentException("Feature name must not be null.");
		}

		if (name.equals(Constants.FEATURE_VERBOSE))
		{
			return verbose;
		}
		else if (name.equals(Constants.FEATURE_IMPORT_DOCUMENTS))
		{
			return importDocuments;
		}
		else
		{
			throw new IllegalArgumentException("Feature name '" + name +
					"' not recognized.");
		}
	}

	/**
	 * Set the extension registry to be used when reading
	 * WSDL documents into a WSDL definition. If an
	 * extension registry is set, that is the extension
	 * registry that will be set as the extensionRegistry
	 * property of the definitions resulting from invoking
	 * readWSDL(...). Default is null.
	 *
	 * @param extReg the extension registry to use for new
	 * definitions
	 */
	public void setExtensionRegistry(ExtensionRegistry extReg)
	{
		this.extReg = extReg;
	}

	/**
	 * Get the extension registry, if one was set. Default is
	 * null.
	 */
	public ExtensionRegistry getExtensionRegistry()
	{
		return extReg;
	}

	/**
	 * Get the WSDLFactory object cached in the reader, or use lazy
	 * instantiation if it is not cached yet.
	 */
	protected WSDLFactory getWSDLFactory() throws WSDLException
	{
		if (factory == null)
		{
			factory = (factoryImplName != null)
					? WSDLFactory.newInstance(factoryImplName)
					: WSDLFactory.newInstance();
		}
		return factory;
	}

	/**
	 * Set a different factory implementation to use for
	 * creating definitions when reading WSDL documents.
	 * As some WSDLReader implementations may only be
	 * capable of creating definitions using the same
	 * factory implementation from which the reader was
	 * obtained, this method is optional. Default is null.
	 *
	 * @param factoryImplName the fully-qualified class name of the
	 * class which provides a concrete implementation of the abstract
	 * class WSDLFactory.
	 * @throws UnsupportedOperationException if this method
	 * is invoked on an implementation which does not
	 * support it.
	 */
	public void setFactoryImplName(String factoryImplName)
			throws UnsupportedOperationException
	{
		//check to see if we really need to change the factory name and clear the cache
		if((this.factoryImplName == null && factoryImplName != null) ||
				(this.factoryImplName != null && !this.factoryImplName.equals(factoryImplName)))
		{
			//the factory object is cached in the reader so set it
			//to null if the factory impl name is reset.
			this.factory = null;

			this.factoryImplName = factoryImplName;
			//if(verbose) System.out.println("WSDLFactory Impl Name set to : "+factoryImplName);
		}
	}

	/**
	 * Get the factoryImplName, if one was set. Default is null.
	 */
	public String getFactoryImplName()
	{
		return factoryImplName;
	}

	protected Definition parseDefinitions(String documentBaseURI,
													  Element defEl,
													  Map importedDefs)
			throws WSDLException
	{
		checkElementName(defEl, Constants.Q_ELEM_DEFINITIONS);

		WSDLFactory factory = getWSDLFactory();
		Definition def = factory.newDefinition();

		if (extReg != null)
		{
			def.setExtensionRegistry(extReg);
		}

		String name = DOMUtils.getAttribute(defEl, Constants.ATTR_NAME);
		String targetNamespace = DOMUtils.getAttribute(defEl,
				Constants.ATTR_TARGET_NAMESPACE);
		NamedNodeMap attrs = defEl.getAttributes();

		if (importedDefs == null)
		{
			importedDefs = new Hashtable();
		}

		if (documentBaseURI != null)
		{
			def.setDocumentBaseURI(documentBaseURI);
			importedDefs.put(documentBaseURI, def);
		}

		if (name != null)
		{
			def.setQName(new QName(targetNamespace, name));
		}

		if (targetNamespace != null)
		{
			def.setTargetNamespace(targetNamespace);
		}

		int size = attrs.getLength();

		for (int i = 0; i < size; i++)
		{
			Attr attr = (Attr)attrs.item(i);
			String namespaceURI = attr.getNamespaceURI();
			String localPart = attr.getLocalName();
			String value = attr.getValue();

			if (namespaceURI != null && namespaceURI.equals(Constants.NS_URI_XMLNS))
			{
				if (localPart != null && !localPart.equals(Constants.ATTR_XMLNS))
				{
					def.addNamespace(localPart, value);
				}
				else
				{
					def.addNamespace(null, value);
				}
			}
		}

		Element tempEl = DOMUtils.getFirstChildElement(defEl);

		while (tempEl != null)
		{
			if (QNameUtils.matches(Constants.Q_ELEM_IMPORT, tempEl))
			{
				def.addImport(parseImport(tempEl, def, importedDefs));
			}
			else if (QNameUtils.matches(Constants.Q_ELEM_DOCUMENTATION, tempEl))
			{
				def.setDocumentationElement(tempEl);
			}
			else if (QNameUtils.matches(Constants.Q_ELEM_TYPES, tempEl))
			{
				def.setTypes(parseTypes(tempEl, def));
			}
			else if (QNameUtils.matches(Constants.Q_ELEM_MESSAGE, tempEl))
			{
				def.addMessage(parseMessage(tempEl, def));
			}
			else if (QNameUtils.matches(Constants.Q_ELEM_PORT_TYPE, tempEl))
			{
				def.addPortType(parsePortType(tempEl, def));
			}
			else if (QNameUtils.matches(Constants.Q_ELEM_BINDING, tempEl))
			{
				def.addBinding(parseBinding(tempEl, def));
			}
			else if (QNameUtils.matches(Constants.Q_ELEM_SERVICE, tempEl))
			{
				def.addService(parseService(tempEl, def));
			}
			else
			{
				def.addExtensibilityElement(
						parseExtensibilityElement(Definition.class, tempEl, def));
			}

			tempEl = DOMUtils.getNextSiblingElement(tempEl);
		}

		parseExtensibilityAttributes(defEl, Definition.class, def, def);

		return def;
	}

	protected Import parseImport(Element importEl,
										  Definition def,
										  Map importedDefs)
			throws WSDLException
	{
		Import importDef = def.createImport();

		try
		{
			String namespaceURI = DOMUtils.getAttribute(importEl,
					Constants.ATTR_NAMESPACE);
			String locationURI = DOMUtils.getAttribute(importEl,
					Constants.ATTR_LOCATION);
			String contextURI = null;

			if (namespaceURI != null)
			{
				importDef.setNamespaceURI(namespaceURI);
			}

			if (locationURI != null)
			{
				importDef.setLocationURI(locationURI);

				if (importDocuments)
				{
					try
					{
						contextURI = def.getDocumentBaseURI();
						Definition importedDef = null;
						InputStream inputStream = null;
						InputSource inputSource = null;
						URL url = null;

						if (loc != null)
						{
							inputSource = loc.getImportInputSource(contextURI, locationURI);

							/*
												 We now have available the latest import URI. This might
												 differ from the locationURI so check the importedDefs for it
												 since it is this that we pass as the documentBaseURI later.
											  */
							String liu = loc.getLatestImportURI();

							importedDef = (Definition)importedDefs.get(liu);

							inputSource.setSystemId(liu);
						}
						else
						{
							URL contextURL = (contextURI != null)
									? StringUtils.getURL(null, contextURI)
									: null;

							url = StringUtils.getURL(contextURL, locationURI);
							importedDef = (Definition)importedDefs.get(url.toString());

							if (importedDef == null)
							{
								inputStream = StringUtils.getContentAsInputStream(url);

								if (inputStream != null)
								{
									inputSource = new InputSource(inputStream);
									inputSource.setSystemId(url.toString());
								}
							}
						}

						if (importedDef == null)
						{
							if (inputSource == null)
							{
								throw new WSDLException(WSDLException.OTHER_ERROR,
										"Unable to locate imported document " +
												"at '" + locationURI + "'" +
												(contextURI == null
														? "."
														: ", relative to '" + contextURI +
														"'."));
							}

							Document doc = getDocument(inputSource, inputSource.getSystemId());

							if (inputStream != null)
							{
								inputStream.close();
							}

							Element documentElement = doc.getDocumentElement();

							/*
												 Check if it's a wsdl document.
												 If it's not, don't retrieve and process it.
												 This should later be extended to allow other types of
												 documents to be retrieved and processed, such as schema
												 documents (".xsd"), etc...
											  */
							if (QNameUtils.matches(Constants.Q_ELEM_DEFINITIONS,
									documentElement))
							{
								if (verbose)
								{
									System.out.println("Retrieving document at '" + locationURI +
											"'" +
											(contextURI == null
													? "."
													: ", relative to '" + contextURI + "'."));
								}

								String urlString =
										(loc != null)
												? loc.getLatestImportURI()
												: (url != null)
												? url.toString()
												: locationURI;

								importedDef = readWSDL(urlString,
										documentElement,
										importedDefs);
							}
							else
							{
								QName docElementQName = QNameUtils.newQName(documentElement);

								if (SchemaConstants.XSD_QNAME_LIST.contains(docElementQName))
								{
									if (verbose)
									{
										System.out.println("Retrieving schema wsdl:imported from '" + locationURI +
												"'" +
												(contextURI == null
														? "."
														: ", relative to '" + contextURI + "'."));
									}

									WSDLFactory factory = getWSDLFactory();

									importedDef = factory.newDefinition();

									if (extReg != null)
									{
										importedDef.setExtensionRegistry(extReg);
									}

									String urlString =
											(loc != null)
													? loc.getLatestImportURI()
													: (url != null)
													? url.toString()
													: locationURI;

									importedDef.setDocumentBaseURI(urlString);

									Types types = importedDef.createTypes();
									types.addExtensibilityElement(
											parseSchema(Types.class, documentElement, importedDef));
									importedDef.setTypes(types);
								}
							}
						}

						if (importedDef != null)
						{
							importDef.setDefinition(importedDef);
						}
					}
					catch (WSDLException e)
					{
						throw e;
					}
					catch (RuntimeException e)
					{
						throw e;
					}
					catch (Exception e)
					{
						throw new WSDLException(WSDLException.OTHER_ERROR,
								"Unable to resolve imported document at '" +
										locationURI +
										(contextURI == null
												? "'." : "', relative to '" + contextURI + "'")
								, e);
					}
				} //end importDocs
			} //end locationURI

		}
		catch (WSDLException e)
		{
			if (e.getLocation() == null)
			{
				e.setLocation(XPathUtils.getXPathExprFromNode(importEl));
			}
			else
			{
				//If definitions are being parsed recursively for nested imports
				//the exception location must be built up recursively too so
				//prepend this element's xpath to exception location.
				String loc = XPathUtils.getXPathExprFromNode(importEl) + e.getLocation();
				e.setLocation(loc);
			}

			throw e;
		}

		//register any NS decls with the Definition
		NamedNodeMap attrs = importEl.getAttributes();
		registerNSDeclarations(attrs, def);

		Element tempEl = DOMUtils.getFirstChildElement(importEl);

		while (tempEl != null)
		{
			if (QNameUtils.matches(Constants.Q_ELEM_DOCUMENTATION, tempEl))
			{
				importDef.setDocumentationElement(tempEl);
			}
			else
			{
				importDef.addExtensibilityElement(
						parseExtensibilityElement(Import.class, tempEl, def));
			}

			tempEl = DOMUtils.getNextSiblingElement(tempEl);
		}

		parseExtensibilityAttributes(importEl, Import.class, importDef, def);

		return importDef;

	}

	protected Types parseTypes(Element typesEl, Definition def)
			throws WSDLException
	{
		//register any NS decls with the Definition
		NamedNodeMap attrs = typesEl.getAttributes();
		registerNSDeclarations(attrs, def);

		Types types = def.createTypes();
		Element tempEl = DOMUtils.getFirstChildElement(typesEl);
		QName tempElType;

		while (tempEl != null)
		{
			tempElType = QNameUtils.newQName(tempEl);

			if (QNameUtils.matches(Constants.Q_ELEM_DOCUMENTATION, tempEl))
			{
				types.setDocumentationElement(tempEl);
			}
			else if ((SchemaConstants.XSD_QNAME_LIST).contains(tempElType))
			{
				//the element qname indicates it is a schema.
				types.addExtensibilityElement(
						parseSchema(Types.class, tempEl, def));
			}
			else
			{
				types.addExtensibilityElement(
						parseExtensibilityElement(Types.class, tempEl, def));
			}

			tempEl = DOMUtils.getNextSiblingElement(tempEl);
		}

		parseExtensibilityAttributes(typesEl, Types.class, types, def);

		return types;
	}

	protected ExtensibilityElement parseSchema( Class parentType,
															  Element el,
															  Definition def)
			throws WSDLException
	{
		QName elementType = null;
		ExtensionRegistry extReg = null;

		try
		{
			extReg = def.getExtensionRegistry();

			if (extReg == null)
			{
				throw new WSDLException(WSDLException.CONFIGURATION_ERROR,
						"No ExtensionRegistry set for this " +
								"Definition, so unable to deserialize " +
								"a '" + elementType + "' element in the " +
								"context of a '" + parentType.getName() +
								"'.");
			}

			return parseSchema(parentType, el, def, extReg);
		}
		catch (WSDLException e)
		{
			if (e.getLocation() == null)
			{
				e.setLocation(XPathUtils.getXPathExprFromNode(el));
			}

			throw e;
		}
	}


	protected ExtensibilityElement parseSchema( Class parentType,
															  Element el,
															  Definition def,
															  ExtensionRegistry extReg)
			throws WSDLException
	{
		/*
			  * This method returns ExtensibilityElement rather than Schema because we
			  * do not insist that a suitable XSD schema deserializer is registered.
			  * PopulatedExtensionRegistry registers SchemaDeserializer by default, but
			  * if the user chooses not to register a suitable deserializer then the
			  * UnknownDeserializer will be used, returning an UnknownExtensibilityElement.
			  */

		Schema schema = null;
		SchemaReference schemaRef = null;
		try
		{

			QName elementType = QNameUtils.newQName(el);

			ExtensionDeserializer exDS =
					extReg.queryDeserializer(parentType, elementType);

			//Now unmarshall the DOM element.
			ExtensibilityElement ee =
					exDS.unmarshall(parentType, elementType, el, def, extReg);

			if (ee instanceof Schema)
			{
				schema = (Schema) ee;
			}
			else
			{
				//Unknown extensibility element, so don't do any more schema parsing on it.
				return ee;
			}


			//Keep track of parsed schemas to avoid duplicating Schema objects
			//through duplicate or circular references (eg: A imports B imports A).
			if (schema.getDocumentBaseURI() != null)
			{
				this.allSchemas.put(schema.getDocumentBaseURI(), schema);
			}

			//At this point, any SchemaReference objects held by the schema will not
			//yet point to their referenced schemas, so we must now retrieve these
			//schemas and set the schema references.

			//First, combine the schema references for imports, includes and redefines
			//into a single list

			ArrayList allSchemaRefs = new ArrayList();

			Collection ic = schema.getImports().values();
			Iterator importsIterator = ic.iterator();
			while(importsIterator.hasNext())
			{
				allSchemaRefs.addAll( (Collection) importsIterator.next() );
			}

			allSchemaRefs.addAll(schema.getIncludes());
			allSchemaRefs.addAll(schema.getRedefines());

			//Then, retrieve the schema referred to by each schema reference. If the
			//schema has been read in previously, use the existing schema object.
			//Otherwise unmarshall the DOM element into a new schema object.

			ListIterator schemaRefIterator = allSchemaRefs.listIterator();

			while(schemaRefIterator.hasNext())
			{
				try
				{
					schemaRef = (SchemaReference) schemaRefIterator.next();

					if (schemaRef.getSchemaLocationURI() == null)
					{
						//cannot get the referenced schema, so ignore this schema reference
						continue;
					}

					if (verbose)
					{
						System.out.println("Retrieving schema at '" +
								schemaRef.getSchemaLocationURI() +
								(schema.getDocumentBaseURI() == null
										? "'."
										: "', relative to '" +
										schema.getDocumentBaseURI() + "'."));
					}


					InputStream inputStream = null;
					InputSource inputSource = null;

					//This is the child schema referred to by the schemaReference
					Schema referencedSchema = null;

					//This is the child schema's location obtained from the WSDLLocator or the URL
					String location = null;

					if (loc != null)
					{
						//Try to get the referenced schema using the wsdl locator
						inputSource = loc.getImportInputSource(
								schema.getDocumentBaseURI(), schemaRef.getSchemaLocationURI());

						if (inputSource == null)
						{
							throw new WSDLException(WSDLException.OTHER_ERROR,
									"Unable to locate with a locator "
											+ "the schema referenced at '"
											+ schemaRef.getSchemaLocationURI()
											+ "' relative to document base '"
											+ schema.getDocumentBaseURI() + "'");
						}
						location = loc.getLatestImportURI();

						//if a schema from this location has been read previously, use it.
						referencedSchema = (Schema) this.allSchemas.get(location);
					}
					else
					{
						// We don't have a wsdl locator, so try to retrieve the schema by its URL
						String contextURI = schema.getDocumentBaseURI();
						URL contextURL = (contextURI != null) ? StringUtils.getURL(null, contextURI) : null;
						URL url = StringUtils.getURL(contextURL, schemaRef.getSchemaLocationURI());
						location = url.toExternalForm();

						//if a schema from this location has been retrieved previously, use it.
						referencedSchema = (Schema) this.allSchemas.get(location);

						if (referencedSchema == null)
						{
							// We haven't read this schema in before so do it now
							inputStream = StringUtils.getContentAsInputStream(url);

							if (inputStream != null)
							{
								inputSource = new InputSource(inputStream);
							}

							if (inputSource == null)
							{
								throw new WSDLException(WSDLException.OTHER_ERROR,
										"Unable to locate with a url "
												+ "the document referenced at '"
												+ schemaRef.getSchemaLocationURI()
												+ "'"
												+ (contextURI == null ? "." : ", relative to '"
												+ contextURI + "'."));
							}
						}

					} //end if loc

					// If we have not previously read the schema, get its DOM element now.
					if (referencedSchema == null)
					{
						inputSource.setSystemId(location);
						Document doc = getDocument(inputSource, location);

						if (inputStream != null)
						{
							inputStream.close();
						}

						Element documentElement = doc.getDocumentElement();

						// If it's a schema doc process it, otherwise the schema reference remains null

						QName docElementQName = QNameUtils.newQName(documentElement);

						if (SchemaConstants.XSD_QNAME_LIST.contains(docElementQName))
						{
							//We now need to call parseSchema recursively to parse the referenced
							//schema. The document base URI of the referenced schema will be set to
							//the document base URI of the current schema plus the schemaLocation in
							//the schemaRef. We cannot explicitly pass in a new document base URI
							//to the schema deserializer, so instead we will create a dummy
							//Definition and set its documentBaseURI to the new document base URI.
							//We can leave the other definition fields empty because we know
							//that the SchemaDeserializer.unmarshall method uses the definition
							//parameter only to get its documentBaseURI. If the unmarshall method
							//implementation changes (ie: its use of definition changes) we may need
							//to rethink this approach.

							WSDLFactory factory = getWSDLFactory();
							Definition dummyDef = factory.newDefinition();

							dummyDef.setDocumentBaseURI(location);

							//By this point, we know we have a SchemaDeserializer registered
							//so we can safely cast the ExtensibilityElement to a Schema.
							referencedSchema = (Schema) parseSchema( parentType,
									documentElement,
									dummyDef,
									extReg);
						}

					} //end if referencedSchema

					schemaRef.setReferencedSchema(referencedSchema);
				}
				catch (WSDLException e)
				{
					throw e;
				}
				catch (RuntimeException e)
				{
					throw e;
				}
				catch (Exception e)
				{
					throw new WSDLException(WSDLException.OTHER_ERROR,
							"An error occurred trying to resolve schema referenced at '"
									+ schemaRef.getSchemaLocationURI()
									+ "'"
									+ (schema.getDocumentBaseURI() == null ? "." : ", relative to '"
									+ schema.getDocumentBaseURI() + "'."),
							e);
				}

			} //end while loop

			return schema;

		}
		catch (WSDLException e)
		{
			if (e.getLocation() == null)
			{
				e.setLocation(XPathUtils.getXPathExprFromNode(el));
			}
			else
			{
				//If this method has been called recursively for nested schemas
				//the exception location must be built up recursively too so
				//prepend this element's xpath to exception location.
				String loc = XPathUtils.getXPathExprFromNode(el) + e.getLocation();
				e.setLocation(loc);
			}

			throw e;
		}

	}


	protected Binding parseBinding(Element bindingEl, Definition def)
			throws WSDLException
	{
		Binding binding = null;

		List remainingAttrs = DOMUtils.getAttributes(bindingEl);
		String name = DOMUtils.getAttribute(bindingEl, Constants.ATTR_NAME, remainingAttrs);
		QName portTypeName = getQualifiedAttributeValue(bindingEl,
				Constants.ATTR_TYPE,
				Constants.ELEM_BINDING,
				def,
				remainingAttrs);

		PortType portType = null;

		if (name != null)
		{
			QName bindingName = new QName(def.getTargetNamespace(), name);

			binding = def.getBinding(bindingName);

			if (binding == null)
			{
				binding = def.createBinding();
				binding.setQName(bindingName);
			}
		}
		else
		{
			binding = def.createBinding();
		}

		// Whether it was retrieved or created, the definition has been found.
		binding.setUndefined(false);

		if (portTypeName != null)
		{
			portType = def.getPortType(portTypeName);

			if (portType == null)
			{
				portType = def.createPortType();
				portType.setQName(portTypeName);
				def.addPortType(portType);
			}

			binding.setPortType(portType);
		}

		//register any NS decls with the Definition
		NamedNodeMap attrs = bindingEl.getAttributes();
		registerNSDeclarations(attrs, def);

		Element tempEl = DOMUtils.getFirstChildElement(bindingEl);

		while (tempEl != null)
		{
			if (QNameUtils.matches(Constants.Q_ELEM_DOCUMENTATION, tempEl))
			{
				binding.setDocumentationElement(tempEl);
			}
			else if (QNameUtils.matches(Constants.Q_ELEM_OPERATION, tempEl))
			{
				binding.addBindingOperation(parseBindingOperation(tempEl,
						portType,
						def));
			}
			else
			{
				binding.addExtensibilityElement(parseExtensibilityElement(
						Binding.class, tempEl, def));
			}

			tempEl = DOMUtils.getNextSiblingElement(tempEl);
		}

		return binding;
	}

	protected BindingOperation parseBindingOperation(
			Element bindingOperationEl,
			PortType portType,
			Definition def)
			throws WSDLException
	{
		BindingOperation bindingOperation = def.createBindingOperation();

		List remainingAttrs = DOMUtils.getAttributes(bindingOperationEl);
		String name = DOMUtils.getAttribute(bindingOperationEl,
				Constants.ATTR_NAME,
				remainingAttrs);

		if (name != null)
		{
			bindingOperation.setName(name);
		}

		//register any NS decls with the Definition
		NamedNodeMap attrs = bindingOperationEl.getAttributes();
		registerNSDeclarations(attrs, def);

		Element tempEl = DOMUtils.getFirstChildElement(bindingOperationEl);

		while (tempEl != null)
		{
			if (QNameUtils.matches(Constants.Q_ELEM_DOCUMENTATION, tempEl))
			{
				bindingOperation.setDocumentationElement(tempEl);
			}
			else if (QNameUtils.matches(Constants.Q_ELEM_INPUT, tempEl))
			{
				bindingOperation.setBindingInput(parseBindingInput(tempEl, def));
			}
			else if (QNameUtils.matches(Constants.Q_ELEM_OUTPUT, tempEl))
			{
				bindingOperation.setBindingOutput(parseBindingOutput(tempEl, def));
			}
			else if (QNameUtils.matches(Constants.Q_ELEM_FAULT, tempEl))
			{
				bindingOperation.addBindingFault(parseBindingFault(tempEl, def));
			}
			else
			{
				bindingOperation.addExtensibilityElement(
						parseExtensibilityElement(BindingOperation.class, tempEl, def));
			}

			tempEl = DOMUtils.getNextSiblingElement(tempEl);
		}

		if (portType != null)
		{
			BindingInput bindingInput = bindingOperation.getBindingInput();
			BindingOutput bindingOutput = bindingOperation.getBindingOutput();
			String inputName = (bindingInput != null
					? (bindingInput.getName() != null ? bindingInput.getName() : Constants.NONE)
					: null);
			String outputName = (bindingOutput != null
					? (bindingOutput.getName() != null ? bindingOutput.getName() : Constants.NONE)
					: null);
			Operation op = portType.getOperation(name, inputName, outputName);

			/*
					 * If the bindingOp input or output message names are null we will search first
					 * for a porttypeOp with corresponding unnamed input or output messages (using
					 * Constants.NONE for inputName or outputName, as above).
					 * However, input and output message names need not be used at all if operation
					 * overloading is not used, so if no match was found we will try again ignoring
					 * these unnamed messages from the search criteria (i.e. using null instead of
					 * Constants.NONE for inputName or outputName).
					 */

			if(op == null)
			{
				if(Constants.NONE.equals(inputName) && Constants.NONE.equals(outputName))
				{
					//There was no porttype op with unnamed input and output messages,
					//so ignore input and output name and search on the op name only.
					op = portType.getOperation(name, null, null);
				}
				else if(Constants.NONE.equals(inputName))
				{
					//There was no porttype op with an unnamed input message,
					//so ignore input name and search on the op name and output name only.
					op = portType.getOperation(name, null, outputName);
				}
				else if(Constants.NONE.equals(outputName))
				{
					//There was no porttype op with an unnamed output message,
					//so ignore output name and search on the op name and input name only.
					op = portType.getOperation(name, inputName, null);
				}
			}

			if (op == null)
			{
				Input input = def.createInput();
				Output output = def.createOutput();

				op = def.createOperation();
				op.setName(name);
				input.setName(inputName);
				output.setName(outputName);
				op.setInput(input);
				op.setOutput(output);
				portType.addOperation(op);
			}

			bindingOperation.setOperation(op);
		}

		parseExtensibilityAttributes(bindingOperationEl, BindingOperation.class, bindingOperation, def);

		return bindingOperation;
	}

	protected BindingInput parseBindingInput(Element bindingInputEl,
														  Definition def)
			throws WSDLException
	{
		BindingInput bindingInput = def.createBindingInput();

		List remainingAttrs = DOMUtils.getAttributes(bindingInputEl);
		String name = DOMUtils.getAttribute(bindingInputEl,
				Constants.ATTR_NAME,
				remainingAttrs);

		if (name != null)
		{
			bindingInput.setName(name);
		}

		//register any NS decls with the Definition
		NamedNodeMap attrs = bindingInputEl.getAttributes();
		registerNSDeclarations(attrs, def);

		Element tempEl = DOMUtils.getFirstChildElement(bindingInputEl);

		while (tempEl != null)
		{
			if (QNameUtils.matches(Constants.Q_ELEM_DOCUMENTATION, tempEl))
			{
				bindingInput.setDocumentationElement(tempEl);
			}
			else
			{
				bindingInput.addExtensibilityElement(
						parseExtensibilityElement(BindingInput.class, tempEl, def));
			}

			tempEl = DOMUtils.getNextSiblingElement(tempEl);
		}

		return bindingInput;
	}

	protected BindingOutput parseBindingOutput(Element bindingOutputEl,
															 Definition def)
			throws WSDLException
	{
		BindingOutput bindingOutput = def.createBindingOutput();

		List remainingAttrs = DOMUtils.getAttributes(bindingOutputEl);
		String name = DOMUtils.getAttribute(bindingOutputEl,
				Constants.ATTR_NAME,
				remainingAttrs);

		if (name != null)
		{
			bindingOutput.setName(name);
		}

		//register any NS decls with the Definition
		NamedNodeMap attrs = bindingOutputEl.getAttributes();
		registerNSDeclarations(attrs, def);

		Element tempEl = DOMUtils.getFirstChildElement(bindingOutputEl);

		while (tempEl != null)
		{
			if (QNameUtils.matches(Constants.Q_ELEM_DOCUMENTATION, tempEl))
			{
				bindingOutput.setDocumentationElement(tempEl);
			}
			else
			{
				bindingOutput.addExtensibilityElement(
						parseExtensibilityElement(BindingOutput.class, tempEl, def));
			}

			tempEl = DOMUtils.getNextSiblingElement(tempEl);
		}

		return bindingOutput;
	}

	protected BindingFault parseBindingFault(Element bindingFaultEl,
														  Definition def)
			throws WSDLException
	{
		BindingFault bindingFault = def.createBindingFault();

		List remainingAttrs = DOMUtils.getAttributes(bindingFaultEl);
		String name = DOMUtils.getAttribute(bindingFaultEl,
				Constants.ATTR_NAME,
				remainingAttrs);

		if (name != null)
		{
			bindingFault.setName(name);
		}

		//register any NS decls with the Definition
		NamedNodeMap attrs = bindingFaultEl.getAttributes();
		registerNSDeclarations(attrs, def);

		Element tempEl = DOMUtils.getFirstChildElement(bindingFaultEl);

		while (tempEl != null)
		{
			if (QNameUtils.matches(Constants.Q_ELEM_DOCUMENTATION, tempEl))
			{
				bindingFault.setDocumentationElement(tempEl);
			}
			else
			{
				bindingFault.addExtensibilityElement(
						parseExtensibilityElement(BindingFault.class, tempEl, def));
			}

			tempEl = DOMUtils.getNextSiblingElement(tempEl);
		}

		parseExtensibilityAttributes(bindingFaultEl, BindingFault.class, bindingFault, def);

		return bindingFault;
	}

	protected Message parseMessage(Element msgEl, Definition def)
			throws WSDLException
	{
		Message msg = null;

		List remainingAttrs = DOMUtils.getAttributes(msgEl);
		String name = DOMUtils.getAttribute(msgEl, Constants.ATTR_NAME, remainingAttrs);

		if (name != null)
		{
			QName messageName = new QName(def.getTargetNamespace(), name);

			msg = def.getMessage(messageName);

			if (msg == null)
			{
				msg = def.createMessage();
				msg.setQName(messageName);
			}
		}
		else
		{
			msg = def.createMessage();
		}

		// Whether it was retrieved or created, the definition has been found.
		msg.setUndefined(false);

		//register any NS decls with the Definition
		NamedNodeMap attrs = msgEl.getAttributes();
		registerNSDeclarations(attrs, def);

		Element tempEl = DOMUtils.getFirstChildElement(msgEl);

		while (tempEl != null)
		{
			if (QNameUtils.matches(Constants.Q_ELEM_DOCUMENTATION, tempEl))
			{
				msg.setDocumentationElement(tempEl);
			}
			else if (QNameUtils.matches(Constants.Q_ELEM_PART, tempEl))
			{
				msg.addPart(parsePart(tempEl, def));
			}
			else
			{
				msg.addExtensibilityElement(
						parseExtensibilityElement(Message.class, tempEl, def));
			}

			tempEl = DOMUtils.getNextSiblingElement(tempEl);
		}

		parseExtensibilityAttributes(msgEl, Message.class, msg, def);

		return msg;
	}

	protected Part parsePart(Element partEl, Definition def)
			throws WSDLException
	{
		Part part = def.createPart();
		String name = DOMUtils.getAttribute(partEl, Constants.ATTR_NAME);
		QName elementName = getQualifiedAttributeValue(partEl,
				Constants.ATTR_ELEMENT,
				Constants.ELEM_MESSAGE,
				def);
		QName typeName = getQualifiedAttributeValue(partEl,
				Constants.ATTR_TYPE,
				Constants.ELEM_MESSAGE,
				def);

		if (name != null)
		{
			part.setName(name);
		}

		if (elementName != null)
		{
			part.setElementName(elementName);
		}

		if (typeName != null)
		{
			part.setTypeName(typeName);
		}

		//register any NS decls with the Definition
		NamedNodeMap attrs = partEl.getAttributes();
		registerNSDeclarations(attrs, def);

		Element tempEl = DOMUtils.getFirstChildElement(partEl);

		while (tempEl != null)
		{
			if (QNameUtils.matches(Constants.Q_ELEM_DOCUMENTATION, tempEl))
			{
				part.setDocumentationElement(tempEl);
			}
			else
			{
				part.addExtensibilityElement(
						parseExtensibilityElement(Part.class, tempEl, def));
			}

			tempEl = DOMUtils.getNextSiblingElement(tempEl);
		}

		parseExtensibilityAttributes(partEl, Part.class, part, def);

		return part;
	}

	protected void parseExtensibilityAttributes(Element el,
															  Class parentType,
															  AttributeExtensible attrExt,
															  Definition def)
			throws WSDLException
	{
		List nativeAttributeNames = attrExt.getNativeAttributeNames();
		NamedNodeMap nodeMap = el.getAttributes();
		int length = nodeMap.getLength();

		for (int i = 0; i < length; i++)
		{
			Attr attribute = (Attr)nodeMap.item(i);
			String localName = attribute.getLocalName();
			String namespaceURI = attribute.getNamespaceURI();
			String prefix = attribute.getPrefix();
			QName qname = new QName(namespaceURI, localName);

			if (namespaceURI != null && !namespaceURI.equals(Constants.NS_URI_WSDL))
			{
				if (!namespaceURI.equals(Constants.NS_URI_XMLNS))
				{
					DOMUtils.registerUniquePrefix(prefix, namespaceURI, def);

					String strValue = attribute.getValue();
					int attrType = AttributeExtensible.NO_DECLARED_TYPE;
					ExtensionRegistry extReg = def.getExtensionRegistry();

					if (extReg != null)
					{
						attrType = extReg.queryExtensionAttributeType(parentType, qname);
					}

					Object val = parseExtensibilityAttribute(el, attrType, strValue, def);

					attrExt.setExtensionAttribute(qname, val);
				}
			}
			else if (!nativeAttributeNames.contains(localName))
			{
				WSDLException wsdlExc = new WSDLException(WSDLException.INVALID_WSDL,
						"Encountered illegal " +
								"extension attribute '" +
								qname + "'. Extension " +
								"attributes must be in " +
								"a namespace other than " +
								"WSDL's.");

				wsdlExc.setLocation(XPathUtils.getXPathExprFromNode(el));

				throw wsdlExc;
			}
		}
	}

	protected Object parseExtensibilityAttribute(Element el,
																int attrType,
																String attrValue,
																Definition def)
			throws WSDLException
	{
		if (attrType == AttributeExtensible.QNAME_TYPE)
		{
			return DOMUtils.getQName(attrValue, el, def);
		}
		else if (attrType == AttributeExtensible.LIST_OF_STRINGS_TYPE)
		{
			return StringUtils.parseNMTokens(attrValue);
		}
		else if (attrType == AttributeExtensible.LIST_OF_QNAMES_TYPE)
		{
			List oldList = StringUtils.parseNMTokens(attrValue);
			int size = oldList.size();
			List newList = new Vector(size);

			for (int i = 0; i < size; i++)
			{
				String str = (String)oldList.get(i);
				QName qValue = DOMUtils.getQName(str, el, def);

				newList.add(qValue);
			}

			return newList;
		}
		else if (attrType == AttributeExtensible.STRING_TYPE)
		{
			return attrValue;
		}
		else
		{
			QName qValue = null;

			try
			{
				qValue = DOMUtils.getQName(attrValue, el, def);
			}
			catch (WSDLException e)
			{
				qValue = new QName(attrValue);
			}

			return qValue;
		}
	}

	protected PortType parsePortType(Element portTypeEl, Definition def)
			throws WSDLException
	{
		PortType portType = null;
		String name = DOMUtils.getAttribute(portTypeEl, Constants.ATTR_NAME);

		if (name != null)
		{
			QName portTypeName = new QName(def.getTargetNamespace(), name);

			portType = def.getPortType(portTypeName);

			if (portType == null)
			{
				portType = def.createPortType();
				portType.setQName(portTypeName);
			}
		}
		else
		{
			portType = def.createPortType();
		}

		// Whether it was retrieved or created, the definition has been found.
		portType.setUndefined(false);

		//register any NS decls with the Definition
		NamedNodeMap attrs = portTypeEl.getAttributes();
		registerNSDeclarations(attrs, def);

		Element tempEl = DOMUtils.getFirstChildElement(portTypeEl);

		while (tempEl != null)
		{
			if (QNameUtils.matches(Constants.Q_ELEM_DOCUMENTATION, tempEl))
			{
				portType.setDocumentationElement(tempEl);
			}
			else if (QNameUtils.matches(Constants.Q_ELEM_OPERATION, tempEl))
			{
				Operation op = parseOperation(tempEl, portType, def);

				if (op != null)
				{
					portType.addOperation(op);
				}
			}
			else
			{
				portType.addExtensibilityElement(
						parseExtensibilityElement(PortType.class, tempEl, def));
			}

			tempEl = DOMUtils.getNextSiblingElement(tempEl);
		}

		parseExtensibilityAttributes(portTypeEl, PortType.class, portType, def);

		return portType;
	}

	protected Operation parseOperation(Element opEl,
												  PortType portType,
												  Definition def)
			throws WSDLException
	{
		Operation op = null;

		List remainingAttrs = DOMUtils.getAttributes(opEl);
		String name = DOMUtils.getAttribute(opEl, Constants.ATTR_NAME, remainingAttrs);
		String parameterOrderStr = DOMUtils.getAttribute(opEl,
				Constants.ATTR_PARAMETER_ORDER,
				remainingAttrs);

		//register any NS decls with the Definition
		NamedNodeMap attrs = opEl.getAttributes();
		registerNSDeclarations(attrs, def);

		Element tempEl = DOMUtils.getFirstChildElement(opEl);
		List messageOrder = new Vector();
		Element docEl = null;
		Input input = null;
		Output output = null;
		List faults = new Vector();
		List extElements = new Vector();
		boolean retrieved = true;

		while (tempEl != null)
		{
			if (QNameUtils.matches(Constants.Q_ELEM_DOCUMENTATION, tempEl))
			{
				docEl = tempEl;
			}
			else if (QNameUtils.matches(Constants.Q_ELEM_INPUT, tempEl))
			{
				input = parseInput(tempEl, def);
				messageOrder.add(Constants.ELEM_INPUT);
			}
			else if (QNameUtils.matches(Constants.Q_ELEM_OUTPUT, tempEl))
			{
				output = parseOutput(tempEl, def);
				messageOrder.add(Constants.ELEM_OUTPUT);
			}
			else if (QNameUtils.matches(Constants.Q_ELEM_FAULT, tempEl))
			{
				faults.add(parseFault(tempEl, def));
			}
			else
			{
				extElements.add(
						parseExtensibilityElement(Operation.class, tempEl, def));
			}

			tempEl = DOMUtils.getNextSiblingElement(tempEl);
		}

		if (name != null)
		{
			String inputName = (input != null
					? (input.getName() != null ? input.getName() : Constants.NONE)
					: null);
			String outputName = (output != null
					? (output.getName() != null ? output.getName() : Constants.NONE)
					: null);

			op = portType.getOperation(name, inputName, outputName);

			if (op != null && !op.isUndefined())
			{
				op = null;
			}

			if (op != null)
			{
				if (inputName == null)
				{
					Input tempIn = op.getInput();

					if (tempIn != null)
					{
						if (tempIn.getName() != null)
						{
							op = null;
						}
					}
				}
			}

			if (op != null)
			{
				if (outputName == null)
				{
					Output tempOut = op.getOutput();

					if (tempOut != null)
					{
						if (tempOut.getName() != null)
						{
							op = null;
						}
					}
				}
			}

			if (op == null)
			{
				op = def.createOperation();
				op.setName(name);
				retrieved = false;
			}
		}
		else
		{
			op = def.createOperation();
			retrieved = false;
		}

		// Whether it was retrieved or created, the definition has been found.
		op.setUndefined(false);

		if (parameterOrderStr != null)
		{
			op.setParameterOrdering(StringUtils.parseNMTokens(parameterOrderStr));
		}

		if (docEl != null)
		{
			op.setDocumentationElement(docEl);
		}

		if (input != null)
		{
			op.setInput(input);
		}

		if (output != null)
		{
			op.setOutput(output);
		}

		if (faults.size() > 0)
		{
			Iterator faultIterator = faults.iterator();

			while (faultIterator.hasNext())
			{
				op.addFault((Fault)faultIterator.next());
			}
		}

		if (extElements.size() > 0)
		{
			Iterator eeIterator = extElements.iterator();

			while (eeIterator.hasNext())
			{
				op.addExtensibilityElement(
						(ExtensibilityElement) eeIterator.next() );
			}
		}

		OperationType style = null;

		if (messageOrder.equals(STYLE_ONE_WAY))
		{
			style = OperationType.ONE_WAY;
		}
		else if (messageOrder.equals(STYLE_REQUEST_RESPONSE))
		{
			style = OperationType.REQUEST_RESPONSE;
		}
		else if (messageOrder.equals(STYLE_SOLICIT_RESPONSE))
		{
			style = OperationType.SOLICIT_RESPONSE;
		}
		else if (messageOrder.equals(STYLE_NOTIFICATION))
		{
			style = OperationType.NOTIFICATION;
		}

		if (style != null)
		{
			op.setStyle(style);
		}

		if (retrieved)
		{
			op = null;
		}

		parseExtensibilityAttributes(opEl, Operation.class, op, def);

		return op;
	}

	protected Service parseService(Element serviceEl, Definition def)
			throws WSDLException
	{
		Service service = def.createService();

		List remainingAttrs = DOMUtils.getAttributes(serviceEl);
		String name = DOMUtils.getAttribute(serviceEl, Constants.ATTR_NAME, remainingAttrs);

		if (name != null)
		{
			service.setQName(new QName(def.getTargetNamespace(), name));
		}

		//register any NS decls with the Definition
		NamedNodeMap attrs = serviceEl.getAttributes();
		registerNSDeclarations(attrs, def);

		Element tempEl = DOMUtils.getFirstChildElement(serviceEl);

		while (tempEl != null)
		{
			if (QNameUtils.matches(Constants.Q_ELEM_DOCUMENTATION, tempEl))
			{
				service.setDocumentationElement(tempEl);
			}
			else if (QNameUtils.matches(Constants.Q_ELEM_PORT, tempEl))
			{
				service.addPort(parsePort(tempEl, def));
			}
			else
			{
				service.addExtensibilityElement(
						parseExtensibilityElement(Service.class, tempEl, def));
			}

			tempEl = DOMUtils.getNextSiblingElement(tempEl);
		}

		parseExtensibilityAttributes(serviceEl, Service.class, service, def);

		return service;
	}

	protected Port parsePort(Element portEl, Definition def)
			throws WSDLException
	{
		Port port = def.createPort();

		List remainingAttrs = DOMUtils.getAttributes(portEl);
		String name = DOMUtils.getAttribute(portEl, Constants.ATTR_NAME, remainingAttrs);
		QName bindingStr = getQualifiedAttributeValue(portEl,
				Constants.ATTR_BINDING,
				Constants.ELEM_PORT,
				def,
				remainingAttrs);

		if (name != null)
		{
			port.setName(name);
		}

		if (bindingStr != null)
		{
			Binding binding = def.getBinding(bindingStr);

			if (binding == null)
			{
				binding = def.createBinding();
				binding.setQName(bindingStr);
				def.addBinding(binding);
			}

			port.setBinding(binding);
		}

		//register any NS decls with the Definition
		NamedNodeMap attrs = portEl.getAttributes();
		registerNSDeclarations(attrs, def);

		Element tempEl = DOMUtils.getFirstChildElement(portEl);

		while (tempEl != null)
		{
			if (QNameUtils.matches(Constants.Q_ELEM_DOCUMENTATION, tempEl))
			{
				port.setDocumentationElement(tempEl);
			}
			else
			{
				port.addExtensibilityElement(parseExtensibilityElement(Port.class,
						tempEl,
						def));
			}

			tempEl = DOMUtils.getNextSiblingElement(tempEl);
		}

		parseExtensibilityAttributes(portEl, Port.class, port, def);

		return port;
	}

	protected ExtensibilityElement parseExtensibilityElement(
			Class parentType,
			Element el,
			Definition def)
			throws WSDLException
	{
		QName elementType = QNameUtils.newQName(el);

		String namespaceURI = el.getNamespaceURI();

		try
		{
			if (namespaceURI == null || namespaceURI.equals(Constants.NS_URI_WSDL))
			{
				throw new WSDLException(WSDLException.INVALID_WSDL,
						"Encountered illegal extension element '" +
								elementType +
								"' in the context of a '" +
								parentType.getName() +
								"'. Extension elements must be in " +
								"a namespace other than WSDL's.");
			}

			ExtensionRegistry extReg = def.getExtensionRegistry();

			if (extReg == null)
			{
				throw new WSDLException(WSDLException.CONFIGURATION_ERROR,
						"No ExtensionRegistry set for this " +
								"Definition, so unable to deserialize " +
								"a '" + elementType + "' element in the " +
								"context of a '" + parentType.getName() +
								"'.");
			}

			ExtensionDeserializer extDS = extReg.queryDeserializer(parentType,
					elementType);

			return extDS.unmarshall(parentType, elementType, el, def, extReg);
		}
		catch (WSDLException e)
		{
			if (e.getLocation() == null)
			{
				e.setLocation(XPathUtils.getXPathExprFromNode(el));
			}

			throw e;
		}
	}

	protected Input parseInput(Element inputEl, Definition def)
			throws WSDLException
	{
		Input input = def.createInput();
		String name = DOMUtils.getAttribute(inputEl, Constants.ATTR_NAME);
		QName messageName = getQualifiedAttributeValue(inputEl,
				Constants.ATTR_MESSAGE,
				Constants.ELEM_INPUT,
				def);

		if (name != null)
		{
			input.setName(name);
		}

		if (messageName != null)
		{
			Message message = def.getMessage(messageName);

			if (message == null)
			{
				message = def.createMessage();
				message.setQName(messageName);
				def.addMessage(message);
			}

			input.setMessage(message);
		}

		//register any NS decls with the Definition
		NamedNodeMap attrs = inputEl.getAttributes();
		registerNSDeclarations(attrs, def);

		Element tempEl = DOMUtils.getFirstChildElement(inputEl);

		while (tempEl != null)
		{
			if (QNameUtils.matches(Constants.Q_ELEM_DOCUMENTATION, tempEl))
			{
				input.setDocumentationElement(tempEl);
			}
			else
			{
				input.addExtensibilityElement(
						parseExtensibilityElement(Input.class, tempEl, def));
			}

			tempEl = DOMUtils.getNextSiblingElement(tempEl);
		}

		parseExtensibilityAttributes(inputEl, Input.class, input, def);

		return input;
	}

	protected Output parseOutput(Element outputEl, Definition def)
			throws WSDLException
	{
		Output output = def.createOutput();
		String name = DOMUtils.getAttribute(outputEl, Constants.ATTR_NAME);
		QName messageName = getQualifiedAttributeValue(outputEl,
				Constants.ATTR_MESSAGE,
				Constants.ELEM_OUTPUT,
				def);

		if (name != null)
		{
			output.setName(name);
		}

		if (messageName != null)
		{
			Message message = def.getMessage(messageName);

			if (message == null)
			{
				message = def.createMessage();
				message.setQName(messageName);
				def.addMessage(message);
			}

			output.setMessage(message);
		}

		//register any NS decls with the Definition
		NamedNodeMap attrs = outputEl.getAttributes();
		registerNSDeclarations(attrs, def);

		Element tempEl = DOMUtils.getFirstChildElement(outputEl);

		while (tempEl != null)
		{
			if (QNameUtils.matches(Constants.Q_ELEM_DOCUMENTATION, tempEl))
			{
				output.setDocumentationElement(tempEl);
			}
			else
			{
				output.addExtensibilityElement(
						parseExtensibilityElement(Output.class, tempEl, def));
			}

			tempEl = DOMUtils.getNextSiblingElement(tempEl);
		}

		parseExtensibilityAttributes(outputEl, Output.class, output, def);

		return output;
	}

	protected Fault parseFault(Element faultEl, Definition def)
			throws WSDLException
	{
		Fault fault = def.createFault();
		String name = DOMUtils.getAttribute(faultEl, Constants.ATTR_NAME);
		QName messageName = getQualifiedAttributeValue(faultEl,
				Constants.ATTR_MESSAGE,
				Constants.ELEM_FAULT,
				def);

		if (name != null)
		{
			fault.setName(name);
		}

		if (messageName != null)
		{
			Message message = def.getMessage(messageName);

			if (message == null)
			{
				message = def.createMessage();
				message.setQName(messageName);
				def.addMessage(message);
			}

			fault.setMessage(message);
		}

		//register any NS decls with the Definition
		NamedNodeMap attrs = faultEl.getAttributes();
		registerNSDeclarations(attrs, def);

		Element tempEl = DOMUtils.getFirstChildElement(faultEl);

		while (tempEl != null)
		{
			if (QNameUtils.matches(Constants.Q_ELEM_DOCUMENTATION, tempEl))
			{
				fault.setDocumentationElement(tempEl);
			}
			else
			{
				fault.addExtensibilityElement(
						parseExtensibilityElement(Fault.class, tempEl, def));
			}

			tempEl = DOMUtils.getNextSiblingElement(tempEl);
		}

		parseExtensibilityAttributes(faultEl, Fault.class, fault, def);

		return fault;
	}

	/**
	 * This method should be used for elements that support extension
	 * attributes because it does not track unexpected remaining attributes.
	 */
	private static QName getQualifiedAttributeValue(Element el,
																	String attrName,
																	String elDesc,
																	Definition def)
			throws WSDLException
	{
		try
		{
			return DOMUtils.getQualifiedAttributeValue(el,
					attrName,
					elDesc,
					false,
					def);
		}
		catch (WSDLException e)
		{
			if (e.getFaultCode().equals(WSDLException.NO_PREFIX_SPECIFIED))
			{
				String attrValue = DOMUtils.getAttribute(el, attrName);

				return new QName(attrValue);
			}
			else
			{
				throw e;
			}
		}
	}

	/**
	 * This method should be used for elements that do not support extension
	 * attributes because it tracks unexpected remaining attributes.
	 */
	private static QName getQualifiedAttributeValue(Element el,
																	String attrName,
																	String elDesc,
																	Definition def,
																	List remainingAttrs)
			throws WSDLException
	{
		try
		{
			return DOMUtils.getQualifiedAttributeValue(el,
					attrName,
					elDesc,
					false,
					def,
					remainingAttrs);
		}
		catch (WSDLException e)
		{
			if (e.getFaultCode().equals(WSDLException.NO_PREFIX_SPECIFIED))
			{
				String attrValue = DOMUtils.getAttribute(el, attrName, remainingAttrs);

				return new QName(attrValue);
			}
			else
			{
				throw e;
			}
		}
	}

	private static void checkElementName(Element el, QName qname)
			throws WSDLException
	{
		if (!QNameUtils.matches(qname, el))
		{
			WSDLException wsdlExc = new WSDLException(WSDLException.INVALID_WSDL,
					"Expected element '" +
							qname + "'.");

			wsdlExc.setLocation(XPathUtils.getXPathExprFromNode(el));

			throw wsdlExc;
		}
	}

	private static Document getDocument(InputSource inputSource,
													String desc) throws WSDLException
	{
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

		factory.setNamespaceAware(true);
		factory.setValidating(false);

		try
		{
			DocumentBuilder builder = factory.newDocumentBuilder();
			builder.setEntityResolver( new JBossWSEntityResolver() );
			Document doc = builder.parse(inputSource);

			return doc;
		}
		catch (RuntimeException e)
		{
			throw e;
		}
		catch (Exception e)
		{
			throw new WSDLException(WSDLException.PARSER_ERROR,
					"Problem parsing '" + desc + "'.",
					e);
		}
	}

	private static void registerNSDeclarations(NamedNodeMap attrs, Definition def)
	{
		int size = attrs.getLength();

		for (int i = 0; i < size; i++)
		{
			Attr attr = (Attr)attrs.item(i);
			String namespaceURI = attr.getNamespaceURI();
			String localPart = attr.getLocalName();
			String value = attr.getValue();

			if (namespaceURI != null && namespaceURI.equals(Constants.NS_URI_XMLNS))
			{
				if (localPart != null && !localPart.equals(Constants.ATTR_XMLNS))
				{
					DOMUtils.registerUniquePrefix(localPart, value, def);
				}
				else
				{
					DOMUtils.registerUniquePrefix(null, value, def);
				}
			}
		}
	}

	/**
	 * Read the WSDL document accessible via the specified
	 * URI into a WSDL definition.
	 *
	 * @param wsdlURI a URI (can be a filename or URL) pointing to a
	 * WSDL XML definition.
	 * @return the definition.
	 */
	public Definition readWSDL(String wsdlURI) throws WSDLException
	{
		return readWSDL(null, wsdlURI);
	}

	/**
	 * Read the WSDL document accessible via the specified
	 * URI into a WSDL definition.
	 *
	 * @param contextURI the context in which to resolve the
	 * wsdlURI, if the wsdlURI is relative. Can be null, in which
	 * case it will be ignored.
	 * @param wsdlURI a URI (can be a filename or URL) pointing to a
	 * WSDL XML definition.
	 * @return the definition.
	 */
	public Definition readWSDL(String contextURI, String wsdlURI)
			throws WSDLException
	{
		try
		{
			if (verbose)
			{
				System.out.println("Retrieving document at '" + wsdlURI + "'" +
						(contextURI == null
								? "."
								: ", relative to '" + contextURI + "'."));
			}

			URL contextURL = (contextURI != null)
					? StringUtils.getURL(null, contextURI)
					: null;
			URL url = StringUtils.getURL(contextURL, wsdlURI);
			InputStream inputStream = StringUtils.getContentAsInputStream(url);
			InputSource inputSource = new InputSource(inputStream);
			inputSource.setSystemId(url.toString());
			Document doc = getDocument(inputSource, url.toString());

			inputStream.close();

			Definition def = readWSDL(url.toString(), doc);

			return def;
		}
		catch (WSDLException e)
		{
			throw e;
		}
		catch (RuntimeException e)
		{
			throw e;
		}
		catch (Exception e)
		{
			throw new WSDLException(WSDLException.OTHER_ERROR,
					"Unable to resolve imported document at '" +
							wsdlURI +
							(contextURI == null
									? "'."
									: "', relative to '" + contextURI + "'.")
					, e);
		}
	}

	/**
	 * Read the specified &lt;wsdl:definitions&gt; element into a WSDL
	 * definition.
	 *
	 * @param documentBaseURI the document base URI of the WSDL definition
	 * described by the element. Will be set as the documentBaseURI
	 * of the returned Definition. Can be null, in which case it
	 * will be ignored.
	 * @param definitionsElement the &lt;wsdl:definitions&gt; element
	 * @return the definition described by the element.
	 */
	public Definition readWSDL(String documentBaseURI,
										Element definitionsElement)
			throws WSDLException
	{
		return readWSDL(documentBaseURI, definitionsElement, null);
	}

	/**
	 * Read the specified &lt;wsdl:definitions&gt; element into a WSDL
	 * definition. The WSDLLocator is used to provide the document
	 * base URIs. The InputSource of the WSDLLocator is ignored, instead
	 * the WSDL is parsed from the given Element.
	 *
	 * @param locator A WSDLLocator object used to provide
	 * the document base URI of the WSDL definition described by the
	 * element.
	 * @param definitionsElement the &lt;wsdl:definitions&gt; element
	 * @return the definition described by the element.
	 */
	public Definition readWSDL(WSDLLocator locator,
										Element definitionsElement)
			throws WSDLException
	{
		try
		{
			this.loc = locator;
			return readWSDL(locator.getBaseURI(), definitionsElement, null);
		}
		finally
		{
			locator.close();
			this.loc = null;
		}
	}

	protected Definition readWSDL(String documentBaseURI,
											Element definitionsElement,
											Map importedDefs)
			throws WSDLException
	{
		return parseDefinitions(documentBaseURI, definitionsElement, importedDefs);
	}

	/**
	 * Read the specified WSDL document into a WSDL definition.
	 *
	 * @param documentBaseURI the document base URI of the WSDL definition
	 * described by the document. Will be set as the documentBaseURI
	 * of the returned Definition. Can be null, in which case it
	 * will be ignored.
	 * @param wsdlDocument the WSDL document, an XML
	 * document obeying the WSDL schema.
	 * @return the definition described in the document.
	 */
	public Definition readWSDL(String documentBaseURI, Document wsdlDocument)
			throws WSDLException
	{
		return readWSDL(documentBaseURI, wsdlDocument.getDocumentElement());
	}

	/**
	 * Read a WSDL document into a WSDL definition.
	 *
	 * @param documentBaseURI the document base URI of the WSDL definition
	 * described by the document. Will be set as the documentBaseURI
	 * of the returned Definition. Can be null, in which case it
	 * will be ignored.
	 * @param inputSource an InputSource pointing to the
	 * WSDL document, an XML document obeying the WSDL schema.
	 * @return the definition described in the document pointed to
	 * by the InputSource.
	 */
	public Definition readWSDL(String documentBaseURI, InputSource inputSource)
			throws WSDLException
	{
		String location = (inputSource.getSystemId() != null ?
				inputSource.getSystemId() : "- WSDL Document -");

		return readWSDL(documentBaseURI,
				getDocument(inputSource, location));
	}

	/**
	 * Read a WSDL document into a WSDL definition.
	 *
	 * @param locator A WSDLLocator object used to provide InputSources
	 * pointing to the wsdl file.
	 * @return the definition described in the document
	 */
	public Definition readWSDL(WSDLLocator locator) throws WSDLException
	{
		InputSource is = locator.getBaseInputSource();
		String base = locator.getBaseURI();

		if (is == null)
		{
			throw new WSDLException(WSDLException.OTHER_ERROR,
					"Unable to locate document at '" + base + "'.");
		}
		is.setSystemId(base);

		this.loc = locator;

		if (verbose)
		{
			System.out.println("Retrieving document at '" + base + "'.");
		}

		try
		{
			return readWSDL(base, is);
		}
		finally
		{
			this.loc.close();
			this.loc = null;
		}
	}
}