HTMLParsingTransformer.java 10.4 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  org.apache.cocoon.xml.sax.AbstractSAXPipe
 *  org.apache.cocoon.xml.sax.AttributesImpl
 *  org.apache.cocoon.xml.sax.SAXUtils
 *  org.apache.sling.commons.html.HtmlParser
 *  org.apache.sling.rewriter.ProcessingComponentConfiguration
 *  org.apache.sling.rewriter.ProcessingContext
 *  org.apache.sling.rewriter.Transformer
 */
package com.day.cq.rewriter.xml;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Stack;
import org.apache.cocoon.xml.sax.AbstractSAXPipe;
import org.apache.cocoon.xml.sax.AttributesImpl;
import org.apache.cocoon.xml.sax.SAXUtils;
import org.apache.sling.commons.html.HtmlParser;
import org.apache.sling.rewriter.ProcessingComponentConfiguration;
import org.apache.sling.rewriter.ProcessingContext;
import org.apache.sling.rewriter.Transformer;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.ext.LexicalHandler;

public class HTMLParsingTransformer
extends AbstractSAXPipe
implements Transformer {
    private static final String ATTR_TEXT = "text";
    private static final String ATTR_RICH_TEXT = "textIsRich";
    private static final String ATTR_RESOURCE_TYPE = "sling:resourceType";
    private static final String COLCTRL_RESOURCE_TYPE = "foundation/components/parsys/colctrl";
    private static final String ATTR_LAYOUT = "layout";
    private static final String ATTR_CONTROL_TYPE = "controlType";
    private static final String COLCTRL = "colctrl";
    private static final String TYPE_START = "start";
    private static final String TYPE_END = "end";
    private static final String TYPE_BREAK = "break";
    private final HtmlParser htmlParser;
    private int ignoreEndElement = 0;
    private final Stack<Boolean> isInRowStack = new Stack();

    public HTMLParsingTransformer(HtmlParser parser) {
        this.htmlParser = parser;
    }

    public void init(ProcessingContext context, ProcessingComponentConfiguration config) throws IOException {
        this.isInRowStack.push(false);
    }

    public void startElement(String uri, String loc, String raw, Attributes a) throws SAXException {
        if ("file".equals(loc) && "nt:file".equals(a.getValue("jcr:primaryType"))) {
            ++this.ignoreEndElement;
            return;
        }
        if (this.ignoreEndElement > 0) {
            ++this.ignoreEndElement;
            return;
        }
        String resourceType = a.getValue("sling:resourceType");
        if (resourceType != null && resourceType.endsWith("colctrl")) {
            AttributesImpl ai = new AttributesImpl(a);
            ai.removeAttribute("sling:resourceType");
            ai.addCDATAAttribute("sling:resourceType", "foundation/components/parsys/colctrl");
            String type = a.getValue("controlType");
            if (type == null || "start".equals(type)) {
                ai.removeAttribute("controlType");
                ai.addCDATAAttribute("controlType", "start");
                super.startElement("", "colctrl", "colctrl", (Attributes)ai);
                String layout = a.getValue("layout");
                int numCols = 1;
                if (layout != null) {
                    int pos = layout.indexOf(";");
                    numCols = pos == -1 ? Integer.valueOf(layout).intValue() : Integer.valueOf(layout.substring(0, pos)).intValue();
                }
                for (int i = 0; i < numCols; ++i) {
                    super.startElement("", "col", "col", SAXUtils.EMPTY_ATTRIBUTES);
                    super.endElement("", "col", "col");
                }
                super.startElement("", "row", "row", SAXUtils.EMPTY_ATTRIBUTES);
                super.startElement("", "cell", "cell", SAXUtils.EMPTY_ATTRIBUTES);
                super.startElement("", "colctrl", "colctrl", (Attributes)ai);
                super.startElement("", "col", "col", SAXUtils.EMPTY_ATTRIBUTES);
                super.endElement("", "col", "col");
                this.isInRowStack.push(true);
            } else if ("break".equals(type)) {
                super.endElement("", "colctrl", "colctrl");
                super.endElement("", "cell", "cell");
                super.startElement("", "cell", "cell", SAXUtils.EMPTY_ATTRIBUTES);
                super.startElement("", "colctrl", "colctrl", (Attributes)ai);
                super.startElement("", "col", "col", SAXUtils.EMPTY_ATTRIBUTES);
                super.endElement("", "col", "col");
            } else if ("end".equals(type)) {
                super.endElement("", "colctrl", "colctrl");
                super.endElement("", "cell", "cell");
                super.endElement("", "row", "row");
                this.isInRowStack.pop();
                super.endElement("", "colctrl", "colctrl");
            }
            ++this.ignoreEndElement;
            return;
        }
        if (this.isInRowStack.peek().booleanValue()) {
            super.startElement("", "row", "row", SAXUtils.EMPTY_ATTRIBUTES);
            super.startElement("", "cell", "cell", SAXUtils.EMPTY_ATTRIBUTES);
        }
        super.startElement(uri, loc, raw, a);
        this.isInRowStack.push(false);
        String text = a.getValue("text");
        if (text != null) {
            super.startElement("", "text", "text", (Attributes)new AttributesImpl());
            if ("true".equals(a.getValue("textIsRich")) || a.getValue("textIsRich") == null) {
                String html = "<html><body>" + text + "</body></html>";
                try {
                    this.htmlParser.parse((InputStream)new ByteArrayInputStream(html.getBytes("UTF-8")), "UTF-8", HTMLParsingTransformer.getContentFilter(this.contentHandler));
                }
                catch (UnsupportedEncodingException e) {}
            } else {
                super.characters(text.toCharArray(), 0, text.length());
            }
            super.endElement("", "text", "text");
        }
    }

    public void endElement(String uri, String loc, String raw) throws SAXException {
        if (this.ignoreEndElement > 0) {
            --this.ignoreEndElement;
            return;
        }
        super.endElement(uri, loc, raw);
        this.isInRowStack.pop();
        if (this.isInRowStack.peek().booleanValue()) {
            super.endElement("", "cell", "cell");
            super.endElement("", "row", "row");
        }
    }

    public void dispose() {
    }

    public static ContentHandler getContentFilter(ContentHandler ch) {
        if (ch instanceof LexicalHandler) {
            return new ExtendedContentFilter(ch);
        }
        return new ContentFilter(ch);
    }

    protected static class ExtendedContentFilter
    extends ContentFilter
    implements LexicalHandler {
        protected final LexicalHandler lh;

        public ExtendedContentFilter(ContentHandler ch) {
            super(ch);
            this.lh = (LexicalHandler)((Object)ch);
        }

        @Override
        public void comment(char[] arg0, int arg1, int arg2) throws SAXException {
            this.lh.comment(arg0, arg1, arg2);
        }

        @Override
        public void endCDATA() throws SAXException {
            this.lh.endCDATA();
        }

        @Override
        public void endDTD() throws SAXException {
            this.lh.endDTD();
        }

        @Override
        public void endEntity(String arg0) throws SAXException {
            this.lh.endEntity(arg0);
        }

        @Override
        public void startCDATA() throws SAXException {
            this.lh.startCDATA();
        }

        @Override
        public void startDTD(String arg0, String arg1, String arg2) throws SAXException {
            this.lh.startDTD(arg0, arg1, arg2);
        }

        @Override
        public void startEntity(String arg0) throws SAXException {
            this.lh.startEntity(arg0);
        }
    }

    protected static class ContentFilter
    implements ContentHandler {
        protected final ContentHandler ch;
        private static final String NAMESPACE = "http://www.w3.org/1999/xhtml";
        private String prefix;

        public ContentFilter(ContentHandler ch) {
            this.ch = ch;
        }

        @Override
        public void endElement(String uri, String loc, String raw) throws SAXException {
            if (!loc.equals("html") && !loc.equals("body")) {
                if (uri.equals("http://www.w3.org/1999/xhtml")) {
                    uri = "";
                }
                this.ch.endElement(uri, loc, raw);
            }
        }

        @Override
        public void startElement(String uri, String loc, String raw, Attributes a) throws SAXException {
            if (!loc.equals("html") && !loc.equals("body")) {
                if (uri.equals("http://www.w3.org/1999/xhtml")) {
                    uri = "";
                }
                this.ch.startElement(uri, loc, raw, a);
            }
        }

        @Override
        public void characters(char[] ch, int start, int length) throws SAXException {
            this.ch.characters(ch, start, length);
        }

        @Override
        public void endDocument() throws SAXException {
        }

        @Override
        public void endPrefixMapping(String prefix) throws SAXException {
            if (prefix.equals(this.prefix)) {
                this.prefix = null;
            } else {
                this.ch.endPrefixMapping(prefix);
            }
        }

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

        @Override
        public void processingInstruction(String target, String data) throws SAXException {
            this.ch.processingInstruction(target, data);
        }

        @Override
        public void setDocumentLocator(Locator locator) {
        }

        @Override
        public void skippedEntity(String name) throws SAXException {
            this.ch.skippedEntity(name);
        }

        @Override
        public void startDocument() throws SAXException {
        }

        @Override
        public void startPrefixMapping(String prefix, String uri) throws SAXException {
            if ("http://www.w3.org/1999/xhtml".equals(uri)) {
                this.prefix = prefix;
            } else {
                this.ch.startPrefixMapping(prefix, uri);
            }
        }
    }

}