001: /*
002: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.xml.internal.bind.v2.runtime;
027:
028: import java.io.IOException;
029:
030: import javax.xml.bind.annotation.W3CDomHandler;
031: import javax.xml.bind.helpers.ValidationEventImpl;
032: import javax.xml.bind.ValidationEvent;
033: import javax.xml.namespace.QName;
034: import javax.xml.stream.XMLStreamException;
035:
036: import com.sun.xml.internal.bind.v2.WellKnownNamespace;
037: import com.sun.xml.internal.bind.v2.model.runtime.RuntimeTypeInfo;
038: import com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext;
039: import com.sun.xml.internal.bind.v2.runtime.unmarshaller.DomLoader;
040: import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader;
041: import com.sun.xml.internal.bind.v2.runtime.unmarshaller.XsiTypeLoader;
042:
043: import org.w3c.dom.Attr;
044: import org.w3c.dom.Element;
045: import org.w3c.dom.NamedNodeMap;
046: import org.w3c.dom.Node;
047: import org.w3c.dom.NodeList;
048: import org.xml.sax.SAXException;
049:
050: /**
051: * {@link JaxBeanInfo} for handling <tt>xs:anyType</tt>.
052: *
053: * @author Kohsuke Kawaguchi
054: */
055: final class AnyTypeBeanInfo extends JaxBeanInfo<Object> {
056:
057: public AnyTypeBeanInfo(JAXBContextImpl grammar,
058: RuntimeTypeInfo anyTypeInfo) {
059: super (grammar, anyTypeInfo, Object.class, new QName(
060: WellKnownNamespace.XML_SCHEMA, "anyType"), false, true,
061: false);
062: }
063:
064: public String getElementNamespaceURI(Object element) {
065: throw new UnsupportedOperationException();
066: }
067:
068: public String getElementLocalName(Object element) {
069: throw new UnsupportedOperationException();
070: }
071:
072: public Object createInstance(UnmarshallingContext context) {
073: throw new UnsupportedOperationException();
074: // return JAXBContextImpl.createDom().createElementNS("","noname");
075: }
076:
077: public boolean reset(Object element, UnmarshallingContext context) {
078: return false;
079: // NodeList nl = element.getChildNodes();
080: // while(nl.getLength()>0)
081: // element.removeChild(nl.item(0));
082: // NamedNodeMap al = element.getAttributes();
083: // while(al.getLength()>0)
084: // element.removeAttributeNode((Attr)al.item(0));
085: // return true;
086: }
087:
088: public String getId(Object element, XMLSerializer target) {
089: return null;
090: }
091:
092: public void serializeBody(Object element, XMLSerializer target)
093: throws SAXException, IOException, XMLStreamException {
094: NodeList childNodes = ((Element) element).getChildNodes();
095: int len = childNodes.getLength();
096: for (int i = 0; i < len; i++) {
097: Node child = childNodes.item(i);
098: switch (child.getNodeType()) {
099: case Node.CDATA_SECTION_NODE:
100: case Node.TEXT_NODE:
101: target.text(child.getNodeValue(), null);
102: break;
103: case Node.ELEMENT_NODE:
104: target
105: .writeDom((Element) child, domHandler, null,
106: null);
107: break;
108: }
109: }
110: }
111:
112: public void serializeAttributes(Object element, XMLSerializer target)
113: throws SAXException {
114: NamedNodeMap al = ((Element) element).getAttributes();
115: int len = al.getLength();
116: for (int i = 0; i < len; i++) {
117: Attr a = (Attr) al.item(i);
118: // work defensively
119: String uri = a.getNamespaceURI();
120: if (uri == null)
121: uri = "";
122: String local = a.getLocalName();
123: String name = a.getName();
124: if (local == null)
125: local = name;
126:
127: if (name.startsWith("xmlns"))
128: continue;// DOM reports ns decls as attributes
129:
130: target.attribute(uri, local, a.getValue());
131: }
132: }
133:
134: public void serializeRoot(Object element, XMLSerializer target)
135: throws SAXException {
136: target.reportError(new ValidationEventImpl(
137: ValidationEvent.ERROR,
138: Messages.UNABLE_TO_MARSHAL_NON_ELEMENT.format(element
139: .getClass().getName()), null, null));
140: }
141:
142: public void serializeURIs(Object element, XMLSerializer target) {
143: NamedNodeMap al = ((Element) element).getAttributes();
144: int len = al.getLength();
145: NamespaceContext2 context = target.getNamespaceContext();
146: for (int i = 0; i < len; i++) {
147: Attr a = (Attr) al.item(i);
148: if ("xmlns".equals(a.getPrefix())) {
149: context.declareNamespace(a.getValue(),
150: a.getLocalName(), true);
151: continue;
152: }
153: if ("xmlns".equals(a.getName())) {
154: context.declareNamespace(a.getValue(), "", false);
155: continue;
156: }
157: String nsUri = a.getNamespaceURI();
158: if (nsUri != null && nsUri.length() > 0)
159: context.declareNamespace(nsUri, a.getPrefix(), true);
160: }
161: }
162:
163: public Transducer<Object> getTransducer() {
164: return null;
165: }
166:
167: public Loader getLoader(JAXBContextImpl context,
168: boolean typeSubstitutionCapable) {
169: if (typeSubstitutionCapable)
170: return substLoader;
171: else
172: return domLoader;
173: }
174:
175: private static final W3CDomHandler domHandler = new W3CDomHandler();
176: private static final DomLoader domLoader = new DomLoader(domHandler);
177: private final XsiTypeLoader substLoader = new XsiTypeLoader(this);
178: }
|