001 /* This file is part of the project "Hilbert II" - http://www.qedeq.org
002 *
003 * Copyright 2000-2013, Michael Meyling <mime@qedeq.org>.
004 *
005 * "Hilbert II" is free software; you can redistribute
006 * it and/or modify it under the terms of the GNU General Public
007 * License as published by the Free Software Foundation; either
008 * version 2 of the License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013 * GNU General Public License for more details.
014 */
015
016 package org.qedeq.kernel.xml.parser;
017
018 import java.io.FileNotFoundException;
019 import java.io.InputStream;
020
021 import org.qedeq.base.trace.Trace;
022 import org.qedeq.kernel.bo.KernelContext;
023 import org.qedeq.kernel.xml.handler.common.SaxDefaultHandler;
024 import org.xml.sax.EntityResolver;
025 import org.xml.sax.InputSource;
026 import org.xml.sax.SAXException;
027 import org.xml.sax.SAXParseException;
028
029
030 /**
031 * Resolve QEDEQ module XML schema.
032 *
033 * @version $Revision: 1.1 $
034 * @author Michael Meyling
035 */
036 public class SaxEntityResolver implements EntityResolver {
037
038 /** This class. */
039 private static final Class CLASS = SaxEntityResolver.class;
040
041 /** Handler that parses the document. */
042 private final SaxDefaultHandler handler;
043
044 /**
045 * Constructor.
046 *
047 * @param handler This one does the parsing.
048 */
049 public SaxEntityResolver(final SaxDefaultHandler handler) {
050 this.handler = handler;
051 }
052
053 /* (non-Javadoc)
054 * @see org.xml.sax.EntityResolver#resolveEntity(java.lang.String, java.lang.String)
055 */
056 public InputSource resolveEntity(final String publicId, final String systemId)
057 throws FileNotFoundException, SAXException {
058 final String method = "resolveEntity";
059 Trace.param(CLASS, this, method, "systemId", systemId);
060 Trace.param(CLASS, this, method, "publicId", publicId);
061 if ((systemId == null)) {
062 return null;
063 }
064 if (systemId.equals("http://www.qedeq.org/" + KernelContext.getInstance()
065 .getKernelVersionDirectory() + "/xml/qedeq.xsd")) {
066 InputStream in = SaxEntityResolver.class.getResourceAsStream(
067 "/org/qedeq/kernel/xml/schema/qedeq.xsd");
068 if (in == null) {
069 throw new FileNotFoundException("/org/qedeq/kernel/xml/schema/qedeq.xsd");
070 }
071 InputSource inputSource = new InputSource(in);
072 inputSource.setPublicId(publicId);
073 inputSource.setSystemId(systemId);
074 return inputSource;
075 } else if (systemId.equals("http://www.qedeq.org/"
076 + KernelContext.getInstance().getKernelVersionDirectory()
077 + "/xml/parser.xsd")) {
078 InputStream in = SaxEntityResolver.class.getResourceAsStream(
079 "/org/qedeq/kernel/xml/schema/parser.xsd");
080 if (in == null) {
081 throw new FileNotFoundException("/org/qedeq/kernel/xml/schema/parser.xsd");
082 }
083 InputSource inputSource = new InputSource(in);
084 inputSource.setPublicId(publicId);
085 inputSource.setSystemId(systemId);
086 return inputSource;
087 }
088 Trace.trace(CLASS, this, method, "unknown entity");
089 SAXParseException sax = handler.createSAXParseException(
090 "this kernel supports only the following XSDs: "
091 + "\"http://www.qedeq.org/" + KernelContext.getInstance()
092 .getKernelVersionDirectory() + "/xml/qedeq.xsd\"" + " and \n"
093 + "\"http://www.qedeq.org/"
094 + KernelContext.getInstance().getKernelVersionDirectory()
095 + "/xml/parser.xsd\" "
096 + "\nbut the document \"" + handler.getUrl() + "\" referenced \n\"" + systemId + "\"");
097 throw sax;
098 // LATER mime 20070604: this error should have correct location information,
099 // but is hasn't! this problem should be solved later...
100 }
101 }
|