01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.cocoon.components.validation.jaxp;
18:
19: import javax.xml.validation.Schema;
20: import javax.xml.validation.ValidatorHandler;
21:
22: import org.apache.cocoon.components.validation.ValidationHandler;
23: import org.apache.cocoon.components.validation.impl.AbstractSchema;
24: import org.apache.cocoon.components.validation.impl.DefaultValidationHandler;
25: import org.apache.cocoon.components.validation.impl.DraconianErrorHandler;
26: import org.apache.excalibur.source.SourceValidity;
27: import org.xml.sax.ErrorHandler;
28: import org.xml.sax.SAXException;
29:
30: /**
31: * <p>An extension of the {@link AbstractSchema} class specific to the
32: * {@link JaxpSchemaParser} implementation.</p>
33: *
34: */
35: public class JaxpSchema extends AbstractSchema {
36:
37: /** <p>The wrapped JAXP {@link Schema} instance.</p> */
38: private final Schema schema;
39:
40: /**
41: * <p>Create a new {@link JaxpSchema} instance.</p>
42: *
43: * @param schema the {@link Schema} instance to wrap.
44: * @param validity the {@link SourceValidity} associated with the schema.
45: */
46: public JaxpSchema(Schema schema, SourceValidity validity) {
47: super (validity);
48: this .schema = schema;
49: }
50:
51: /**
52: * <p>Return a new {@link ValidationHandler} instance that can be used to
53: * validate an XML document by sending SAX events to it.</p>
54: *
55: * <p>The specified {@link ErrorHandler} will be notified of all warnings or
56: * errors encountered validating the SAX events sent to the returned
57: * {@link ValidationHandler}, and <b>must not</b> be <b>null</b>.</p>
58: *
59: * <p>The returned {@link ValidationHandler} can be used to validate <b>only
60: * one</b> XML document. To validate more than one document, this method should
61: * be called once for each document to validate.</p>
62: *
63: * @param handler an {@link ErrorHandler} to notify of validation errors.
64: * @return a <b>non-null</b> {@link ValidationHandler} instance.
65: * @throws SAXException if an error occurred creating the validation handler.
66: */
67: public ValidationHandler createValidator(ErrorHandler handler)
68: throws SAXException {
69: if (handler == null)
70: handler = DraconianErrorHandler.INSTANCE;
71: ValidatorHandler validator = this .schema.newValidatorHandler();
72: validator.setErrorHandler(handler);
73: return new DefaultValidationHandler(this.getValidity(),
74: validator);
75: }
76: }
|