📄 wsdlcpp.java
字号:
/* wsdlcpp.javaThe contents of this file are subject to the MPL 1.1 (the "License");you may not use this file except in compliance with the License.You may obtain a copy of the License athttp://www.cs.fsu.edu/~engelen/wsdllicense.htmlSoftware distributed under the License is distributed on an "AS IS" basis,WITHOUT WARRANTY OF ANY KIND, either express or implied. See the Licensefor the specific language governing rights and limitations under the License.The Initial Developers of the Original Code are Kiran Kaja and Robert A. van Engelen.Copyright (C) 2000-2002 Kiran Kaja and Robert A. van Engelen. All Rights Reserved.*/import java.io.IOException;import java.util.*;import java.io.*;import org.xml.sax.SAXException;import org.w3c.dom.Document;import org.w3c.dom.DocumentType;import org.w3c.dom.NamedNodeMap;import org.w3c.dom.Node;import org.w3c.dom.NodeList;// Import your vendor's DOM parserimport org.apache.xerces.parsers.DOMParser;public class wsdlcpp{ Hashtable tobeProcessedArrays = new Hashtable(); Hashtable processedArrays = new Hashtable(); Hashtable structureNodes = new Hashtable(); Hashtable bindingsList = new Hashtable(); Hashtable soapActions = new Hashtable(); Hashtable nameSpaces = new Hashtable(); /*sets the name spaces of the entire documents it looks only into the * definitions node*/ String docNS=null; String definitionNS=null; String soapNS=null; String defaultNS=null; Hashtable allPrimitiveDataType = new Hashtable(); Hashtable allPrimitiveDataTypeEquivalent = new Hashtable(); boolean defWsdl=false; boolean defSoap=false; public void setNameSpaces(Node node)//get the doucument node { String wsdlNS=null; String xsdNS=null; String encodingNS=null; NodeSearch defSearch = new NodeSearch(node,"definitions",new Hashtable()); Node defNode = defSearch.getNextPartialMatch(); String tns=null; String defans=null; if(defNode==null) { System.out.println("NO Definitions node found"); return; } String defname = defNode.getNodeName(); docNS = defname.substring(0,defname.indexOf("definitions")); NamedNodeMap attributes = defNode.getAttributes(); for (int i=0; i<attributes.getLength(); i++) { Node current = attributes.item(i); if(current.getNodeName().compareToIgnoreCase("targetNamespace")!=0) nameSpaces.put(current.getNodeValue(),current.getNodeName()); else { tns = current.getNodeValue(); continue; } if(current.getNodeName().compareToIgnoreCase("xmlns")==0) { defans = current.getNodeValue(); if(defans.endsWith("/wsdl/")) { defWsdl = true; } if(defans.endsWith("/soap/")) { defSoap = true; } continue; } if(current.getNodeValue().endsWith("/wsdl/")) { StringTokenizer st = new StringTokenizer(current.getNodeName(),":"); if(st.countTokens()==2) { st.nextToken(); wsdlNS =new String(st.nextToken()+":"); } } if(current.getNodeValue().endsWith("/soap/")) { StringTokenizer st = new StringTokenizer(current.getNodeName(),":"); if(st.countTokens()==2) { st.nextToken(); soapNS =new String(st.nextToken()+":"); } else { //System.out.println("Found soap name space but name has more than 2 elements on tokenizer :"); } } if(current.getNodeValue().endsWith("/soap/encoding/")) { StringTokenizer st = new StringTokenizer(current.getNodeName(),":"); if(st.countTokens()==2) { st.nextToken(); encodingNS =new String(st.nextToken()+":"); } else { //System.out.println("Found encoding name space but name has more than 2 elements on tokenizer :"); } } if(current.getNodeValue().endsWith("/XMLSchema")) { StringTokenizer st = new StringTokenizer(current.getNodeName(),":"); if(st.countTokens()==2) { st.nextToken(); xsdNS =new String(st.nextToken()+":"); } else { //System.out.println("Found xsd name space but name has more than 2 elements on tokenizer :"); } } } if(tns!=null) { for (int i=0; i<attributes.getLength(); i++) { Node current = attributes.item(i); if(current.getNodeName().compareToIgnoreCase("targetNamespace")==0) continue; if(current.getNodeValue().equals(tns)) { StringTokenizer st = new StringTokenizer(current.getNodeName(),":"); if(st.countTokens()==2) { st.nextToken(); definitionNS =new String(st.nextToken()+":"); } else { //System.out.println("Found default name space but name has more than 2 elements on tokenizer :"); } } } } else { System.out.println("TNS not found"); } if(defans!=null) { for (int i=0; i<attributes.getLength(); i++) { Node current = attributes.item(i); if(current.getNodeName().compareToIgnoreCase("targetNamespace")==0) continue; if(current.getNodeName().compareToIgnoreCase("xmlns")==0) continue; if(current.getNodeValue().equals(defans)) { StringTokenizer st = new StringTokenizer(current.getNodeName(),":"); if(st.countTokens()==2) { st.nextToken(); defaultNS =new String(st.nextToken()+":"); } else { //System.out.println("Found default name space but name has more than 2 elements on tokenizer :"); } } } } } /*call the parsing process by creating hte document object */ public void myParser(String uri) { System.out.println("/*Parsing XML File: " + uri + "*/"); // Instantiate your vendor's DOM parser implementation DOMParser parser = new DOMParser(); try { parser.parse(uri); Document doc = parser.getDocument(); parseMyDoc(doc); /*write .h file*/ String outfilename = uri.substring(0,uri.indexOf('.'))+".h"; FileWriter fw = new FileWriter(outfilename); PrintWriter pw = new PrintWriter(fw); System.out.println("Processing Done.\n\t\t Header File\t\t"+outfilename); NsNodeSearch ndefinition = new NsNodeSearch(doc,docNS+"definitions",new Hashtable()); NsNode ndefinitionNode = ndefinition.getNextNode(); /*System.out.println(soapActions);*/ Hashtable namespaces = ndefinitionNode.getNameSpace(); Enumeration keyList; String key; String twsdlns = getNsEquivalent(namespaces,"wsdlNS"); String txsdns = getNsEquivalent(namespaces,"xsdNS"); String tsoapns = getNsEquivalent(namespaces,"soapNS"); String defaultns = getNsEquivalent(namespaces,"definitionNS"); String name; keyList = bindingsList.keys(); while(keyList.hasMoreElements()) { key = (String)keyList.nextElement(); pw.println("//gsoap "+(String)bindingsList.get(key)+" schema namespace: "+ key); } keyList = namespaces.keys(); while(keyList.hasMoreElements()) { key = (String)keyList.nextElement(); name = getName(key)+":"; if(key.startsWith("xmlns:")) { if(!(name.equals(twsdlns)||name.equals(txsdns)||name.equals(tsoapns)||name.equals(defaultns))) { pw.println("//gsoap "+ getName(key)+" schema namespace: "+(String)namespaces.get(key)); } } } pw.println(""); if(defaultns!=null) { defaultns = defaultns.substring(0,defaultns.length()-1); pw.println("//gsoap "+ defaultns + " service namespace: "+namespaces.get("xmlns:"+defaultns)); } pw.println(""); pw.println("//gsoap "+defaultns+" service location: "+serviceAddrLocation); pw.println("//gsoap "+defaultns+" service name: "+"soap"+serviceName); pw.println("\n/*start primitive data types*/"); keyList = allPrimitiveDataType.keys(); while(keyList.hasMoreElements()) { key=(String)keyList.nextElement(); if(((Boolean)allPrimitiveDataType.get(key)).booleanValue()) pw.print(allPrimitiveDataTypeEquivalent.get(key)); } /*System.out.println(allPrimitiveDataType);*/ pw.println("\n/*end primitive data types*/\n"); keyList = allDataType.keys(); while(keyList.hasMoreElements()) { pw.println( (String)allDataType.get(keyList.nextElement())); } keyList = allOperation.keys(); while(keyList.hasMoreElements()) { key = (String)keyList.nextElement(); pw.println("//soapAction : "+(String)soapActions.get(key)); pw.println( (String)allOperation.get(key)); } pw.close(); /*write .c file*/ outfilename = uri.substring(0,uri.indexOf('.'))+".c"; fw = new FileWriter(outfilename); pw = new PrintWriter(fw); System.out.println("\t\t Sample C File\t\t"+outfilename); pw.println("#include \"soapH.h\""); pw.println("#include \""+"soap"+serviceName+".nsmap\""); pw.println("main()"); pw.println("{\n\tstruct soap soap;"); pw.println("\tsoap_init(&soap);"); pw.println("\n"); keyList = allOperation.keys(); String description=""; String functionName=""; String parameters=""; int index1=0,index2=0; String saction=""; while(keyList.hasMoreElements()) { key = (String)keyList.nextElement(); description = (String)allOperation.get(key); saction = (String)soapActions.get(key); index1 = description.indexOf('('); index2= description.lastIndexOf(')'); functionName = description.substring(0,index1).trim(); parameters = description.substring(index1+1,index2).trim(); pw.print("\tif (soap_call_"+functionName+" ( &soap, \""+serviceAddrLocation+"\", \""); pw.println(saction+"\",/* "+parameters+"*/))"); pw.println("\t\tsoap_print_fault(&soap,stderr);\n\n"); } pw.println("}"); pw.close(); } // catch the excepetion catch (IOException e) { System.out.println("Error reading URI: " + e.getMessage()); } catch (SAXException e) { System.out.println("Error in parsing: " + e.getMessage()); } } /*convert the datatype ie convert : to __*/ private String dataType(String str) { if (str==null){System.out.println("str null");System.exit(0);} StringBuffer finalString = new StringBuffer(); StringTokenizer colon = new StringTokenizer(str,":",true); String temp; while(colon.hasMoreElements()) { temp= (String)colon.nextElement(); if(temp.equals(":")) finalString.append("__"); else finalString.append(convertToCpp(temp,false)); } String uscores=""; if(keyWords.contains(finalString.toString())) uscores="_"; return finalString.toString()+uscores; //return finalString.toString(); } /*convert the string to print format _ , . and __ */ private String convertToCpp(String str,boolean uscoreflag) { StringBuffer finalString = new StringBuffer(); //replace all '_' with _USCORE_ this should be done first as all // other replacements will add'_' to the string StringTokenizer uscore = new StringTokenizer(str,"_",true); String temp; // finalString.append((String)uscore.nextElement()); while(uscore.hasMoreElements()) { temp = (String)uscore.nextElement(); if(temp.equals("_")) finalString.append("_USCORE_"); else finalString.append(temp); } //replace all '.' with '_DOT_' StringTokenizer dot = new StringTokenizer(finalString.toString(),".",true); finalString = new StringBuffer(); //finalString.append((String)dot.nextElement()); while(dot.hasMoreElements()) { temp = (String)dot.nextElement(); if(temp.equals(".")) finalString.append("_DOT_"); else finalString.append(temp); } //replace all '-' with '_' StringTokenizer hyphen = new StringTokenizer(finalString.toString(),"-",true);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -