You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

XMLSchemaResolver.java 3.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.intermediate;
  19. import java.io.InputStream;
  20. import java.util.MissingResourceException;
  21. import javax.xml.XMLConstants;
  22. import org.w3c.dom.bootstrap.DOMImplementationRegistry;
  23. import org.w3c.dom.ls.DOMImplementationLS;
  24. import org.w3c.dom.ls.LSInput;
  25. import org.w3c.dom.ls.LSResourceResolver;
  26. /**
  27. * A resource resolver that returns a cached instance of the XML Schema, that can normally
  28. * be found at {@linkplain http://www.w3.org/2001/xml.xsd}. This can be used to avoid
  29. * unnecessary connection to the W3C website.
  30. */
  31. final class XMLSchemaResolver implements LSResourceResolver {
  32. private static final String XML_SCHEMA_SYSTEM_ID = "http://www.w3.org/2001/xml.xsd";
  33. private static final LSInput XML_SCHEMA_INPUT;
  34. private static final XMLSchemaResolver INSTANCE = new XMLSchemaResolver();
  35. private XMLSchemaResolver() { }
  36. static {
  37. DOMImplementationRegistry registry = getDOMImplementationRegistry();
  38. DOMImplementationLS impl
  39. = (DOMImplementationLS) registry.getDOMImplementation("LS 3.0");
  40. XML_SCHEMA_INPUT = impl.createLSInput();
  41. InputStream xmlSchema = loadXMLSchema();
  42. XML_SCHEMA_INPUT.setByteStream(xmlSchema);
  43. }
  44. private static DOMImplementationRegistry getDOMImplementationRegistry() {
  45. try {
  46. return DOMImplementationRegistry.newInstance();
  47. } catch (ClassCastException e) {
  48. throw new ExceptionInInitializerError(e);
  49. } catch (ClassNotFoundException e) {
  50. throw new ExceptionInInitializerError(e);
  51. } catch (InstantiationException e) {
  52. throw new ExceptionInInitializerError(e);
  53. } catch (IllegalAccessException e) {
  54. throw new ExceptionInInitializerError(e);
  55. }
  56. }
  57. private static InputStream loadXMLSchema() {
  58. String xmlSchemaResource = "xml.xsd";
  59. InputStream xmlSchema = XMLSchemaResolver.class.getResourceAsStream(xmlSchemaResource);
  60. if (xmlSchema == null) {
  61. throw new MissingResourceException("Schema for XML namespace not found."
  62. + " Did you run ant junit-intermediate-format?",
  63. XMLSchemaResolver.class.getName(), xmlSchemaResource);
  64. }
  65. return xmlSchema;
  66. }
  67. public static XMLSchemaResolver getInstance() {
  68. return INSTANCE;
  69. }
  70. public LSInput resolveResource(String type, String namespaceURI, String publicId,
  71. String systemId, String baseURI) {
  72. if (XMLConstants.XML_NS_URI.equals(namespaceURI) && XML_SCHEMA_SYSTEM_ID.equals(systemId)) {
  73. return XML_SCHEMA_INPUT;
  74. } else {
  75. return null;
  76. }
  77. }
  78. }