001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.xjc.ts;
019:
020: import java.io.IOException;
021: import java.util.logging.Logger;
022:
023: import org.xml.sax.ErrorHandler;
024:
025: import com.sun.codemodel.JClass;
026: import com.sun.codemodel.JDefinedClass;
027: import com.sun.codemodel.JDocComment;
028: import com.sun.codemodel.JExpr;
029: import com.sun.codemodel.JFieldRef;
030: import com.sun.codemodel.JInvocation;
031: import com.sun.codemodel.JMethod;
032: import com.sun.codemodel.JMod;
033: import com.sun.tools.xjc.BadCommandLineException;
034: import com.sun.tools.xjc.Options;
035: import com.sun.tools.xjc.Plugin;
036: import com.sun.tools.xjc.outline.ClassOutline;
037: import com.sun.tools.xjc.outline.Outline;
038:
039: import org.apache.cxf.common.logging.LogUtils;
040: import org.apache.cxf.jaxb.JAXBToStringBuilder;
041: import org.apache.cxf.jaxb.JAXBToStringStyle;
042:
043: /**
044: * Modifies the JAXB code model to override the Object.toString() method with an
045: * implementation that provides a String representation of the xml content.
046: */
047: public class ToStringPlugin extends Plugin {
048:
049: private static final Logger LOG = LogUtils
050: .getL7dLogger(ToStringPlugin.class);
051:
052: private String styleFieldName = "DEFAULT_STYLE";
053:
054: public String getOptionName() {
055: return "Xts";
056: }
057:
058: public String getUsage() {
059: return " -Xts : Activate plugin to add a toString() method to generated classes\n"
060: + " -Xts:style:multiline : Have toString produce multi line output\n"
061: + " -Xts:style:simple : Have toString produce single line terse output\n";
062: }
063:
064: public int parseArgument(Options opt, String[] args, int index)
065: throws BadCommandLineException, IOException {
066: int ret = 0;
067: if (args[index].equals("-Xts:style:multiline")) {
068: styleFieldName = "MULTI_LINE_STYLE";
069: ret = 1;
070: } else if (args[index].equals("-Xts:style:simple")) {
071: styleFieldName = "SIMPLE_STYLE";
072: ret = 1;
073: }
074: return ret;
075: }
076:
077: public boolean run(Outline outline, Options opt,
078: ErrorHandler errorHandler) {
079: LOG.fine("Running toString() plugin.");
080:
081: final JClass toStringDelegateImpl = outline.getCodeModel().ref(
082: JAXBToStringBuilder.class);
083: final JClass styleClass = outline.getCodeModel().ref(
084: JAXBToStringStyle.class);
085: final JFieldRef toStringDelegateStyleParam = styleClass
086: .staticRef(styleFieldName);
087:
088: for (ClassOutline co : outline.getClasses()) {
089: addToStringMethod(co, toStringDelegateImpl,
090: toStringDelegateStyleParam);
091: }
092:
093: return true;
094: }
095:
096: private void addToStringMethod(ClassOutline co,
097: JClass delegateImpl, JFieldRef toStringDelegateStyleParam) {
098: final JDefinedClass implementation = co.implClass;
099: final JMethod toStringMethod = implementation.method(
100: JMod.PUBLIC, String.class, "toString");
101: final JInvocation invoke = delegateImpl.staticInvoke("valueOf");
102: invoke.arg(JExpr._this ());
103: invoke.arg(toStringDelegateStyleParam);
104: toStringMethod.body()._return(invoke);
105:
106: JDocComment doc = toStringMethod.javadoc();
107: doc
108: .add("Generates a String representation of the contents of this type.");
109: doc
110: .add("\nThis is an extension method, produced by the 'ts' xjc plugin");
111: toStringMethod.annotate(Override.class);
112: }
113: }
|