325N/A/*
325N/A * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
325N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
325N/A *
325N/A * This code is free software; you can redistribute it and/or modify it
325N/A * under the terms of the GNU General Public License version 2 only, as
325N/A * published by the Free Software Foundation. Oracle designates this
325N/A * particular file as subject to the "Classpath" exception as provided
325N/A * by Oracle in the LICENSE file that accompanied this code.
325N/A *
325N/A * This code is distributed in the hope that it will be useful, but WITHOUT
325N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
325N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
325N/A * version 2 for more details (a copy is included in the LICENSE file that
325N/A * accompanied this code).
325N/A *
325N/A * You should have received a copy of the GNU General Public License version
325N/A * 2 along with this work; if not, write to the Free Software Foundation,
325N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
325N/A *
325N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
325N/A * or visit www.oracle.com if you need additional information or have any
325N/A * questions.
325N/A */
325N/A
325N/Apackage com.sun.tools.internal.xjc.model;
325N/A
325N/Aimport javax.activation.MimeType;
325N/Aimport javax.xml.bind.annotation.adapters.XmlAdapter;
325N/A
325N/Aimport com.sun.codemodel.internal.JExpr;
325N/Aimport com.sun.codemodel.internal.JExpression;
325N/Aimport com.sun.codemodel.internal.JStringLiteral;
325N/Aimport com.sun.tools.internal.xjc.outline.Outline;
325N/Aimport com.sun.xml.internal.bind.v2.ClassFactory;
325N/Aimport com.sun.xml.internal.bind.v2.model.core.ID;
325N/Aimport com.sun.xml.internal.xsom.XmlString;
325N/A
325N/A
325N/A/**
325N/A * General-purpose {@link TypeUse} implementation.
325N/A *
325N/A * @author Kohsuke Kawaguchi
325N/A */
325N/Afinal class TypeUseImpl implements TypeUse {
325N/A private final CNonElement coreType;
325N/A private final boolean collection;
325N/A private final CAdapter adapter;
325N/A private final ID id;
325N/A private final MimeType expectedMimeType;
325N/A
325N/A
325N/A public TypeUseImpl(CNonElement itemType, boolean collection, ID id, MimeType expectedMimeType, CAdapter adapter) {
325N/A this.coreType = itemType;
325N/A this.collection = collection;
325N/A this.id = id;
325N/A this.expectedMimeType = expectedMimeType;
325N/A this.adapter = adapter;
325N/A }
325N/A
325N/A public boolean isCollection() {
325N/A return collection;
325N/A }
325N/A
325N/A public CNonElement getInfo() {
325N/A return coreType;
325N/A }
325N/A
325N/A public CAdapter getAdapterUse() {
325N/A return adapter;
325N/A }
325N/A
325N/A public ID idUse() {
325N/A return id;
325N/A }
325N/A
325N/A public MimeType getExpectedMimeType() {
325N/A return expectedMimeType;
325N/A }
325N/A
325N/A public boolean equals(Object o) {
325N/A if (this == o) return true;
325N/A if (!(o instanceof TypeUseImpl)) return false;
325N/A
325N/A final TypeUseImpl that = (TypeUseImpl) o;
325N/A
325N/A if (collection != that.collection) return false;
325N/A if (this.id != that.id ) return false;
325N/A if (adapter != null ? !adapter.equals(that.adapter) : that.adapter != null) return false;
325N/A if (coreType != null ? !coreType.equals(that.coreType) : that.coreType != null) return false;
325N/A
325N/A return true;
325N/A }
325N/A
325N/A public int hashCode() {
325N/A int result;
325N/A result = (coreType != null ? coreType.hashCode() : 0);
325N/A result = 29 * result + (collection ? 1 : 0);
325N/A result = 29 * result + (adapter != null ? adapter.hashCode() : 0);
325N/A return result;
325N/A }
325N/A
325N/A
325N/A public JExpression createConstant(Outline outline, XmlString lexical) {
325N/A if(isCollection()) return null;
325N/A
325N/A if(adapter==null) return coreType.createConstant(outline, lexical);
325N/A
325N/A // [RESULT] new Adapter().unmarshal(CONSTANT);
325N/A JExpression cons = coreType.createConstant(outline, lexical);
325N/A Class<? extends XmlAdapter> atype = adapter.getAdapterIfKnown();
325N/A
325N/A // try to run the adapter now rather than later.
325N/A if(cons instanceof JStringLiteral && atype!=null) {
325N/A JStringLiteral scons = (JStringLiteral) cons;
325N/A XmlAdapter a = ClassFactory.create(atype);
325N/A try {
325N/A Object value = a.unmarshal(scons.str);
325N/A if(value instanceof String) {
325N/A return JExpr.lit((String)value);
325N/A }
325N/A } catch (Exception e) {
325N/A // assume that we can't eagerly bind this
325N/A }
325N/A }
325N/A
325N/A return JExpr._new(adapter.getAdapterClass(outline)).invoke("unmarshal").arg(cons);
325N/A }
325N/A}