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

changeset 286
f50545b5e2f1
child 368
0989ad8c0860
     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	Tue Mar 06 16:09:35 2012 -0800
     1.3 @@ -0,0 +1,163 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2011, 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 com.sun.xml.internal.bind.v2.WellKnownNamespace;
    1.32 +import javax.xml.bind.annotation.DomHandler;
    1.33 +import javax.xml.transform.Result;
    1.34 +import javax.xml.transform.sax.TransformerHandler;
    1.35 +import com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl;
    1.36 +import org.xml.sax.SAXException;
    1.37 +
    1.38 +/**
    1.39 + * Loads a DOM.
    1.40 + *
    1.41 + * @author Kohsuke Kawaguchi
    1.42 + */
    1.43 +public class DomLoader<ResultT extends Result> extends Loader {
    1.44 +
    1.45 +    private final DomHandler<?,ResultT> dom;
    1.46 +
    1.47 +    /**
    1.48 +     * Used to capture the state.
    1.49 +     *
    1.50 +     * This instance is created for each unmarshalling episode.
    1.51 +     */
    1.52 +    private final class State {
    1.53 +        /** This handler will receive SAX events. */
    1.54 +        private final TransformerHandler handler = JAXBContextImpl.createTransformerHandler();
    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 +            result = dom.createUnmarshaller(context);
    1.64 +
    1.65 +            handler.setResult(result);
    1.66 +
    1.67 +            // emulate the start of documents
    1.68 +            try {
    1.69 +                handler.setDocumentLocator(context.getLocator());
    1.70 +                handler.startDocument();
    1.71 +                declarePrefixes( context, context.getAllDeclaredPrefixes() );
    1.72 +            } catch( SAXException e ) {
    1.73 +                context.handleError(e);
    1.74 +                throw e;
    1.75 +            }
    1.76 +        }
    1.77 +
    1.78 +        public Object getElement() {
    1.79 +            return dom.getElement(result);
    1.80 +        }
    1.81 +
    1.82 +        private void declarePrefixes( UnmarshallingContext context, String[] prefixes ) throws SAXException {
    1.83 +            for( int i=prefixes.length-1; i>=0; i-- ) {
    1.84 +                String nsUri = context.getNamespaceURI(prefixes[i]);
    1.85 +                if(nsUri==null)     throw new IllegalStateException("prefix \'"+prefixes[i]+"\' isn't bound");
    1.86 +                handler.startPrefixMapping(prefixes[i],nsUri );
    1.87 +            }
    1.88 +        }
    1.89 +
    1.90 +        private void undeclarePrefixes( String[] prefixes ) throws SAXException {
    1.91 +            for( int i=prefixes.length-1; i>=0; i-- )
    1.92 +                handler.endPrefixMapping( prefixes[i] );
    1.93 +        }
    1.94 +    }
    1.95 +
    1.96 +    public DomLoader(DomHandler<?, ResultT> dom) {
    1.97 +        super(true);
    1.98 +        this.dom = dom;
    1.99 +    }
   1.100 +
   1.101 +    @Override
   1.102 +    public void startElement(UnmarshallingContext.State state, TagName ea) throws SAXException {
   1.103 +        UnmarshallingContext context = state.getContext();
   1.104 +        if (state.target == null)
   1.105 +            state.target = new State(context);
   1.106 +
   1.107 +        State s = (State) state.target;
   1.108 +        try {
   1.109 +            s.declarePrefixes(context, context.getNewlyDeclaredPrefixes());
   1.110 +            s.handler.startElement(ea.uri, ea.local, ea.getQname(), ea.atts);
   1.111 +        } catch (SAXException e) {
   1.112 +            context.handleError(e);
   1.113 +            throw e;
   1.114 +        }
   1.115 +    }
   1.116 +
   1.117 +    @Override
   1.118 +    public void childElement(UnmarshallingContext.State state, TagName ea) throws SAXException {
   1.119 +        state.loader = this;
   1.120 +        State s = (State) state.prev.target;
   1.121 +        s.depth++;
   1.122 +        state.target = s;
   1.123 +    }
   1.124 +
   1.125 +    @Override
   1.126 +    public void text(UnmarshallingContext.State state, CharSequence text) throws SAXException {
   1.127 +        if(text.length()==0)
   1.128 +            return;     // there's no point in creating an empty Text node in DOM.
   1.129 +        try {
   1.130 +            State s = (State) state.target;
   1.131 +            s.handler.characters(text.toString().toCharArray(),0,text.length());
   1.132 +        } catch( SAXException e ) {
   1.133 +            state.getContext().handleError(e);
   1.134 +            throw e;
   1.135 +        }
   1.136 +    }
   1.137 +
   1.138 +    @Override
   1.139 +    public void leaveElement(UnmarshallingContext.State state, TagName ea) throws SAXException {
   1.140 +        State s = (State) state.target;
   1.141 +        UnmarshallingContext context = state.getContext();
   1.142 +
   1.143 +        try {
   1.144 +            s.handler.endElement(ea.uri, ea.local, ea.getQname());
   1.145 +            s.undeclarePrefixes(context.getNewlyDeclaredPrefixes());
   1.146 +        } catch( SAXException e ) {
   1.147 +            context.handleError(e);
   1.148 +            throw e;
   1.149 +        }
   1.150 +
   1.151 +        if((--s.depth)==0) {
   1.152 +            // emulate the end of the document
   1.153 +            try {
   1.154 +                s.undeclarePrefixes(context.getAllDeclaredPrefixes());
   1.155 +                s.handler.endDocument();
   1.156 +            } catch( SAXException e ) {
   1.157 +                context.handleError(e);
   1.158 +                throw e;
   1.159 +            }
   1.160 +
   1.161 +            // we are done
   1.162 +            state.target = s.getElement();
   1.163 +        }
   1.164 +    }
   1.165 +
   1.166 +}

mercurial