aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.xml.internal.bind.v2.runtime.property; aoqi@0: aoqi@0: import java.lang.reflect.Constructor; aoqi@0: import java.lang.reflect.InvocationTargetException; aoqi@0: import java.util.Collection; aoqi@0: aoqi@0: import com.sun.xml.internal.bind.v2.model.core.ID; aoqi@0: import com.sun.xml.internal.bind.v2.model.core.PropertyKind; aoqi@0: import com.sun.xml.internal.bind.v2.model.runtime.RuntimeAttributePropertyInfo; aoqi@0: import com.sun.xml.internal.bind.v2.model.runtime.RuntimeElementPropertyInfo; aoqi@0: import com.sun.xml.internal.bind.v2.model.runtime.RuntimeNonElement; aoqi@0: import com.sun.xml.internal.bind.v2.model.runtime.RuntimePropertyInfo; aoqi@0: import com.sun.xml.internal.bind.v2.model.runtime.RuntimeTypeInfo; aoqi@0: import com.sun.xml.internal.bind.v2.model.runtime.RuntimeValuePropertyInfo; aoqi@0: import com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl; aoqi@0: aoqi@0: /** aoqi@0: * Create {@link Property} objects. aoqi@0: * aoqi@0: * @author Kohsuke Kawaguchi (kk@kohsuke.org) aoqi@0: */ aoqi@0: public abstract class PropertyFactory { aoqi@0: private PropertyFactory() {} aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * Constructors of the {@link Property} implementation. aoqi@0: */ aoqi@0: private static final Constructor[] propImpls; aoqi@0: aoqi@0: static { aoqi@0: Class[] implClasses = new Class[] { aoqi@0: SingleElementLeafProperty.class, aoqi@0: null, // single reference leaf --- but there's no such thing as "reference leaf" aoqi@0: null, // no such thing as "map leaf" aoqi@0: aoqi@0: ArrayElementLeafProperty.class, aoqi@0: null, // array reference leaf --- but there's no such thing as "reference leaf" aoqi@0: null, // no such thing as "map leaf" aoqi@0: aoqi@0: SingleElementNodeProperty.class, aoqi@0: SingleReferenceNodeProperty.class, aoqi@0: SingleMapNodeProperty.class, aoqi@0: aoqi@0: ArrayElementNodeProperty.class, aoqi@0: ArrayReferenceNodeProperty.class, aoqi@0: null, // map is always a single property (Map doesn't implement Collection) aoqi@0: }; aoqi@0: aoqi@0: propImpls = new Constructor[implClasses.length]; aoqi@0: for( int i=0; i c = propImpls[(isLeaf?0:6)+(isCollection?3:0)+kind.propertyIndex]; aoqi@0: try { aoqi@0: return c.newInstance( grammar, info ); aoqi@0: } catch (InstantiationException e) { aoqi@0: throw new InstantiationError(e.getMessage()); aoqi@0: } catch (IllegalAccessException e) { aoqi@0: throw new IllegalAccessError(e.getMessage()); aoqi@0: } catch (InvocationTargetException e) { aoqi@0: Throwable t = e.getCause(); aoqi@0: if(t instanceof Error) aoqi@0: throw (Error)t; aoqi@0: if(t instanceof RuntimeException) aoqi@0: throw (RuntimeException)t; aoqi@0: aoqi@0: throw new AssertionError(t); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Look for the case that can be optimized as a leaf, aoqi@0: * which is a kind of type whose XML representation is just PCDATA. aoqi@0: */ aoqi@0: static boolean isLeaf(RuntimePropertyInfo info) { aoqi@0: Collection types = info.ref(); aoqi@0: if(types.size()!=1) return false; aoqi@0: aoqi@0: RuntimeTypeInfo rti = types.iterator().next(); aoqi@0: if(!(rti instanceof RuntimeNonElement)) return false; aoqi@0: aoqi@0: if(info.id()==ID.IDREF) aoqi@0: // IDREF is always handled as leaf -- Transducer maps IDREF String back to an object aoqi@0: return true; aoqi@0: aoqi@0: if(((RuntimeNonElement)rti).getTransducer()==null) aoqi@0: // Transducer!=null means definitely binds to PCDATA. aoqi@0: // even if transducer==null, a referene might be IDREF, aoqi@0: // in which case it will still produce PCDATA in this reference. aoqi@0: return false; aoqi@0: aoqi@0: if(!info.getIndividualType().equals(rti.getType())) aoqi@0: return false; aoqi@0: aoqi@0: return true; aoqi@0: } aoqi@0: }