/**
 * Licensed to Wyona
 */
package org.wyona.yanel.impl.resources.search;

import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;

import org.apache.log4j.Logger;

/**
 * Content handler for collecting the various typical XHTML content which has been generated by some parser implementation
 */
public class XHTMLBeanContentHandler implements ContentHandler {

    private static Logger log = Logger.getLogger(XHTMLBeanContentHandler.class);

    private XHTMLBean xhtmlBean;
    private boolean insideTitle = false;
    private boolean insideMeta = false;
    private boolean insidePara = false;

    /**
     * @param xhtmlBean XHTMLBean to set various content
     */
    public XHTMLBeanContentHandler(XHTMLBean xhtmlBean) {
        this.xhtmlBean = xhtmlBean;  
    }

   public void setDocumentLocator (Locator locator) { 
	  log.warn("Hello from setDocumentLocator()!"); 
	}

    public void startDocument ()
	throws SAXException  { 
	  log.warn("Hello from startDocument()!"); 
	}

    public void endDocument()
	throws SAXException {  
	  log.warn("Hello from endDocument()!"); 
	}

    /**
     *
     */
    public void startPrefixMapping (String prefix, String uri) throws SAXException {  
        log.warn("Prefix: " + prefix);
        log.warn("uri: " + uri);
    }

    /**
     *
     */
    public void endPrefixMapping (String prefix) throws SAXException  {  
        log.warn("Prefix: " + prefix);
    }

    /**
     *
     */
    public void startElement (String uri, String localName, String qName, Attributes atts) throws SAXException {  
        log.warn("Local name: " + localName);
        log.warn("qName: " + qName);
        log.warn("uri: " + uri);
        if (localName.equals("title")) {
            insideTitle = true;
        }
        if (localName.equals("meta")) {
            insideMeta = true;
        }
        if (localName.equals("p")) {
            insidePara = true;
        }
    }

    /**
     *
     */
    public void endElement (String uri, String localName, String qName) throws SAXException {  
        log.warn("Local name: " + localName); 
        if (localName.equals("title")) {
            insideTitle = false;
        }
        if (localName.equals("meta")) {
            insideMeta = false;
        }
        if (localName.equals("p")) {
            insidePara = false;
        }
    }

    /**
     *
     */
    public void characters (char ch[], int start, int length) throws SAXException {  
        if (insideTitle) {
            xhtmlBean.setTitle(new String(ch));
            log.warn("DEBUG: Title: " + new String(ch));
        }
        if (insideMeta) {
            log.warn("DEBUG: Meta: " + new String(ch));
        }
        if (insidePara) {
            log.warn("DEBUG: Paragraph: " + new String(ch));
        }
    }

    public void ignorableWhitespace (char ch[], int start, int length)
	throws SAXException {  
	  log.warn("Hello from ignorableWhitespace()!"); 
	}

    public void processingInstruction (String target, String data)
	throws SAXException {  
	  log.warn("Hello from processingInstruction()!"); 
	}

    public void skippedEntity (String name)
	throws SAXException {  
	  log.warn("Hello from skippedEntity()!"); 
	}
}

@@ -844,16 +844,17 @@
   %7D%0A%0A   
+ 
 public v
@@ -943,32 +943,55 @@
 ator()!%22); %0A%09%7D%0A%0A
+    /**%0A     *%0A     */%0A
     public void 
@@ -1006,18 +1006,17 @@
 ument ()
-%0A%09
+ 
 throws S
@@ -1027,27 +1027,34 @@
 ception  %7B %0A
-%09
   
+      //
 log.warn(%22He
@@ -1043,32 +1043,39 @@
     //log.warn(%22
+DEBUG: 
 Hello from start
@@ -1082,33 +1082,36 @@
 Document()!%22); %0A
-%09
+    
 %7D%0A%0A    public vo
@@ -1126,18 +1126,17 @@
 cument()
-%0A%09
+ 
 throws S
@@ -1143,35 +1143,42 @@
 AXException %7B  %0A
-%09
   
+      //
 log.warn(%22Hello 
@@ -1163,32 +1163,39 @@
     //log.warn(%22
+DEBUG: 
 Hello from endDo
@@ -1200,33 +1200,36 @@
 Document()!%22); %0A
-%09
+    
 %7D%0A%0A    /**%0A     
@@ -1317,32 +1317,35 @@
 AXException %7B  %0A
+/*%0A
         log.warn
@@ -1338,32 +1338,39 @@
       log.warn(%22
+DEBUG: 
 Prefix: %22 + pref
@@ -1384,32 +1384,39 @@
       log.warn(%22
+DEBUG: 
 uri: %22 + uri);%0A 
@@ -1410,24 +1410,27 @@
 : %22 + uri);%0A
+*/%0A
     %7D%0A%0A    /
@@ -1521,32 +1521,34 @@
 on  %7B  %0A        
+//
 log.warn(%22Prefix
@@ -1541,16 +1541,23 @@
 g.warn(%22
+DEBUG: 
 Prefix: 
@@ -1706,32 +1706,35 @@
 AXException %7B  %0A
+/*%0A
         log.warn
@@ -1727,32 +1727,39 @@
       log.warn(%22
+DEBUG: 
 Local name: %22 + 
@@ -1788,16 +1788,23 @@
 g.warn(%22
+DEBUG: 
 qName: %22
@@ -1832,16 +1832,23 @@
 g.warn(%22
+DEBUG: 
 uri: %22 +
@@ -1854,16 +1854,19 @@
 + uri);%0A
+*/%0A
         
@@ -2225,32 +2225,34 @@
 ion %7B  %0A        
+//
 log.warn(%22Local 
@@ -2245,16 +2245,23 @@
 g.warn(%22
+DEBUG: 
 Local na
@@ -2721,32 +2721,34 @@
 ));%0A            
+//
 log.warn(%22DEBUG:
@@ -2816,32 +2816,34 @@
 ) %7B%0A            
+//
 log.warn(%22DEBUG:
@@ -2910,32 +2910,34 @@
 ) %7B%0A            
+//
 log.warn(%22DEBUG:
@@ -3077,32 +3077,34 @@
 xception %7B  %0A%09  
+//
 log.warn(%22Hello 
@@ -3089,32 +3089,39 @@
 %0A%09  //log.warn(%22
+DEBUG: 
 Hello from ignor

debug statements commented and formatting