summaryrefslogtreecommitdiffstats
path: root/sandbox/axis2-1.4/tools/wsdl2java/src/main/java/org/apache/tuscany/tools/wsdl2java/generate/JavaInterfaceEmitter.java
blob: 08c71312085d335aee8d3b82e09f389ae7a59c23 (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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 static org.apache.tuscany.tools.wsdl2java.util.XMLNameUtil.getJavaNameFromXMLName;

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

import javax.xml.namespace.QName;

import org.apache.axis2.description.AxisBindingOperation;
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.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;

    @Override
    public void setCodeGenConfiguration(CodeGenConfiguration configuration) {
        super.setCodeGenConfiguration(configuration);
        codegenConfiguration = configuration;
    }

    @Override
    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 = typeMappingEntry.getPropertyClassNames();
                if(typeMappings == null) {
                    typeMappings = new ArrayList();
                    typeMappings.add(typeMappingEntry.getClassName());
                }
            } 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)) {
                if (typeMapper.getDefaultMappingName().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;
    }

    @Override
    public 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;
    }

    @Override
    protected Element getInputElement(Document doc, AxisBindingOperation operation, List headerParameterQNameList) {
        return getElement(doc,
                          "input",
                          operation.getAxisOperation().getMessage(WSDLConstants.MESSAGE_LABEL_IN_VALUE),
                          operation.getAxisOperation().getMessage(WSDLConstants.MESSAGE_LABEL_IN_VALUE).isWrapped(),
                          headerParameterQNameList);
    }

    @Override
    protected Element getOutputElement(Document doc, AxisBindingOperation operation, List headerParameterQNameList) {
        return getElement(doc,
                          "output",
                          operation.getAxisOperation().getMessage(WSDLConstants.MESSAGE_LABEL_OUT_VALUE),
                          operation.getAxisOperation().getMessage(WSDLConstants.MESSAGE_LABEL_OUT_VALUE).isWrapped(),
                          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;
    }

    @Override
    protected void writeInterface(boolean writeDatabinders) throws Exception {
        Document interfaceModel = createDOMDocumentForInterface(writeDatabinders);
        if (!codegenConfiguration.getOutputLocation().exists()) {
            codegenConfiguration.getOutputLocation().mkdirs();
        }
        InterfaceWriter interfaceWriter =
            new RemotableInterfaceWritter(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();
        }

//      JIRA TUSCANY-1561 Port to Axis2 1.3                
//        writeClass(interfaceModel, interfaceWriter);

        writeFile(interfaceModel, interfaceWriter);
    }

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

    @Override
    protected Element[] getFaultParamElements(Document doc, AxisOperation operation) {
        ArrayList params = new ArrayList();
        ArrayList faultMessages = operation.getFaultMessages();

        if (faultMessages != null && !faultMessages.isEmpty()) {
            Element paramElement;
            AxisMessage msg;
            for (int i = 0; i < faultMessages.size(); i++) {
                paramElement = doc.createElement("param");
                msg = (AxisMessage)faultMessages.get(i);
                String msgClassName = WSDL2JavaGenerator.normalizeClassName(msg.getName());
                addAttribute(doc, "name", msgClassName, paramElement);
                params.add(paramElement);
            }

            return (Element[])params.toArray(new Element[params.size()]);
        } else {
            return new Element[] {};// return empty array
        }
    }
}