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: package com.sun.xml.bind.v2.runtime.unmarshaller;
037:
038: import javax.xml.namespace.NamespaceContext;
039: import javax.xml.validation.Schema;
040: import javax.xml.validation.ValidatorHandler;
041:
042: import com.sun.xml.bind.v2.util.FatalAdapter;
043:
044: import org.xml.sax.SAXException;
045:
046: /**
047: * {@link XmlVisitor} decorator that validates the events by using JAXP validation API.
048: *
049: * @author Kohsuke Kawaguchi
050: */
051: final class ValidatingUnmarshaller implements XmlVisitor,
052: XmlVisitor.TextPredictor {
053:
054: private final XmlVisitor next;
055: private final ValidatorHandler validator;
056:
057: /**
058: * {@link TextPredictor} of the next {@link XmlVisitor}.
059: */
060: private final TextPredictor predictor;
061:
062: private char[] buf = new char[256];
063:
064: /**
065: * Creates a new instance of ValidatingUnmarshaller.
066: */
067: public ValidatingUnmarshaller(Schema schema, XmlVisitor next) {
068: this .validator = schema.newValidatorHandler();
069: this .next = next;
070: this .predictor = next.getPredictor();
071: // if the user bothers to use a validator, make validation errors fatal
072: // so that it will abort unmarshalling.
073: validator.setErrorHandler(new FatalAdapter(getContext()));
074: }
075:
076: public void startDocument(LocatorEx locator,
077: NamespaceContext nsContext) throws SAXException {
078: // when nsContext is non-null, validator won't probably work correctly.
079: // should we warn?
080: validator.setDocumentLocator(locator);
081: validator.startDocument();
082: next.startDocument(locator, nsContext);
083: }
084:
085: public void endDocument() throws SAXException {
086: validator.endDocument();
087: next.endDocument();
088: }
089:
090: public void startElement(TagName tagName) throws SAXException {
091: validator.startElement(tagName.uri, tagName.local, tagName
092: .getQname(), tagName.atts);
093: next.startElement(tagName);
094: }
095:
096: public void endElement(TagName tagName) throws SAXException {
097: validator.endElement(tagName.uri, tagName.local, tagName
098: .getQname());
099: next.endElement(tagName);
100: }
101:
102: public void startPrefixMapping(String prefix, String nsUri)
103: throws SAXException {
104: validator.startPrefixMapping(prefix, nsUri);
105: next.startPrefixMapping(prefix, nsUri);
106: }
107:
108: public void endPrefixMapping(String prefix) throws SAXException {
109: validator.endPrefixMapping(prefix);
110: next.endPrefixMapping(prefix);
111: }
112:
113: public void text(CharSequence pcdata) throws SAXException {
114: int len = pcdata.length();
115: if (buf.length < len) {
116: buf = new char[len];
117: }
118: for (int i = 0; i < len; i++)
119: buf[i] = pcdata.charAt(i); // isn't this kinda slow?
120:
121: validator.characters(buf, 0, len);
122: if (predictor.expectText())
123: next.text(pcdata);
124: }
125:
126: public UnmarshallingContext getContext() {
127: return next.getContext();
128: }
129:
130: public TextPredictor getPredictor() {
131: return this ;
132: }
133:
134: // should be always invoked through TextPredictor
135: @Deprecated
136: public boolean expectText() {
137: // validator needs to make sure that there's no text
138: // even when it's not expected. So always have them
139: // send text, ignoring optimization hints from the unmarshaller
140: return true;
141: }
142: }
|