Package com.skaringa.javaxml.handler.sax

Source Code of com.skaringa.javaxml.handler.sax.AbstractXMLReader

package com.skaringa.javaxml.handler.sax;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.xml.sax.ContentHandler;
import org.xml.sax.DTDHandler;
import org.xml.sax.EntityResolver;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;

import com.skaringa.javaxml.SerializerException;
import com.skaringa.javaxml.handler.DocumentOutputHandlerInterface;

/**
* An abstract class that implements the SAX2 XMLReader interface.
*/
public abstract class AbstractXMLReader implements org.xml.sax.XMLReader {

  private Map _featureMap = new HashMap();
  private Map _propertyMap = new HashMap();
  private EntityResolver _entityResolver;
  private DTDHandler _dtdHandler;
  private ContentHandler _contentHandler;
  private ErrorHandler _errorHandler;

  /**
   * Parse an object and generate messages for a DocumentOutputHandler
   * from its content.
   * @param obj The object to parse.
   * @param type The type of the object.
   * @param output The DocumentOutputHandler that gets the result messages.
   * @throws SerializerException If the object can't be parsed or the
   * DocumentOutputHandler can't handle the result messages.
   */
  public abstract void parseObject(
    Object obj,
    Class type,
    DocumentOutputHandlerInterface output)
    throws SerializerException;

  /**
   * Parse an input source.
   * The input source must be of the type ObjectInputSource for this concrete
   * implementation.
   * @see org.xml.sax.XMLReader#parse(org.xml.sax.InputSource)
   * @see ObjectInputSource
   * @param input The input source to parse.
   * @throws IOException IOException
   * @throws SAXException SAXException
   */
  public final void parse(InputSource input) throws IOException, SAXException {

    ContentHandler ch = getContentHandler();
    if (ch == null) {
      throw
        new SAXException("AbstractXMLReader: No content handler registered");
    }

    if (!(input instanceof ObjectInputSource)) {
      throw new SAXException("AbstractXMLReader: Invalid InputSource object");
    }

    SAXOutputHandler output = new SAXOutputHandler(ch);

    ObjectInputSource objInput = (ObjectInputSource) input;
    Object obj = objInput.getObj();
    Class type = Object.class;
    if (obj != null) {
      type = obj.getClass();
    }

    try {
      parseObject(obj, type, output);
    }
    catch (SerializerException e) {
      throw new SAXException(e);
    }
  }

  /**
   * @see org.xml.sax.XMLReader#getFeature(java.lang.String)
   */
  public final boolean getFeature(String name)
    throws SAXNotRecognizedException, SAXNotSupportedException {
    Boolean featureValue = (Boolean) _featureMap.get(name);
    return (featureValue == null) ? false : featureValue.booleanValue();
  }

  /**
   * @see org.xml.sax.XMLReader#setFeature(java.lang.String, boolean)
   */
  public final void setFeature(String name, boolean value)
    throws SAXNotRecognizedException, SAXNotSupportedException {
    _featureMap.put(name, new Boolean(value)); // keep 1.3 compatible!
  }

  /**
   * @see org.xml.sax.XMLReader#getProperty(java.lang.String)
   */
  public final Object getProperty(String name)
    throws SAXNotRecognizedException, SAXNotSupportedException {
    return _propertyMap.get(name);
  }

  /**
   * @see org.xml.sax.XMLReader#setProperty(java.lang.String, java.lang.Object)
   */
  public final void setProperty(String name, Object value)
    throws SAXNotRecognizedException, SAXNotSupportedException {
    _propertyMap.put(name, value);
  }

  /**
   * Get all properties.
   * @return Map with all properties.
   */
  public final Map getPropertyMap() {
    return _propertyMap;
  }

  /**
   * @see org.xml.sax.XMLReader#setEntityResolver(org.xml.sax.EntityResolver)
   */
  public final void setEntityResolver(EntityResolver entityResolver) {
    _entityResolver = entityResolver;
  }

  /**
   * @see org.xml.sax.XMLReader#getEntityResolver()
   */
  public final EntityResolver getEntityResolver() {
    return _entityResolver;
  }

  /**
   * @see org.xml.sax.XMLReader#setDTDHandler(org.xml.sax.DTDHandler)
   */
  public final void setDTDHandler(DTDHandler dtdHandler) {
    _dtdHandler = dtdHandler;
  }

  /**
   * @see org.xml.sax.XMLReader#getDTDHandler()
   */
  public final DTDHandler getDTDHandler() {
    return _dtdHandler;
  }

  /**
   * @see org.xml.sax.XMLReader#setContentHandler(org.xml.sax.ContentHandler)
   */
  public final void setContentHandler(ContentHandler contentHandler) {
    _contentHandler = contentHandler;
  }

  /**
   * @see org.xml.sax.XMLReader#getContentHandler()
   */
  public final ContentHandler getContentHandler() {
    return _contentHandler;
  }

  /**
   * @see org.xml.sax.XMLReader#setErrorHandler(org.xml.sax.ErrorHandler)
   */
  public final void setErrorHandler(ErrorHandler errorHandler) {
    _errorHandler = errorHandler;
  }

  /**
   * @see org.xml.sax.XMLReader#getErrorHandler()
   */
  public final ErrorHandler getErrorHandler() {
    return _errorHandler;
  }

  /**
   * @see org.xml.sax.XMLReader#parse(java.lang.String)
   */
  public final void parse(String systemId) throws IOException, SAXException {
    parse(new InputSource(systemId));
  }
}
TOP

Related Classes of com.skaringa.javaxml.handler.sax.AbstractXMLReader

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.