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 
  18 package com.sun.org.apache.xml.internal.resolver.readers;
  19 
  20 import java.io.IOException;
  21 
  22 import org.xml.sax.*;
  23 import org.xml.sax.helpers.*;
  24 
  25 /**
  26  * An entity-resolving DefaultHandler.
  27  *
  28  * <p>This class provides a SAXParser DefaultHandler that performs
  29  * entity resolution.
  30  * </p>
  31  *
  32  * @author Norman Walsh
  33  * <a href="mailto:Norman.Walsh@Sun.COM">Norman.Walsh@Sun.COM</a>
  34  */
  35 public class SAXParserHandler extends DefaultHandler {
  36   private EntityResolver er = null;
  37   private ContentHandler ch = null;
  38 
  39   public SAXParserHandler() {
  40     super();
  41   }
  42 
  43   public void setEntityResolver(EntityResolver er) {
  44     this.er = er;
  45   }
  46 
  47   public void setContentHandler(ContentHandler ch) {
  48     this.ch = ch;
  49   }
  50 
  51   // Entity Resolver
  52   public InputSource resolveEntity(String publicId, String systemId)
  53     throws SAXException {
  54 
  55     if (er != null) {
  56       try {
  57         return er.resolveEntity(publicId, systemId);
  58       } catch (IOException e) {
  59           System.out.println("resolveEntity threw IOException!");
  60           return null;
  61       }
  62     } else {
  63       return null;
  64     }
  65   }
  66 
  67   // Content Handler
  68   public void characters(char[] ch, int start, int length)
  69     throws SAXException {
  70     if (this.ch != null) {
  71       this.ch.characters(ch, start, length);
  72     }
  73   }
  74 
  75   public void endDocument()
  76     throws SAXException {
  77     if (ch != null) {
  78       ch.endDocument();
  79     }
  80   }
  81 
  82   public void endElement(String namespaceURI, String localName, String qName)
  83     throws SAXException {
  84     if (ch != null) {
  85       ch.endElement(namespaceURI, localName, qName);
  86     }
  87   }
  88 
  89   public void endPrefixMapping(String prefix)
  90     throws SAXException {
  91     if (ch != null) {
  92       ch.endPrefixMapping(prefix);
  93     }
  94   }
  95 
  96   public void ignorableWhitespace(char[] ch, int start, int length)
  97     throws SAXException {
  98     if (this.ch != null) {
  99       this.ch.ignorableWhitespace(ch, start, length);
 100     }
 101   }
 102 
 103   public void processingInstruction(String target, String data)
 104     throws SAXException {
 105     if (ch != null) {
 106       ch.processingInstruction(target, data);
 107     }
 108   }
 109 
 110   public void setDocumentLocator(Locator locator) {
 111     if (ch != null) {
 112       ch.setDocumentLocator(locator);
 113     }
 114   }
 115 
 116   public void skippedEntity(String name)
 117     throws SAXException {
 118     if (ch != null) {
 119       ch.skippedEntity(name);
 120     }
 121   }
 122 
 123   public void startDocument()
 124     throws SAXException {
 125     if (ch != null) {
 126       ch.startDocument();
 127     }
 128   }
 129 
 130   public void startElement(String namespaceURI, String localName,
 131                            String qName, Attributes atts)
 132     throws SAXException {
 133     if (ch != null) {
 134       ch.startElement(namespaceURI, localName, qName, atts);
 135     }
 136   }
 137 
 138   public void startPrefixMapping(String prefix, String uri)
 139     throws SAXException {
 140     if (ch != null) {
 141       ch.startPrefixMapping(prefix, uri);
 142     }
 143   }
 144 }