src/share/jaxws_classes/com/sun/xml/internal/bind/v2/runtime/unmarshaller/DomLoader.java

Sun, 31 Aug 2014 16:14:36 +0400

author
aefimov
date
Sun, 31 Aug 2014 16:14:36 +0400
changeset 707
31893650acaf
parent 650
121e938cb9c3
child 760
e530533619ec
permissions
-rw-r--r--

8036981: JAXB not preserving formatting for xsd:any Mixed content
Reviewed-by: lancea, mkos

ohair@286 1 /*
aefimov@650 2 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
ohair@286 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ohair@286 4 *
ohair@286 5 * This code is free software; you can redistribute it and/or modify it
ohair@286 6 * under the terms of the GNU General Public License version 2 only, as
ohair@286 7 * published by the Free Software Foundation. Oracle designates this
ohair@286 8 * particular file as subject to the "Classpath" exception as provided
ohair@286 9 * by Oracle in the LICENSE file that accompanied this code.
ohair@286 10 *
ohair@286 11 * This code is distributed in the hope that it will be useful, but WITHOUT
ohair@286 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ohair@286 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ohair@286 14 * version 2 for more details (a copy is included in the LICENSE file that
ohair@286 15 * accompanied this code).
ohair@286 16 *
ohair@286 17 * You should have received a copy of the GNU General Public License version
ohair@286 18 * 2 along with this work; if not, write to the Free Software Foundation,
ohair@286 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ohair@286 20 *
ohair@286 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@286 22 * or visit www.oracle.com if you need additional information or have any
ohair@286 23 * questions.
ohair@286 24 */
ohair@286 25
ohair@286 26 package com.sun.xml.internal.bind.v2.runtime.unmarshaller;
ohair@286 27
ohair@286 28 import javax.xml.bind.annotation.DomHandler;
ohair@286 29 import javax.xml.transform.Result;
ohair@286 30 import javax.xml.transform.sax.TransformerHandler;
ohair@286 31 import com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl;
ohair@286 32 import org.xml.sax.SAXException;
ohair@286 33
ohair@286 34 /**
ohair@286 35 * Loads a DOM.
ohair@286 36 *
ohair@286 37 * @author Kohsuke Kawaguchi
ohair@286 38 */
ohair@286 39 public class DomLoader<ResultT extends Result> extends Loader {
ohair@286 40
ohair@286 41 private final DomHandler<?,ResultT> dom;
ohair@286 42
ohair@286 43 /**
ohair@286 44 * Used to capture the state.
ohair@286 45 *
ohair@286 46 * This instance is created for each unmarshalling episode.
ohair@286 47 */
ohair@286 48 private final class State {
alanb@368 49
ohair@286 50 /** This handler will receive SAX events. */
alanb@368 51 private TransformerHandler handler = null;
ohair@286 52
ohair@286 53 /** {@link #handler} will produce this result. */
ohair@286 54 private final ResultT result;
ohair@286 55
ohair@286 56 // nest level of elements.
ohair@286 57 int depth = 1;
ohair@286 58
ohair@286 59 public State( UnmarshallingContext context ) throws SAXException {
alanb@368 60 handler = JAXBContextImpl.createTransformerHandler(context.getJAXBContext().disableSecurityProcessing);
ohair@286 61 result = dom.createUnmarshaller(context);
ohair@286 62
ohair@286 63 handler.setResult(result);
ohair@286 64
ohair@286 65 // emulate the start of documents
ohair@286 66 try {
ohair@286 67 handler.setDocumentLocator(context.getLocator());
ohair@286 68 handler.startDocument();
ohair@286 69 declarePrefixes( context, context.getAllDeclaredPrefixes() );
ohair@286 70 } catch( SAXException e ) {
ohair@286 71 context.handleError(e);
ohair@286 72 throw e;
ohair@286 73 }
ohair@286 74 }
ohair@286 75
ohair@286 76 public Object getElement() {
ohair@286 77 return dom.getElement(result);
ohair@286 78 }
ohair@286 79
ohair@286 80 private void declarePrefixes( UnmarshallingContext context, String[] prefixes ) throws SAXException {
ohair@286 81 for( int i=prefixes.length-1; i>=0; i-- ) {
ohair@286 82 String nsUri = context.getNamespaceURI(prefixes[i]);
ohair@286 83 if(nsUri==null) throw new IllegalStateException("prefix \'"+prefixes[i]+"\' isn't bound");
ohair@286 84 handler.startPrefixMapping(prefixes[i],nsUri );
ohair@286 85 }
ohair@286 86 }
ohair@286 87
ohair@286 88 private void undeclarePrefixes( String[] prefixes ) throws SAXException {
ohair@286 89 for( int i=prefixes.length-1; i>=0; i-- )
ohair@286 90 handler.endPrefixMapping( prefixes[i] );
ohair@286 91 }
ohair@286 92 }
ohair@286 93
ohair@286 94 public DomLoader(DomHandler<?, ResultT> dom) {
ohair@286 95 super(true);
ohair@286 96 this.dom = dom;
ohair@286 97 }
ohair@286 98
ohair@286 99 @Override
ohair@286 100 public void startElement(UnmarshallingContext.State state, TagName ea) throws SAXException {
ohair@286 101 UnmarshallingContext context = state.getContext();
aefimov@650 102 if (state.getTarget() == null)
aefimov@650 103 state.setTarget(new State(context));
ohair@286 104
aefimov@650 105 State s = (State) state.getTarget();
ohair@286 106 try {
ohair@286 107 s.declarePrefixes(context, context.getNewlyDeclaredPrefixes());
ohair@286 108 s.handler.startElement(ea.uri, ea.local, ea.getQname(), ea.atts);
ohair@286 109 } catch (SAXException e) {
ohair@286 110 context.handleError(e);
ohair@286 111 throw e;
ohair@286 112 }
ohair@286 113 }
ohair@286 114
ohair@286 115 @Override
ohair@286 116 public void childElement(UnmarshallingContext.State state, TagName ea) throws SAXException {
aefimov@650 117 state.setLoader(this);
aefimov@650 118 State s = (State) state.getPrev().getTarget();
ohair@286 119 s.depth++;
aefimov@650 120 state.setTarget(s);
ohair@286 121 }
ohair@286 122
ohair@286 123 @Override
ohair@286 124 public void text(UnmarshallingContext.State state, CharSequence text) throws SAXException {
ohair@286 125 if(text.length()==0)
ohair@286 126 return; // there's no point in creating an empty Text node in DOM.
ohair@286 127 try {
aefimov@650 128 State s = (State) state.getTarget();
ohair@286 129 s.handler.characters(text.toString().toCharArray(),0,text.length());
ohair@286 130 } catch( SAXException e ) {
ohair@286 131 state.getContext().handleError(e);
ohair@286 132 throw e;
ohair@286 133 }
ohair@286 134 }
ohair@286 135
ohair@286 136 @Override
ohair@286 137 public void leaveElement(UnmarshallingContext.State state, TagName ea) throws SAXException {
aefimov@650 138 State s = (State) state.getTarget();
ohair@286 139 UnmarshallingContext context = state.getContext();
ohair@286 140
ohair@286 141 try {
ohair@286 142 s.handler.endElement(ea.uri, ea.local, ea.getQname());
ohair@286 143 s.undeclarePrefixes(context.getNewlyDeclaredPrefixes());
ohair@286 144 } catch( SAXException e ) {
ohair@286 145 context.handleError(e);
ohair@286 146 throw e;
ohair@286 147 }
ohair@286 148
ohair@286 149 if((--s.depth)==0) {
ohair@286 150 // emulate the end of the document
ohair@286 151 try {
ohair@286 152 s.undeclarePrefixes(context.getAllDeclaredPrefixes());
ohair@286 153 s.handler.endDocument();
ohair@286 154 } catch( SAXException e ) {
ohair@286 155 context.handleError(e);
ohair@286 156 throw e;
ohair@286 157 }
ohair@286 158
ohair@286 159 // we are done
aefimov@650 160 state.setTarget(s.getElement());
ohair@286 161 }
ohair@286 162 }
ohair@286 163
ohair@286 164 }

mercurial