<commit_before>/**
 *
 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either 
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public 
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 * 
 **/
package lucee.runtime.dump;

import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.TimeZone;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpSession;

import lucee.commons.date.TimeZoneUtil;
import lucee.commons.io.res.Resource;
import lucee.commons.lang.IDGenerator;
import lucee.commons.lang.StringUtil;
import lucee.runtime.PageContext;
import lucee.runtime.coder.Base64Coder;
import lucee.runtime.converter.WDDXConverter;
import lucee.runtime.engine.ThreadLocalPageContext;
import lucee.runtime.exp.PageException;
import lucee.runtime.i18n.LocaleFactory;
import lucee.runtime.net.rpc.Pojo;
import lucee.runtime.op.Caster;
import lucee.runtime.op.Decision;
import lucee.runtime.text.xml.XMLCaster;
import lucee.runtime.type.Array;
import lucee.runtime.type.Collection;
import lucee.runtime.type.ObjectWrap;
import lucee.runtime.type.QueryImpl;
import lucee.runtime.type.Struct;
import lucee.runtime.type.StructImpl;
import lucee.runtime.type.dt.DateTimeImpl;
import lucee.runtime.type.scope.CookieImpl;
import lucee.runtime.type.util.KeyConstants;

import org.apache.felix.framework.BundleWiringImpl.BundleClassLoader;
import org.osgi.framework.Bundle;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class DumpUtil {

	public static final DumpData MAX_LEVEL_REACHED;

	static {

		MAX_LEVEL_REACHED = new DumpTable("Max Level Reached","#e0e0e0","#ffcc99","#888888");
		((DumpTable)MAX_LEVEL_REACHED).appendRow( new DumpRow(1, new SimpleDumpData("[Max Dump Level Reached]") ) );
	}

	public static DumpData toDumpData(Object o, PageContext pageContext, int maxlevel, DumpProperties props) {
		if(maxlevel<0)
			return MAX_LEVEL_REACHED;

		// null
		if(o == null) {
			DumpTable table=new DumpTable("null","#ff6600","#ffcc99","#000000");
			table.appendRow(new DumpRow(0,new SimpleDumpData("Empty:null")));
			return table;
		}
		if(o instanceof DumpData) {
			return ((DumpData)o);
		}
		// Date
		if(o instanceof Date) {
			return new DateTimeImpl((Date) o).toDumpData(pageContext,maxlevel,props);
		}
		// Calendar
		if(o instanceof Calendar) {
			Calendar c=(Calendar)o;
			
			SimpleDateFormat df = new SimpleDateFormat("EE, dd MMM yyyy HH:mm:ss zz",Locale.ENGLISH);
			df.setTimeZone(c.getTimeZone());
			
			DumpTable table=new DumpTable("date","#ff9900","#ffcc00","#000000");
			table.setTitle("java.util.Calendar");
			table.appendRow(1, new SimpleDumpData("Timezone"), new SimpleDumpData(TimeZoneUtil.toString(c.getTimeZone())));
			table.appendRow(1, new SimpleDumpData("Time"), new SimpleDumpData(df.format(c.getTime())));
	        
			return table;
		}
		// StringBuffer
		if(o instanceof StringBuffer) {
			DumpTable dt=(DumpTable)toDumpData(o.toString(), pageContext, maxlevel, props);
			if(StringUtil.isEmpty(dt.getTitle()))
				dt.setTitle(Caster.toClassName(o));
			return dt;
		}
		// StringBuilder
		if(o instanceof StringBuilder) {
			DumpTable dt=(DumpTable)toDumpData(o.toString(), pageContext, maxlevel, props);
			if(StringUtil.isEmpty(dt.getTitle()))
				dt.setTitle(Caster.toClassName(o));
			return dt;
		}
		// String
		if(o instanceof String) {
			String str=(String) o;
			if(str.trim().startsWith("<wddxPacket ")) {
				try {
					WDDXConverter converter =new WDDXConverter(pageContext.getTimeZone(),false,true);
					converter.setTimeZone(pageContext.getTimeZone());
					Object rst = converter.deserialize(str,false);
					DumpData data = toDumpData(rst, pageContext, maxlevel, props);
					
					DumpTable table = new DumpTable("string","#cc9999","#ffffff","#000000");
					table.setTitle("WDDX");
					table.appendRow(1,new SimpleDumpData("encoded"),data);
					table.appendRow(1,new SimpleDumpData("raw"),new SimpleDumpData(str));
					return table;
				}
				catch(Exception e) {
					// this dump entry is optional, so if it is not possible to create the decoded wddx entry, we simply don't do it
				}
			}
			DumpTable table = new DumpTable("string","#ff6600","#ffcc99","#000000");
			table.appendRow(1,new SimpleDumpData("string"),new SimpleDumpData(str));
			return table;
		}
		// Character
		if(o instanceof Character) {
			DumpTable table = new DumpTable("character","#ff6600","#ffcc99","#000000");
			table.appendRow(1,new SimpleDumpData("character"),new SimpleDumpData(o.toString()));
			return table;
		}
		// Number
		if(o instanceof Number) {
			DumpTable table = new DumpTable("numeric","#ff6600","#ffcc99","#000000");
			table.appendRow(1,new SimpleDumpData("number"),new SimpleDumpData(Caster.toString(((Number)o))));
			return table;
		}
		// Locale
		if(o instanceof Locale) {
			Locale l=(Locale) o;
			Locale env = ThreadLocalPageContext.getLocale(null);
			DumpTable table = new DumpTable("locale","#ff6600","#ffcc99","#000000");
			table.setTitle("Locale "+LocaleFactory.getDisplayName(l));
			table.appendRow(1,new SimpleDumpData("Code (ISO-3166)"),new SimpleDumpData(l.toString()));
			table.appendRow(1,new SimpleDumpData("Country"),new SimpleDumpData(l.getDisplayCountry(env)));
			table.appendRow(1,new SimpleDumpData("Language"),new SimpleDumpData(l.getDisplayLanguage(env)));
			return table;
		}
		// TimeZone
		if(o instanceof TimeZone) {
			DumpTable table = new DumpTable("numeric","#ff6600","#ffcc99","#000000");
			table.appendRow(1,new SimpleDumpData("TimeZone"),new SimpleDumpData(TimeZoneUtil.toString(((TimeZone)o))));
			return table;
		}
		// Boolean
		if(o instanceof Boolean) {
			DumpTable table = new DumpTable("boolean","#ff6600","#ffcc99","#000000");
			table.appendRow(1,new SimpleDumpData("boolean"),new SimpleDumpData(((Boolean)o).booleanValue()));
			return table;
		}
		// File
		if(o instanceof File) {<commit_msg>improve the output for charset objects<commit_after>/**
 *
 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either 
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public 
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 * 
 **/
package lucee.runtime.dump;

import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.nio.charset.Charset;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.TimeZone;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpSession;

import lucee.commons.date.TimeZoneUtil;
import lucee.commons.io.CharsetUtil;
import lucee.commons.io.res.Resource;
import lucee.commons.lang.CharSet;
import lucee.commons.lang.IDGenerator;
import lucee.commons.lang.StringUtil;
import lucee.runtime.PageContext;
import lucee.runtime.coder.Base64Coder;
import lucee.runtime.converter.WDDXConverter;
import lucee.runtime.engine.ThreadLocalPageContext;
import lucee.runtime.exp.PageException;
import lucee.runtime.i18n.LocaleFactory;
import lucee.runtime.net.rpc.Pojo;
import lucee.runtime.op.Caster;
import lucee.runtime.op.Decision;
import lucee.runtime.text.xml.XMLCaster;
import lucee.runtime.type.Array;
import lucee.runtime.type.Collection;
import lucee.runtime.type.ObjectWrap;
import lucee.runtime.type.QueryImpl;
import lucee.runtime.type.Struct;
import lucee.runtime.type.StructImpl;
import lucee.runtime.type.dt.DateTimeImpl;
import lucee.runtime.type.scope.CookieImpl;
import lucee.runtime.type.util.KeyConstants;

import org.apache.felix.framework.BundleWiringImpl.BundleClassLoader;
import org.osgi.framework.Bundle;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class DumpUtil {

	public static final DumpData MAX_LEVEL_REACHED;

	static {

		MAX_LEVEL_REACHED = new DumpTable("Max Level Reached","#e0e0e0","#ffcc99","#888888");
		((DumpTable)MAX_LEVEL_REACHED).appendRow( new DumpRow(1, new SimpleDumpData("[Max Dump Level Reached]") ) );
	}

	public static DumpData toDumpData(Object o, PageContext pageContext, int maxlevel, DumpProperties props) {
		if(maxlevel<0)
			return MAX_LEVEL_REACHED;

		// null
		if(o == null) {
			DumpTable table=new DumpTable("null","#ff6600","#ffcc99","#000000");
			table.appendRow(new DumpRow(0,new SimpleDumpData("Empty:null")));
			return table;
		}
		if(o instanceof DumpData) {
			return ((DumpData)o);
		}
		// Date
		if(o instanceof Date) {
			return new DateTimeImpl((Date) o).toDumpData(pageContext,maxlevel,props);
		}
		// Calendar
		if(o instanceof Calendar) {
			Calendar c=(Calendar)o;
			
			SimpleDateFormat df = new SimpleDateFormat("EE, dd MMM yyyy HH:mm:ss zz",Locale.ENGLISH);
			df.setTimeZone(c.getTimeZone());
			
			DumpTable table=new DumpTable("date","#ff9900","#ffcc00","#000000");
			table.setTitle("java.util.Calendar");
			table.appendRow(1, new SimpleDumpData("Timezone"), new SimpleDumpData(TimeZoneUtil.toString(c.getTimeZone())));
			table.appendRow(1, new SimpleDumpData("Time"), new SimpleDumpData(df.format(c.getTime())));
	        
			return table;
		}
		// StringBuffer
		if(o instanceof StringBuffer) {
			DumpTable dt=(DumpTable)toDumpData(o.toString(), pageContext, maxlevel, props);
			if(StringUtil.isEmpty(dt.getTitle()))
				dt.setTitle(Caster.toClassName(o));
			return dt;
		}
		// StringBuilder
		if(o instanceof StringBuilder) {
			DumpTable dt=(DumpTable)toDumpData(o.toString(), pageContext, maxlevel, props);
			if(StringUtil.isEmpty(dt.getTitle()))
				dt.setTitle(Caster.toClassName(o));
			return dt;
		}
		// String
		if(o instanceof String) {
			String str=(String) o;
			if(str.trim().startsWith("<wddxPacket ")) {
				try {
					WDDXConverter converter =new WDDXConverter(pageContext.getTimeZone(),false,true);
					converter.setTimeZone(pageContext.getTimeZone());
					Object rst = converter.deserialize(str,false);
					DumpData data = toDumpData(rst, pageContext, maxlevel, props);
					
					DumpTable table = new DumpTable("string","#cc9999","#ffffff","#000000");
					table.setTitle("WDDX");
					table.appendRow(1,new SimpleDumpData("encoded"),data);
					table.appendRow(1,new SimpleDumpData("raw"),new SimpleDumpData(str));
					return table;
				}
				catch(Exception e) {
					// this dump entry is optional, so if it is not possible to create the decoded wddx entry, we simply don't do it
				}
			}
			DumpTable table = new DumpTable("string","#ff6600","#ffcc99","#000000");
			table.appendRow(1,new SimpleDumpData("string"),new SimpleDumpData(str));
			return table;
		}
		// Character
		if(o instanceof Character) {
			DumpTable table = new DumpTable("character","#ff6600","#ffcc99","#000000");
			table.appendRow(1,new SimpleDumpData("character"),new SimpleDumpData(o.toString()));
			return table;
		}
		// Number
		if(o instanceof Number) {
			DumpTable table = new DumpTable("numeric","#ff6600","#ffcc99","#000000");
			table.appendRow(1,new SimpleDumpData("number"),new SimpleDumpData(Caster.toString(((Number)o))));
			return table;
		}
		// Charset
		if(o instanceof Charset) {
			DumpTable table = new DumpTable("charset","#ff6600","#ffcc99","#000000");
			table.appendRow(1,new SimpleDumpData("charset"),new SimpleDumpData(((Charset)o).name()));
			return table;
		}
		// CharSet
		if(o instanceof CharSet) {
			DumpTable table = new DumpTable("charset","#ff6600","#ffcc99","#000000");
			table.appendRow(1,new SimpleDumpData("charset"),new SimpleDumpData(((CharSet)o).name()));
			return table;
		}
		// Locale
		if(o instanceof Locale) {
			Locale l=(Locale) o;
			Locale env = ThreadLocalPageContext.getLocale(null);
			DumpTable table = new DumpTable("locale","#ff6600","#ffcc99","#000000");
			table.setTitle("Locale "+LocaleFactory.getDisplayName(l));
			table.appendRow(1,new SimpleDumpData("Code (ISO-3166)"),new SimpleDumpData(l.toString()));
			table.appendRow(1,new SimpleDumpData("Country"),new SimpleDumpData(l.getDisplayCountry(env)));
			table.appendRow(1,new SimpleDumpData("Language"),new SimpleDumpData(l.getDisplayLanguage(env)));
			return table;
		}
		// TimeZone
		if(o instanceof TimeZone) {
			DumpTable table = new DumpTable("numeric","#ff6600","#ffcc99","#000000");
			table.appendRow(1,new SimpleDumpData("TimeZone"),new SimpleDumpData(TimeZoneUtil.toString(((TimeZone)o))));
			return table;
		}
		// Boolean
		if(o instanceof Boolean) {
			DumpTable table = new DumpTable("boolean","#ff6600","#ffcc99","#000000");
			table.appendRow(1,new SimpleDumpData("boolean"),new SimpleDumpData(((Boolean)o).booleanValue()));
			return table;
		}
		// File
		if(o instanceof File) {
{'fraction_non_alphanumeric': 0.1359017781541067, 'fraction_numerical': 0.02286198137171888, 'mean_word_length': 4.992811839323467, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, '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': '3133130', 'n_tokens_mistral': 5304, 'n_tokens_neox': 4655, 'n_words': 987}