/*******************************************************************************
 * Copyright (c) 1998, 2008 Oracle. All rights reserved.
 * This program and the accompanying materials are made available under the 
 * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0 
 * which accompanies this distribution. 
 * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
 * and the Eclipse Distribution License is available at 
 * http://www.eclipse.org/org/documents/edl-v10.php.
 *
 * Contributors:
 *     Oracle - initial API and implementation from Oracle TopLink
 ******************************************************************************/  
package org.eclipse.persistence.jaxb;

import javax.xml.bind.JAXBElement;
import javax.xml.namespace.QName;

import org.eclipse.persistence.oxm.XMLContext;
import org.eclipse.persistence.oxm.XMLDescriptor;
import org.eclipse.persistence.sessions.Session;

/**
 * INTERNAL
 * <p><b>Purpose:</b>Provide a TopLink implementation of JAXBIntrospector
 * <p><b>Responsibilities:</b><ul>
 * <li>Determine if a an object has an associated Global Element</li>
 * <li>Get an element QName for an object that has an associated global element</li>
 * </ul>
 * <p>This class is the TopLink implementation of JAXBIntrospector. An Introspector is created
 * by a JAXBContext and allows the user to access certain peices of meta-data about an instance
 * of a JAXB bound class.
 * 
 * @see javax.xml.bind.JAXBIntrospector
 * @see org.eclipse.persistence.jaxb.JAXB20Context
 * @author mmacivor
 * @since Oracle TopLink 11.1.1.0.0
 */

public class JAXBIntrospector extends javax.xml.bind.JAXBIntrospector {
    private XMLContext context;
    public JAXBIntrospector(XMLContext context) {
        this.context = context;
    }
    
    public boolean isElement(Object obj) {
    	if (obj instanceof JAXBElement) {
    		return true;
    	}
    	
        Session session = context.getSession(obj);
        if(session == null) {
            return false;
        }
        XMLDescriptor descriptor = (XMLDescriptor)session.getDescriptor(obj);
        if(descriptor == null) {
            return false;
        }
        
        return descriptor.getDefaultRootElement() != null;
    }
    
    public QName getElementName(Object obj) {
        if(!isElement(obj)) {
            return null;
        }
        XMLDescriptor descriptor = (XMLDescriptor)context.getSession(obj).getDescriptor(obj);
        String rootElem = descriptor.getDefaultRootElement();
        int prefixIndex = rootElem.indexOf(":");
        if(prefixIndex == -1) {
            return new QName(rootElem);
        } else {
            String prefix = rootElem.substring(0, prefixIndex);
            String localPart = rootElem.substring(prefixIndex + 1);
            String URI = descriptor.getNamespaceResolver().resolveNamespacePrefix(prefix);
            return new QName(URI, localPart);
        }
        
    }     
}

@@ -824,24 +824,88 @@
 e.QName;%0D%0A%0D%0A
+import org.eclipse.persistence.exceptions.XMLMarshalException;%0D%0A
 import org.e
@@ -2044,16 +2044,35 @@
 %0A    %09%0D%0A
+        try %7B%0D%0A    
         
@@ -2107,32 +2107,36 @@
 tSession(obj);%0D%0A
+    
         if(sessi
@@ -2154,32 +2154,36 @@
  %7B%0D%0A            
+    
 return false;%0D%0A 
@@ -2181,35 +2181,43 @@
 false;%0D%0A        
-%7D%0D%0A
+    %7D%0D%0A    
         XMLDescr
@@ -2287,16 +2287,20 @@
         
+    
 if(descr
@@ -2321,32 +2321,36 @@
  %7B%0D%0A            
+    
 return false;%0D%0A 
@@ -2352,37 +2352,35 @@
 e;%0D%0A        
-%7D%0D%0A
     
+%7D%0D%0A
     
-%0D%0A
         retu
@@ -2423,24 +2423,104 @@
 ) != null;%0D%0A
+        %7D catch(XMLMarshalException e) %7B%0D%0A            return false;%0D%0A        %7D%0D%0A
     %7D%0D%0A    %0D
@@ -2627,32 +2627,157 @@
 ll;%0D%0A        %7D%0D%0A
+%0D%0A        if(obj instanceof JAXBElement) %7B%0D%0A            return ((JAXBElement) obj).getName();%0D%0A        %7D%0D%0A        try %7B%0D%0A    
         XMLDescr
@@ -2859,24 +2859,28 @@
 );%0D%0A        
+    
 String rootE
@@ -2930,16 +2930,20 @@
         
+ 
+   
 int pref
@@ -2984,16 +2984,20 @@
         
+    
 if(prefi
@@ -3009,24 +3009,28 @@
 x == -1) %7B%0D%0A
+    
             
@@ -3066,16 +3066,20 @@
         
+    
 %7D else %7B
@@ -3076,24 +3076,28 @@
   %7D else %7B%0D%0A
+    
             
@@ -3145,24 +3145,28 @@
 fixIndex);%0D%0A
+    
             
@@ -3230,24 +3230,28 @@
             
+    
 String URI =
@@ -3322,32 +3322,36 @@
 );%0D%0A            
+    
 return new QName
@@ -3369,32 +3369,36 @@
 Part);%0D%0A        
+   
+ 
 %7D%0D%0A        %0D%0A   
@@ -3396,20 +3396,84 @@
     
+%7D catch(XMLMarshalException e) %7B%0D%0A            return null;%0D%0A        %7D
 %0D%0A    %7D
-     
 %0D%0A%7D%0D

Fix for bug #268553