Showing posts with label wsdl_parser. Show all posts
Showing posts with label wsdl_parser. Show all posts

Monday, August 1, 2011

Groovy WSDL Parser

class PVWSDLParser {
 public static void main(String[] args) {
  PVWSDLParser parser = new PVWSDLParser();
  parser.parse("/path/to/your.wsdl");
 }
 
 public void parse(String filename) {
  
  def nsxsd = new groovy.xml.Namespace("http://www.w3.org/2001/XMLSchema", 'xsd')
  def nswsdl = new groovy.xml.Namespace("http://schemas.xmlsoap.org/wsdl/", 'wsdl')
  
  def wsdlDocument = new XmlParser().parse(new File(filename));
  def targetNamespace = wsdlDocument.'@targetNamespace';
  
  def imports = wsdlDocument[nswsdl.types][nsxsd.schema][nsxsd.import];
  imports.each { theimport ->
   System.out.println(theimport.'@schemaLocation');  
   System.out.println(theimport.'@namespace');
  } 
 }
}



A LOT easier than in Java!

Thursday, July 28, 2011

WSDL parser reloaded

Here a previous post on the topic

Here the WSDL 1.1 Schema:
http://schemas.xmlsoap.org/wsdl/


Import it into a Eclipse Web Service project (it has all the facets needed for JAXB)
and generate JAXB classes (I use org.example.wsdl package)

Trying to unmarshal a WSDL, I get a

java.lang.ClassCastException: javax.xml.bind.JAXBElement

(see here)

so I choose a better API for Unmarshaller (don(t try to understand; just make it work)


And.... here is the WSDLParser!

In reality as you start working with TDefinitions you realize that it's quite difficult to extract the information you need, because the http://schemas.xmlsoap.org/wsdl schema is VERY abstract.

As usual, simple things made complicated by academics.

Thursday, June 10, 2010

WSDL Parser

Since I am not a monkey (well, I am, but with some value added),
I don't like to type what I can generate.

It happens often to have to generate the same kind of artifacts for each operation in a WSDL.
It becomes then very important to be able to parse the WSLD and find out all its components.

Good candidates containing a WSDLDocument class are:
C:\bea11\oracle_common\modules\oracle.webservices_11.1.1\orawsdl.jar
C:\bea11\Oracle_OSB11\lib\modules\com.bea.alsb.resources.wsdl.jar
C:\bea11\Oracle_OSB11\lib\modules\com.bea.alsb.ws.schemas.wsdl.jar

Also

C:\bea11\jdk160_18\lib\tools.jar
or C:\bea11\jrockit_160_17_R28.0.0-679\lib\tools.jar
C:\bea11\modules\glassfish.jaxws.tools_1.0.0.0_2-1-5.jar
C:\bea11\oepe_11gR1PS2\plugins\org.apache.wsil4j_1.0.0.v200901211807.jar
C:\bea11\oepe_11gR1PS2\plugins\org.eclipse.wst.wsdl.validation_1.1.402.v200911251542.jar
C:\bea11\oepe_11gR1PS2\plugins\org.eclipse.wst.wsi_1.0.305.v200902181431.jar
C:\bea11\oracle_common\modules\oracle.webservices_11.1.1\oracle.webservices.standalone.client.jar

C:\bea11\oracle_common\modules\sun.tools_1.6.0.jar
C:\bea11\Oracle_OSB11\lib\sb-kernel-resources.jar


I have tried some of them but could not make them work.


This http://oracled.wordpress.com/2009/02/04/parse-wsdl-effectively/ gives some clues.

Oracle WSDL parser: you get it by installing JDeveloper, it's in C:\beajdev11\jdeveloper\webservices\lib\wsdl.jar

Apache Woden http://ws.apache.org/woden/userguide.html

JWSDL http://wsdl4j.sourceforge.net/


Javadoc of Oracle WSDL parser is at
http://download-uk.oracle.com/docs/cd/A97688_16/generic.903/q20234/index.html


Using the Oracle WSDL parser, I get a


Exception in thread "Main Thread" oracle.wsdl.internal.WSDLException: oracle.xml.parser.v2.XMLParseException: Start of root element expected.
    at oracle.wsdl.WSDLDocument.getDefinitions(WSDLDocument.java:105)
    at com.acme.wsdl.WSDLParser.execute(WSDLParser.java:21)
    at com.acme.wsdl.WSDLParser.main(WSDLParser.java:16)


I give up.

I try Woden and I get:

WSDLException: faultCode=INVALID_WSDL: Fatal WSDL error:
0:0,WSDL501,Expected a "{http://www.w3.org/ns/wsdl}description" element, but found a "{http://schemas.xmlsoap.org/wsdl/}definitions" element instead.:


Googling around I discover that this means that my WSDL is a 1.1 while Woden processes only 2.0.

You can find here http://www.w3.org/2006/02/wsdl11to20.xsl a XSL converter from 1.1 to 2.0
Once converted 1.1 into 2.0 format, Woden manages to parse the WSDL but I get a warning:

The targetNamespace 'http://www.acme.com/NGP3Geo/2010/06/10/GeoOsbService' is not dereferencable.

which means (I think) that a URL connection open to retrieve the resource http://www.acme.com/NGP3Geo/2010/06/10/GeoOsbService gets a "connection refused" (to dereference= to access).

This is a piece of code to print all operations declared in a WSDL:


package com.acme.wsdl;

import org.apache.woden.WSDLException;
import org.apache.woden.WSDLFactory;
import org.apache.woden.WSDLReader;
import org.apache.woden.wsdl20.Description;
import org.apache.woden.wsdl20.xml.BindingElement;
import org.apache.woden.wsdl20.xml.DescriptionElement;
import org.apache.woden.wsdl20.xml.InterfaceElement;
import org.apache.woden.wsdl20.xml.InterfaceOperationElement;
import org.apache.woden.wsdl20.xml.ServiceElement;



public class WSDLParserApache {
public static void main(String[] args) throws WSDLException {
    WSDLParserApache wsdlParserApache = new WSDLParserApache();
     wsdlParserApache.execute();
}

public void execute() throws WSDLException {

String filename1 = "C:/bea11/user_projects/workshops/default/GEO_OSB_Portal/WSDL/GEOOSBService20.WSDL";

WSDLFactory factory = WSDLFactory.newInstance();

WSDLReader reader = factory.newWSDLReader();

reader.setFeature(WSDLReader.FEATURE_VALIDATION, true);

String wsdlurl = filename1;

DescriptionElement descElem = (DescriptionElement) reader.readWSDL(wsdlurl);

InterfaceElement[] interfaces = descElem.getInterfaceElements();

// getting all operations
for (InterfaceElement iface : interfaces) {
   for (InterfaceOperationElement ioe : iface.getInterfaceOperationElements()) {
       System.out.println(ioe.getName().getLocalPart());
   }
}

}
}