001: //
002: // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v1.0.4-b18-fcs
003: // See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
004: // Any modifications to this file will be lost upon recompilation of the source schema.
005: // Generated on: 2005.03.04 at 10:20:40 PST
006: //
007:
008: package com.nabhinc.portal.config.impl.runtime;
009:
010: import java.io.IOException;
011:
012: import javax.xml.bind.DatatypeConverter;
013: import javax.xml.bind.JAXBException;
014: import javax.xml.bind.UnmarshallerHandler;
015: import javax.xml.bind.helpers.AbstractUnmarshallerImpl;
016:
017: import org.w3c.dom.Document;
018: import org.w3c.dom.Element;
019: import org.w3c.dom.Node;
020: import org.xml.sax.InputSource;
021: import org.xml.sax.SAXException;
022: import org.xml.sax.XMLReader;
023: import org.xml.sax.helpers.DefaultHandler;
024:
025: import com.sun.xml.bind.DatatypeConverterImpl;
026: import com.sun.xml.bind.unmarshaller.DOMScanner;
027: import com.sun.xml.bind.unmarshaller.InterningXMLReader;
028: import com.sun.xml.bind.validator.DOMLocator;
029: import com.sun.xml.bind.validator.Locator;
030: import com.sun.xml.bind.validator.SAXLocator;
031:
032: /**
033: * Default Unmarshall implementation.
034: *
035: * <p>
036: * This class can be extended by the generated code to provide
037: * type-safe unmarshall methods.
038: *
039: * @author
040: * <a href="mailto:kohsuke.kawaguchi@sun.com">Kohsuke KAWAGUCHI</a>
041: */
042: public class UnmarshallerImpl extends AbstractUnmarshallerImpl {
043: /** parent JAXBContext object that created this unmarshaller */
044: private DefaultJAXBContextImpl context = null;
045:
046: private final GrammarInfo grammarInfo;
047:
048: public UnmarshallerImpl(DefaultJAXBContextImpl context,
049: GrammarInfo gi) {
050:
051: this .context = context;
052: this .grammarInfo = gi;
053:
054: // initialize datatype converter with ours
055: DatatypeConverter
056: .setDatatypeConverter(DatatypeConverterImpl.theInstance);
057: }
058:
059: public void setValidating(boolean validating) throws JAXBException {
060: super .setValidating(validating);
061: if (validating == true)
062: // make sure that we can actually load the grammar.
063: // this could be a lengthy operation if your schema is big.
064: context.getGrammar();
065: }
066:
067: public UnmarshallerHandler getUnmarshallerHandler() {
068:
069: // use InterningUnmarshallerHandler since we don't know
070: // if the caller will intern Strings before firing SAX events.
071:
072: // we don't know the Locator to be used,
073: // but SAXLocator would always be a good default,
074: // as the source of SAX2 events can always set org.xml.sax.Locator.
075: return new InterningUnmarshallerHandler(
076: createUnmarshallerHandler(new SAXLocator()));
077: }
078:
079: /**
080: * Creates and configures a new unmarshalling pipe line.
081: * Depending on the setting, we put a validator as a filter.
082: *
083: * @return
084: * A component that implements both UnmarshallerHandler
085: * and ValidationEventHandler. All the parsing errors
086: * should be reported to this error handler for the unmarshalling
087: * process to work correctly.
088: *
089: * @param locator
090: * The object that is responsible to obtain the source
091: * location information for {@link ValidationEvent}s.
092: */
093: private SAXUnmarshallerHandler createUnmarshallerHandler(
094: Locator locator) {
095:
096: SAXUnmarshallerHandler unmarshaller = new SAXUnmarshallerHandlerImpl(
097: this , grammarInfo);
098:
099: try {
100:
101: // use the simple check to determine if validation is on
102: if (isValidating()) {
103: // if the validation is turned on, insert another
104: // component into the event pipe line.
105: unmarshaller = ValidatingUnmarshaller.create(context
106: .getGrammar(), unmarshaller, locator);
107: }
108: } catch (JAXBException e) {
109: // impossible since we've already made sure that a grammar is accessible.
110: e.printStackTrace();
111: }
112:
113: return unmarshaller;
114: }
115:
116: protected Object unmarshal(XMLReader reader, InputSource source)
117: throws JAXBException {
118:
119: SAXLocator locator = new SAXLocator();
120: SAXUnmarshallerHandler handler = createUnmarshallerHandler(locator);
121:
122: reader = InterningXMLReader.adapt(reader);
123:
124: reader.setContentHandler(handler);
125: // saxErrorHandler will be set by the createUnmarshallerHandler method.
126: // configure XMLReader so that the error will be sent to it.
127: // This is essential for the UnmarshallerHandler to be able to abort
128: // unmarshalling when an error is found.
129: //
130: // Note that when this XMLReader is provided by the client code,
131: // it might be already configured to call a client error handler.
132: // This will clobber such handler, if any.
133: //
134: // Ryan noted that we might want to report errors to such a client
135: // error handler as well.
136: reader
137: .setErrorHandler(new ErrorHandlerAdaptor(handler,
138: locator));
139:
140: try {
141: reader.parse(source);
142: } catch (IOException e) {
143: throw new JAXBException(e);
144: } catch (SAXException e) {
145: throw createUnmarshalException(e);
146: }
147:
148: Object result = handler.getResult();
149:
150: // avoid keeping unnecessary references too long to let the GC
151: // reclaim more memory.
152: // setting null upsets some parsers, so use a dummy instance instead.
153: reader.setContentHandler(dummyHandler);
154: reader.setErrorHandler(dummyHandler);
155:
156: return result;
157: }
158:
159: public final Object unmarshal(Node node) throws JAXBException {
160: try {
161: DOMScanner scanner = new DOMScanner();
162: UnmarshallerHandler handler = new InterningUnmarshallerHandler(
163: createUnmarshallerHandler(new DOMLocator(scanner)));
164:
165: if (node instanceof Element)
166: scanner.parse((Element) node, handler);
167: else if (node instanceof Document)
168: scanner.parse(((Document) node).getDocumentElement(),
169: handler);
170: else
171: // no other type of input is supported
172: throw new IllegalArgumentException();
173:
174: return handler.getResult();
175: } catch (SAXException e) {
176: throw createUnmarshalException(e);
177: }
178: }
179:
180: private static final DefaultHandler dummyHandler = new DefaultHandler();
181: }
|