/*
 * (C) Copyright 2006-2008 Nuxeo SAS (http://nuxeo.com/) and contributors.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * (LGPL) version 2.1 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * Contributors:
 *     troger
 *
 * $Id$
 */

package org.nuxeo.ecm.platform.transform.plugin.html;

import org.apache.xerces.parsers.AbstractSAXParser;
import org.apache.xerces.xni.Augmentations;
import org.apache.xerces.xni.NamespaceContext;
import org.apache.xerces.xni.XMLLocator;
import org.apache.xerces.xni.XMLString;
import org.apache.xerces.xni.XNIException;
import org.cyberneko.html.HTMLConfiguration;

/**
 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
 * 
 */
public class HtmlParser extends AbstractSAXParser {

    private StringBuffer buffer;

    public HtmlParser() {

        super(new HTMLConfiguration());
    }

    public void startDocument(XMLLocator arg0, String arg1,
            NamespaceContext arg2, Augmentations arg3) throws XNIException {

        super.startDocument(arg0, arg1, arg2, arg3);

        buffer = new StringBuffer();
    }

    public void characters(XMLString xmlString, Augmentations augmentations)
            throws XNIException {

        super.characters(xmlString, augmentations);

        buffer.append(xmlString.toString());
    }

    private String filterAndJoin(String text) {

        boolean space = false;
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < text.length(); i++) {
            char c = text.charAt(i);

            if ((c == '\n') || (c == ' ') || Character.isWhitespace(c)) {
                if (space) {
                    continue;
                } else {
                    space = true;
                    buffer.append(' ');
                    continue;
                }
            } else {
                if (!Character.isLetter(c) && !Character.isDigit(c)) {
                    if (!space) {
                        space = true;
                        buffer.append(' ');
                        continue;
                    }
                    continue;
                }
            }
            space = false;
            buffer.append(c);
        }
        return buffer.toString();
    }

    /**
     * Returns parsed content
     * 
     * @return String Parsed content
     */
    public String getContents() {

        String text = filterAndJoin(buffer.toString());
        return text;
    }

}

@@ -701,16 +701,101 @@
 .html;%0A%0A
+import org.apache.commons.logging.Log;%0Aimport org.apache.commons.logging.LogFactory;%0A
 import o
@@ -1097,16 +1097,49 @@
 uration;
+%0Aimport org.xml.sax.SAXException;
 %0A%0A/**%0A *
@@ -1200,17 +1200,16 @@
 r%3C/a%3E%0A *
- 
 %0A */%0Apub
@@ -1254,24 +1254,97 @@
 AXParser %7B%0A%0A
+    private static final Log log = LogFactory.getLog(HtmlParser.class);%0A%0A
     private 
@@ -1387,25 +1387,24 @@
 lParser() %7B%0A
-%0A
         supe
@@ -1427,24 +1427,457 @@
 uration());%0A
+        try %7B%0A            // make sure we do not download the DTD URI%0A            setFeature(%22http://xml.org/sax/features/validation%22, false);%0A            setFeature(%0A                    %22http://apache.org/xml/features/nonvalidating/load-external-dtd%22,%0A                    false);%0A        %7D catch (SAXException e) %7B%0A            log.debug(%22Could not switch parser to non-validating: %22 +%0A                    e.getMessage());%0A        %7D%0A
     %7D%0A%0A    p
@@ -3300,17 +3300,16 @@
 t%0A     *
- 
 %0A     * 

Make sure we do not download the DTD URI when parsing HTML