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