All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.squeakysand.commons.xml.SaxMultiplexContentHandler Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2010-2012 Craig S. Dickson (http://craigsdickson.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.squeakysand.commons.xml;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;

/**
 * Convenience class to allow multiple {@link ContentHandler} implementations to be used at the same time with the same
 * {@link org.xml.sax.XMLReader} instance.
 */
public class SaxMultiplexContentHandler implements ContentHandler {

    private static final Logger LOG = LoggerFactory.getLogger(SaxMultiplexContentHandler.class);
    private List handlers;

    /**
     * Creates a new SaxMultiplexContentHandler object.
     *
     * @param   handlers  DOCUMENT ME!
     * @throws  java.lang.IllegalArgumentException  DOCUMENT ME!
     */
    public SaxMultiplexContentHandler(List handlers) {
        if (handlers == null) {
            throw new IllegalArgumentException("handlers cannot be null");
        } else {
            this.handlers = handlers;
        }
    }

    /** {@inheritDoc} */
    @Override
    public void characters(char[] ch, int start, int length) {
        for (ContentHandler handler : handlers) {
            try {
                handler.characters(ch, start, length);
            } catch (SAXException e) {
                LOG.warn(e.getMessage(), e);
            }
        }
    }

    /** {@inheritDoc} */
    @Override
    public void endDocument() {
        for (ContentHandler handler : handlers) {
            try {
                handler.endDocument();
            } catch (SAXException e) {
                LOG.warn(e.getMessage(), e);
            }
        }
    }

    /** {@inheritDoc} */
    @Override
    public void endElement(String uri, String localName, String qName) {
        for (ContentHandler handler : handlers) {
            try {
                handler.endElement(uri, localName, qName);
            } catch (SAXException e) {
                LOG.warn(e.getMessage(), e);
            }
        }
    }

    /** {@inheritDoc} */
    @Override
    public void endPrefixMapping(String prefix) {
        for (ContentHandler handler : handlers) {
            try {
                handler.endPrefixMapping(prefix);
            } catch (SAXException e) {
                LOG.warn(e.getMessage(), e);
            }
        }
    }

    /** {@inheritDoc} */
    @Override
    public void ignorableWhitespace(char[] ch, int start, int length) {
        for (ContentHandler handler : handlers) {
            try {
                handler.ignorableWhitespace(ch, start, length);
            } catch (SAXException e) {
                LOG.warn(e.getMessage(), e);
            }
        }
    }

    /** {@inheritDoc} */
    @Override
    public void processingInstruction(String target, String data) {
        for (ContentHandler handler : handlers) {
            try {
                handler.processingInstruction(target, data);
            } catch (SAXException e) {
                LOG.warn(e.getMessage(), e);
            }
        }
    }

    /** {@inheritDoc} */
    @Override
    public void setDocumentLocator(Locator locator) {
        for (ContentHandler handler : handlers) {
            handler.setDocumentLocator(locator);
        }
    }

    /** {@inheritDoc} */
    @Override
    public void skippedEntity(String name) {
        for (ContentHandler handler : handlers) {
            try {
                handler.skippedEntity(name);
            } catch (SAXException e) {
                LOG.warn(e.getMessage(), e);
            }
        }
    }

    /** {@inheritDoc} */
    @Override
    public void startDocument() {
        for (ContentHandler handler : handlers) {
            try {
                handler.startDocument();
            } catch (SAXException e) {
                LOG.warn(e.getMessage(), e);
            }
        }
    }

    /** {@inheritDoc} */
    @Override
    public void startElement(String uri, String localName, String qName, Attributes atts) {
        for (ContentHandler handler : handlers) {
            try {
                handler.startElement(uri, localName, qName, atts);
            } catch (SAXException e) {
                LOG.warn(e.getMessage(), e);
            }
        }
    }

    /** {@inheritDoc} */
    @Override
    public void startPrefixMapping(String prefix, String uri) {
        for (ContentHandler handler : handlers) {
            try {
                handler.startPrefixMapping(prefix, uri);
            } catch (SAXException e) {
                LOG.warn(e.getMessage(), e);
            }
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy