/*******************************************************************************
* Copyright (c) 2010, Luca Conte @ BERT Project
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* �        Redistributions of source code must retain the above copyright
*          notice, this list of conditions and the following disclaimer.
*
* �        Redistributions in binary form must reproduce the above copyright
*       notice, this list of conditions and the following disclaimer in the
*       documentation and/or other materials provided with the distribution.
*
* �        Neither the name of Adobe Systems Incorporated nor the names of its
*       contributors may be used to endorse or promote products derived from
*       this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*******************************************************************************/
package org.bert.ebooks.processors;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import org.bert.ebooks.BlogEPUBlisher;
import org.bert.ebooks.BlogEntryProcessor;
import org.bert.ebooks.DeveloperUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.adobe.dp.epub.ncx.TOCEntry;
import com.adobe.dp.epub.opf.NCXResource;
import com.adobe.dp.epub.opf.OPSResource;
import com.adobe.dp.epub.opf.Publication;
import com.adobe.dp.epub.ops.OPSDocument;
import com.adobe.dp.epub.style.Stylesheet;

public class NazioneIndiana implements BlogEntryProcessor {
 
private boolean processImages=true;
 
@Override
 
public boolean isProcessImages() {
   
   
return processImages;
 
}

 
@Override
 
public void processURL(BlogEPUBlisher publisher,OPSResource post,
      TOCEntry rootTOCEntry,  String url
)
     
throws MalformedURLException, IOException {
   
Document doc = Jsoup.parse(new URL(url), 60000);
   
// get the post DIV
   
Elements e = doc.select("div.post");
   
// get chapter document
   
OPSDocument mainDoc = post.getDocument();

   
// add chapter to the table of contents with title=post's title
   
TOCEntry mainTOCEntry = publisher.getToc().createTOCEntry(e.select("h2.entry-title").text(),
        mainDoc.getRootXRef
());
    rootTOCEntry.add
(mainTOCEntry);

   
// chapter XHTML body element
   
com.adobe.dp.epub.ops.Element body = mainDoc.getBody();
   
// add a header
   
com.adobe.dp.epub.ops.Element h = mainDoc.createElement("h1");
   
//the title
   
BlogEPUBlisher.processContents(publisher.getEpub(),mainDoc, BlogEPUBlisher.getFirstElement(e.select("h2.entry-title")), h,isProcessImages());
    body.add
(h);
    h = mainDoc.createElement
("h4");
    h.add
("( "+url+" )");
   
//the entry date
   
h = mainDoc.createElement("h2");
    BlogEPUBlisher.processContents
(publisher.getEpub(),mainDoc, BlogEPUBlisher.getFirstElement(e.select("abbr.published")), h,isProcessImages());
    body.add
(h);

   
// add a paragraph
   
com.adobe.dp.epub.ops.Element paragraph = null;

    e = e.select
("div.entry-content").select("p");

   
for (Element element : e) {
     
if (element.hasText()&&!element.parent().classNames().contains("entry-yarpp")) {
       
paragraph = mainDoc.createElement("p");
        BlogEPUBlisher.processContents
(publisher.getEpub(),mainDoc, element, paragraph,isProcessImages());
        body.add
(paragraph);
     
}
    }

   
Elements c = doc.select("li.comment");
   
if (c.size() > 0) {
     
com.adobe.dp.epub.ops.Element elm = mainDoc.createElement("h2");
      elm.add
("COMMENTI");
      body.add
(elm);
      mainTOCEntry.add
(publisher.getToc().createTOCEntry("Commenti", elm.getSelfRef()));

     
for (Element ce : c) {
       
elm = mainDoc.createElement("hr");

        body.add
(elm);

        elm = mainDoc.createElement
("h3");
        BlogEPUBlisher.processContents
(publisher.getEpub(),mainDoc, BlogEPUBlisher.getFirstElement(ce.select("div.comment-author")), elm,isProcessImages());
        body.add
(elm);

        elm = mainDoc.createElement
("h4");
        BlogEPUBlisher.processContents
(publisher.getEpub(),mainDoc,
            BlogEPUBlisher.getFirstElement
(ce.select("div.comment-meta")), elm,isProcessImages());
        body.add
(elm);

       
for (Element pe : ce.select("p")) {
         
elm = mainDoc.createElement("p");
          BlogEPUBlisher.processContents
(publisher.getEpub(),mainDoc, pe, elm,isProcessImages());
          body.add
(elm);
       
}

      }
    }
   
  }

 
public void setProcessImages(boolean processImage) {
   
this.processImages = processImage;
 
}

}