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 Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036: package com.sun.tools.xjc.util;
037:
038: import com.sun.codemodel.ClassType;
039: import com.sun.codemodel.JClassAlreadyExistsException;
040: import com.sun.codemodel.JClassContainer;
041: import com.sun.codemodel.JDefinedClass;
042: import com.sun.codemodel.JJavaName;
043: import com.sun.codemodel.JMod;
044: import com.sun.tools.xjc.ErrorReceiver;
045:
046: import org.xml.sax.Locator;
047: import org.xml.sax.SAXParseException;
048:
049: /**
050: * Create new {@link JDefinedClass} and report class collision errors,
051: * if necessary.
052: *
053: * This is just a helper class that simplifies the class name collision
054: * detection. This object maintains no state, so it is OK to use
055: * multiple instances of this.
056: *
057: * @author
058: * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
059: */
060: public final class CodeModelClassFactory {
061:
062: /** errors are reported to this object. */
063: private ErrorReceiver errorReceiver;
064:
065: /** unique id generator. */
066: private int ticketMaster = 0;
067:
068: public CodeModelClassFactory(ErrorReceiver _errorReceiver) {
069: this .errorReceiver = _errorReceiver;
070: }
071:
072: public JDefinedClass createClass(JClassContainer parent,
073: String name, Locator source) {
074: return createClass(parent, JMod.PUBLIC, name, source);
075: }
076:
077: public JDefinedClass createClass(JClassContainer parent, int mod,
078: String name, Locator source) {
079: return createClass(parent, mod, name, source, ClassType.CLASS);
080: }
081:
082: public JDefinedClass createInterface(JClassContainer parent,
083: String name, Locator source) {
084: return createInterface(parent, JMod.PUBLIC, name, source);
085: }
086:
087: public JDefinedClass createInterface(JClassContainer parent,
088: int mod, String name, Locator source) {
089: return createClass(parent, mod, name, source,
090: ClassType.INTERFACE);
091: }
092:
093: public JDefinedClass createClass(JClassContainer parent,
094: String name, Locator source, ClassType kind) {
095: return createClass(parent, JMod.PUBLIC, name, source, kind);
096: }
097:
098: public JDefinedClass createClass(JClassContainer parent, int mod,
099: String name, Locator source, ClassType kind) {
100:
101: if (!JJavaName.isJavaIdentifier(name)) {
102: // report the error
103: errorReceiver.error(new SAXParseException(Messages.format(
104: Messages.ERR_INVALID_CLASSNAME, name), source));
105: return createDummyClass(parent);
106: }
107:
108: try {
109: if (parent.isClass() && kind == ClassType.CLASS)
110: mod |= JMod.STATIC;
111:
112: JDefinedClass r = parent._class(mod, name, kind);
113: // use the metadata field to store the source location,
114: // so that we can report class name collision errors.
115: r.metadata = source;
116:
117: return r;
118: } catch (JClassAlreadyExistsException e) {
119: // class collision.
120: JDefinedClass cls = e.getExistingClass();
121:
122: // report the error
123: errorReceiver.error(new SAXParseException(Messages.format(
124: Messages.ERR_CLASSNAME_COLLISION, cls.fullName()),
125: (Locator) cls.metadata));
126: errorReceiver.error(new SAXParseException(Messages.format(
127: Messages.ERR_CLASSNAME_COLLISION_SOURCE, name),
128: source));
129:
130: if (!name.equals(cls.name())) {
131: // on Windows, FooBar and Foobar causes name collision
132: errorReceiver
133: .error(new SAXParseException(
134: Messages
135: .format(
136: Messages.ERR_CASE_SENSITIVITY_COLLISION,
137: name, cls.name()), null));
138: }
139:
140: if (Util.equals((Locator) cls.metadata, source)) {
141: errorReceiver
142: .error(new SAXParseException(
143: Messages
144: .format(Messages.ERR_CHAMELEON_SCHEMA_GONE_WILD),
145: source));
146: }
147:
148: return createDummyClass(parent);
149: }
150: }
151:
152: /**
153: * Create a dummy class to recover from an error.
154: *
155: * We won't generate the code, so the client will never see this class
156: * getting generated.
157: */
158: private JDefinedClass createDummyClass(JClassContainer parent) {
159: try {
160: return parent._class("$$$garbage$$$" + (ticketMaster++));
161: } catch (JClassAlreadyExistsException ee) {
162: return ee.getExistingClass();
163: }
164: }
165: }
|