import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

		InputStreamReader origInput = new InputStreamReader(service.getSourceStream());
        StringBuffer contents_1 = new StringBuffer();
        BufferedReader reader_1 = null;
        String fileContents_1;
		try
        {
		// Stream generated from previous activity, passing it to BufferedReader
            reader_1 = new BufferedReader(origInput);
            String text_1 = null;
            while (((text_1 = reader_1.readLine()) != null))
            {
                contents_1.append(text_1);
            }
        } catch (FileNotFoundException e)
        {
            e.printStackTrace();
        } catch (IOException e)
        {
            e.printStackTrace();
        } finally
        {
            try
            {
                if ((reader_1 != null))
                {
                    reader_1.close();
                }
            } catch (IOException e)
            {
                e.printStackTrace();
            }
        }
       fileContents_1 = contents_1.toString();
try {		
	// Searching for XML version and encoding, if it is present in source input stream from previous activity.
	int countPattern = 0;
	String str_1 = "";
		String input_1 = "<\\?xml[[a-z][A-Z][0-9][=.\"\\-\\s]]+\\?>" ; 
       Pattern contentRegEx = Pattern.compile(input_1);
       Matcher matchRegEx_1 = contentRegEx.matcher(fileContents_1); 
	   
       StringBuffer sb_1 = new StringBuffer();
       boolean result_1 = matchRegEx_1.find();
       System.out.println("result_1   " +result_1);
       while(result_1) {
		countPattern ++;
    	   matchRegEx_1.appendReplacement(sb_1, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
           result_1 = matchRegEx_1.find();
       }
	   if(countPattern == 0)
	   {	
	   //Append XML version if not present in retrieved XML file. 
	   		str_1 =  "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" + fileContents_1;
			System.out.println(str_1);
	   }else{
	       matchRegEx_1.appendTail(sb_1);
		   str_1 = sb_1.toString();
		   System.out.println(str_1);
	   }
	        
      OutputStream os = null;
	  os = service.getOutputStream();
	  
       try {
				os.write(str_1.getBytes());
				os.flush();
				os.close();
		      	       
	        }
       catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
} catch (XmlException xEException) {
	// TODO Auto-generated catch block
	xEException.printStackTrace();
} catch (Throwable tException) {
	// TODO Auto-generated catch block
	tException.printStackTrace();
}