1 /*
   2  * Copyright (c) 1997, 2012, 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 com.sun.xml.internal.bind.v2.runtime;
  27 
  28 import java.io.IOException;
  29 
  30 import javax.xml.bind.annotation.W3CDomHandler;
  31 import javax.xml.bind.helpers.ValidationEventImpl;
  32 import javax.xml.bind.ValidationEvent;
  33 import javax.xml.namespace.QName;
  34 import javax.xml.stream.XMLStreamException;
  35 
  36 import com.sun.xml.internal.bind.v2.WellKnownNamespace;
  37 import com.sun.xml.internal.bind.v2.model.runtime.RuntimeTypeInfo;
  38 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext;
  39 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.DomLoader;
  40 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader;
  41 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.XsiTypeLoader;
  42 
  43 import org.w3c.dom.Attr;
  44 import org.w3c.dom.Element;
  45 import org.w3c.dom.NamedNodeMap;
  46 import org.w3c.dom.Node;
  47 import org.w3c.dom.NodeList;
  48 import org.xml.sax.SAXException;
  49 
  50 /**
  51  * {@link JaxBeanInfo} for handling {@code xs:anyType}.
  52  *
  53  * @author Kohsuke Kawaguchi
  54  */
  55 final class AnyTypeBeanInfo extends JaxBeanInfo<Object> implements AttributeAccessor {
  56 
  57     private boolean nilIncluded = false;
  58 
  59     public AnyTypeBeanInfo(JAXBContextImpl grammar,RuntimeTypeInfo anyTypeInfo) {
  60         super(grammar, anyTypeInfo, Object.class, new QName(WellKnownNamespace.XML_SCHEMA,"anyType"), false, true, false);
  61     }
  62 
  63     public String getElementNamespaceURI(Object element) {
  64         throw new UnsupportedOperationException();
  65     }
  66 
  67     public String getElementLocalName(Object element) {
  68         throw new UnsupportedOperationException();
  69     }
  70 
  71     public Object createInstance(UnmarshallingContext context) {
  72         throw new UnsupportedOperationException();
  73         // return JAXBContextImpl.createDom().createElementNS("","noname");
  74     }
  75 
  76     public boolean reset(Object element, UnmarshallingContext context) {
  77         return false;
  78 //        NodeList nl = element.getChildNodes();
  79 //        while(nl.getLength()>0)
  80 //            element.removeChild(nl.item(0));
  81 //        NamedNodeMap al = element.getAttributes();
  82 //        while(al.getLength()>0)
  83 //            element.removeAttributeNode((Attr)al.item(0));
  84 //        return true;
  85     }
  86 
  87     public String getId(Object element, XMLSerializer target) {
  88         return null;
  89     }
  90 
  91     public void serializeBody(Object element, XMLSerializer target) throws SAXException, IOException, XMLStreamException {
  92         NodeList childNodes = ((Element)element).getChildNodes();
  93         int len = childNodes.getLength();
  94         for( int i=0; i<len; i++ ) {
  95             Node child = childNodes.item(i);
  96             switch(child.getNodeType()) {
  97             case Node.CDATA_SECTION_NODE:
  98             case Node.TEXT_NODE:
  99                 target.text(child.getNodeValue(),null);
 100                 break;
 101             case Node.ELEMENT_NODE:
 102                 target.writeDom((Element)child,domHandler,null,null);
 103                 break;
 104             }
 105         }
 106     }
 107 
 108     public void serializeAttributes(Object element, XMLSerializer target) throws SAXException {
 109         NamedNodeMap al = ((Element)element).getAttributes();
 110         int len = al.getLength();
 111         for( int i=0; i<len; i++ ) {
 112             Attr a = (Attr)al.item(i);
 113             // work defensively
 114             String uri = a.getNamespaceURI();
 115             if(uri==null)   uri="";
 116             String local = a.getLocalName();
 117             String name = a.getName();
 118             if(local==null) local = name;
 119             if (uri.equals(WellKnownNamespace.XML_SCHEMA_INSTANCE) && ("nil".equals(local))) {
 120                 isNilIncluded = true;
 121             }
 122             if(name.startsWith("xmlns")) continue;// DOM reports ns decls as attributes
 123 
 124             target.attribute(uri,local,a.getValue());
 125         }
 126     }
 127 
 128     public void serializeRoot(Object element, XMLSerializer target) throws SAXException {
 129         target.reportError(
 130                 new ValidationEventImpl(
 131                         ValidationEvent.ERROR,
 132                         Messages.UNABLE_TO_MARSHAL_NON_ELEMENT.format(element.getClass().getName()),
 133                         null,
 134                         null));
 135     }
 136 
 137     public void serializeURIs(Object element, XMLSerializer target) {
 138         NamedNodeMap al = ((Element)element).getAttributes();
 139         int len = al.getLength();
 140         NamespaceContext2 context = target.getNamespaceContext();
 141         for( int i=0; i<len; i++ ) {
 142             Attr a = (Attr)al.item(i);
 143             if ("xmlns".equals(a.getPrefix())) {
 144                 context.force(a.getValue(), a.getLocalName());
 145                 continue;
 146             }
 147             if ("xmlns".equals(a.getName())) {
 148                 if (element instanceof org.w3c.dom.Element) {
 149                     context.declareNamespace(a.getValue(), null, false);
 150                     continue;
 151                 } else {
 152                     context.force(a.getValue(), "");
 153                     continue;
 154                 }
 155             }
 156             String nsUri = a.getNamespaceURI();
 157             if(nsUri!=null && nsUri.length()>0)
 158                 context.declareNamespace( nsUri, a.getPrefix(), true );
 159         }
 160     }
 161 
 162     public Transducer<Object> getTransducer() {
 163         return null;
 164     }
 165 
 166     public Loader getLoader(JAXBContextImpl context, boolean typeSubstitutionCapable) {
 167         if(typeSubstitutionCapable)
 168             return substLoader;
 169         else
 170             return domLoader;
 171     }
 172 
 173     private static final W3CDomHandler domHandler = new W3CDomHandler();
 174     private static final DomLoader domLoader = new DomLoader(domHandler);
 175     private final XsiTypeLoader substLoader = new XsiTypeLoader(this);
 176 
 177     }