001: /*
002: * Portions Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.tools.internal.ws.processor.generator;
027:
028: import com.sun.tools.internal.ws.processor.model.*;
029: import com.sun.tools.internal.ws.processor.model.java.JavaInterface;
030: import com.sun.tools.internal.ws.processor.model.java.JavaStructureMember;
031: import com.sun.tools.internal.ws.processor.model.jaxb.JAXBProperty;
032: import com.sun.tools.internal.ws.processor.util.DirectoryUtil;
033: import com.sun.tools.internal.ws.processor.util.ProcessorEnvironment;
034: import com.sun.tools.internal.ws.util.ClassNameInfo;
035: import com.sun.xml.internal.ws.streaming.PrefixFactory;
036: import com.sun.xml.internal.ws.streaming.PrefixFactoryImpl;
037: import com.sun.xml.internal.ws.util.StringUtils;
038: import com.sun.xml.internal.bind.api.JAXBRIContext;
039:
040: import javax.xml.namespace.QName;
041: import java.io.File;
042: import java.util.HashMap;
043: import java.util.Map;
044:
045: /**
046: * Names provides utility methods used by other wscompile classes
047: * for dealing with identifiers.
048: *
049: * @author WS Development Team
050: */
051: public class Names implements GeneratorConstants {
052:
053: public Names() {
054: }
055:
056: public String stubFor(Port port, String infix) {
057: String result = (String) port
058: .getProperty(ModelProperties.PROPERTY_STUB_CLASS_NAME);
059: if (result == null) {
060: result = makeDerivedClassName(port.getJavaInterface(),
061: STUB_SUFFIX, infix);
062: }
063: return result;
064: }
065:
066: public String tieFor(Port port, String infix) {
067: String result = (String) port
068: .getProperty(ModelProperties.PROPERTY_TIE_CLASS_NAME);
069: if (result == null) {
070: result = makeDerivedClassName(port.getJavaInterface(),
071: TIE_SUFFIX, infix);
072: }
073: return result;
074: }
075:
076: public String makeDerivedClassName(JavaInterface javaInterface,
077: String suffix, String infix) {
078:
079: String name = ClassNameInfo.replaceInnerClassSym(javaInterface
080: .getRealName());
081: return name + (infix == null ? "" : UNDERSCORE + infix)
082: + suffix;
083: }
084:
085: public static String getPortName(Port port) {
086: String javaPortName = (String) port
087: .getProperty(ModelProperties.PROPERTY_JAVA_PORT_NAME);
088: if (javaPortName != null) {
089: return javaPortName;
090: } else {
091: QName portName = (QName) port
092: .getProperty(ModelProperties.PROPERTY_WSDL_PORT_NAME);
093: if (portName != null) {
094: return portName.getLocalPart();
095: } else {
096: String name = stripQualifier(port.getJavaInterface()
097: .getName());
098: return ClassNameInfo.replaceInnerClassSym(name);
099: }
100: }
101: }
102:
103: public static String stripQualifier(Class classObj) {
104: String name = classObj.getName();
105: return stripQualifier(name);
106: }
107:
108: public static String stripQualifier(String name) {
109: return ClassNameInfo.getName(name);
110: }
111:
112: public static String getPackageName(String className) {
113: String packageName = ClassNameInfo.getQualifier(className);
114: return packageName != null ? packageName : "";
115: }
116:
117: public static String getUnqualifiedClassName(String className) {
118: return ClassNameInfo.getName(className).replace('$', '.');
119: }
120:
121: /**
122: * Return the File object that should be used as the source file
123: * for the given Java class, using the supplied destination
124: * directory for the top of the package hierarchy.
125: */
126: public File sourceFileForClass(String className,
127: String outputClassName, File destDir,
128: ProcessorEnvironment env) throws GeneratorException {
129: File packageDir = DirectoryUtil.getOutputDirectoryFor(
130: className, destDir, env);
131: String outputName = stripQualifier(outputClassName);
132:
133: String outputFileName = outputName + JAVA_SRC_SUFFIX;
134: return new File(packageDir, outputFileName);
135: }
136:
137: public static String getPackageName(Service service) {
138: String portPackage = getPackageName(service.getJavaInterface()
139: .getName());
140: return portPackage;
141: }
142:
143: public String customJavaTypeClassName(JavaInterface intf) {
144: String intName = intf.getName();
145: return intName;
146: }
147:
148: public String customExceptionClassName(Fault fault) {
149: String typeName = fault.getJavaException().getName();
150: return typeName;
151: }
152:
153: public String getExceptionClassMemberName() {
154: return FAULT_CLASS_MEMBER_NAME;
155: }
156:
157: public String getQNameName(QName name) {
158: String qname = getPrefix(name) + UNDERSCORE
159: + name.getLocalPart() + QNAME_SUFFIX;
160: return validInternalJavaIdentifier(qname);
161: }
162:
163: public String getBlockQNameName(Operation operation, Block block) {
164: QName blockName = block.getName();
165: String qname = getPrefix(blockName);
166: if (operation != null)
167: qname += UNDERSCORE + operation.getUniqueName();
168: qname += UNDERSCORE + blockName.getLocalPart() + QNAME_SUFFIX;
169: return validInternalJavaIdentifier(qname);
170: }
171:
172: public String getTypeQName(QName name) {
173: String qname = getPrefix(name) + UNDERSCORE
174: + name.getLocalPart() + TYPE_QNAME_SUFFIX;
175: return validInternalJavaIdentifier(qname);
176: }
177:
178: /* (non-Javadoc)
179: * @see Names#validJavaClassName(String)
180: */
181: public String validJavaClassName(String name) {
182: return JAXBRIContext.mangleNameToClassName(name);
183: }
184:
185: /* (non-Javadoc)
186: * @see Names#validJavaMemberName(String)
187: */
188: public String validJavaMemberName(String name) {
189: return JAXBRIContext.mangleNameToVariableName(name);
190: }
191:
192: public String validInternalJavaIdentifier(String name) {
193: // return a valid java identifier without dropping characters (i.e. do not apply
194: // the mapping of XML names to Java identifiers in the spec); it's only meant
195: // to be used to generate internal identifiers (e.g. variable names in generated code)
196:
197: StringBuffer sb = new StringBuffer();
198: for (int i = 0; i < name.length(); ++i) {
199: char ch = name.charAt(i);
200: if (i == 0) {
201: if (Character.isJavaIdentifierStart(ch)) {
202: sb.append(ch);
203: } else {
204: sb.append("_$");
205: sb.append(Integer.toHexString((int) ch));
206: sb.append("$");
207: }
208: } else {
209: if (Character.isJavaIdentifierPart(ch)) {
210: sb.append(ch);
211: } else {
212: sb.append("$");
213: sb.append(Integer.toHexString((int) ch));
214: sb.append("$");
215: }
216: }
217: }
218:
219: String id = sb.toString();
220:
221: String tmp = (String) reservedWords.get(id);
222: if (tmp != null)
223: id = tmp;
224: return id;
225: }
226:
227: public String validExternalJavaIdentifier(String name) {
228: return validInternalJavaIdentifier(name).replace('$', '_');
229: }
230:
231: public String validJavaName(String name) {
232: name = wordBreakString(name);
233: name = removeWhiteSpace(name);
234:
235: String tmp = (String) reservedWords.get(name);
236: if (tmp != null)
237: name = tmp;
238: return name;
239: }
240:
241: public boolean isJavaReservedWord(String name) {
242: return reservedWords.get(name) != null;
243: }
244:
245: /* here we check on wether return values datatype is
246: boolean. If its boolean, instead of a get method
247: its set a is<MethodName> to comply with JavaBeans
248: Pattern spec */
249: public String getJavaMemberReadMethod(JavaStructureMember member) {
250: String return_value = null;
251: if ((member.getType().getRealName()) == "boolean") {
252: return_value = IS
253: + StringUtils.capitalize(member.getName());
254: } else {
255: return_value = GET
256: + StringUtils.capitalize(member.getName());
257: }
258: return (return_value);
259: }
260:
261: public String getJavaMemberWriteMethod(JavaStructureMember member) {
262: return SET + StringUtils.capitalize(member.getName());
263: }
264:
265: public static String getResponseName(String messageName) {
266: return messageName + RESPONSE;
267: }
268:
269: public String getJavaReadMethod(JAXBProperty prop) {
270: if (prop.getType().getName().equals("boolean"))
271: return IS + StringUtils.capitalize(prop.getName());
272: return getJavaReadMethod(prop.getName());
273: }
274:
275: public String getJavaWriteMethod(JAXBProperty prop) {
276: return getJavaWriteMethod(prop.getName());
277: }
278:
279: public String getJavaReadMethod(String prop) {
280: return GET + StringUtils.capitalize(prop);
281: }
282:
283: public String getJavaWriteMethod(String prop) {
284: return SET + StringUtils.capitalize(prop);
285: }
286:
287: public String removeWhiteSpace(String str) {
288: String tmp = removeCharacter(' ', str);
289: return tmp;
290: }
291:
292: public String wordBreakString(String str) {
293: StringBuffer buf = new StringBuffer(str);
294: char ch;
295: for (int i = 0; i < buf.length(); i++) {
296: ch = buf.charAt(i);
297: if (Character.isDigit(ch)) {
298: if (i + 1 < buf.length()
299: && !Character.isDigit(buf.charAt(i + 1))) {
300: buf.insert(1 + i++, ' ');
301: }
302: } else if (Character.isSpaceChar(ch) || ch == '_') {
303: continue;
304: } else if (!Character.isJavaIdentifierPart(ch)) {
305: buf.setCharAt(i, ' ');
306: } else if (!Character.isLetter(ch)) {
307: buf.setCharAt(i, ' ');
308: }
309: }
310: return buf.toString();
311: }
312:
313: public String removeCharacter(int ch, String str) {
314: String tmp;
315: int idx = str.indexOf(ch);
316: while (idx >= 0) {
317: str = str.substring(0, idx)
318: + StringUtils.capitalize(str.substring(idx + 1)
319: .trim());
320: idx = str.indexOf(' ');
321: }
322:
323: return str;
324: }
325:
326: public String getPrefix(QName name) {
327: return getPrefix(name.getNamespaceURI());
328: }
329:
330: public String getPrefix(String uri) {
331: return prefixFactory.getPrefix(uri);
332: }
333:
334: public void resetPrefixFactory() {
335: prefixFactory = new PrefixFactoryImpl(NS_PREFIX);
336: }
337:
338: public void setSerializerNameInfix(String serNameInfix) {
339: if (serNameInfix != null && serNameInfix.length() > 0)
340: serializerNameInfix = UNDERSCORE + serNameInfix;
341: }
342:
343: public String getSerializerNameInfix() {
344: // Fix for bug 4811625 and 4778136, undoing what setter does (remove beginning underscore)
345: String str = serializerNameInfix;
346: if ((serializerNameInfix != null)
347: && (serializerNameInfix.charAt(0) == '_'))
348: str = serializerNameInfix.substring(1);
349: return str;
350: }
351:
352: protected String serializerNameInfix = null;
353: protected PrefixFactory prefixFactory = new PrefixFactoryImpl(
354: NS_PREFIX);
355: protected static Map reservedWords;
356:
357: static {
358: reservedWords = new HashMap();
359: reservedWords.put("abstract", "_abstract");
360: reservedWords.put("assert", "_assert");
361: reservedWords.put("boolean", "_boolean");
362: reservedWords.put("break", "_break");
363: reservedWords.put("byte", "_byte");
364: reservedWords.put("case", "_case");
365: reservedWords.put("catch", "_catch");
366: reservedWords.put("char", "_char");
367: reservedWords.put("class", "_class");
368: reservedWords.put("const", "_const");
369: reservedWords.put("continue", "_continue");
370: reservedWords.put("default", "_default");
371: reservedWords.put("do", "_do");
372: reservedWords.put("double", "_double");
373: reservedWords.put("else", "_else");
374: reservedWords.put("extends", "_extends");
375: reservedWords.put("false", "_false");
376: reservedWords.put("final", "_final");
377: reservedWords.put("finally", "_finally");
378: reservedWords.put("float", "_float");
379: reservedWords.put("for", "_for");
380: reservedWords.put("goto", "_goto");
381: reservedWords.put("if", "_if");
382: reservedWords.put("implements", "_implements");
383: reservedWords.put("import", "_import");
384: reservedWords.put("instanceof", "_instanceof");
385: reservedWords.put("int", "_int");
386: reservedWords.put("interface", "_interface");
387: reservedWords.put("long", "_long");
388: reservedWords.put("native", "_native");
389: reservedWords.put("new", "_new");
390: reservedWords.put("null", "_null");
391: reservedWords.put("package", "_package");
392: reservedWords.put("private", "_private");
393: reservedWords.put("protected", "_protected");
394: reservedWords.put("public", "_public");
395: reservedWords.put("return", "_return");
396: reservedWords.put("short", "_short");
397: reservedWords.put("static", "_static");
398: reservedWords.put("strictfp", "_strictfp");
399: reservedWords.put("super", "_super");
400: reservedWords.put("switch", "_switch");
401: reservedWords.put("synchronized", "_synchronized");
402: reservedWords.put("this", "_this");
403: reservedWords.put("throw", "_throw");
404: reservedWords.put("throws", "_throws");
405: reservedWords.put("transient", "_transient");
406: reservedWords.put("true", "_true");
407: reservedWords.put("try", "_try");
408: reservedWords.put("void", "_void");
409: reservedWords.put("volatile", "_volatile");
410: reservedWords.put("while", "_while");
411: reservedWords.put("enum", "_enum");
412: }
413: }
|