001package edu.pdx.cs.joy;
002
003import org.xml.sax.*;
004
005import java.io.IOException;
006import java.io.InputStream;
007import java.net.URL;
008
009public abstract class ProjectXmlHelper implements ErrorHandler, EntityResolver {
010  private final String publicId;
011  private final String dtdFileName;
012
013  protected ProjectXmlHelper(String publicId, String dtdFileName) {
014    this.publicId = publicId;
015    this.dtdFileName = dtdFileName;
016  }
017
018  @Override
019  public void warning(SAXParseException exception) throws SAXException {
020    throw exception;
021  }
022
023  @Override
024  public void error(SAXParseException exception) throws SAXException {
025    throw exception;
026  }
027
028  @Override
029  public void fatalError(SAXParseException exception) throws SAXException {
030    throw exception;
031  }
032
033  @Override
034  public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
035    if (this.publicId.equals(publicId)) {
036      // We're resolving the external entity for the DTD
037      // Check to see if it's in the jar file.  This way we don't
038      // need to go all the way to the website to find the DTD.
039      InputStream stream =
040        ProjectXmlHelper.class.getResourceAsStream(this.dtdFileName);
041      if (stream != null) {
042        return new InputSource(stream);
043      }
044    }
045
046    return null;
047  }
048}