01: /*
02: * TigerSchemaSetter.java
03: */
04:
05: package org.pnuts.xml;
06:
07: import java.io.IOException;
08: import java.io.InputStream;
09: import java.util.Properties;
10: import javax.xml.parsers.DocumentBuilderFactory;
11: import javax.xml.parsers.SAXParser;
12: import javax.xml.parsers.SAXParserFactory;
13: import javax.xml.transform.Source;
14: import javax.xml.validation.Schema;
15: import javax.xml.validation.SchemaFactory;
16: import pnuts.lang.Context;
17:
18: /**
19: *
20: */
21: public class TigerSchemaSetter implements Util.SchemaSetter {
22: static String SCHEMA_DICTIONARY = "schema.properties";
23: static Properties dic = new Properties();
24: static {
25: InputStream in = TigerSchemaSetter.class
26: .getResourceAsStream(SCHEMA_DICTIONARY);
27: if (in != null) {
28: try {
29: dic.load(in);
30: } catch (IOException e) {
31: e.printStackTrace();
32: }
33: }
34: }
35:
36: /**
37: * Constructor
38: */
39: public TigerSchemaSetter() {
40: }
41:
42: static Schema getSchema(Object schema, Context context) {
43: if (schema instanceof Schema) {
44: return (Schema) schema;
45: }
46: String str = String.valueOf(schema);
47: String ext = getExtension(str);
48: String uri = (String) dic.get(ext);
49: if (uri != null) {
50: SchemaFactory factory = SchemaFactory.newInstance(uri);
51: try {
52: Source source = Util.streamSource(schema, context);
53: return factory.newSchema(source);
54: } catch (Exception e) {
55: // skip
56: }
57: }
58: return null;
59: }
60:
61: static String getExtension(String str) {
62: int idx = str.lastIndexOf('.');
63: if (idx >= 0) {
64: return str.substring(idx + 1);
65: } else {
66: return "";
67: }
68: }
69:
70: public void setSchema(Object schema, SAXParser parser,
71: Context context) {
72: // nothing
73: }
74:
75: public void setSchema(Object schema, SAXParserFactory factory,
76: Context context) {
77: Schema s = getSchema(schema, context);
78: if (s != null) {
79: factory.setSchema(s);
80: }
81: }
82:
83: public void setSchema(Object schema,
84: DocumentBuilderFactory factory, Context context) {
85: Schema s = getSchema(schema, context);
86: if (s != null) {
87: factory.setSchema(s);
88: }
89: }
90: }
|