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:
037: package com.sun.xml.bind.v2.runtime.property;
038:
039: import java.lang.reflect.Constructor;
040: import java.lang.reflect.InvocationTargetException;
041: import java.util.Collection;
042:
043: import com.sun.xml.bind.v2.model.core.ID;
044: import com.sun.xml.bind.v2.model.core.PropertyKind;
045: import com.sun.xml.bind.v2.model.runtime.RuntimeAttributePropertyInfo;
046: import com.sun.xml.bind.v2.model.runtime.RuntimeElementPropertyInfo;
047: import com.sun.xml.bind.v2.model.runtime.RuntimeNonElement;
048: import com.sun.xml.bind.v2.model.runtime.RuntimePropertyInfo;
049: import com.sun.xml.bind.v2.model.runtime.RuntimeTypeInfo;
050: import com.sun.xml.bind.v2.model.runtime.RuntimeValuePropertyInfo;
051: import com.sun.xml.bind.v2.runtime.JAXBContextImpl;
052:
053: /**
054: * Create {@link Property} objects.
055: *
056: * @author Kohsuke Kawaguchi (kk@kohsuke.org)
057: */
058: public abstract class PropertyFactory {
059: private PropertyFactory() {
060: }
061:
062: /**
063: * Constructors of the {@link Property} implementation.
064: */
065: private static final Constructor<? extends Property>[] propImpls;
066:
067: static {
068: Class<? extends Property>[] implClasses = new Class[] {
069: SingleElementLeafProperty.class,
070: null, // single reference leaf --- but there's no such thing as "reference leaf"
071: null, // no such thing as "map leaf"
072:
073: ArrayElementLeafProperty.class,
074: null, // array reference leaf --- but there's no such thing as "reference leaf"
075: null, // no such thing as "map leaf"
076:
077: SingleElementNodeProperty.class,
078: SingleReferenceNodeProperty.class,
079: SingleMapNodeProperty.class,
080:
081: ArrayElementNodeProperty.class,
082: ArrayReferenceNodeProperty.class, null, // map is always a single property (Map doesn't implement Collection)
083: };
084:
085: propImpls = new Constructor[implClasses.length];
086: for (int i = 0; i < propImpls.length; i++) {
087: if (implClasses[i] != null)
088: // this pointless casting necessary for Mustang
089: propImpls[i] = (Constructor) implClasses[i]
090: .getConstructors()[0];
091: }
092: }
093:
094: /**
095: * Creates/obtains a properly configured {@link Property}
096: * object from the given description.
097: */
098: public static Property create(JAXBContextImpl grammar,
099: RuntimePropertyInfo info) {
100:
101: PropertyKind kind = info.kind();
102:
103: switch (kind) {
104: case ATTRIBUTE:
105: return new AttributeProperty(grammar,
106: (RuntimeAttributePropertyInfo) info);
107: case VALUE:
108: return new ValueProperty(grammar,
109: (RuntimeValuePropertyInfo) info);
110: case ELEMENT:
111: if (((RuntimeElementPropertyInfo) info).isValueList())
112: return new ListElementProperty(grammar,
113: (RuntimeElementPropertyInfo) info);
114: break;
115: case REFERENCE:
116: case MAP:
117: break;
118: default:
119: assert false;
120: }
121:
122: boolean isCollection = info.isCollection();
123: boolean isLeaf = isLeaf(info);
124:
125: Constructor<? extends Property> c = propImpls[(isLeaf ? 0 : 6)
126: + (isCollection ? 3 : 0) + kind.propertyIndex];
127: try {
128: return c.newInstance(grammar, info);
129: } catch (InstantiationException e) {
130: throw new InstantiationError(e.getMessage());
131: } catch (IllegalAccessException e) {
132: throw new IllegalAccessError(e.getMessage());
133: } catch (InvocationTargetException e) {
134: Throwable t = e.getCause();
135: if (t instanceof Error)
136: throw (Error) t;
137: if (t instanceof RuntimeException)
138: throw (RuntimeException) t;
139:
140: throw new AssertionError(t);
141: }
142: }
143:
144: /**
145: * Look for the case that can be optimized as a leaf,
146: * which is a kind of type whose XML representation is just PCDATA.
147: */
148: static boolean isLeaf(RuntimePropertyInfo info) {
149: Collection<? extends RuntimeTypeInfo> types = info.ref();
150: if (types.size() != 1)
151: return false;
152:
153: RuntimeTypeInfo rti = types.iterator().next();
154: if (!(rti instanceof RuntimeNonElement))
155: return false;
156:
157: if (info.id() == ID.IDREF)
158: // IDREF is always handled as leaf -- Transducer maps IDREF String back to an object
159: return true;
160:
161: if (((RuntimeNonElement) rti).getTransducer() == null)
162: // Transducer!=null means definitely binds to PCDATA.
163: // even if transducer==null, a referene might be IDREF,
164: // in which case it will still produce PCDATA in this reference.
165: return false;
166:
167: if (!info.getIndividualType().equals(rti.getType()))
168: return false;
169:
170: return true;
171: }
172: }
|