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.tools.internal.xjc.model; aoqi@0: aoqi@0: import java.util.AbstractList; aoqi@0: import java.util.ArrayList; aoqi@0: import java.util.List; aoqi@0: import java.util.Map; aoqi@0: aoqi@0: import javax.activation.MimeType; aoqi@0: import javax.xml.namespace.QName; aoqi@0: aoqi@0: import com.sun.tools.internal.xjc.model.nav.NClass; aoqi@0: import com.sun.tools.internal.xjc.model.nav.NType; aoqi@0: import com.sun.tools.internal.xjc.reader.RawTypeSet; aoqi@0: import com.sun.xml.internal.bind.v2.model.core.ElementPropertyInfo; 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.xsom.XSComponent; aoqi@0: aoqi@0: import org.xml.sax.Locator; aoqi@0: aoqi@0: /** aoqi@0: * {@link ElementPropertyInfo} for the compiler. aoqi@0: * aoqi@0: * @author Kohsuke Kawaguchi aoqi@0: */ aoqi@0: public final class CElementPropertyInfo extends CPropertyInfo implements ElementPropertyInfo { aoqi@0: aoqi@0: /** aoqi@0: * True if this property can never be absent legally. aoqi@0: */ aoqi@0: private final boolean required; aoqi@0: aoqi@0: private final MimeType expectedMimeType; aoqi@0: /** aoqi@0: * aoqi@0: *

aoqi@0: * Currently, this is set inside {@link RawTypeSet} in a very ugly way. aoqi@0: */ aoqi@0: private CAdapter adapter; aoqi@0: aoqi@0: private final boolean isValueList; aoqi@0: aoqi@0: private ID id; aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * List of referenced types. aoqi@0: */ aoqi@0: private final List types = new ArrayList(); aoqi@0: aoqi@0: private final List ref = new AbstractList() { aoqi@0: public CNonElement get(int index) { aoqi@0: return getTypes().get(index).getTarget(); aoqi@0: } aoqi@0: public int size() { aoqi@0: return getTypes().size(); aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: // TODO: shouldn't they get id and expectedMimeType from TypeUses of CTypeRef? aoqi@0: public CElementPropertyInfo(String name, CollectionMode collection, ID id, MimeType expectedMimeType, XSComponent source, aoqi@0: CCustomizations customizations, Locator locator, boolean required) { aoqi@0: super(name, collection.col, source, customizations, locator); aoqi@0: this.required = required; aoqi@0: this.id = id; aoqi@0: this.expectedMimeType = expectedMimeType; aoqi@0: this.isValueList = collection.val; aoqi@0: } aoqi@0: aoqi@0: public ID id() { aoqi@0: return id; aoqi@0: } aoqi@0: aoqi@0: public List getTypes() { aoqi@0: return types; aoqi@0: } aoqi@0: aoqi@0: public List ref() { aoqi@0: return ref; aoqi@0: } aoqi@0: aoqi@0: public QName getSchemaType() { aoqi@0: if(types.size()!=1) aoqi@0: // if more than one kind is here, can't produce @XmlSchemaType. aoqi@0: // TODO: is it allowed to have one generated if types aoqi@0: return null; aoqi@0: aoqi@0: CTypeRef t = types.get(0); aoqi@0: if(needsExplicitTypeName(t.getTarget(),t.typeName)) aoqi@0: return t.typeName; aoqi@0: else aoqi@0: return null; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * XJC never uses the wrapper element. Always return null. aoqi@0: */ aoqi@0: @Deprecated aoqi@0: public QName getXmlName() { aoqi@0: return null; aoqi@0: } aoqi@0: aoqi@0: public boolean isCollectionRequired() { aoqi@0: // in XJC, we never recognize a nillable collection pattern, so this is always false. aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: public boolean isCollectionNillable() { aoqi@0: // in XJC, we never recognize a nillable collection pattern, so this is always false. aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: public boolean isRequired() { aoqi@0: return required; aoqi@0: } aoqi@0: aoqi@0: public boolean isValueList() { aoqi@0: return isValueList; aoqi@0: } aoqi@0: aoqi@0: public boolean isUnboxable() { aoqi@0: if(!isCollection() && !required) aoqi@0: // if the property can be legally absent, it's not unboxable aoqi@0: return false; aoqi@0: // we need to have null to represent the absence of value. not unboxable. aoqi@0: for (CTypeRef t : getTypes()) { aoqi@0: if(t.isNillable()) aoqi@0: return false; aoqi@0: } aoqi@0: return super.isUnboxable(); aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public boolean isOptionalPrimitive() { aoqi@0: // we need to have null to represent the absence of value. not unboxable. aoqi@0: for (CTypeRef t : getTypes()) { aoqi@0: if(t.isNillable()) aoqi@0: return false; aoqi@0: } aoqi@0: return !isCollection() && !required && super.isUnboxable(); aoqi@0: } aoqi@0: aoqi@0: public V accept(CPropertyVisitor visitor) { aoqi@0: return visitor.onElement(this); aoqi@0: } aoqi@0: aoqi@0: public CAdapter getAdapter() { aoqi@0: return adapter; aoqi@0: } aoqi@0: aoqi@0: public void setAdapter(CAdapter a) { aoqi@0: assert adapter==null; aoqi@0: adapter = a; aoqi@0: } aoqi@0: aoqi@0: public final PropertyKind kind() { aoqi@0: return PropertyKind.ELEMENT; aoqi@0: } aoqi@0: aoqi@0: public MimeType getExpectedMimeType() { aoqi@0: return expectedMimeType; aoqi@0: } aoqi@0: aoqi@0: public static enum CollectionMode { aoqi@0: NOT_REPEATED(false,false), aoqi@0: REPEATED_ELEMENT(true,false), aoqi@0: REPEATED_VALUE(true,true); aoqi@0: aoqi@0: private final boolean col,val; aoqi@0: aoqi@0: CollectionMode(boolean col,boolean val) { aoqi@0: this.col = col; aoqi@0: this.val = val; aoqi@0: } aoqi@0: aoqi@0: public boolean isRepeated() { return col; } aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public QName collectElementNames(Map table) { aoqi@0: for (CTypeRef t : types) { aoqi@0: QName n = t.getTagName(); aoqi@0: if(table.containsKey(n)) return n; aoqi@0: table.put(n, this); aoqi@0: } aoqi@0: return null; aoqi@0: } aoqi@0: }