package org.icij.extract.parser.emf;

import org.apache.tika.exception.TikaException;
import org.apache.tika.metadata.Metadata;
import org.apache.tika.mime.MediaType;
import org.apache.tika.parser.AbstractParser;
import org.apache.tika.parser.ParseContext;
import org.apache.tika.sax.XHTMLContentHandler;
import org.freehep.graphicsio.emf.EMFInputStream;
import org.freehep.graphicsio.emf.EMFTag;
import org.freehep.graphicsio.emf.gdi.*;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import org.freehep.util.io.Tag;
import org.xml.sax.helpers.AttributesImpl;

import static org.apache.tika.sax.XHTMLContentHandler.XHTML;

public class EMFParser extends AbstractParser {

	private static final Set<MediaType> SUPPORTED_TYPES = Collections.unmodifiableSet(new HashSet<>(Arrays
			.asList(MediaType.application("x-emf"), MediaType.image("emf"))));
	private static final String EMF_MIME_TYPE = "image/emf";
	private static final long serialVersionUID = 5516989102471431040L;

	@Override
	public Set<MediaType> getSupportedTypes(final ParseContext context) {
		return SUPPORTED_TYPES;
	}

	@Override
	public void parse(final InputStream stream, final ContentHandler handler, final Metadata metadata, final
	ParseContext context) throws IOException, SAXException, TikaException {
		metadata.set(Metadata.CONTENT_TYPE, EMF_MIME_TYPE);

		final EMFInputStream input = new EMFInputStream(stream);
		Tag tag;

		final XHTMLContentHandler xhtml = new XHTMLContentHandler(handler, metadata);
		xhtml.startDocument();

		while ((tag = input.readTag()) != null) {
			if (!(tag instanceof EMFTag)) {
				continue;
			}

			if ((tag instanceof ExtTextOutA) || (tag instanceof ExtTextOutW)) {
				xhtml.startElement(XHTML, "p", "p", new AttributesImpl());
				xhtml.characters(((AbstractExtTextOut) tag).getText().getString());
				xhtml.endElement(XHTML, "p", "p");
			}
		}

		xhtml.endDocument();

		input.close();
	}
}

@@ -290,32 +290,33 @@
 ContentHandler;%0A
+%0A
 import org.freeh
@@ -432,16 +432,17 @@
 .gdi.*;%0A
+%0A
 import o
@@ -502,16 +502,44 @@
 ption;%0A%0A
+import java.awt.Rectangle;%0A%0A
 import j
@@ -582,24 +582,25 @@
 nputStream;%0A
+%0A
 import java.
@@ -1607,16 +1607,265 @@
 g tag;%0A%0A
+%09%09final Rectangle rectangle = input.readHeader().getBounds();%0A%09%09metadata.set(%22height%22, Integer.toString(Double.valueOf(rectangle.getHeight()).intValue()));%0A%09%09metadata.set(%22width%22, Integer.toString(Double.valueOf(rectangle.getWidth()).intValue()));%0A%0A
 %09%09final 

Set the image width and height in metadata for EMF files