You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-dev@axis.apache.org by Ravi Kumar <rk...@borland.com> on 2001/10/18 15:56:59 UTC

Emitter patches

Team,

Attached is a diff for some emitter changes I would like to check in. I
will wait for comments till afternoon before doing so.

Briefly, this is what they do.

1. The current code doen't handle return / output types correctly when
parameterOrder attribute is set in binding->operations. The root of the
problem is that getOrderedList does not return all the parts. More
specifically, it does not return the "return" part. (read 2.4.6 in the
WSDL spec).

2. Added namespace support. This basically maintains a map of all
namespace to package mapping in the TypeFactory. Currently, the top
level package (aka package for the portType) is settable, otheres are
auto generated. But, I intend to add a setPackageMap(namespace, package)
to the emitter. That will give granular control of all namespaces
involved.

That still leaves surfacing it through Wsdl2Java, which I beleive Brian
/Russel are looking into. If esle, I can take care of it.

3. Added a fix to makepackagename to handle hyphen's in identifiers

cheers,
Ravi
JBuilder R&D





Re: Emitter patches

Posted by Ravi Kumar <rk...@borland.com>.
forgot to list one more change

-- propagating the namespace change down to the deploy.xml typemappings


Ravi Kumar wrote:

> Team,
>
> Attached is a diff for some emitter changes I would like to check in. I
> will wait for comments till afternoon before doing so.
>
> Briefly, this is what they do.
>
> 1. The current code doen't handle return / output types correctly when
> parameterOrder attribute is set in binding->operations. The root of the
> problem is that getOrderedList does not return all the parts. More
> specifically, it does not return the "return" part. (read 2.4.6 in the
> WSDL spec).
>
> 2. Added namespace support. This basically maintains a map of all
> namespace to package mapping in the TypeFactory. Currently, the top
> level package (aka package for the portType) is settable, otheres are
> auto generated. But, I intend to add a setPackageMap(namespace, package)
> to the emitter. That will give granular control of all namespaces
> involved.
>
> That still leaves surfacing it through Wsdl2Java, which I beleive Brian
> /Russel are looking into. If esle, I can take care of it.
>
> 3. Added a fix to makepackagename to handle hyphen's in identifiers
>
> cheers,
> Ravi
> JBuilder R&D
>
>   ------------------------------------------------------------------------
> Index: Emitter.java
> ===================================================================
> RCS file: /home/cvspublic/xml-axis/java/src/org/apache/axis/wsdl/Emitter.java,v
> retrieving revision 1.63
> diff -r1.63 Emitter.java
> 127c127
> <     private boolean bGeneratePackageName = false;
> ---
> >     private boolean bGeneratePackageName = true;
> 186d185
> <             setup();
> 188a188
> >             setup();
> 202a203
> >             emitFactory.map(def.getTargetNamespace(), packageName);
> 715c719,720
> <             boolean firstOutput = true;
> ---
> >             boolean firstOutput = !getReturnType(parameters, output, parameterOrder,
> >               (wsdlAttr.getOutputBodyType(operation) == WsdlAttributes.USE_LITERAL));
> 786c791
> <         if (allOuts == 0)
> ---
> >         if ((allOuts == 0) && (parms.returnName == null))
> 938a944,945
> >         this.classList.add(exceptionName);
> >         this.fileList.add(fileName);
> 940d946
> <
> 1110a1117,1118
> >             this.classList.add(skelName);
> >             this.fileList.add(skelFileName);
> 1112d1119
> <
> 1323c1330
> <                         "\", " + p.name + "._value)");
> ---
> >                         "\", " + wrapPrimitiveType(p.type, p.name + "._value")+ ")");
> 1343c1350,1355
> <                     pw.println("            " + p.name + "._value = " + getResponseString(p.type, "resp"));
> ---
> >                     if (parms.returnName != null) {
> >                         pw.println ("            " + p.name + "._value = " + getResponseString(p.type, "((org.apache.axis.message.RPCParam) call.getOutputParams().get(" + 0 + ")).getValue()"));
> >                         pw.println("             return " + getResponseString(parms.returnType, "resp"));
> >                     }
> >                     else
> >                         pw.println("             return " + getResponseString(parms.returnType, "resp"));
> 1345c1357
> <                 else {
> ---
> >                 else if (parms.returnName != null) {
> 1358c1370
> <                 boolean firstInoutIsResp = (parms.outputs == 0);
> ---
> >                 boolean firstInoutIsResp = ((parms.outputs == 0) && (parms.returnName == null));
> 1372c1384
> <                 if (parms.outputs > 0)
> ---
> >                 if ((parms.outputs > 0) || (parms.returnName != null))
> 1373a1386
> >
> 1376a1390,1393
> >         else if (parms.returnName != null) {
> >             pw.println("             return " + getResponseString(parms.returnType, "resp"));
> >         }
> >
> 1402c1419
> <         if (parms.outputs == 0)
> ---
> >         if ((parms.outputs == 0) && (parms.returnName == null))
> 1427c1444,1447
> <         pw.println(wrapPrimitiveType(parms.returnType, call) + ";");
> ---
> >         if ((parms.outputs == 0) && (parms.returnName == null))
> >             pw.println(call + ";");
> >         else
> >             pw.println(wrapPrimitiveType(parms.returnType, call) + ";");
> 1431c1451
> <             if (parms.inouts == 0 && parms.outputs == 1)
> ---
> >             if ((parms.inouts == 0 && parms.outputs == 1) && (parms.returnName == null))
> 1434c1454
> <             else if (parms.outputs == 0 && parms.inouts == 1) {
> ---
> >             else if ((parms.outputs == 0 && parms.inouts == 1) && (parms.returnName == null)) {
> 1441c1461
> <                 pw.println("        return " + p.name + "Holder._value;");
> ---
> >                 pw.println("        return " + wrapPrimitiveType(p.type, p.name + "Holder._value") + ";");
> 1446c1466
> <                 if (parms.outputs > 0)
> ---
> >                 if (parms.returnName != null)
> 1452c1472
> <                         pw.println("        list.add(new org.apache.axis.message.RPCParam(\"" + p.name + "\", " + p.name + "Holder._value));");
> ---
> >                         pw.println("        list.add(new org.apache.axis.message.RPCParam(\"" + p.name + "\", " + wrapPrimitiveType(p.type, p.name + "Holder._value") +"));");
> 1456a1477,1480
> >         else if (parms.returnName != null) {
> >             pw.println("        return ret;");
> >         }
> >
> 1716c1740,1760
> <         //assumes all complex type elements are under one parent
> ---
> > //        //assumes all complex type elements are under one parent
> > //        Iterator it = types.values().iterator();
> > //        Node node = ((Type) it.next()).getNode();
> > //        String namespaceURI = Utils.getScopedAttribute(node, "targetNamespace");
> > //
> > //        //grab the namespace prefix from the attributes of the root (if it is there)
> > //        String namespacePrefix = "ns";
> > //        NamedNodeMap docAttrs = doc.getDocumentElement().getAttributes();
> > //        for (int i = 0; i < docAttrs.getLength(); i++) {
> > //            Attr attr = (Attr) docAttrs.item(i);
> > //            if (attr.getValue().equals(namespaceURI)) {
> > //                namespacePrefix = ((Attr) docAttrs.item(i)).getLocalName();
> > //                break;
> > //            }
> > //        }
> >
> > //RRK        pw.println("   <beanMappings xmlns:" + namespacePrefix + "=\"" + namespaceURI + "\">");
> >         pw.print("   <beanMappings ");
> >         HashMap nsMap = new HashMap();
> >         int i = 1;
> >         String nsPrefix = null;
> 1718,1728c1762,1770
> <         Node node = ((Type) it.next()).getNode();
> <         String namespaceURI = Utils.getScopedAttribute(node, "targetNamespace");
> <
> <         //grab the namespace prefix from the attributes of the root (if it is there)
> <         String namespacePrefix = "ns";
> <         NamedNodeMap docAttrs = doc.getDocumentElement().getAttributes();
> <         for (int i = 0; i < docAttrs.getLength(); i++) {
> <             Attr attr = (Attr) docAttrs.item(i);
> <             if (attr.getValue().equals(namespaceURI)) {
> <                 namespacePrefix = ((Attr) docAttrs.item(i)).getLocalName();
> <                 break;
> ---
> >         while (it.hasNext()) {
> >             Type type = (Type) it.next();
> >             if (type.getBaseType() == null) {
> >                 pw.println("");
> >                 if (!nsMap.containsKey(type.getQName().getNamespaceURI())) {
> >                   nsPrefix = "ns" + i++;
> >                   nsMap.put(type.getQName().getNamespaceURI(), nsPrefix);
> >                   pw.print("     xmlns:" + nsPrefix  + "=\"" + type.getQName().getNamespaceURI() + "\"");
> >                 }
> 1731,1733c1773
> <
> <         pw.println("   <beanMappings xmlns:" + namespacePrefix + "=\"" + namespaceURI + "\">");
> <
> ---
> >         pw.println(" >");
> 1738,1746c1778,1782
> <                 pw.println();
> <                 if (packageName == null) {
> <                     pw.println("     <" + namespacePrefix + ":" + type.getQName().getLocalPart()
> <                            + " classname=\"" + type.getJavaName() +"\"/>");
> <                 }
> <                 else {
> <                     pw.println("     <" + namespacePrefix + ":" + type.getQName().getLocalPart()
> <                            + " classname=\"" + packageName + "." + type.getJavaName() +"\"/>");
> <                 }
> ---
> >                 nsPrefix = (String)nsMap.get(type.getQName().getNamespaceURI());
> >                 pw.println("     <" + nsPrefix + ":" + type.getQName().getLocalPart()
> >                        + " classname=\"" + type.getJavaName() +"\"/>");
> > //RRK                pw.println("     <" + namespacePrefix + ":" + type.getQName().getLocalPart()
> > //                       + " classname=\"" + type.getJavaName() +"\"/>");
> 1895a1934
> >         PrintWriter typePW = printWriter(fileName, type.getJavaPackageName());
> 1897d1935
> <         PrintWriter typePW = printWriter(fileName);
> 1901c1939
> <         writeFileHeader(fileName, typePW);
> ---
> >         writeFileHeader(fileName, type.getJavaPackageName(), typePW);
> 1973d2010
> <
> 1982c2022
> <         PrintWriter typePW = printWriter(fileName);
> ---
> >         PrintWriter typePW = printWriter(fileName, eType.getJavaPackageName());
> 1986c2026
> <         writeFileHeader(fileName, typePW);
> ---
> >         writeFileHeader(fileName, eType.getJavaPackageName(), typePW);
> 2014c2054
> <         PrintWriter pw = printWriter(fileName);
> ---
> >         PrintWriter pw = printWriter(fileName, type.getJavaPackageName());
> 2018c2058
> <         writeFileHeader(fileName, pw);
> ---
> >         writeFileHeader(fileName, type.getJavaPackageName(), pw);
> 2066,2067c2106,2117
> <         if (outputDir == null) {
> <             return new PrintWriter(new FileWriter(packageDirName + name));
> ---
> >       return printWriter(name, null);
> >     } // printWriter
> >
> >     /**
> >      * Get a PrintWriter attached to a file with the given name.  The location of this file
> >      * is determined from the values of outputDir and packageDirName.
> >      */
> >     private PrintWriter printWriter(String name, String packageName) throws IOException
> >     {
> >         String pkgDirName = packageDirName;
> >         if (packageName != null) {
> >           pkgDirName = packageName.replace('.', '/');
> 2069,2070c2119,2133
> <         else {
> <             return new PrintWriter(new FileWriter(outputDir + File.separatorChar + packageDirName + name));
> ---
> >
> >         File dir = null;
> >         if (pkgDirName != null) {
> >           if (outputDir == null)
> >               dir = new File(pkgDirName);
> >           else
> >               dir = new File(outputDir, pkgDirName);
> >           if (!dir.exists()) {
> >             StringTokenizer st = new StringTokenizer(pkgDirName, "/");
> >             dir = new File(outputDir);
> >             while (st.hasMoreTokens()) {
> >               dir = new File(dir, st.nextToken());
> >               dir.mkdir();
> >             }
> >           }
> 2071a2135,2136
> >
> >         return new PrintWriter(new FileWriter(new File(dir, name)));
> 2203a2269,2276
> >         writeFileHeader(filename, null, pw);
> >     }
> >
> >     /**
> >      * Write a common header, including the package name (if any) to the
> >      * provided stream
> >      */
> >     private void writeFileHeader(String filename, String pkgName, PrintWriter pw) {
> 2213,2214c2286,2293
> <         if (packageName != null) {
> <             pw.println("package " + packageName + ";");
> ---
> >         if (pkgName == null) {
> >           if (packageName != null) {
> >               pw.println("package " + packageName + ";");
> >               pw.println();
> >           }
> >         }
> >         else {
> >             pw.println("package " + pkgName + ";");
> 2221,2256c2300,2301
> <         String hostname = null;
> <
> <         // get the target namespace of the document
> <          String namespace = def.getTargetNamespace();
> <          try {
> <              hostname = new URL(namespace).getHost();
> <          }
> <          catch (MalformedURLException e) {
> <              // do nothing
> <              return;
> <          }
> <
> <         // if we didn't file a hostname, bail
> <         if (hostname == null) {
> <             return;
> <         }
> <
> <         // tokenize the hostname and reverse it
> <         StringTokenizer st = new StringTokenizer( hostname, "." );
> <         String[] words = new String[ st.countTokens() ];
> <         for(int i = 0; i < words.length; ++i)
> <             words[i] = st.nextToken();
> <
> <         StringBuffer sb = new StringBuffer(80);
> <         for(int i = words.length-1; i >= 0; --i) {
> <             String word = words[i];
> <             // seperate with dot
> <             if( i != words.length-1 )
> <                 sb.append('.');
> <
> <             // convert digits to underscores
> <             if( Character.isDigit(word.charAt(0)) )
> <                 sb.append('_');
> <             sb.append( word );
> <         }
> <         setPackageName(sb.toString());
> ---
> >         String pkgName = Utils.makePackageName(def.getTargetNamespace());
> >         setPackageName(pkgName);
> 2260a2306,2341
> >     }
> >
> >     private boolean getReturnType(Parameters parameters, Output output, List parameterOrder, boolean literal) {
> >         if (output.getMessage().getOrderedParts(parameterOrder) ==
> >             output.getMessage().getOrderedParts(null))
> >             return false;
> >         Map parts = output.getMessage().getParts();
> >         Iterator itr = parts.values().iterator();
> >         List outputParts = output.getMessage().getOrderedParts(parameterOrder);
> >         while (itr.hasNext()) {
> >            Part part = (Part)itr.next();
> >            if (!outputParts.contains(part)) {
> >                if (literal) {
> >                    QName elementName = part.getElementName();
> >                    if (elementName != null) {
> >                        parameters.returnType = Utils.capitalize(elementName.getLocalPart());
> >                        parameters.returnType = part.getName();
> >                        return true;
> >                    }
> >                } else {
> >                    QName typeName = part.getTypeName();
> >                    if (typeName != null) {
> >                        // Handle the special "java" namespace for types
> >                        if (typeName.getNamespaceURI().equalsIgnoreCase("java")) {
> >                            parameters.returnType = typeName.getLocalPart();
> >                        } else {
> >                            parameters.returnType = emitFactory.getType(typeName).getJavaName();
> >                        }
> >                        parameters.returnName = part.getName();
> >                        return true;
> >                    }
> >                 }
> >            }
> >        }
> >        // should never happen ?
> >        return false;
> Index: Utils.java
> ===================================================================
> RCS file: /home/cvspublic/xml-axis/java/src/org/apache/axis/wsdl/Utils.java,v
> retrieving revision 1.4
> diff -r1.4 Utils.java
> 60a61,63
> > import java.util.StringTokenizer;
> > import java.net.MalformedURLException;
> > import java.net.URL;
> 62d64
> <
> 67c69
> <  * This class contains static utility methods for the emitter.
> ---
> >  * This class contains static utility methods for the emitter.
> 78c80
> <
> ---
> >
> 84,85c86,87
> <     } // capitalize
> <
> ---
> >     } // capitalize
> >
> 112c114
> <
> ---
> >
> 146c148
> <
> ---
> >
> 160c162
> <         // the complexType may be anonymous, which is why the getScopedAttribute
> ---
> >         // the complexType may be anonymous, which is why the getScopedAttribute
> 209c211
> <
> ---
> >
> 231,234c233,277
> <     /**
> <      * These are java keywords as specified at the following URL (sorted alphabetically).
> <      * http://java.sun.com/docs/books/jls/second_edition/html/lexical.doc.html#229308
> <      */
> ---
> >     public static String makePackageName(String namespace)
> >     {
> >         String hostname = null;
> >
> >         // get the target namespace of the document
> >          try {
> >              hostname = new URL(namespace).getHost();
> >          }
> >          catch (MalformedURLException e) {
> >            if (namespace.indexOf(":") > -1) {
> >              hostname = namespace.substring(namespace.indexOf(":") + 1);
> >              if (hostname.indexOf("/") > -1)
> >                hostname = hostname.substring(0, hostname.indexOf("/") );
> >            }
> >          }
> >
> >         // if we didn't file a hostname, bail
> >         if (hostname == null) {
> >             return null;
> >         }
> >
> >         //convert illegal java identifier
> >         hostname = hostname.replace('-', '_');
> >
> >         // tokenize the hostname and reverse it
> >         StringTokenizer st = new StringTokenizer( hostname, "." );
> >         String[] words = new String[ st.countTokens() ];
> >         for(int i = 0; i < words.length; ++i)
> >             words[i] = st.nextToken();
> >
> >         StringBuffer sb = new StringBuffer(80);
> >         for(int i = words.length-1; i >= 0; --i) {
> >             String word = words[i];
> >             // seperate with dot
> >             if( i != words.length-1 )
> >                 sb.append('.');
> >
> >             // convert digits to underscores
> >             if( Character.isDigit(word.charAt(0)) )
> >                 sb.append('_');
> >             sb.append( word );
> >         }
> >         return sb.toString();
> >     }
> >
> Index: TypeFactory.java
> ===================================================================
> RCS file: /home/cvspublic/xml-axis/java/src/org/apache/axis/wsdl/TypeFactory.java,v
> retrieving revision 1.4
> diff -r1.4 TypeFactory.java
> 61a62
> > import java.lang.reflect.*;
> 75a77,78
> >     private HashMap mapPackageToNamespace;  // Mapping from Java Package to Namespace
> >     private static int nameSpaceSuffix = 1;
> 85a89,101
> >      * Invoke this method to associate a namespace URI with a autogenerated Java Package
> >      * name, if an entry is not already present
> >      *
> >      */
> >
> >
> >     public void map (String namespace) {
> >         if (mapNamespaceToPackage.get(namespace) == null) {
> >           mapNamespaceToPackage.put(namespace, Utils.makePackageName(namespace));
> >         }
> >     }
> >
> >     /**
> 349a430
> >             map(qName.getNamespaceURI());