package org.skyve.impl.content;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.logging.Level;

import org.pf4j.Extension;
import org.apache.tika.Tika;
import org.apache.tika.metadata.Metadata;
import org.apache.tika.metadata.Office;
import org.apache.tika.metadata.TikaCoreProperties;
import org.skyve.content.AttachmentContent;
import org.skyve.content.TextExtractor;
import org.skyve.impl.util.UtilImpl;
import org.skyve.util.Util;

@Extension(points = {TextExtractor.class})
public class TextExtractorImpl implements TextExtractor {
	private static final Tika TIKA = new Tika();
	
	@Override
	public String extractTextFromMarkup(String markup) {
		String result = null;
		try {
			result = UtilImpl.processStringValue(TIKA.parseToString(new ByteArrayInputStream(markup.getBytes())));
		}
		catch (Exception e) {
			UtilImpl.LOGGER.log(Level.SEVERE, 
									"TextExtractorImpl.extractTextFromMarkup(): Markup could not be extracted by TIKA",
									e);
		}
		return result;
	}
	
	@Override
	public String extractTextFromContent(AttachmentContent content) {
		StringBuilder result = new StringBuilder(102400);

		try {
			try (InputStream contentStream = content.getContentStream()) {
				Metadata metadata = new Metadata();

				// Set the maximum length of strings returned by the parseToString method, -1 sets no limit
				String text = Util.processStringValue(TIKA.parseToString(contentStream, metadata, 100000));
				if (text != null) {
					result.append(text);
				}
				
				// Meta data
				// Title
				String title = metadata.get(TikaCoreProperties.TITLE);
				if (title != null) {
					if (result.length() > 0) {
						result.append(". ");
					}
					result.append(title);
				}
				// Author
				String author = metadata.get(Office.AUTHOR);
				if (author != null) {
					if (result.length() > 0) {
						result.append(". ");
					}
					result.append(author);
				}
				// Keywords
				String keywords = metadata.get(TikaCoreProperties.KEYWORDS);
				if (keywords != null) {
					if (result.length() > 0) {
						result.append(". ");
					}
					result.append(keywords);
				}
			}
		}
		catch (Exception e) {
			UtilImpl.LOGGER.log(Level.SEVERE, 
									"TextExtractorImpl.extractTextFromContent(): Attachment could not be extracted by TIKA",
									e);
		}
		
		return (result.length() == 0) ? null : result.toString();
	}
	
	@Override
	public void sniffContentType(AttachmentContent attachment) {
		// Sniff content type if necessary
		String contentType = attachment.getContentType();
		if (contentType == null) {
			try {
				byte[] content = attachment.getContentBytes();
				String fileName = attachment.getFileName();
				if (fileName == null) {
					contentType = TIKA.detect(content);
				}
				else {
					contentType = TIKA.detect(content, fileName);
				}
				attachment.setContentType(contentType);
			}
			catch (Exception e) {
				UtilImpl.LOGGER.log(Level.SEVERE, 
										"TextExtractorImpl.sniffContentType(): Attachment could not be sniffed by TIKA",
										e);
			}
		}
	}
}

@@ -438,36 +438,8 @@
 mpl;
-%0Aimport org.skyve.util.Util;
 %0A%0A@E
@@ -674,24 +674,122 @@
 = null;%0A
+%09%09String processedMarkup = UtilImpl.processStringValue(markup);%0A%09%09if (processedMarkup != null) %7B%0A%09
 %09%09try %7B%0A
 %09%09%09resul
@@ -780,16 +780,17 @@
 %09%09try %7B%0A
+%09
 %09%09%09resul
@@ -865,17 +865,26 @@
 tStream(
-m
+processedM
 arkup.ge
@@ -896,22 +896,24 @@
 s())));%0A
+%09
 %09%09%7D%0A%09%09
+%09
 catch (E
@@ -918,32 +918,33 @@
 (Exception e) %7B%0A
+%09
 %09%09%09UtilImpl.LOGG
@@ -966,32 +966,33 @@
 VERE, %0A%09%09%09%09%09%09%09%09%09
+%09
 %22TextExtractorIm
@@ -1064,28 +1064,34 @@
 %22,%0A%09%09%09%09%09%09%09%09%09
+%09
 e);%0A
+%09%09%09%7D%0A
 %09%09%7D%0A%09%09return
@@ -1446,16 +1446,16 @@
 o limit%0A
-
 %09%09%09%09Stri
@@ -1468,16 +1468,20 @@
 t = Util
+Impl
 .process
@@ -2769,24 +2769,52 @@
 FileName();%0A
+%09%09%09%09if (content != null) %7B%0A%09
 %09%09%09%09if (file
@@ -2825,24 +2825,25 @@
  == null) %7B%0A
+%09
 %09%09%09%09%09content
@@ -2867,24 +2867,25 @@
 t(content);%0A
+%09
 %09%09%09%09%7D%0A%09%09%09%09el
@@ -2886,15 +2886,17 @@
 %09%09%09%09
+%09
 else %7B%0A
+%09
 %09%09%09%09
@@ -2938,24 +2938,25 @@
  fileName);%0A
+%09
 %09%09%09%09%7D%0A%09%09%09%09at
@@ -2953,16 +2953,17 @@
 %09%09%7D%0A%09%09%09%09
+%09
 attachme
@@ -2990,24 +2990,30 @@
 ntentType);%0A
+%09%09%09%09%7D%0A
 %09%09%09%7D%0A%09%09%09catc

Add extra zero length and null checks to TextExtractorImpl