1:
37:
38: package ;
39:
40: import ;
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51: import ;
52: import ;
53: import ;
54: import ;
55: import ;
56: import ;
57:
58:
63: public class RELAXNGSchemaFactory
64: extends SchemaFactory
65: {
66:
67: LSResourceResolver resourceResolver;
68: ErrorHandler errorHandler;
69:
70: public LSResourceResolver getResourceResolver()
71: {
72: return resourceResolver;
73: }
74:
75: public void setResourceResolver(LSResourceResolver resourceResolver)
76: {
77: this.resourceResolver = resourceResolver;
78: }
79:
80: public ErrorHandler getErrorHandler()
81: {
82: return this.errorHandler;
83: }
84:
85: public void setErrorHandler(ErrorHandler errorHandler)
86: {
87: this.errorHandler = errorHandler;
88: }
89:
90: public boolean isSchemaLanguageSupported(String schemaLanguage)
91: {
92: return XMLConstants.RELAXNG_NS_URI.equals(schemaLanguage);
93: }
94:
95: public Schema newSchema()
96: throws SAXException
97: {
98:
99: throw new UnsupportedOperationException();
100: }
101:
102: public Schema newSchema(Source[] schemata)
103: throws SAXException
104: {
105: if (schemata == null || schemata.length != 1)
106: throw new IllegalArgumentException("must specify one source");
107:
108:
109: try
110: {
111: Document doc = getDocument(schemata[0]);
112: FullSyntaxBuilder builder = new FullSyntaxBuilder();
113: return builder.parse(doc);
114: }
115: catch (IOException e)
116: {
117: SAXException e2 = new SAXException(e.getMessage());
118: e2.initCause(e);
119: throw e2;
120: }
121: }
122:
123: private static Document getDocument(Source source)
124: throws SAXException, IOException
125: {
126: if (source instanceof DOMSource)
127: {
128: Node node = ((DOMSource) source).getNode();
129: if (node != null && node instanceof Document)
130: return (Document) node;
131: }
132: String url = source.getSystemId();
133: try
134: {
135: InputSource input = new InputSource(url);
136: if (source instanceof StreamSource)
137: {
138: StreamSource streamSource = (StreamSource) source;
139: input.setByteStream(streamSource.getInputStream());
140: input.setCharacterStream(streamSource.getReader());
141: }
142: if (input.getByteStream() == null &&
143: input.getCharacterStream() == null &&
144: url != null)
145: input.setByteStream(new URL(url).openStream());
146: DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
147: f.setNamespaceAware(true);
148: f.setCoalescing(true);
149: f.setExpandEntityReferences(true);
150: f.setIgnoringComments(true);
151: f.setIgnoringElementContentWhitespace(true);
152: DocumentBuilder b = f.newDocumentBuilder();
153: return b.parse(input);
154: }
155: catch (ParserConfigurationException e)
156: {
157: SAXException e2 = new SAXException(e.getMessage());
158: e2.initCause(e);
159: throw e2;
160: }
161: }
162:
163: }