SAXWriter.java 4.07 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  org.apache.felix.scr.annotations.Component
 */
package com.day.cq.rewriter.htmlparser;

import com.day.cq.rewriter.pipeline.Serializer;
import com.day.cq.rewriter.processor.ProcessingComponentConfiguration;
import com.day.cq.rewriter.processor.ProcessingContext;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import org.apache.felix.scr.annotations.Component;
import org.xml.sax.Attributes;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;

@Deprecated
@Component(factory="com.day.cq.rewriter.pipeline.Serializer/htmlwriter")
public class SAXWriter
implements Serializer {
    private PrintWriter delegatee;
    private List<String> emptyTags;
    private static final List<String> DEFAULT_EMPTY_TAGS = new ArrayList<String>();

    @Override
    public void init(ProcessingContext pipelineContext, ProcessingComponentConfiguration config) throws IOException {
        PrintWriter writer = pipelineContext.getWriter();
        if (writer == null) {
            throw new IllegalArgumentException("Writer must not be null");
        }
        this.delegatee = writer;
        this.emptyTags = DEFAULT_EMPTY_TAGS;
    }

    @Override
    public void endDocument() throws SAXException {
        this.delegatee.flush();
    }

    @Override
    public void startElement(String uri, String localName, String name, Attributes atts) throws SAXException {
        boolean endSlash = false;
        this.delegatee.write(60);
        this.delegatee.write(localName);
        String quotesString = atts.getValue("http://com.day/cq/rewriter", "quotes");
        for (int i = 0; i < atts.getLength(); ++i) {
            if ("endSlash".equals(atts.getQName(i))) {
                endSlash = true;
                continue;
            }
            if ("http://com.day/cq/rewriter".equals(atts.getURI(i))) continue;
            this.delegatee.write(32);
            this.delegatee.write(atts.getLocalName(i));
            String value = atts.getValue(i);
            if (value == null) continue;
            this.delegatee.write(61);
            int quoteChar = quotesString != null && quotesString.length() > i ? (int)quotesString.charAt(i) : 34;
            this.delegatee.write(quoteChar);
            this.delegatee.write(value);
            this.delegatee.write(quoteChar);
        }
        if (endSlash) {
            this.delegatee.write("/");
        }
        this.delegatee.write(">");
    }

    @Override
    public void endElement(String uri, String localName, String name) throws SAXException {
        if (!this.emptyTags.contains(localName)) {
            this.delegatee.write("</");
            this.delegatee.write(localName);
            this.delegatee.write(62);
        }
    }

    @Override
    public void characters(char[] buffer, int offset, int length) throws SAXException {
        if (length == 0) {
            this.delegatee.flush();
        } else {
            this.delegatee.write(buffer, offset, length);
        }
    }

    @Override
    public void endPrefixMapping(String prefix) throws SAXException {
    }

    @Override
    public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
    }

    @Override
    public void processingInstruction(String target, String data) throws SAXException {
    }

    @Override
    public void setDocumentLocator(Locator locator) {
    }

    @Override
    public void skippedEntity(String name) throws SAXException {
    }

    @Override
    public void startDocument() throws SAXException {
    }

    @Override
    public void startPrefixMapping(String prefix, String uri) throws SAXException {
    }

    static {
        DEFAULT_EMPTY_TAGS.add("br");
        DEFAULT_EMPTY_TAGS.add("area");
        DEFAULT_EMPTY_TAGS.add("link");
        DEFAULT_EMPTY_TAGS.add("img");
        DEFAULT_EMPTY_TAGS.add("param");
        DEFAULT_EMPTY_TAGS.add("hr");
        DEFAULT_EMPTY_TAGS.add("input");
        DEFAULT_EMPTY_TAGS.add("col");
        DEFAULT_EMPTY_TAGS.add("base");
        DEFAULT_EMPTY_TAGS.add("meta");
    }
}