001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.xml.bind.v2.runtime.unmarshaller;
038:
039: import javax.xml.bind.annotation.DomHandler;
040: import javax.xml.transform.Result;
041: import javax.xml.transform.sax.TransformerHandler;
042:
043: import com.sun.xml.bind.v2.runtime.JAXBContextImpl;
044:
045: import org.xml.sax.SAXException;
046:
047: /**
048: * Loads a DOM.
049: *
050: * @author Kohsuke Kawaguchi
051: */
052: public class DomLoader<ResultT extends Result> extends Loader {
053:
054: private final DomHandler<?, ResultT> dom;
055:
056: /**
057: * Used to capture the state.
058: *
059: * This instance is created for each unmarshalling episode.
060: */
061: private final class State {
062: /** This handler will receive SAX events. */
063: private final TransformerHandler handler = JAXBContextImpl
064: .createTransformerHandler();
065:
066: /** {@link #handler} will produce this result. */
067: private final ResultT result;
068:
069: // nest level of elements.
070: int depth = 1;
071:
072: public State(UnmarshallingContext context) throws SAXException {
073: result = dom.createUnmarshaller(context);
074:
075: handler.setResult(result);
076:
077: // emulate the start of documents
078: try {
079: handler.setDocumentLocator(context.getLocator());
080: handler.startDocument();
081: declarePrefixes(context, context
082: .getAllDeclaredPrefixes());
083: } catch (SAXException e) {
084: context.handleError(e);
085: throw e;
086: }
087: }
088:
089: public Object getElement() {
090: return dom.getElement(result);
091: }
092:
093: private void declarePrefixes(UnmarshallingContext context,
094: String[] prefixes) throws SAXException {
095: for (int i = prefixes.length - 1; i >= 0; i--) {
096: String nsUri = context.getNamespaceURI(prefixes[i]);
097: if (nsUri == null)
098: throw new IllegalStateException("prefix \'"
099: + prefixes[i] + "\' isn't bound");
100: handler.startPrefixMapping(prefixes[i], nsUri);
101: }
102: }
103:
104: private void undeclarePrefixes(String[] prefixes)
105: throws SAXException {
106: for (int i = prefixes.length - 1; i >= 0; i--)
107: handler.endPrefixMapping(prefixes[i]);
108: }
109: }
110:
111: public DomLoader(DomHandler<?, ResultT> dom) {
112: super (true);
113: this .dom = dom;
114: }
115:
116: public void startElement(UnmarshallingContext.State state,
117: TagName ea) throws SAXException {
118: UnmarshallingContext context = state.getContext();
119: if (state.target == null)
120: state.target = new State(context);
121:
122: State s = (State) state.target;
123: try {
124: s.declarePrefixes(context, context
125: .getNewlyDeclaredPrefixes());
126: s.handler.startElement(ea.uri, ea.local, ea.getQname(),
127: ea.atts);
128: } catch (SAXException e) {
129: context.handleError(e);
130: throw e;
131: }
132: }
133:
134: public void childElement(UnmarshallingContext.State state,
135: TagName ea) throws SAXException {
136: state.loader = this ;
137: State s = (State) state.prev.target;
138: s.depth++;
139: state.target = s;
140: }
141:
142: public void text(UnmarshallingContext.State state, CharSequence text)
143: throws SAXException {
144: if (text.length() == 0)
145: return; // there's no point in creating an empty Text node in DOM.
146: try {
147: State s = (State) state.target;
148: s.handler.characters(text.toString().toCharArray(), 0, text
149: .length());
150: } catch (SAXException e) {
151: state.getContext().handleError(e);
152: throw e;
153: }
154: }
155:
156: public void leaveElement(UnmarshallingContext.State state,
157: TagName ea) throws SAXException {
158: State s = (State) state.target;
159: UnmarshallingContext context = state.getContext();
160:
161: try {
162: s.handler.endElement(ea.uri, ea.local, ea.getQname());
163: s.undeclarePrefixes(context.getNewlyDeclaredPrefixes());
164: } catch (SAXException e) {
165: context.handleError(e);
166: throw e;
167: }
168:
169: if ((--s.depth) == 0) {
170: // emulate the end of the document
171: try {
172: s.undeclarePrefixes(context.getAllDeclaredPrefixes());
173: s.handler.endDocument();
174: } catch (SAXException e) {
175: context.handleError(e);
176: throw e;
177: }
178:
179: // we are done
180: state.target = s.getElement();
181: }
182: }
183:
184: }
|