Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / gnu / xml / validation / relaxng / RELAXNGSchemaFactory.java
blob890ca8eeb896019fdbd20c5b90bc1705248af0e8
1 /* RelaxNGSchemaFactory.java --
2 Copyright (C) 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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.
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.
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.
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.
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. */
38 package gnu.xml.validation.relaxng;
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;
58 /**
59 * Schema factory for RELAX NG grammars.
61 * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
63 public class RELAXNGSchemaFactory
64 extends SchemaFactory
67 LSResourceResolver resourceResolver;
68 ErrorHandler errorHandler;
70 public LSResourceResolver getResourceResolver()
72 return resourceResolver;
75 public void setResourceResolver(LSResourceResolver resourceResolver)
77 this.resourceResolver = resourceResolver;
80 public ErrorHandler getErrorHandler()
82 return this.errorHandler;
85 public void setErrorHandler(ErrorHandler errorHandler)
87 this.errorHandler = errorHandler;
90 public boolean isSchemaLanguageSupported(String schemaLanguage)
92 return XMLConstants.RELAXNG_NS_URI.equals(schemaLanguage);
95 public Schema newSchema()
96 throws SAXException
98 // TODO
99 throw new UnsupportedOperationException();
102 public Schema newSchema(Source[] schemata)
103 throws SAXException
105 if (schemata == null || schemata.length != 1)
106 throw new IllegalArgumentException("must specify one source");
107 // TODO multiple schemata
108 // TODO compact syntax
111 Document doc = getDocument(schemata[0]);
112 FullSyntaxBuilder builder = new FullSyntaxBuilder();
113 return builder.parse(doc);
115 catch (IOException e)
117 SAXException e2 = new SAXException(e.getMessage());
118 e2.initCause(e);
119 throw e2;
123 private static Document getDocument(Source source)
124 throws SAXException, IOException
126 if (source instanceof DOMSource)
128 Node node = ((DOMSource) source).getNode();
129 if (node != null && node instanceof Document)
130 return (Document) node;
132 String url = source.getSystemId();
135 InputSource input = new InputSource(url);
136 if (source instanceof StreamSource)
138 StreamSource streamSource = (StreamSource) source;
139 input.setByteStream(streamSource.getInputStream());
140 input.setCharacterStream(streamSource.getReader());
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);
155 catch (ParserConfigurationException e)
157 SAXException e2 = new SAXException(e.getMessage());
158 e2.initCause(e);
159 throw e2;