Welcome, guest | Sign In | My Account | Store | Cart

This utility lets us use the 'wsdl2java' utility from Axis-1 to generate POJOs from the schema embedded in a WSDL. If we just want POJOs without the Axis marshalling/unmarshalling code, then this script will strip out the Axis code, leaving just the POJOs.

Python, 52 lines
 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
import os, sys, shutil, tempfile, string
from os.path import join
"""
This utility lets us use the 'wsdl2java' utility from Axis-1 to 
generate POJOs from the schema embedded in a WSDL.  If we just want
POJOs without the Axis marshalling/unmarshalling code, then this
script will strip out the Axis code, leaving just the POJOs.

As of Axis-1.x, fortunately, all the Axis marshall/unmarshall code
is at the last part of the generated class files, so we can get away
with just truncating the file rather then truly parsing and performing 
an AST transformation. (i.e. don't need a real java parser)

Author: Chris Wolf cw10025 gmail com
"""

def stripAxisCode(fileName):
  """copy lines from in file to out file up to first occurance
     of the string 'org.apache.axis', then just write closing brace.
     hasAxisCode detects of the file was already processed such that
     this is an idempotent operation.
  """ 
  hasAxisCode = False
  fin = open(fileName, 'r')
  outName = ''.join([fileName, '.tmp'])
  fout = open(outName, 'wr')
  for line in fin:
    if (string.find(line, 'org.apache.axis') != -1 and
        string.find(line, 'extends') == -1 and
        string.find(line, 'implements') == -1):
      hasAxisCode = True
      break
    else:
      fout.write(line)

  fin.close() 
  if hasAxisCode:
    fout.write("}\n")
  fout.close
  shutil.move(outName, fileName)

def processDirTree(tld):
  """From the top level package dir of Axis-generated POJOs, 
     strip out Axis code to make just plain, old java objects 
     (real POJOs - without binding-specific marshalling guts)
  """
  for root, dirs, files in os.walk(tld):
    for name in files:
      stripAxisCode(join(root, name))


processDirTree(sys.argv[1])

The motivation for this script is to be able to have some way of generating POJOs from the embedded schema inside a WSDL. Most utilities that do this generate POJOs which contain additional code that performs XML data binding, i.e. marshal/unmarshalling code. What was needed were POJOs that are just plain beans without any XML data binding code or binding-specific annotations.

For this, the "wsdl2java" utility from the Axis-1.x project works well because as of Axis-1.x, fortunately, all the Axis marshall/unmarshall code is at the last part of the generated class files, so we can get away with just truncating the file rather then truly parsing and performing an AST transformation. (i.e. don't need a real java parser)