001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package com.sun.j2mews.sg;
028:
029: import java.io.*;
030: import java.util.*;
031: import java.text.*;
032:
033: import com.sun.xml.rpc.processor.*;
034: import com.sun.xml.rpc.processor.model.*;
035: import com.sun.xml.rpc.processor.config.Configuration;
036: import com.sun.xml.rpc.processor.model.Service;
037: import com.sun.xml.rpc.processor.ProcessorOptions;
038: import com.sun.xml.rpc.processor.model.Model;
039: import com.sun.xml.rpc.processor.model.AbstractType;
040: import com.sun.xml.rpc.processor.model.java.*;
041: import com.sun.xml.rpc.util.localization.*;
042:
043: /**
044: * MakeCldc1_0 will take the internal data structures and coerce them
045: * to be CLDC 1.0 compliant (float and double become String).
046: *
047: */
048: public class MakeCldc1_0 implements ProcessorAction {
049: protected com.sun.xml.rpc.processor.util.ProcessorEnvironment env;
050: private Map originalTypes;
051:
052: protected LocalizableMessageFactory messageFactory;
053:
054: public MakeCldc1_0(Map originalTypes) {
055: messageFactory = new LocalizableMessageFactory(
056: "com.sun.j2mews.sg.stubgenerator");
057: this .originalTypes = originalTypes;
058: }
059:
060: public void setEnvironment(
061: com.sun.xml.rpc.processor.util.ProcessorEnvironment e) {
062: env = e;
063: }
064:
065: public void perform(Model model, Configuration config,
066: Properties options) {
067: //env = config.getEnvironment();
068: for (Iterator it = model.getServices(); it.hasNext();) {
069: Service service = (Service) it.next();
070: mungeTo1_0(service);
071: }
072: }
073:
074: public void mungeTo1_0(Service service) {
075: for (Iterator it = service.getPorts(); it.hasNext();) {
076: Port port = (Port) it.next();
077: mungeTo1_0(port);
078: }
079: }
080:
081: protected void mungeTo1_0(Port port) {
082: for (Iterator operations = port.getOperations(); operations
083: .hasNext();) {
084: Operation operation = (Operation) operations.next();
085: mungeTo1_0(operation, port);
086: }
087: }
088:
089: protected void mungeTo1_0(Operation operation, Port port) {
090: JavaMethod method = operation.getJavaMethod();
091: //System.out.println("mungeTo1_0: method.getName="+method.getName());
092: JavaType returnType = method.getReturnType();
093: mungeTo1_0(returnType, port);
094: for (Iterator parameters = method.getParameters(); parameters
095: .hasNext();) {
096: JavaParameter parameter = (JavaParameter) parameters.next();
097: JavaType parameterType = parameter.getType();
098: mungeTo1_0(parameterType, port);
099: }
100: for (Iterator faults = operation.getFaults(); faults.hasNext();) {
101: Fault fault = (Fault) faults.next();
102: Block faultBlock = fault.getBlock();
103: JavaException jexcep = fault.getJavaException();
104: mungeTo1_0(jexcep, port);
105: }
106: }
107:
108: protected void mungeTo1_0(JavaType type, Port port) {
109: //System.out.println("mungeTo1_0: type="+type);
110: if (type == null)
111: return;
112: if (type instanceof JavaSimpleType) {
113: String typeName = javaTypeToString(type, port).intern();
114: //System.out.println("typeName="+typeName);
115: if (typeName == "float" || typeName == "Float"
116: || typeName == "java.lang.Float"
117: || typeName == "double" || typeName == "Double"
118: || typeName == "java.lang.Double") {
119: //System.out.println("Found odd type: "+typeName+" name="+type.getName());
120: originalTypes.put(type, typeName);
121: type.doSetName("java.lang.String");
122: }
123: } else if (type instanceof JavaStructureType) {
124: JavaStructureType jst = (JavaStructureType) type;
125: for (Iterator members = jst.getMembers(); members.hasNext();) {
126: JavaStructureMember jsm = (JavaStructureMember) members
127: .next();
128: JavaType memberType = jsm.getType();
129: mungeTo1_0(memberType, port);
130: }
131: } else if (type instanceof JavaArrayType) {
132: JavaArrayType jat = (JavaArrayType) type;
133: JavaType elementType = jat.getElementType();
134: mungeTo1_0(elementType, port);
135: if (originalTypes.containsKey(elementType)) {
136: String typeName = javaTypeToString(type, port);
137: //System.out.println("Changing array type, was: "+typeName);
138: originalTypes.put(type, typeName);
139: jat.doSetName(elementType.getName() + "[]");
140: }
141: } else if (type instanceof JavaEnumerationType) {
142: JavaEnumerationType jet = (JavaEnumerationType) type;
143: JavaType baseType = jet.getBaseType();
144: mungeTo1_0(baseType, port);
145: } else {
146: onWarning(getMessage("stubgenerator.unknownType",
147: "mungeTo1_0", type.toString()));
148: }
149: }
150:
151: protected String javaTypeToString(JavaType type, Port port) {
152: if (type == null)
153: return null;
154: String result;
155: if (type.isHolder())
156: result = env.getNames().holderClassName(port, type);
157: else
158: result = env.getNames().typeClassName(type);
159: return result;
160: }
161:
162: protected void onWarning(Localizable msg) {
163: //System.err.println("Warning: "+msg);
164: env.warn(msg);
165: }
166:
167: protected Localizable getMessage(String key, String arg) {
168: return messageFactory.getMessage(key, new Object[] { arg });
169: }
170:
171: protected Localizable getMessage(String key, String arg1,
172: String arg2) {
173: return messageFactory.getMessage(key,
174: new Object[] { arg1, arg2 });
175: }
176: }
|