import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;

import javax.xml.transform.Templates;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

public class XslTestCase {

	public String transformXmlToHtml(String xmlFileStr)
	{
		String htmlFile = "";
		URL resource = null;
	    File is = null;
		FileInputStream fis = null;
		ByteArrayOutputStream bos = null; 
		try {
			TransformerFactory tFactory = TransformerFactory.newInstance();
			resource = this.getClass().getClassLoader().getResource("ViewSubmission_http60.xsl");

			is = new File(resource.toURI());

			fis = new FileInputStream( is );
			Templates template = tFactory.newTemplates( new StreamSource( fis ) );
			Transformer transformer = template.newTransformer();
			bos = new ByteArrayOutputStream();
			transformer.transform(new StreamSource(new ByteArrayInputStream(xmlFileStr.getBytes() ) ), new StreamResult( bos ));
			
			htmlFile = bos.toString();
		}
		catch ( Throwable t ) {
			System.out.println("transformXmlToHtml error: " + t);
		}
		finally{
			if (fis != null){
				try {
					fis.close();
				} catch (IOException ignore) {}
			}
			if (bos != null){
				try {
					bos.close();
				} catch (IOException ignore) {}
			}
		}
		return htmlFile;
	}

	public static void main(String[] args) 
	{		
		XslTestCase obj = new XslTestCase();
		System.out.println(obj.transformXmlToHtml("<TRI:TRI xsi:schemaLocation=\"http://www.exchangenetwork.net/schema/TRI/6\" xmlns:sc=\"urn:us:net:exchangenetwork:sc:1:0\" xmlns:TRI=\"http://www.exchangenetwork.net/schema/TRI/6\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" +
"    <TRI:Submission>" +
"        <TRI:Facility>" +
"            <TRI:FacilityIdentifier FacilitySiteIdentifierContext=\"TRIME-WEB\">1946XXXXXXXXX</TRI:FacilityIdentifier>" +
"            <sc:FacilitySiteName>TEST</sc:FacilitySiteName>" +
"            <sc:LocationAddress>" +
"                <sc:LocationAddressText>12601 FAIR LAKES CIRCLE</sc:LocationAddressText>" +
"                <sc:LocalityName>FAIRFAX</sc:LocalityName>" +
"                <sc:StateIdentity>" +
"                    <sc:StateName>VA</sc:StateName>" +
"                </sc:StateIdentity>" +
"                <sc:AddressPostalCode>22033</sc:AddressPostalCode>" +
"                <sc:CountryIdentity/>" +
"                <sc:CountyIdentity>" +
"                    <sc:CountyCode>55</sc:CountyCode>" +
"                    <sc:CountyName>Fairfax</sc:CountyName>" +
"                </sc:CountyIdentity>" +
"                <sc:TribalIdentity>" +
"                    <sc:TribalCode>006</sc:TribalCode>" +
"                </sc:TribalIdentity>" +
"            </sc:LocationAddress>" +
"            <TRI:MailingFacilitySiteName>NA</TRI:MailingFacilitySiteName>" +
"            <TRI:MailingAddress>" +
"                <sc:StateIdentity/>" +
"                <sc:CountryIdentity/>" +
"            </TRI:MailingAddress>" +
"            <TRI:FacilityNAICS>" +
"                <sc:NAICSCode>325520</sc:NAICSCode>" +
"                <sc:NAICSPrimaryIndicator>Primary</sc:NAICSPrimaryIndicator>" +
"            </TRI:FacilityNAICS>" +
"            <TRI:ParentCompanyNameNAIndicator>false</TRI:ParentCompanyNameNAIndicator>" +
"            <TRI:ParentCompanyNameText>TEST Parent</TRI:ParentCompanyNameText>" +
"            <TRI:ParentDunBradstreetCode>100000000</TRI:ParentDunBradstreetCode>" +
"            <TRI:FacilityDunBradstreetCode>NA</TRI:FacilityDunBradstreetCode>" +
"        </TRI:Facility>" +
"        <TRI:Report>" +
"            <sc:ReportIdentifier>1234567</sc:ReportIdentifier>" +
"            <TRI:ReportType>" +
"                <TRI:ReportTypeCode>TRI_FORM_A</TRI:ReportTypeCode>" +
"            </TRI:ReportType>" +
"            <TRI:SubmissionReportingYear>2016</TRI:SubmissionReportingYear>" +
"            <sc:RevisionIndicator>N</sc:RevisionIndicator>" +
"            <TRI:ChemicalTradeSecretIndicator>false</TRI:ChemicalTradeSecretIndicator>" +
"            <TRI:SubmissionEntireFacilityIndicator>true</TRI:SubmissionEntireFacilityIndicator>" +
"            <TRI:SubmissionPartialFacilityIndicator>false</TRI:SubmissionPartialFacilityIndicator>" +
"            <TRI:SubmissionFederalFacilityIndicator>N</TRI:SubmissionFederalFacilityIndicator>" +
"            <TRI:SubmissionGOCOFacilityIndicator>false</TRI:SubmissionGOCOFacilityIndicator>" +
"            <TRI:TechnicalContactNameText>" +
"                <sc:IndividualFullName>Daniel</sc:IndividualFullName>" +
"            </TRI:TechnicalContactNameText>" +
"            <TRI:TechnicalContactPhoneText>7032274992</TRI:TechnicalContactPhoneText>" +
"            <TRI:TechnicalContactEmailAddressText>daniel.sporea@cgifederal.com</TRI:TechnicalContactEmailAddressText>" +
"            <TRI:PublicContactNameText>" +
"                <sc:IndividualFullName>Daniel</sc:IndividualFullName>" +
"            </TRI:PublicContactNameText>" +
"            <TRI:PublicContactPhoneText>7032274992</TRI:PublicContactPhoneText>" +
"            <TRI:PublicContactEmailAddressText>daniel.sporea@cgifederal.com</TRI:PublicContactEmailAddressText>" +
"            <TRI:ChemicalIdentification>" +
"                <sc:CASNumber>000075865</sc:CASNumber>" +
"                <TRI:ChemicalNameText>2-Methyllactonitrile</TRI:ChemicalNameText>" +
"                <TRI:ChemicalMixtureNameText>NA</TRI:ChemicalMixtureNameText>" +
"            </TRI:ChemicalIdentification>" +
"            <TRI:TRIComment>" +
"                <TRI:TRICommentSequence>1</TRI:TRICommentSequence>" +
"                <TRI:TRICommentSection>9.1</TRI:TRICommentSection>" +
"                <TRI:TRICommentType>DB NUM</TRI:TRICommentType>" +
"                <TRI:TRICommentTypeDescription>Dun and Bradstreet Not Applicable</TRI:TRICommentTypeDescription>" +
"                <TRI:TRICommentText>4.6 Testing</TRI:TRICommentText>" +
"                <TRI:TRICommentP2Classification>Miscellaneous</TRI:TRICommentP2Classification>" +
"            </TRI:TRIComment>" +
"        </TRI:Report>" +
"    </TRI:Submission>" +
"</TRI:TRI>"));
	}

}
