/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.
 * 
 */
package org.apache.tika.parser.envi;

import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.Set;
import java.nio.charset.Charset;

import org.apache.tika.detect.AutoDetectReader;
import org.apache.tika.io.CloseShieldInputStream;
import org.apache.tika.io.IOUtils;
import org.apache.tika.exception.TikaException;
import org.apache.tika.metadata.Metadata;
import org.apache.tika.mime.MediaType;
import org.apache.tika.parser.ParseContext;
import org.apache.tika.parser.AbstractParser;
import org.apache.tika.sax.XHTMLContentHandler;

import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;

public class EnviHeaderParser extends AbstractParser {

	private static final long serialVersionUID = -1479368523072408091L;

	public static final String ENVI_MIME_TYPE = "application/envi.hdr";

	private static final Set<MediaType> SUPPORTED_TYPES = Collections
			.singleton(MediaType.application("envi.hdr"));

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

	public void parse(InputStream stream, ContentHandler handler,
			Metadata metadata, ParseContext context) throws IOException,
			SAXException, TikaException {

		// Only outputting the MIME type as metadata
		metadata.set(Metadata.CONTENT_TYPE, ENVI_MIME_TYPE);

		// The following code was taken from the TXTParser
		// Automatically detect the character encoding
		AutoDetectReader reader = new AutoDetectReader(
				new CloseShieldInputStream(stream), metadata);

		try {
			Charset charset = reader.getCharset();
			MediaType type = new MediaType(MediaType.TEXT_PLAIN, charset);
			// deprecated, see TIKA-431
			metadata.set(Metadata.CONTENT_ENCODING, charset.name());

			XHTMLContentHandler xhtml = new XHTMLContentHandler(handler,
					metadata);

			xhtml.startDocument();

			// text contents of the xhtml
            String line;
            while ((line = reader.readLine()) != null) {
                xhtml.startElement("p");
                xhtml.characters(line);
                xhtml.endElement("p");
            }

			xhtml.endDocument();
		} finally {
			reader.close();
		}

	}
}

@@ -1081,43 +1081,8 @@
 am;%0A
-import org.apache.tika.io.IOUtils;%0A
 impo
@@ -1470,17 +1470,20 @@
 rser %7B%0A%0A
-%09
+    
 private 
@@ -1543,17 +1543,20 @@
 8091L;%0A%0A
-%09
+    
 public s
@@ -1616,17 +1616,20 @@
 .hdr%22;%0A%0A
-%09
+    
 private 
@@ -1686,19 +1686,28 @@
 ections%0A
-%09%09%09
+            
 .singlet
@@ -1746,17 +1746,20 @@
 dr%22));%0A%0A
-%09
+    
 public S
@@ -1814,18 +1814,24 @@
 text) %7B%0A
-%09%09
+        
 return S
@@ -1846,21 +1846,27 @@
 _TYPES;%0A
-%09%7D%0A%0A%09
+    %7D%0A%0A    
 public v
@@ -1919,19 +1919,28 @@
 andler,%0A
-%09%09%09
+            
 Metadata
@@ -1992,19 +1992,28 @@
 eption,%0A
-%09%09%09
+            
 SAXExcep
@@ -2035,18 +2035,24 @@
 tion %7B%0A%0A
-%09%09
+        
 // Only 
@@ -2088,18 +2088,24 @@
 etadata%0A
-%09%09
+        
 metadata
@@ -2150,18 +2150,24 @@
 TYPE);%0A%0A
-%09%09
+        
 // The f
@@ -2209,18 +2209,24 @@
 TParser%0A
-%09%09
+        
 // Autom
@@ -2264,18 +2264,24 @@
 ncoding%0A
-%09%09
+        
 AutoDete
@@ -2320,20 +2320,32 @@
 Reader(%0A
-%09%09%09%09
+                
 new Clos
@@ -2388,19 +2388,34 @@
 );%0A%0A
-%09%09try %7B%0A%09%09%09
+        try %7B%0A            
 Char
@@ -2449,19 +2449,28 @@
 rset();%0A
-%09%09%09
+            
 MediaTyp
@@ -2524,19 +2524,28 @@
 arset);%0A
-%09%09%09
+            
 // depre
@@ -2564,19 +2564,28 @@
 IKA-431%0A
-%09%09%09
+            
 metadata
@@ -2634,19 +2634,28 @@
 me());%0A%0A
-%09%09%09
+            
 XHTMLCon
@@ -2711,28 +2711,52 @@
 er,%0A
-%09%09%09%09%09metadata);%0A%0A%09%09%09
+                    metadata);%0A%0A            
 xhtm
@@ -2775,19 +2775,28 @@
 ent();%0A%0A
-%09%09%09
+            
 // text 
@@ -3033,20 +3033,41 @@
       %7D%0A
-%0A%09%09%09
+            %0A            
 xhtml.en
@@ -3079,18 +3079,24 @@
 ment();%0A
-%09%09
+        
 %7D finall
@@ -3103,11 +3103,20 @@
 y %7B%0A
-%09%09%09
+            
 read
@@ -3131,14 +3131,22 @@
 ();%0A
-%09%09%7D%0A%0A%09
+        %7D%0A    
 %7D%0A%7D%0A

Fix whitespace in EnviHeaderParser.