View Javadoc

1   /*
2    * Copyright 2001-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.geronimo.ews.ws4j2ee.parsers;
18  
19  import org.apache.geronimo.ews.ws4j2ee.context.webservices.client.ServiceReferanceImpl;
20  import org.apache.geronimo.ews.ws4j2ee.context.webservices.client.interfaces.ServiceReferanceContext;
21  import org.apache.geronimo.ews.ws4j2ee.toWs.GenerationFault;
22  import org.apache.geronimo.ews.ws4j2ee.utils.Utils;
23  import org.w3c.dom.Document;
24  import org.w3c.dom.Element;
25  import org.w3c.dom.Node;
26  import org.w3c.dom.NodeList;
27  
28  import java.io.InputStream;
29  
30  /***
31   * <service-ref>
32   * <service-ref-name>service/Joe</service-ref-name>
33   * <service-interface>javax.xml.rpc.Service</service-interface>
34   * <wsdl-file>WEB-INF/joe.wsdl</wsdl-file>
35   * <jaxrpc-mapping-file>WEB-INF/joe.xml</jaxrpc-mapping-file>
36   * <service-qname></service-qname>
37   * <port-component-ref>
38   * <service-endpoint-interface>sample.Joe</service-endpoint-interface>
39   * <port-component-link>JoePort</port-component-link>
40   * </port-component-ref>
41   * <handler>
42   * <handler-name></handler-name>
43   * <handler-class></handler-class>
44   * </handler>
45   * </service-ref>
46   *
47   * @author Srinath Perera(hemapani@opensource.lk)
48   */
49  public class ServiceReferanceParser {
50      private ServiceReferanceContext ref;
51  
52      public ServiceReferanceParser(InputStream inputStream) throws GenerationFault {
53          try {
54              Document doc = Utils.createDocument(inputStream);
55              Element root = doc.getDocumentElement();
56              Element serviceref = findServiceReferance(root);
57              if (serviceref != null)
58                  parse(serviceref);
59              else
60                  throw new GenerationFault("No service Referance in the file");
61          } catch (Exception e) {
62              e.printStackTrace();
63              throw GenerationFault.createGenerationFault(e);
64          }
65      }
66  
67      /***
68       * find the service-ref element from the xml file.
69       *
70       * @param ele
71       * @return
72       */
73      public Element findServiceReferance(Element ele) {
74          if ("service-ref".equals((ele).getLocalName())) {
75              return ele;
76          } else {
77              NodeList nodes = ele.getChildNodes();
78              for (int i = 0; i < nodes.getLength(); i++) {
79                  Node node = nodes.item(i);
80                  if (node instanceof Element) {
81                      return findServiceReferance((Element) node);
82                  }
83              }
84              return null;
85          }
86      }
87  
88      public ServiceReferanceParser(Element refEle) {
89          parse(refEle);
90      }
91  
92      public void parse(Element refEle) {
93          ref = new ServiceReferanceImpl();
94          Element root = refEle;
95          ref.setServicerefName(Utils.getElementValue(root.getElementsByTagName("service-ref-name")));
96          ref.setServiceInterface(Utils.getElementValue(root.getElementsByTagName("service-interface")));
97          ref.setWsdlFile(Utils.getElementValue(root.getElementsByTagName("wsdl-file")));
98          ref.setJaxrpcmappingFile(Utils.getElementValue(root.getElementsByTagName("jaxrpc-mapping-file")));
99      }
100 
101     /***
102      * @return
103      */
104     public ServiceReferanceContext getRef() {
105         return ref;
106     }
107 
108 }