01: /*
02: * DefaultSchemaSetter.java
03: */
04:
05: package org.pnuts.xml;
06:
07: import javax.xml.parsers.DocumentBuilderFactory;
08: import javax.xml.parsers.SAXParser;
09: import javax.xml.parsers.SAXParserFactory;
10: import org.xml.sax.SAXException;
11: import pnuts.lang.Context;
12:
13: /**
14: *
15: */
16: public class DefaultSchemaSetter implements Util.SchemaSetter {
17:
18: /**
19: * Constructor
20: */
21: public DefaultSchemaSetter() {
22: }
23:
24: public void setSchema(Object schema, SAXParserFactory factory,
25: Context context) {
26: factory.setValidating(true);
27: }
28:
29: public void setSchema(Object schema, SAXParser parser,
30: Context context) {
31: if (String.valueOf(schema).endsWith(".xsd")) {
32: try {
33: parser
34: .setProperty(
35: "http://java.sun.com/xml/jaxp/properties/schemaLanguage",
36: "http://www.w3.org/2001/XMLSchema");
37: parser
38: .setProperty(
39: "http://java.sun.com/xml/jaxp/properties/schemaSource",
40: schema);
41: } catch (SAXException e) {
42: // skip
43: }
44: }
45: }
46:
47: public void setSchema(Object schema,
48: DocumentBuilderFactory factory, Context context) {
49: factory.setValidating(true);
50: String s = String.valueOf(schema);
51: if (s.endsWith(".xsd")) {
52: try {
53: factory
54: .setAttribute(
55: "http://java.sun.com/xml/jaxp/properties/schemaLanguage",
56: "http://www.w3.org/2001/XMLSchema");
57: factory
58: .setAttribute(
59: "http://java.sun.com/xml/jaxp/properties/schemaSource",
60: schema);
61: } catch (IllegalArgumentException e) {
62: // skip
63: }
64: }
65: }
66: }
|