View Javadoc

1   /*
2    * Copyright 2006-2007 the original author or authors.
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.springframework.ws.wsdl.wsdl11;
18  
19  import java.io.IOException;
20  import javax.xml.transform.Source;
21  
22  import org.xml.sax.SAXException;
23  import org.xml.sax.XMLReader;
24  import org.xml.sax.helpers.XMLReaderFactory;
25  
26  import org.springframework.beans.factory.InitializingBean;
27  import org.springframework.core.io.Resource;
28  import org.springframework.util.Assert;
29  import org.springframework.ws.wsdl.WsdlDefinitionException;
30  import org.springframework.xml.transform.ResourceSource;
31  
32  /**
33   * The default [email protected] Wsdl11Definition} implementation.
34   * <p/>
35   * Allows a WSDL to be set by the [email protected] #setWsdl wsdl} property, or directly in the [email protected]
36   * #SimpleWsdl11Definition(Resource) constructor}.
37   *
38   * @author Arjen Poutsma
39   * @since 1.0.0
40   */
41  public class SimpleWsdl11Definition implements Wsdl11Definition, InitializingBean {
42  
43      private Resource wsdlResource;
44  
45      /**
46       * Create a new instance of the [email protected] SimpleWsdl11Definition} class.
47       * <p/>
48       * A subsequent call to the [email protected] #setWsdl(Resource)} method is required.
49       */
50      public SimpleWsdl11Definition() {
51      }
52  
53      /**
54       * Create a new instance of the  [email protected] SimpleWsdl11Definition} class with the specified resource.
55       *
56       * @param wsdlResource the WSDL resource; must not be <code>null</code>
57       * @throws IllegalArgumentException if the supplied <code>wsdlResource</code> is <code>null</code>
58       */
59      public SimpleWsdl11Definition(Resource wsdlResource) {
60          Assert.notNull(wsdlResource, "wsdlResource must not be null");
61          this.wsdlResource = wsdlResource;
62      }
63  
64      public void afterPropertiesSet() throws Exception {
65          Assert.notNull(this.wsdlResource, "wsdl is required");
66          Assert.isTrue(this.wsdlResource.exists(), "wsdl '" + this.wsdlResource + "' does not exist");
67      }
68  
69      public Source getSource() {
70          try {
71              XMLReader xmlReader = XMLReaderFactory.createXMLReader();
72              xmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
73              return new ResourceSource(xmlReader, wsdlResource);
74          }
75          catch (SAXException ex) {
76              throw new WsdlDefinitionException("Could not create XMLReader", ex);
77          }
78          catch (IOException ex) {
79              throw new WsdlDefinitionException("Could not create source from " + this.wsdlResource, ex);
80          }
81      }
82  
83      /**
84       * Set the WSDL resource to be exposed by calls to this instances' [email protected] #getSource()} method.
85       *
86       * @param wsdlResource the WSDL resource
87       */
88      public void setWsdl(Resource wsdlResource) {
89          this.wsdlResource = wsdlResource;
90      }
91  
92      public String toString() {
93          return "SimpleWsdl11Definition " + wsdlResource;
94      }
95  
96  
97  }