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: */
18: package org.apache.lenya.xml;
19:
20: import org.apache.avalon.framework.service.ServiceManager;
21: import org.apache.cocoon.components.validation.Validator;
22: import org.apache.cocoon.xml.dom.DOMStreamer;
23: import org.apache.lenya.cms.publication.Document;
24: import org.xml.sax.ContentHandler;
25: import org.xml.sax.ErrorHandler;
26:
27: /**
28: * Validation utility.
29: */
30: public class ValidationUtil {
31:
32: /**
33: * @param manager The service manager.
34: * @param document The document to validate.
35: * @param handler The SAX error handler.
36: * @throws Exception if an error occurs.
37: */
38: public static void validate(ServiceManager manager,
39: Document document, ErrorHandler handler) throws Exception {
40:
41: org.w3c.dom.Document xmlDoc = DocumentHelper
42: .readDocument(document.getInputStream());
43: validate(manager, xmlDoc, document.getResourceType()
44: .getSchema(), handler);
45:
46: }
47:
48: /**
49: * @param manager The service manager.
50: * @param xmlDoc The XML document.
51: * @param schema The schema to use.
52: * @param handler The SAX error handler.
53: * @throws Exception if an error occurs.
54: */
55: public static void validate(ServiceManager manager,
56: org.w3c.dom.Document xmlDoc, Schema schema,
57: ErrorHandler handler) throws Exception {
58:
59: Validator validator = null;
60: try {
61: validator = (Validator) manager.lookup(Validator.ROLE);
62: ContentHandler validatorHandler = validator
63: .getValidationHandler(schema.getURI(), handler);
64:
65: DOMStreamer streamer = new DOMStreamer(validatorHandler);
66: streamer.stream(xmlDoc);
67:
68: } finally {
69: if (validator != null) {
70: manager.release(validator);
71: }
72: }
73: }
74:
75: }
|