Mon, 28 May 2018 10:36:45 +0800
Merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. Oracle designates this |
aoqi@0 | 8 | * particular file as subject to the "Classpath" exception as provided |
aoqi@0 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
aoqi@0 | 10 | * |
aoqi@0 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 15 | * accompanied this code). |
aoqi@0 | 16 | * |
aoqi@0 | 17 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 20 | * |
aoqi@0 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 22 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 23 | * questions. |
aoqi@0 | 24 | */ |
aoqi@0 | 25 | |
aoqi@0 | 26 | package com.sun.xml.internal.bind.v2.runtime; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import java.io.IOException; |
aoqi@0 | 29 | |
aoqi@0 | 30 | import javax.xml.bind.annotation.W3CDomHandler; |
aoqi@0 | 31 | import javax.xml.bind.helpers.ValidationEventImpl; |
aoqi@0 | 32 | import javax.xml.bind.ValidationEvent; |
aoqi@0 | 33 | import javax.xml.namespace.QName; |
aoqi@0 | 34 | import javax.xml.stream.XMLStreamException; |
aoqi@0 | 35 | |
aoqi@0 | 36 | import com.sun.xml.internal.bind.v2.WellKnownNamespace; |
aoqi@0 | 37 | import com.sun.xml.internal.bind.v2.model.runtime.RuntimeTypeInfo; |
aoqi@0 | 38 | import com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext; |
aoqi@0 | 39 | import com.sun.xml.internal.bind.v2.runtime.unmarshaller.DomLoader; |
aoqi@0 | 40 | import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader; |
aoqi@0 | 41 | import com.sun.xml.internal.bind.v2.runtime.unmarshaller.XsiTypeLoader; |
aoqi@0 | 42 | |
aoqi@0 | 43 | import org.w3c.dom.Attr; |
aoqi@0 | 44 | import org.w3c.dom.Element; |
aoqi@0 | 45 | import org.w3c.dom.NamedNodeMap; |
aoqi@0 | 46 | import org.w3c.dom.Node; |
aoqi@0 | 47 | import org.w3c.dom.NodeList; |
aoqi@0 | 48 | import org.xml.sax.SAXException; |
aoqi@0 | 49 | |
aoqi@0 | 50 | /** |
aoqi@0 | 51 | * {@link JaxBeanInfo} for handling <tt>xs:anyType</tt>. |
aoqi@0 | 52 | * |
aoqi@0 | 53 | * @author Kohsuke Kawaguchi |
aoqi@0 | 54 | */ |
aoqi@0 | 55 | final class AnyTypeBeanInfo extends JaxBeanInfo<Object> implements AttributeAccessor { |
aoqi@0 | 56 | |
aoqi@0 | 57 | private boolean nilIncluded = false; |
aoqi@0 | 58 | |
aoqi@0 | 59 | public AnyTypeBeanInfo(JAXBContextImpl grammar,RuntimeTypeInfo anyTypeInfo) { |
aoqi@0 | 60 | super(grammar, anyTypeInfo, Object.class, new QName(WellKnownNamespace.XML_SCHEMA,"anyType"), false, true, false); |
aoqi@0 | 61 | } |
aoqi@0 | 62 | |
aoqi@0 | 63 | public String getElementNamespaceURI(Object element) { |
aoqi@0 | 64 | throw new UnsupportedOperationException(); |
aoqi@0 | 65 | } |
aoqi@0 | 66 | |
aoqi@0 | 67 | public String getElementLocalName(Object element) { |
aoqi@0 | 68 | throw new UnsupportedOperationException(); |
aoqi@0 | 69 | } |
aoqi@0 | 70 | |
aoqi@0 | 71 | public Object createInstance(UnmarshallingContext context) { |
aoqi@0 | 72 | throw new UnsupportedOperationException(); |
aoqi@0 | 73 | // return JAXBContextImpl.createDom().createElementNS("","noname"); |
aoqi@0 | 74 | } |
aoqi@0 | 75 | |
aoqi@0 | 76 | public boolean reset(Object element, UnmarshallingContext context) { |
aoqi@0 | 77 | return false; |
aoqi@0 | 78 | // NodeList nl = element.getChildNodes(); |
aoqi@0 | 79 | // while(nl.getLength()>0) |
aoqi@0 | 80 | // element.removeChild(nl.item(0)); |
aoqi@0 | 81 | // NamedNodeMap al = element.getAttributes(); |
aoqi@0 | 82 | // while(al.getLength()>0) |
aoqi@0 | 83 | // element.removeAttributeNode((Attr)al.item(0)); |
aoqi@0 | 84 | // return true; |
aoqi@0 | 85 | } |
aoqi@0 | 86 | |
aoqi@0 | 87 | public String getId(Object element, XMLSerializer target) { |
aoqi@0 | 88 | return null; |
aoqi@0 | 89 | } |
aoqi@0 | 90 | |
aoqi@0 | 91 | public void serializeBody(Object element, XMLSerializer target) throws SAXException, IOException, XMLStreamException { |
aoqi@0 | 92 | NodeList childNodes = ((Element)element).getChildNodes(); |
aoqi@0 | 93 | int len = childNodes.getLength(); |
aoqi@0 | 94 | for( int i=0; i<len; i++ ) { |
aoqi@0 | 95 | Node child = childNodes.item(i); |
aoqi@0 | 96 | switch(child.getNodeType()) { |
aoqi@0 | 97 | case Node.CDATA_SECTION_NODE: |
aoqi@0 | 98 | case Node.TEXT_NODE: |
aoqi@0 | 99 | target.text(child.getNodeValue(),null); |
aoqi@0 | 100 | break; |
aoqi@0 | 101 | case Node.ELEMENT_NODE: |
aoqi@0 | 102 | target.writeDom((Element)child,domHandler,null,null); |
aoqi@0 | 103 | break; |
aoqi@0 | 104 | } |
aoqi@0 | 105 | } |
aoqi@0 | 106 | } |
aoqi@0 | 107 | |
aoqi@0 | 108 | public void serializeAttributes(Object element, XMLSerializer target) throws SAXException { |
aoqi@0 | 109 | NamedNodeMap al = ((Element)element).getAttributes(); |
aoqi@0 | 110 | int len = al.getLength(); |
aoqi@0 | 111 | for( int i=0; i<len; i++ ) { |
aoqi@0 | 112 | Attr a = (Attr)al.item(i); |
aoqi@0 | 113 | // work defensively |
aoqi@0 | 114 | String uri = a.getNamespaceURI(); |
aoqi@0 | 115 | if(uri==null) uri=""; |
aoqi@0 | 116 | String local = a.getLocalName(); |
aoqi@0 | 117 | String name = a.getName(); |
aoqi@0 | 118 | if(local==null) local = name; |
aoqi@0 | 119 | if (uri.equals(WellKnownNamespace.XML_SCHEMA_INSTANCE) && ("nil".equals(local))) { |
aoqi@0 | 120 | isNilIncluded = true; |
aoqi@0 | 121 | } |
aoqi@0 | 122 | if(name.startsWith("xmlns")) continue;// DOM reports ns decls as attributes |
aoqi@0 | 123 | |
aoqi@0 | 124 | target.attribute(uri,local,a.getValue()); |
aoqi@0 | 125 | } |
aoqi@0 | 126 | } |
aoqi@0 | 127 | |
aoqi@0 | 128 | public void serializeRoot(Object element, XMLSerializer target) throws SAXException { |
aoqi@0 | 129 | target.reportError( |
aoqi@0 | 130 | new ValidationEventImpl( |
aoqi@0 | 131 | ValidationEvent.ERROR, |
aoqi@0 | 132 | Messages.UNABLE_TO_MARSHAL_NON_ELEMENT.format(element.getClass().getName()), |
aoqi@0 | 133 | null, |
aoqi@0 | 134 | null)); |
aoqi@0 | 135 | } |
aoqi@0 | 136 | |
aoqi@0 | 137 | public void serializeURIs(Object element, XMLSerializer target) { |
aoqi@0 | 138 | NamedNodeMap al = ((Element)element).getAttributes(); |
aoqi@0 | 139 | int len = al.getLength(); |
aoqi@0 | 140 | NamespaceContext2 context = target.getNamespaceContext(); |
aoqi@0 | 141 | for( int i=0; i<len; i++ ) { |
aoqi@0 | 142 | Attr a = (Attr)al.item(i); |
aoqi@0 | 143 | if ("xmlns".equals(a.getPrefix())) { |
aoqi@0 | 144 | context.force(a.getValue(), a.getLocalName()); |
aoqi@0 | 145 | continue; |
aoqi@0 | 146 | } |
aoqi@0 | 147 | if ("xmlns".equals(a.getName())) { |
aoqi@0 | 148 | if (element instanceof org.w3c.dom.Element) { |
aoqi@0 | 149 | context.declareNamespace(a.getValue(), null, false); |
aoqi@0 | 150 | continue; |
aoqi@0 | 151 | } else { |
aoqi@0 | 152 | context.force(a.getValue(), ""); |
aoqi@0 | 153 | continue; |
aoqi@0 | 154 | } |
aoqi@0 | 155 | } |
aoqi@0 | 156 | String nsUri = a.getNamespaceURI(); |
aoqi@0 | 157 | if(nsUri!=null && nsUri.length()>0) |
aoqi@0 | 158 | context.declareNamespace( nsUri, a.getPrefix(), true ); |
aoqi@0 | 159 | } |
aoqi@0 | 160 | } |
aoqi@0 | 161 | |
aoqi@0 | 162 | public Transducer<Object> getTransducer() { |
aoqi@0 | 163 | return null; |
aoqi@0 | 164 | } |
aoqi@0 | 165 | |
aoqi@0 | 166 | public Loader getLoader(JAXBContextImpl context, boolean typeSubstitutionCapable) { |
aoqi@0 | 167 | if(typeSubstitutionCapable) |
aoqi@0 | 168 | return substLoader; |
aoqi@0 | 169 | else |
aoqi@0 | 170 | return domLoader; |
aoqi@0 | 171 | } |
aoqi@0 | 172 | |
aoqi@0 | 173 | private static final W3CDomHandler domHandler = new W3CDomHandler(); |
aoqi@0 | 174 | private static final DomLoader domLoader = new DomLoader(domHandler); |
aoqi@0 | 175 | private final XsiTypeLoader substLoader = new XsiTypeLoader(this); |
aoqi@0 | 176 | |
aoqi@0 | 177 | } |