1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/bind/v2/runtime/unmarshaller/DomLoader.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,164 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package com.sun.xml.internal.bind.v2.runtime.unmarshaller; 1.30 + 1.31 +import javax.xml.bind.annotation.DomHandler; 1.32 +import javax.xml.transform.Result; 1.33 +import javax.xml.transform.sax.TransformerHandler; 1.34 +import com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl; 1.35 +import org.xml.sax.SAXException; 1.36 + 1.37 +/** 1.38 + * Loads a DOM. 1.39 + * 1.40 + * @author Kohsuke Kawaguchi 1.41 + */ 1.42 +public class DomLoader<ResultT extends Result> extends Loader { 1.43 + 1.44 + private final DomHandler<?,ResultT> dom; 1.45 + 1.46 + /** 1.47 + * Used to capture the state. 1.48 + * 1.49 + * This instance is created for each unmarshalling episode. 1.50 + */ 1.51 + private final class State { 1.52 + 1.53 + /** This handler will receive SAX events. */ 1.54 + private TransformerHandler handler = null; 1.55 + 1.56 + /** {@link #handler} will produce this result. */ 1.57 + private final ResultT result; 1.58 + 1.59 + // nest level of elements. 1.60 + int depth = 1; 1.61 + 1.62 + public State( UnmarshallingContext context ) throws SAXException { 1.63 + handler = JAXBContextImpl.createTransformerHandler(context.getJAXBContext().disableSecurityProcessing); 1.64 + result = dom.createUnmarshaller(context); 1.65 + 1.66 + handler.setResult(result); 1.67 + 1.68 + // emulate the start of documents 1.69 + try { 1.70 + handler.setDocumentLocator(context.getLocator()); 1.71 + handler.startDocument(); 1.72 + declarePrefixes( context, context.getAllDeclaredPrefixes() ); 1.73 + } catch( SAXException e ) { 1.74 + context.handleError(e); 1.75 + throw e; 1.76 + } 1.77 + } 1.78 + 1.79 + public Object getElement() { 1.80 + return dom.getElement(result); 1.81 + } 1.82 + 1.83 + private void declarePrefixes( UnmarshallingContext context, String[] prefixes ) throws SAXException { 1.84 + for( int i=prefixes.length-1; i>=0; i-- ) { 1.85 + String nsUri = context.getNamespaceURI(prefixes[i]); 1.86 + if(nsUri==null) throw new IllegalStateException("prefix \'"+prefixes[i]+"\' isn't bound"); 1.87 + handler.startPrefixMapping(prefixes[i],nsUri ); 1.88 + } 1.89 + } 1.90 + 1.91 + private void undeclarePrefixes( String[] prefixes ) throws SAXException { 1.92 + for( int i=prefixes.length-1; i>=0; i-- ) 1.93 + handler.endPrefixMapping( prefixes[i] ); 1.94 + } 1.95 + } 1.96 + 1.97 + public DomLoader(DomHandler<?, ResultT> dom) { 1.98 + super(true); 1.99 + this.dom = dom; 1.100 + } 1.101 + 1.102 + @Override 1.103 + public void startElement(UnmarshallingContext.State state, TagName ea) throws SAXException { 1.104 + UnmarshallingContext context = state.getContext(); 1.105 + if (state.target == null) 1.106 + state.target = new State(context); 1.107 + 1.108 + State s = (State) state.target; 1.109 + try { 1.110 + s.declarePrefixes(context, context.getNewlyDeclaredPrefixes()); 1.111 + s.handler.startElement(ea.uri, ea.local, ea.getQname(), ea.atts); 1.112 + } catch (SAXException e) { 1.113 + context.handleError(e); 1.114 + throw e; 1.115 + } 1.116 + } 1.117 + 1.118 + @Override 1.119 + public void childElement(UnmarshallingContext.State state, TagName ea) throws SAXException { 1.120 + state.loader = this; 1.121 + State s = (State) state.prev.target; 1.122 + s.depth++; 1.123 + state.target = s; 1.124 + } 1.125 + 1.126 + @Override 1.127 + public void text(UnmarshallingContext.State state, CharSequence text) throws SAXException { 1.128 + if(text.length()==0) 1.129 + return; // there's no point in creating an empty Text node in DOM. 1.130 + try { 1.131 + State s = (State) state.target; 1.132 + s.handler.characters(text.toString().toCharArray(),0,text.length()); 1.133 + } catch( SAXException e ) { 1.134 + state.getContext().handleError(e); 1.135 + throw e; 1.136 + } 1.137 + } 1.138 + 1.139 + @Override 1.140 + public void leaveElement(UnmarshallingContext.State state, TagName ea) throws SAXException { 1.141 + State s = (State) state.target; 1.142 + UnmarshallingContext context = state.getContext(); 1.143 + 1.144 + try { 1.145 + s.handler.endElement(ea.uri, ea.local, ea.getQname()); 1.146 + s.undeclarePrefixes(context.getNewlyDeclaredPrefixes()); 1.147 + } catch( SAXException e ) { 1.148 + context.handleError(e); 1.149 + throw e; 1.150 + } 1.151 + 1.152 + if((--s.depth)==0) { 1.153 + // emulate the end of the document 1.154 + try { 1.155 + s.undeclarePrefixes(context.getAllDeclaredPrefixes()); 1.156 + s.handler.endDocument(); 1.157 + } catch( SAXException e ) { 1.158 + context.handleError(e); 1.159 + throw e; 1.160 + } 1.161 + 1.162 + // we are done 1.163 + state.target = s.getElement(); 1.164 + } 1.165 + } 1.166 + 1.167 +}