<commit_before>/*
 * $Log: FileViewerServlet.java,v $
 * Revision 1.5  2005-10-17 09:34:05  europe\L190409
 * added log4j xml view facility
 *
 * Revision 1.4  2005/07/19 11:42:09  <NAME> <<EMAIL>>
 * performance improvements
 *
 */
package nl.nn.adapterframework.webcontrol;

import nl.nn.adapterframework.util.AppConstants;
import nl.nn.adapterframework.util.ClassUtils;
import nl.nn.adapterframework.util.DomBuilderException;
import nl.nn.adapterframework.util.EncapsulatingReader;
import nl.nn.adapterframework.util.Misc;
import nl.nn.adapterframework.util.XmlUtils;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.stream.StreamSource;

import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.io.PrintWriter;
import java.io.Reader;
import java.net.URL;
import java.util.StringTokenizer;

/**
 * Shows a textfile either as HTML or as Text.
 * <br/>
 * This is a servlet because in JSP you cannot give the
 * content-type depending on a condition.<br/>
 * <p>Parameters:<br/>
 * <ul>
 * <li>fileName: the filename to retrieve</li>
 * <li>returnType: either HTML or TEXT</li>
 * </ul>
 * </p>
 * <p>When the <code>resultType</code> is HTML, the
 * <code>FileViewerServlet.signal</code> properties are looked
 * up from the <code>AppConstants</code> and additional styling is
 * done.</p>
 * <p>
 * <code><pre>
 	FileViewerServlet.signal=ERROR WARN
 	FileViewerServlet.signal.ERROR.pre=&lt;font color=red&gt;
 	FileViewerServlet.signal.ERROR.post=&lt;/font&gt;
 	FileViewerServlet.signal.WARN.pre=&lt;font color=blue&gt;
 	FileViewerServlet.signal.WARN.post=&lt;/font&gt;
 	etc.
 	FileViewerServlet.signal.css=IE4.css
 	</pre></code>
 	The last item specifies which stylesheet to use.
 * @version Id
 * @author <NAME> 
 */
public class FileViewerServlet extends HttpServlet  {
	public static final String version = "$RCSfile: FileViewerServlet.java,v $ $Revision: 1.5 $ $Date: 2005-10-17 09:34:05 $";
	protected Logger log = Logger.getLogger(this.getClass());	

	// key that is looked up to retrieve texts to be signalled
	private static String fvConfigKey="FileViewerServlet.signal";

	private static String log4j_html_xslt = "/xml/xsl/log4j_html.xsl";
	private static String log4j_text_xslt = "/xml/xsl/log4j_text.xsl";
	private static String log4j_prefix    = "<log4j:log4j xmlns:log4j=\"http://jakarta.apache.org/log4\">\n\n";
	private static String log4j_postfix	  = "</log4j:log4j>";

	public static String makeConfiguredReplacements(String input) {
		StringTokenizer tok=AppConstants.getInstance().getTokenizer(fvConfigKey);
		while (tok.hasMoreTokens()){
			String signal=tok.nextToken();
			String pre=AppConstants.getInstance().getProperty(fvConfigKey+"."+signal+".pre");
			String post=AppConstants.getInstance().getProperty(fvConfigKey+"."+signal+".post");
			input=StringUtils.replace(input, signal, pre+signal+post);
		}
		return StringUtils.replace(input, "\t", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
	}


	public static void transformReader(Reader reader, HttpServletResponse response, String input_prefix, String input_postfix, String stylesheetUrl) throws DomBuilderException, TransformerException, IOException { 
		PrintWriter out = response.getWriter();
		Reader fileReader = new EncapsulatingReader(reader, input_prefix, input_postfix);
		URL xsltSource = ClassUtils.getResourceURL( FileViewerServlet.class, stylesheetUrl);
		if (xsltSource!=null) {
			Transformer transformer = XmlUtils.createTransformer(xsltSource);
			XmlUtils.transformXml(transformer, new StreamSource(fileReader),out);
			out.close();
		} else {
			showReaderContents(fileReader,"text",response);
		}
	}

	public static void showReaderContents(Reader reader, String type, HttpServletResponse response) throws DomBuilderException, TransformerException, IOException {
		PrintWriter out = response.getWriter();
		if (type==null) {
			response.setContentType("text/html");
			out.println("resultType not specified");
			return;
		}
			
		if (type.equalsIgnoreCase("html")){
			response.setContentType("text/html");
	
			out.println("<html>");
			out.println("<head>");
			out.println("<link rel=\"stylesheet\" type=\"text/css\" href=\""+AppConstants.getInstance().getProperty(fvConfigKey+".css")+"\">");
			out.println("</head>");
			out.println("<body>");
	
			LineNumberReader lnr = new LineNumberReader(reader);
			String line;
			while ((line=lnr.readLine())!=null) {<commit_msg>replace non-printable characters from log4j.xml<commit_after>/*
 * $Log: FileViewerServlet.java,v $
 * Revision 1.6  2005-10-17 11:05:14  europe\L190409
 * replace non-printable characters from log4j.xml
 *
 * Revision 1.5  2005/10/17 09:34:05  <NAME> <<EMAIL>>
 * added log4j xml view facility
 *
 * Revision 1.4  2005/07/19 11:42:09  <NAME> <<EMAIL>>
 * performance improvements
 *
 */
package nl.nn.adapterframework.webcontrol;

import nl.nn.adapterframework.util.AppConstants;
import nl.nn.adapterframework.util.ClassUtils;
import nl.nn.adapterframework.util.DomBuilderException;
import nl.nn.adapterframework.util.EncapsulatingReader;
import nl.nn.adapterframework.util.Misc;
import nl.nn.adapterframework.util.XmlUtils;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.stream.StreamSource;

import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.io.PrintWriter;
import java.io.Reader;
import java.net.URL;
import java.util.StringTokenizer;

/**
 * Shows a textfile either as HTML or as Text.
 * <br/>
 * This is a servlet because in JSP you cannot give the
 * content-type depending on a condition.<br/>
 * <p>Parameters:<br/>
 * <ul>
 * <li>fileName: the filename to retrieve</li>
 * <li>returnType: either HTML or TEXT</li>
 * </ul>
 * </p>
 * <p>When the <code>resultType</code> is HTML, the
 * <code>FileViewerServlet.signal</code> properties are looked
 * up from the <code>AppConstants</code> and additional styling is
 * done.</p>
 * <p>
 * <code><pre>
 	FileViewerServlet.signal=ERROR WARN
 	FileViewerServlet.signal.ERROR.pre=&lt;font color=red&gt;
 	FileViewerServlet.signal.ERROR.post=&lt;/font&gt;
 	FileViewerServlet.signal.WARN.pre=&lt;font color=blue&gt;
 	FileViewerServlet.signal.WARN.post=&lt;/font&gt;
 	etc.
 	FileViewerServlet.signal.css=IE4.css
 	</pre></code>
 	The last item specifies which stylesheet to use.
 * @version Id
 * @author <NAME> 
 */
public class FileViewerServlet extends HttpServlet  {
	public static final String version = "$RCSfile: FileViewerServlet.java,v $ $Revision: 1.6 $ $Date: 2005-10-17 11:05:14 $";
	protected Logger log = Logger.getLogger(this.getClass());	

	// key that is looked up to retrieve texts to be signalled
	private static String fvConfigKey="FileViewerServlet.signal";

	private static String log4j_html_xslt = "/xml/xsl/log4j_html.xsl";
	private static String log4j_text_xslt = "/xml/xsl/log4j_text.xsl";
	private static String log4j_prefix    = "<log4j:log4j xmlns:log4j=\"http://jakarta.apache.org/log4\">\n\n";
	private static String log4j_postfix	  = "</log4j:log4j>";

	public static String makeConfiguredReplacements(String input) {
		StringTokenizer tok=AppConstants.getInstance().getTokenizer(fvConfigKey);
		while (tok.hasMoreTokens()){
			String signal=tok.nextToken();
			String pre=AppConstants.getInstance().getProperty(fvConfigKey+"."+signal+".pre");
			String post=AppConstants.getInstance().getProperty(fvConfigKey+"."+signal+".post");
			input=StringUtils.replace(input, signal, pre+signal+post);
		}
		return StringUtils.replace(input, "\t", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
	}


	public static void transformReader(Reader reader, HttpServletResponse response, String input_prefix, String input_postfix, String stylesheetUrl) throws DomBuilderException, TransformerException, IOException { 
		PrintWriter out = response.getWriter();
		Reader fileReader = new EncapsulatingReader(reader, input_prefix, input_postfix, true);
		URL xsltSource = ClassUtils.getResourceURL( FileViewerServlet.class, stylesheetUrl);
		if (xsltSource!=null) {
			Transformer transformer = XmlUtils.createTransformer(xsltSource);
			XmlUtils.transformXml(transformer, new StreamSource(fileReader),out);
			out.close();
		} else {
			showReaderContents(fileReader,"text",response);
		}
	}

	public static void showReaderContents(Reader reader, String type, HttpServletResponse response) throws DomBuilderException, TransformerException, IOException {
		PrintWriter out = response.getWriter();
		if (type==null) {
			response.setContentType("text/html");
			out.println("resultType not specified");
			return;
		}
			
		if (type.equalsIgnoreCase("html")){
			response.setContentType("text/html");
	
			out.println("<html>");
			out.println("<head>");
			out.println("<link rel=\"stylesheet\" type=\"text/css\" href=\""+AppConstants.getInstance().getProperty(fvConfigKey+".css")+"\">");
			out.println("</head>");
			out.println("<body>");
	
			LineNumberReader lnr = new LineNumberReader(reader);
			String line;
			while ((line=lnr.readLine())!=null) {
{'fraction_non_alphanumeric': 0.13376987839101964, 'fraction_numerical': 0.016214530714062987, 'mean_word_length': 5.355350066050198, 'pattern_counts': {'":': 0, '<': 76, '<?xml version=': 0, '>': 76, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 16}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3316086', 'n_tokens_mistral': 3254, 'n_tokens_neox': 3013, 'n_words': 646}