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.jing;
18:
19: import org.apache.cocoon.components.validation.ValidationHandler;
20: import org.apache.cocoon.components.validation.impl.AbstractSchema;
21: import org.apache.cocoon.components.validation.impl.DefaultValidationHandler;
22: import org.apache.cocoon.components.validation.impl.DraconianErrorHandler;
23: import org.apache.excalibur.source.SourceValidity;
24: import org.xml.sax.ContentHandler;
25: import org.xml.sax.ErrorHandler;
26: import org.xml.sax.SAXException;
27:
28: import com.thaiopensource.util.PropertyMap;
29: import com.thaiopensource.util.PropertyMapBuilder;
30: import com.thaiopensource.validate.Schema;
31: import com.thaiopensource.validate.ValidateProperty;
32: import com.thaiopensource.validate.Validator;
33:
34: /**
35: * <p>An extension of {@link AbstractSchema} used by the {@link JingSchemaParser}
36: * implementation.</p>
37: *
38: */
39: public class JingSchema extends AbstractSchema {
40:
41: /** <p>The original schema instance to wrap.</p> */
42: private final Schema schema;
43:
44: /**
45: * <p>Create a new {@link JingSchema} instance.</p>
46: *
47: * @param schema the JING original schema to wrap.
48: * @param validity the {@link SourceValidity} associated with the schema.
49: */
50: protected JingSchema(Schema schema, SourceValidity validity) {
51: super (validity);
52: this .schema = schema;
53: }
54:
55: /**
56: * <p>Return a new {@link ValidationHandler} instance that can be used to send
57: * SAX events to for proper validation.</p>
58: *
59: * <p>The specified {@link ErrorHandler} will be notified of all warnings or
60: * errors encountered validating the SAX events sent to the returned
61: * {@link ValidationHandler}.</p>
62: *
63: * @param errorHandler 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 errorHandler)
68: throws SAXException {
69: if (errorHandler == null)
70: errorHandler = DraconianErrorHandler.INSTANCE;
71: final PropertyMapBuilder builder = new PropertyMapBuilder();
72: ValidateProperty.ERROR_HANDLER.put(builder, errorHandler);
73: final PropertyMap properties = builder.toPropertyMap();
74: final Validator validator = this .schema
75: .createValidator(properties);
76: final ContentHandler handler = validator.getContentHandler();
77: return new DefaultValidationHandler(this.getValidity(), handler);
78: }
79: }
|