001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.websvc.rest.codegen;
042:
043: import com.sun.source.tree.ClassTree;
044: import java.io.IOException;
045: import org.netbeans.api.java.source.JavaSource;
046: import org.netbeans.api.java.source.ModificationResult;
047: import org.netbeans.api.java.source.WorkingCopy;
048: import org.netbeans.modules.websvc.rest.codegen.model.GenericResourceBean;
049: import org.netbeans.modules.websvc.rest.codegen.model.WsdlComponentBean;
050: import org.netbeans.modules.websvc.rest.support.AbstractTask;
051: import org.netbeans.modules.websvc.rest.support.JavaSourceHelper;
052: import org.netbeans.modules.websvc.rest.support.SourceGroupSupport;
053: import org.netbeans.modules.websvc.rest.wizard.Util;
054: import org.openide.filesystems.FileObject;
055:
056: /**
057: * Generator for JAXB XmlRoot JAXB output class annotated with XmlType.
058: *
059: * @author nam
060: */
061: public class XmlOutputWrapperGenerator {
062: private FileObject destDir;
063: private String className;
064: private String packageName;
065: private String wrapElementName;
066: private String[] jaxbOutputClassNames;
067: private JavaSource wrapperJS;
068:
069: public XmlOutputWrapperGenerator(FileObject destDir,
070: String className, String packageName,
071: String[] jaxbOutputClassNames) {
072: if (destDir == null || className == null
073: || jaxbOutputClassNames == null
074: || jaxbOutputClassNames.length == 0) {
075: throw new IllegalArgumentException();
076: }
077: this .destDir = destDir;
078: this .className = className;
079: this .packageName = packageName;
080: this .jaxbOutputClassNames = jaxbOutputClassNames;
081: wrapElementName = Util.lowerFirstChar(className);
082: if (wrapElementName
083: .endsWith(EntityResourcesGenerator.CONVERTER_SUFFIX)) {
084: wrapElementName = wrapElementName
085: .substring(
086: 0,
087: wrapElementName
088: .lastIndexOf(EntityResourcesGenerator.CONVERTER_SUFFIX));
089: }
090: }
091:
092: public FileObject generate() throws IOException {
093: FileObject wrapperFO = destDir.getFileObject(className,
094: Constants.JAVA_EXT);
095: if (wrapperFO != null) {
096: return wrapperFO;
097: }
098:
099: wrapperJS = JavaSourceHelper.createJavaSource(destDir,
100: packageName, className);
101:
102: ModificationResult result = wrapperJS
103: .runModificationTask(new AbstractTask<WorkingCopy>() {
104: public void run(WorkingCopy copy)
105: throws IOException {
106: copy.toPhase(JavaSource.Phase.RESOLVED);
107:
108: JavaSourceHelper.addImports(copy, new String[] {
109: Constants.XML_ELEMENT,
110: Constants.XML_ROOT_ELEMENT });
111:
112: JavaSourceHelper
113: .addClassAnnotation(
114: copy,
115: new String[] { Constants.XML_ROOT_ELEMENT_ANNOTATION },
116: new Object[] { JavaSourceHelper
117: .createAssignmentTree(
118: copy, "name",
119: wrapElementName) });
120:
121: ClassTree tree = JavaSourceHelper
122: .getTopLevelClassTree(copy);
123: ClassTree modifiedTree = tree;
124:
125: for (int i = 0; i < jaxbOutputClassNames.length; i++) {
126: String jaxbOutputClassName = jaxbOutputClassNames[i];
127: String fieldName = getSimpleName(jaxbOutputClassName);
128: fieldName = Util.lowerFirstChar(fieldName);
129: modifiedTree = addField(copy, modifiedTree,
130: fieldName, jaxbOutputClassName);
131: modifiedTree = addGetMethod(copy,
132: modifiedTree, fieldName,
133: jaxbOutputClassName);
134: modifiedTree = addSetMethod(copy,
135: modifiedTree, fieldName,
136: jaxbOutputClassName);
137: }
138:
139: copy.rewrite(tree, modifiedTree);
140: }
141: });
142: result.commit();
143: return wrapperJS.getFileObjects().iterator().next();
144: }
145:
146: private String getSimpleName(String qualifiedName) {
147: String name = qualifiedName;
148: int i = name.lastIndexOf('.');
149: if (i > -1)
150: name = name.substring(i + 1, name.length());
151: return name;
152: }
153:
154: private ClassTree addField(WorkingCopy copy, ClassTree tree,
155: String fieldName, String jaxbOutputClassName) {
156: return JavaSourceHelper.addField(copy, tree, Constants.PRIVATE,
157: null, null, fieldName, jaxbOutputClassName);
158: }
159:
160: private ClassTree addGetMethod(WorkingCopy copy, ClassTree tree,
161: String fieldName, String jaxbOutputClassName) {
162: String[] annotations = new String[] { Constants.XML_ELEMENT_ANNOTATION };
163: String methodName = "get" + Util.upperFirstChar(fieldName); //NOI18N
164: String bodyText = "{ return " + fieldName + "; }"; //NOI18N
165: String comment = "Get method for wrapped element \n" + //NOI18N
166: "@return an instance of " + jaxbOutputClassName; //NOI18N
167: return JavaSourceHelper.addMethod(copy, tree, Constants.PUBLIC,
168: annotations, null, methodName, jaxbOutputClassName,
169: null, null, null, null, bodyText, comment); //NOI18N
170: }
171:
172: private ClassTree addSetMethod(WorkingCopy copy, ClassTree tree,
173: String fieldName, String jaxbOutputClassName) {
174: String methodName = "set" + Util.upperFirstChar(fieldName); //NOI18N
175: String[] parameters = new String[] { fieldName };
176: Object[] paramTypes = new Object[] { jaxbOutputClassName };
177: String bodyText = "{ this.$FIELD$ = $FIELD$; }".replace(
178: "$FIELD$", fieldName); //NOI18N
179: String comment = "Set method for wrapped element of type "
180: + jaxbOutputClassName; //NOI18N
181: return JavaSourceHelper.addMethod(copy, tree, Constants.PUBLIC,
182: null, null, methodName, Constants.VOID, parameters,
183: paramTypes, null, null, bodyText, comment); //NOI18N
184: }
185: }
|