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.model.impl; aoqi@0: aoqi@0: import javax.xml.bind.annotation.XmlRootElement; aoqi@0: import javax.xml.bind.annotation.XmlSchema; aoqi@0: import javax.xml.bind.annotation.XmlType; aoqi@0: import javax.xml.namespace.QName; aoqi@0: aoqi@0: import com.sun.xml.internal.bind.api.impl.NameConverter; aoqi@0: import com.sun.xml.internal.bind.v2.model.annotation.AnnotationReader; aoqi@0: import com.sun.xml.internal.bind.v2.model.annotation.Locatable; aoqi@0: import com.sun.xml.internal.bind.v2.model.core.TypeInfo; aoqi@0: import com.sun.xml.internal.bind.v2.model.core.TypeInfoSet; aoqi@0: import com.sun.xml.internal.bind.v2.model.nav.Navigator; aoqi@0: aoqi@0: /** aoqi@0: * Common implementation between {@link ClassInfoImpl} and {@link ElementInfoImpl}. aoqi@0: * aoqi@0: * @author Kohsuke Kawaguchi aoqi@0: */ aoqi@0: abstract class TypeInfoImpl aoqi@0: implements TypeInfo, Locatable { aoqi@0: aoqi@0: /** aoqi@0: * The Java class that caused this Java class to be a part of the JAXB processing. aoqi@0: * aoqi@0: * null if it's specified explicitly by the user. aoqi@0: */ aoqi@0: private final Locatable upstream; aoqi@0: aoqi@0: /** aoqi@0: * {@link TypeInfoSet} to which this class belongs. aoqi@0: */ aoqi@0: protected final TypeInfoSetImpl owner; aoqi@0: aoqi@0: /** aoqi@0: * Reference to the {@link ModelBuilder}, only until we link {@link TypeInfo}s all together, aoqi@0: * because we don't want to keep {@link ModelBuilder} too long. aoqi@0: */ aoqi@0: protected ModelBuilder builder; aoqi@0: aoqi@0: protected TypeInfoImpl( aoqi@0: ModelBuilder builder, aoqi@0: Locatable upstream) { aoqi@0: aoqi@0: this.builder = builder; aoqi@0: this.owner = builder.typeInfoSet; aoqi@0: this.upstream = upstream; aoqi@0: } aoqi@0: aoqi@0: public Locatable getUpstream() { aoqi@0: return upstream; aoqi@0: } aoqi@0: aoqi@0: /*package*/ void link() { aoqi@0: builder = null; aoqi@0: } aoqi@0: aoqi@0: protected final Navigator nav() { aoqi@0: return owner.nav; aoqi@0: } aoqi@0: aoqi@0: protected final AnnotationReader reader() { aoqi@0: return owner.reader; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Parses an {@link XmlRootElement} annotation on a class aoqi@0: * and determine the element name. aoqi@0: * aoqi@0: * @return null aoqi@0: * if none was found. aoqi@0: */ aoqi@0: protected final QName parseElementName(ClassDeclT clazz) { aoqi@0: XmlRootElement e = reader().getClassAnnotation(XmlRootElement.class,clazz,this); aoqi@0: if(e==null) aoqi@0: return null; aoqi@0: aoqi@0: String local = e.name(); aoqi@0: if(local.equals("##default")) { aoqi@0: // if defaulted... aoqi@0: local = NameConverter.standard.toVariableName(nav().getClassShortName(clazz)); aoqi@0: } aoqi@0: String nsUri = e.namespace(); aoqi@0: if(nsUri.equals("##default")) { aoqi@0: // if defaulted ... aoqi@0: XmlSchema xs = reader().getPackageAnnotation(XmlSchema.class,clazz,this); aoqi@0: if(xs!=null) aoqi@0: nsUri = xs.namespace(); aoqi@0: else { aoqi@0: nsUri = builder.defaultNsUri; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: return new QName(nsUri.intern(),local.intern()); aoqi@0: } aoqi@0: aoqi@0: protected final QName parseTypeName(ClassDeclT clazz) { aoqi@0: return parseTypeName( clazz, reader().getClassAnnotation(XmlType.class,clazz,this) ); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Parses a (potentially-null) {@link XmlType} annotation on a class aoqi@0: * and determine the actual value. aoqi@0: * aoqi@0: * @param clazz aoqi@0: * The class on which the XmlType annotation is checked. aoqi@0: * @param t aoqi@0: * The {@link XmlType} annotation on the clazz. This value aoqi@0: * is taken as a parameter to improve the performance for the case where aoqi@0: * 't' is pre-computed. aoqi@0: */ aoqi@0: protected final QName parseTypeName(ClassDeclT clazz, XmlType t) { aoqi@0: String nsUri="##default"; aoqi@0: String local="##default"; aoqi@0: if(t!=null) { aoqi@0: nsUri = t.namespace(); aoqi@0: local = t.name(); aoqi@0: } aoqi@0: aoqi@0: if(local.length()==0) aoqi@0: return null; // anonymous aoqi@0: aoqi@0: aoqi@0: if(local.equals("##default")) aoqi@0: // if defaulted ... aoqi@0: local = NameConverter.standard.toVariableName(nav().getClassShortName(clazz)); aoqi@0: aoqi@0: if(nsUri.equals("##default")) { aoqi@0: // if defaulted ... aoqi@0: XmlSchema xs = reader().getPackageAnnotation(XmlSchema.class,clazz,this); aoqi@0: if(xs!=null) aoqi@0: nsUri = xs.namespace(); aoqi@0: else { aoqi@0: nsUri = builder.defaultNsUri; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: return new QName(nsUri.intern(),local.intern()); aoqi@0: } aoqi@0: }