1 /*
2 * Copyright (c) 1997, 2013, 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
82 String nsUri = context.getNamespaceURI(prefixes[i]);
83 if(nsUri==null) throw new IllegalStateException("prefix \'"+prefixes[i]+"\' isn't bound");
84 handler.startPrefixMapping(prefixes[i],nsUri );
85 }
86 }
87
88 private void undeclarePrefixes( String[] prefixes ) throws SAXException {
89 for( int i=prefixes.length-1; i>=0; i-- )
90 handler.endPrefixMapping( prefixes[i] );
91 }
92 }
93
94 public DomLoader(DomHandler<?, ResultT> dom) {
95 super(true);
96 this.dom = dom;
97 }
98
99 @Override
100 public void startElement(UnmarshallingContext.State state, TagName ea) throws SAXException {
101 UnmarshallingContext context = state.getContext();
102 if (state.target == null)
103 state.target = new State(context);
104
105 State s = (State) state.target;
106 try {
107 s.declarePrefixes(context, context.getNewlyDeclaredPrefixes());
108 s.handler.startElement(ea.uri, ea.local, ea.getQname(), ea.atts);
109 } catch (SAXException e) {
110 context.handleError(e);
111 throw e;
112 }
113 }
114
115 @Override
116 public void childElement(UnmarshallingContext.State state, TagName ea) throws SAXException {
117 state.loader = this;
118 State s = (State) state.prev.target;
119 s.depth++;
120 state.target = s;
121 }
122
123 @Override
124 public void text(UnmarshallingContext.State state, CharSequence text) throws SAXException {
125 if(text.length()==0)
126 return; // there's no point in creating an empty Text node in DOM.
127 try {
128 State s = (State) state.target;
129 s.handler.characters(text.toString().toCharArray(),0,text.length());
130 } catch( SAXException e ) {
131 state.getContext().handleError(e);
132 throw e;
133 }
134 }
135
136 @Override
137 public void leaveElement(UnmarshallingContext.State state, TagName ea) throws SAXException {
138 State s = (State) state.target;
139 UnmarshallingContext context = state.getContext();
140
141 try {
142 s.handler.endElement(ea.uri, ea.local, ea.getQname());
143 s.undeclarePrefixes(context.getNewlyDeclaredPrefixes());
144 } catch( SAXException e ) {
145 context.handleError(e);
146 throw e;
147 }
148
149 if((--s.depth)==0) {
150 // emulate the end of the document
151 try {
152 s.undeclarePrefixes(context.getAllDeclaredPrefixes());
153 s.handler.endDocument();
154 } catch( SAXException e ) {
155 context.handleError(e);
156 throw e;
157 }
158
159 // we are done
160 state.target = s.getElement();
161 }
162 }
163
164 }
|
1 /*
2 * Copyright (c) 1997, 2014, 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
82 String nsUri = context.getNamespaceURI(prefixes[i]);
83 if(nsUri==null) throw new IllegalStateException("prefix \'"+prefixes[i]+"\' isn't bound");
84 handler.startPrefixMapping(prefixes[i],nsUri );
85 }
86 }
87
88 private void undeclarePrefixes( String[] prefixes ) throws SAXException {
89 for( int i=prefixes.length-1; i>=0; i-- )
90 handler.endPrefixMapping( prefixes[i] );
91 }
92 }
93
94 public DomLoader(DomHandler<?, ResultT> dom) {
95 super(true);
96 this.dom = dom;
97 }
98
99 @Override
100 public void startElement(UnmarshallingContext.State state, TagName ea) throws SAXException {
101 UnmarshallingContext context = state.getContext();
102 if (state.getTarget() == null)
103 state.setTarget(new State(context));
104
105 State s = (State) state.getTarget();
106 try {
107 s.declarePrefixes(context, context.getNewlyDeclaredPrefixes());
108 s.handler.startElement(ea.uri, ea.local, ea.getQname(), ea.atts);
109 } catch (SAXException e) {
110 context.handleError(e);
111 throw e;
112 }
113 }
114
115 @Override
116 public void childElement(UnmarshallingContext.State state, TagName ea) throws SAXException {
117 state.setLoader(this);
118 State s = (State) state.getPrev().getTarget();
119 s.depth++;
120 state.setTarget(s);
121 }
122
123 @Override
124 public void text(UnmarshallingContext.State state, CharSequence text) throws SAXException {
125 if(text.length()==0)
126 return; // there's no point in creating an empty Text node in DOM.
127 try {
128 State s = (State) state.getTarget();
129 s.handler.characters(text.toString().toCharArray(),0,text.length());
130 } catch( SAXException e ) {
131 state.getContext().handleError(e);
132 throw e;
133 }
134 }
135
136 @Override
137 public void leaveElement(UnmarshallingContext.State state, TagName ea) throws SAXException {
138 State s = (State) state.getTarget();
139 UnmarshallingContext context = state.getContext();
140
141 try {
142 s.handler.endElement(ea.uri, ea.local, ea.getQname());
143 s.undeclarePrefixes(context.getNewlyDeclaredPrefixes());
144 } catch( SAXException e ) {
145 context.handleError(e);
146 throw e;
147 }
148
149 if((--s.depth)==0) {
150 // emulate the end of the document
151 try {
152 s.undeclarePrefixes(context.getAllDeclaredPrefixes());
153 s.handler.endDocument();
154 } catch( SAXException e ) {
155 context.handleError(e);
156 throw e;
157 }
158
159 // we are done
160 state.setTarget(s.getElement());
161 }
162 }
163
164 }
|