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