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.List;

/**
 * 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 list - setting the initial capacity to 500 will enhance speed by a tiny bit up to 500 bits, but will require
	 * some RAM
	 */
	private List<String> pages = new ArrayList<>(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();
	}

	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();

		// add to page list at end of page list
		pages.add(page);
	}

	/**
	 * @return all extracted pages
	 */
	public List<String> getPages() {
		return pages;
	}
}
@@ -850,20 +850,68 @@
 va.util.
-List
+HashMap;%0Aimport java.util.List;%0Aimport java.util.Map
 ;%0A%0A/**%0A 
@@ -1345,20 +1345,76 @@
  * page 
-list
+counter%0A%09 */%0A%09private int pageNumber = 0;%0A%0A%09/**%0A%09 * page map
  - setti
@@ -1518,11 +1518,11 @@
 %09 * 
-som
+mor
 e RA
@@ -1537,21 +1537,29 @@
 private 
-List%3C
+Map%3CInteger, 
 String%3E 
@@ -1570,25 +1570,23 @@
 s = new 
-ArrayList
+HashMap
 %3C%3E(500);
@@ -2588,24 +2588,74 @@
 gBuilder();%0A
+%09%09pageNumber++;%0A%09%09System.out.println(pageNumber);%0A
 %09%7D%0A%0A%09protect
@@ -2879,34 +2879,181 @@
 %09// 
-add to page list at end of
+page number already exists?%0A%09%09if (pages.containsKey(pageNumber)) %7B%0A%09%09%09page = pages.get(pageNumber) + %22 %22 + page; // concatenate pages%0A%09%09%09page = page.trim();%0A%09%09%7D%0A%0A%09%09// add to
  pag
@@ -3071,16 +3071,61 @@
 ges.
-add
+put
 (page
+Number, page);%0A%09%09builder = new StringBuilder(
 );%0A%09
@@ -3203,16 +3203,238 @@
 ges() %7B%0A
+%09%09List%3CString%3E pagesReal = new ArrayList%3C%3E(pageNumber);%0A%0A%09%09// convert to list%0A%09%09for (int i = 1; i %3C= pageNumber; i++) %7B%0A%09%09%09String page = pages.get(pageNumber);%0A%09%09%09if (page == null) page = %22%22;%0A%0A%09%09%09pagesReal.add(page);%0A%09%09%7D%0A%0A
 %09%09return
@@ -3439,14 +3439,18 @@
 rn pages
+Real
 ;%0A%09%7D%0A%7D

Fix handler to correctly handle certain PDFs containing multiple page endings.