1 /*
   2  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javax.xml.bind;
  27 import  javax.xml.namespace.QName;
  28 
  29 /**
  30  * Provide access to JAXB xml binding data for a JAXB object.
  31  *
  32  * <p>
  33  * Intially, the intent of this class is to just conceptualize how
  34  * a JAXB application developer can access xml binding information,
  35  * independent if binding model is java to schema or schema to java.
  36  * Since accessing the XML element name related to a JAXB element is
  37  * a highly requested feature, demonstrate access to this
  38  * binding information.
  39  *
  40  * The factory method to get a <code>JAXBIntrospector</code> instance is
  41  * {@link JAXBContext#createJAXBIntrospector()}.
  42  *
  43  * @see JAXBContext#createJAXBIntrospector()
  44  * @since JAXB2.0
  45  */
  46 public abstract class JAXBIntrospector {
  47 
  48     /**
  49      * <p>Return true if <code>object</code> represents a JAXB element.</p>
  50      * <p>Parameter <code>object</code> is a JAXB element for following cases:
  51      * <ol>
  52      *   <li>It is an instance of <code>javax.xml.bind.JAXBElement</code>.</li>
  53      *   <li>The class of <code>object</code> is annotated with
  54      *       <code>&#64XmlRootElement</code>.
  55      *   </li>
  56      * </ol>
  57      *
  58      * @see #getElementName(Object)
  59      */
  60     public abstract boolean isElement(Object object);
  61 
  62     /**
  63      * <p>Get xml element qname for <code>jaxbElement</code>.</p>
  64      *
  65      * @param jaxbElement is an object that {@link #isElement(Object)} returned true.
  66      *
  67      * @return xml element qname associated with jaxbElement;
  68      *         null if <code>jaxbElement</code> is not a JAXB Element.
  69      */
  70     public abstract QName getElementName(Object jaxbElement);
  71 
  72     /**
  73      * <p>Get the element value of a JAXB element.</p>
  74      *
  75      * <p>Convenience method to abstract whether working with either
  76      *    a javax.xml.bind.JAXBElement instance or an instance of
  77      *    <tt>&#64XmlRootElement</tt> annotated Java class.</p>
  78      *
  79      * @param jaxbElement  object that #isElement(Object) returns true.
  80      *
  81      * @return The element value of the <code>jaxbElement</code>.
  82      */
  83     public static Object getValue(Object jaxbElement) {
  84         if (jaxbElement instanceof JAXBElement) {
  85             return ((JAXBElement)jaxbElement).getValue();
  86         } else {
  87             // assume that class of this instance is
  88             // annotated with @XmlRootElement.
  89             return jaxbElement;
  90         }
  91     }
  92 }