summaryrefslogtreecommitdiffstats
path: root/tags/java-M1-final/java/sca/tools/src/main/java/org/apache/tuscany/tools/wsdl2java/generate/JavaInterfaceEmitter.java
blob: 7bb2559b62eb9acf577d06e728da2ce51a0a1d8b (plain)
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/**
 *
 *  Copyright 2005 The Apache Software Foundation or its licensors, as applicable.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.apache.tuscany.tools.wsdl2java.generate;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.xml.namespace.QName;

import org.apache.axis2.description.AxisMessage;
import org.apache.axis2.description.AxisOperation;
import org.apache.axis2.util.FileWriter;
import org.apache.axis2.wsdl.WSDLConstants;
import org.apache.axis2.wsdl.codegen.CodeGenConfiguration;
import org.apache.axis2.wsdl.codegen.emitter.JavaEmitter;
import org.apache.axis2.wsdl.codegen.writer.InterfaceWriter;
import org.apache.axis2.wsdl.databinding.TypeMapper;
import org.apache.tuscany.model.util.XMLNameUtil;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
 * Overrides the Axis2 JavaEmitter to generate unwrapped methods.
 */
public class JavaInterfaceEmitter extends JavaEmitter {
    
    private CodeGenConfiguration codegenConfiguration;
    private TypeMapper typeMapper;

    public void setCodeGenConfiguration(CodeGenConfiguration configuration) {
        super.setCodeGenConfiguration(configuration);
        codegenConfiguration=configuration;
    }
    
    public void setMapper(TypeMapper typeMapper) {
        super.setMapper(typeMapper);
        this.typeMapper = typeMapper;
    }
    
    private List getParameterElementList(Document doc, AxisMessage message, boolean wrapped) {
        List parameterElementList = new ArrayList();
        
        if (message != null && message.getElementQName()!=null) {

            SDODataBindingTypeMappingEntry typeMappingEntry =
                (SDODataBindingTypeMappingEntry)this.typeMapper.getTypeMappingObject(message.getElementQName());
            List typeMappings;
            if (wrapped) {
                typeMappings = (List)typeMappingEntry.getPropertyClassNames();
            } else {
                typeMappings = new ArrayList();
                typeMappings.add(typeMappingEntry.getClassName());
            }
            
            for (int i=0; i<typeMappings.size(); i++) {
                Element param = doc.createElement("param");
                parameterElementList.add(param);
                
                String typeMapping = (String)typeMappings.get(i);
    
                addAttribute(doc, "name", this.typeMapper.getParameterName(message.getElementQName()), param);
                addAttribute(doc, "type", (typeMapping == null)
                        ? ""
                        : typeMapping, param);
    
                // add an extra attribute to say whether the type mapping is the default
                if (TypeMapper.DEFAULT_CLASS_NAME.equals(typeMapping)) {
                    addAttribute(doc, "default", "yes", param);
                }
    
                addAttribute(doc, "value", null, param);
    
                // add this as a body parameter
                addAttribute(doc, "location", "body", param);
    
            }
        }

        return parameterElementList;
    }
    
    private List getParameterElementList(Document doc, List parameters, String location) {
        List parameterElementList = new ArrayList();

        if ((parameters != null) && !parameters.isEmpty()) {
            int count = parameters.size();

            for (int i = 0; i < count; i++) {
                Element param = doc.createElement("param");
                QName name = (QName) parameters.get(i);

                addAttribute(doc, "name", this.typeMapper.getParameterName(name), param);

                String typeMapping = this.typeMapper.getTypeMappingName(name);
                String typeMappingStr = (typeMapping == null)
                        ? ""
                        : typeMapping;

                addAttribute(doc, "type", typeMappingStr, param);
                addAttribute(doc, "location", location, param);
                parameterElementList.add(param);
            }
        }

        return parameterElementList;
    }
    
    protected boolean isWrapped(AxisOperation operation) {
        boolean wrapped = false;

        if (isInputPresentForMEP(operation.getMessageExchangePattern())) {
            QName qname = operation.getMessage(WSDLConstants.MESSAGE_LABEL_IN_VALUE).getElementQName();
            if (qname != null && qname.getLocalPart().equals(operation.getName().getLocalPart())) {
                
                SDODataBindingTypeMappingEntry typeMappingEntry =
                            (SDODataBindingTypeMappingEntry)this.typeMapper.getTypeMappingObject(qname);
                if (typeMappingEntry.isAnonymous()) {
                    wrapped = true;
                }
            }
        }
        
        return wrapped;
    }

    private boolean isInputPresentForMEP(String MEP) {
        return WSDLConstants.MEP_URI_IN_ONLY.equals(MEP) ||
                WSDLConstants.MEP_URI_IN_OPTIONAL_OUT.equals(MEP) ||
                WSDLConstants.MEP_URI_OUT_OPTIONAL_IN.equals(MEP) ||
                WSDLConstants.MEP_URI_ROBUST_OUT_ONLY.equals(MEP) ||
                WSDLConstants.MEP_URI_ROBUST_IN_ONLY.equals(MEP) ||
                WSDLConstants.MEP_URI_IN_OUT.equals(MEP);
    }

    protected Element getInputElement(Document doc, AxisOperation operation, List headerParameterQNameList) {
        return getElement(doc, "input", operation.getMessage(WSDLConstants.MESSAGE_LABEL_IN_VALUE), isWrapped(operation), headerParameterQNameList);
    }
    
    protected Element getOutputElement(Document doc, AxisOperation operation, List headerParameterQNameList) {
        return getElement(doc, "output", operation.getMessage(WSDLConstants.MESSAGE_LABEL_OUT_VALUE), isWrapped(operation), headerParameterQNameList);
    }

    protected Element getElement(Document doc, String elementName, AxisMessage message, boolean wrapped, List headerParameterQNameList) {
        Element element = doc.createElement(elementName);

        List parameterElementList = getParameterElementList(doc, message, wrapped);
        for (int i = 0; i < parameterElementList.size(); i++) {
            element.appendChild((Element) parameterElementList.get(i));
        }

        List outputElementList = getParameterElementList(doc, headerParameterQNameList, "header");

        for (int i = 0; i < outputElementList.size(); i++) {
            element.appendChild((Element) outputElementList.get(i));
        }

        return element;
    }
    
    protected void writeInterface(boolean writeDatabinders) throws Exception {
        Document interfaceModel = createDOMDocumentForInterface(writeDatabinders);
        if (!codegenConfiguration.getOutputLocation().exists()) {
            codegenConfiguration.getOutputLocation().mkdirs();
        }
        InterfaceWriter interfaceWriter = new InterfaceWriter(this.codegenConfiguration
            .getOutputLocation(), this.codegenConfiguration.getOutputLanguage());

        String packageName = interfaceModel.getDocumentElement().getAttribute("package");
        String className = interfaceModel.getDocumentElement().getAttribute("name");

        System.out.println(">>  Generating Java class " + packageName + "." + className);
        File outputFile = FileWriter.createClassFile(this.codegenConfiguration.getOutputLocation(),
                                                     packageName, className, ".java");
        if (outputFile.exists()) {
            outputFile.delete();
        }

        writeClass(interfaceModel, interfaceWriter);
    }

    protected String makeJavaClassName(String word) {
        return XMLNameUtil.getJavaNameFromXMLName(word, true);
    }
    
}