View Javadoc

1   /* This file is part of the project "Hilbert II" - http://www.qedeq.org" target="alexandria_uri">http://www.qedeq.org
2    *
3    * Copyright 2000-2014,  Michael Meyling <mime@qedeq.org>.
4    *
5    * "Hilbert II" is free software; you can redistribute
6    * it and/or modify it under the terms of the GNU General Public
7    * License as published by the Free Software Foundation; either
8    * version 2 of the License, or (at your option) any later version.
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   * GNU General Public License for more details.
14   */
15  
16  package org.qedeq.kernel.xml.parser;
17  
18  import java.io.FileNotFoundException;
19  import java.io.InputStream;
20  
21  import org.qedeq.base.trace.Trace;
22  import org.qedeq.kernel.bo.KernelContext;
23  import org.qedeq.kernel.xml.handler.common.SaxDefaultHandler;
24  import org.xml.sax.EntityResolver;
25  import org.xml.sax.InputSource;
26  import org.xml.sax.SAXException;
27  import org.xml.sax.SAXParseException;
28  
29  
30  /**
31   * Resolve QEDEQ module XML schema.
32   *
33   * @version $Revision: 1.1 $
34   * @author Michael Meyling
35   */
36  public class SaxEntityResolver implements EntityResolver {
37  
38      /** This class. */
39      private static final Class CLASS = SaxEntityResolver.class;
40  
41      /** Handler that parses the document. */
42      private final SaxDefaultHandler handler;
43  
44      /**
45       * Constructor.
46       *
47       * @param   handler     This one does the parsing.
48       */
49      public SaxEntityResolver(final SaxDefaultHandler handler) {
50          this.handler = handler;
51      }
52  
53      /* (non-Javadoc)
54       * @see org.xml.sax.EntityResolver#resolveEntity(java.lang.String, java.lang.String)
55       */
56      public InputSource resolveEntity(final String publicId, final String systemId)
57              throws FileNotFoundException, SAXException {
58          final String method = "resolveEntity";
59          Trace.param(CLASS, this, method, "systemId", systemId);
60          Trace.param(CLASS, this, method, "publicId", publicId);
61          if ((systemId == null)) {
62              return null;
63          }
64          if (systemId.equals("http://www.qedeq.org/" + KernelContext.getInstance()
65                  .getKernelVersionDirectory() + "/xml/qedeq.xsd")) {
66              InputStream in = SaxEntityResolver.class.getResourceAsStream(
67                  "/org/qedeq/kernel/xml/schema/qedeq.xsd");
68              if (in == null) {
69                  throw new FileNotFoundException("/org/qedeq/kernel/xml/schema/qedeq.xsd");
70              }
71              InputSource inputSource = new InputSource(in);
72              inputSource.setPublicId(publicId);
73              inputSource.setSystemId(systemId);
74              return inputSource;
75          } else if (systemId.equals("http://www.qedeq.org/"
76              + KernelContext.getInstance().getKernelVersionDirectory()
77                  + "/xml/parser.xsd")) {
78              InputStream in = SaxEntityResolver.class.getResourceAsStream(
79                  "/org/qedeq/kernel/xml/schema/parser.xsd");
80              if (in == null) {
81                  throw new FileNotFoundException("/org/qedeq/kernel/xml/schema/parser.xsd");
82              }
83              InputSource inputSource = new InputSource(in);
84              inputSource.setPublicId(publicId);
85              inputSource.setSystemId(systemId);
86              return inputSource;
87          }
88          Trace.trace(CLASS, this, method, "unknown entity");
89          SAXParseException sax = handler.createSAXParseException(
90              "this kernel supports only the following XSDs: "
91              + "\"http://www.qedeq.org/" + KernelContext.getInstance()
92              .getKernelVersionDirectory() + "/xml/qedeq.xsd\"" + " and \n"
93              + "\"http://www.qedeq.org/"
94              + KernelContext.getInstance().getKernelVersionDirectory()
95              + "/xml/parser.xsd\" "
96              + "\nbut the document \"" + handler.getUrl() + "\" referenced \n\"" + systemId + "\"");
97          throw sax;
98          // LATER mime 20070604: this error should have correct location information,
99          // but is hasn't! this problem should be solved later...
100     }
101 }