package de.auxnet;

/*
 * (C) Copyright 2016 Dr. Maximilian Kalus (https://auxnet.de/)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Contributors:
 *     Dr. Maximilian Kalus
 */

import org.apache.tika.sax.ToTextContentHandler;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Page Content Handler to parse Tika extracted content into a list containing pages
 * <p>
 * Inspired by http://vteams.com/blog/apache-tika-per-page-content-extraction/
 * Thanks!
 */
public class PageContentHandler extends ToTextContentHandler {
	final static private String pageTag = "div";
	final static private String pageClass = "page";

	/**
	 * StringBuilder of current page
	 */
	private StringBuilder builder;

	/**
	 * page counter
	 */
	private int pageNumber = 0;

	/**
	 * page map - setting the initial capacity to 500 will enhance speed by a tiny bit up to 500 bits, but will require
	 * more RAM
	 */
	private Map<Integer, String> pages = new HashMap<>(500);

	/**
	 * flag telling to compress text information by stripping whitespace?
	 */
	private final boolean compress;

	/**
	 * Default constructor
	 */
	public PageContentHandler() {
		this.compress = true;
	}

	/**
	 * Constructor
	 *
	 * @param compress text information by stripping whitespace?
	 */
	public PageContentHandler(boolean compress) {
		this.compress = compress;
	}

	@Override
	public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
		if (pageTag.endsWith(qName) && pageClass.equals(atts.getValue("class")))
			startPage();
	}

	@Override
	public void endElement(String uri, String localName, String qName) throws SAXException {
		if (pageTag.endsWith(qName))
			endPage();
	}

	@Override
	public void characters(char[] ch, int start, int length) throws SAXException {
		// append data
		if (length > 0 && builder != null) {
			builder.append(ch);
		}
	}

	protected void startPage() throws SAXException {
		builder = new StringBuilder();
		pageNumber++;
		System.out.println(pageNumber);
	}

	protected void endPage() throws SAXException {
		String page = builder.toString();

		// if compression has been turned on, compact whitespace and trim string
		if (compress)
			page = page.replaceAll("\\s+", " ").trim();

		// page number already exists?
		if (pages.containsKey(pageNumber)) {
			page = pages.get(pageNumber) + " " + page; // concatenate pages
			page = page.trim();
		}

		// add to page list
		pages.put(pageNumber, page);
		builder = new StringBuilder();
	}

	/**
	 * @return all extracted pages
	 */
	public List<String> getPages() {
		List<String> pagesReal = new ArrayList<>(pageNumber);

		// convert to list
		for (int i = 1; i <= pageNumber; i++) {
			String page = pages.get(i);
			if (page == null) page = "";

			pagesReal.add(page);
		}

		return pagesReal;
	}
}
@@ -740,16 +740,73 @@
 andler;%0A
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A
 import o
@@ -828,16 +828,16 @@
 ibutes;%0A
-
 import o
@@ -1215,16 +1215,127 @@
 ndler %7B%0A
+%09/**%0A%09 * logger%0A%09 */%0A%09private static final Logger logger = LoggerFactory.getLogger(PageContentHandler.class);%0A%0A
 %09final s
@@ -2782,27 +2782,64 @@
 ;%0A%09%09
-System.out.println(
+if (logger.isDebugEnabled())%0A%09%09%09logger.debug(%22Page: %22 + 
 page
@@ -3616,21 +3616,111 @@
 (page);%0A
-
 %09%09%7D%0A%0A
+%09%09if (logger.isDebugEnabled())%0A%09%09%09logger.debug(%22Returning %22 + pageNumber + %22 page(s).%22);%0A%0A
 %09%09return

Add debug messages.