DocumentHandlerToSAXAdapter.java 3.42 KB
/*
 * Decompiled with CFR 0_118.
 */
package com.day.crx.packaging.impl.proxy;

import com.day.crx.packaging.impl.proxy.AttributeList;
import com.day.crx.packaging.impl.proxy.AttributesImpl;
import com.day.crx.packaging.impl.proxy.DocumentHandler;
import java.io.IOException;
import java.util.Iterator;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;

public class DocumentHandlerToSAXAdapter
implements DocumentHandler {
    public static final String NAMESPACE = "http://com.day/cq/rewriter";
    public static final String END_SLASH_ATTR = "endSlash";
    public static final String QUOTES_ATTR = "quotes";
    private final AttributesImpl atts = new AttributesImpl();
    private final ContentHandler contentHandler;
    private boolean gotStart = false;

    public DocumentHandlerToSAXAdapter(ContentHandler handler) {
        this.contentHandler = handler;
    }

    @Override
    public void characters(char[] buffer, int offset, int length) throws IOException {
        try {
            this.contentHandler.characters(buffer, offset, length);
        }
        catch (SAXException e) {
            throw this.handle(e);
        }
    }

    @Override
    public void onStartElement(String tagName, AttributeList attributes, char[] buffer, int offset, int length, boolean endSlash) throws IOException {
        this.atts.clear();
        char[] quotes = new char[attributes.attributeCount()];
        int index = 0;
        Iterator<String> names = attributes.attributeNames();
        while (names.hasNext()) {
            String name = names.next();
            String value = attributes.getValue(name);
            if (value != null) {
                this.atts.addCDATAAttribute(name, value);
            } else {
                this.atts.addCDATAAttribute(name, "");
            }
            quotes[index] = attributes.getQuoteChar(name);
            ++index;
        }
        if (index > 0) {
            this.atts.addCDATAAttribute("http://com.day/cq/rewriter", "quotes", new String(quotes));
        }
        try {
            if (endSlash) {
                this.atts.addCDATAAttribute("endSlash", "");
            }
            this.contentHandler.startElement("", tagName, tagName, this.atts);
        }
        catch (SAXException e) {
            throw this.handle(e);
        }
    }

    @Override
    public void onEndElement(String tagName, char[] buffer, int offset, int length) throws IOException {
        try {
            this.contentHandler.endElement("", tagName, tagName);
        }
        catch (SAXException e) {
            throw this.handle(e);
        }
    }

    @Override
    public void onEnd() throws IOException {
        if (this.gotStart) {
            try {
                this.contentHandler.endDocument();
            }
            catch (SAXException e) {
                throw this.handle(e);
            }
        }
    }

    @Override
    public void onStart() throws IOException {
        this.gotStart = true;
        try {
            this.contentHandler.startDocument();
        }
        catch (SAXException e) {
            throw this.handle(e);
        }
    }

    protected final IOException handle(SAXException se) {
        if (se.getCause() != null && se.getCause() instanceof IOException) {
            return (IOException)se.getCause();
        }
        IOException ioe = new IOException("Unable to parse document");
        ioe.initCause(se);
        return ioe;
    }
}