1 |
769 |
jeremybenn |
/* RelaxNGSchemaFactory.java --
|
2 |
|
|
Copyright (C) 2006 Free Software Foundation, Inc.
|
3 |
|
|
|
4 |
|
|
This file is part of GNU Classpath.
|
5 |
|
|
|
6 |
|
|
GNU Classpath is free software; you can redistribute it and/or modify
|
7 |
|
|
it under the terms of the GNU General Public License as published by
|
8 |
|
|
the Free Software Foundation; either version 2, or (at your option)
|
9 |
|
|
any later version.
|
10 |
|
|
|
11 |
|
|
GNU Classpath is distributed in the hope that it will be useful, but
|
12 |
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 |
|
|
General Public License for more details.
|
15 |
|
|
|
16 |
|
|
You should have received a copy of the GNU General Public License
|
17 |
|
|
along with GNU Classpath; see the file COPYING. If not, write to the
|
18 |
|
|
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 |
|
|
02110-1301 USA.
|
20 |
|
|
|
21 |
|
|
Linking this library statically or dynamically with other modules is
|
22 |
|
|
making a combined work based on this library. Thus, the terms and
|
23 |
|
|
conditions of the GNU General Public License cover the whole
|
24 |
|
|
combination.
|
25 |
|
|
|
26 |
|
|
As a special exception, the copyright holders of this library give you
|
27 |
|
|
permission to link this library with independent modules to produce an
|
28 |
|
|
executable, regardless of the license terms of these independent
|
29 |
|
|
modules, and to copy and distribute the resulting executable under
|
30 |
|
|
terms of your choice, provided that you also meet, for each linked
|
31 |
|
|
independent module, the terms and conditions of the license of that
|
32 |
|
|
module. An independent module is a module which is not derived from
|
33 |
|
|
or based on this library. If you modify this library, you may extend
|
34 |
|
|
this exception to your version of the library, but you are not
|
35 |
|
|
obligated to do so. If you do not wish to do so, delete this
|
36 |
|
|
exception statement from your version. */
|
37 |
|
|
|
38 |
|
|
package gnu.xml.validation.relaxng;
|
39 |
|
|
|
40 |
|
|
import java.io.IOException;
|
41 |
|
|
import java.net.URL;
|
42 |
|
|
import javax.xml.XMLConstants;
|
43 |
|
|
import javax.xml.parsers.DocumentBuilder;
|
44 |
|
|
import javax.xml.parsers.DocumentBuilderFactory;
|
45 |
|
|
import javax.xml.parsers.ParserConfigurationException;
|
46 |
|
|
import javax.xml.transform.Source;
|
47 |
|
|
import javax.xml.transform.dom.DOMSource;
|
48 |
|
|
import javax.xml.transform.stream.StreamSource;
|
49 |
|
|
import javax.xml.validation.Schema;
|
50 |
|
|
import javax.xml.validation.SchemaFactory;
|
51 |
|
|
import org.w3c.dom.Document;
|
52 |
|
|
import org.w3c.dom.Node;
|
53 |
|
|
import org.w3c.dom.ls.LSResourceResolver;
|
54 |
|
|
import org.xml.sax.ErrorHandler;
|
55 |
|
|
import org.xml.sax.InputSource;
|
56 |
|
|
import org.xml.sax.SAXException;
|
57 |
|
|
|
58 |
|
|
/**
|
59 |
|
|
* Schema factory for RELAX NG grammars.
|
60 |
|
|
*
|
61 |
|
|
* @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
|
62 |
|
|
*/
|
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 |
|
|
// TODO
|
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 |
|
|
// TODO multiple schemata
|
108 |
|
|
// TODO compact syntax
|
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 |
|
|
}
|