source: main/trunk/greenstone3/src/java/org/greenstone/util/Misc.java@ 33043

Last change on this file since 33043 was 33043, checked in by ak19, 5 years ago

Not a bugfix, but to help with encoding issues, including to help with current, still unresolved encoding issue. 1. Introduction of string2hex functions in JavaScript and Java. 2. Overriding GSXML.elemtToString() with introduction of additional debugEncoding parameter that will turn on string2hex use when printing request and response XMLs. Now non-basic ASCII characters in the XML will be printed in hex if debugEncoding parameter passed in is true. 3. The inactive Connector elements in server8.xml.svn now also have the attribute URIEncoding set to UTF-8. Some of these inactive Connectors get turned on at times, such as for https. In which case we will need tomcact to also interpret get/post data coming in through those connectors to be sent on as utf-8.

  • Property svn:keywords set to Author Date Id Revision
File size: 7.6 KB
Line 
1 /*
2 * Misc.java
3 * Copyright (C) 2002 New Zealand Digital Library, http://www.nzdl.org
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20package org.greenstone.util;
21
22import java.util.HashMap;
23import java.util.Set;
24import java.util.Map;
25import java.util.Iterator;
26import java.util.Properties;
27import java.io.InputStream;
28import java.io.BufferedReader;
29import java.io.InputStreamReader;
30import java.io.IOException;
31import java.net.HttpURLConnection;
32import java.net.URL;
33import java.net.URLConnection;
34
35/** contains miscellaneous functions */
36public class Misc {
37
38 // Can initialise static final vars on declaration or in static initialisation code block
39 // http://stackoverflow.com/questions/2339932/java-can-final-variables-be-initialized-in-static-initialization-block
40 // Initialise object member final vars on declaration or in constructors
41 public static final String NEWLINE;
42
43 // Before Java 7, no System.lineSeparator() or System.getProperty("line.separator")
44 // And on local linux, am compiling with JDK 6, so need this.
45 // http://stackoverflow.com/questions/207947/how-do-i-get-a-platform-dependent-new-line-character
46
47 static {
48 // http://stackoverflow.com/questions/2591083/getting-java-version-at-runtime
49 // https://www.tutorialspoint.com/java/lang/package_getspecificationversion.htm
50
51 double java_version = Double.parseDouble(System.getProperty("java.specification.version"));
52 if(java_version >= 1.7) { // https://docs.oracle.com/javase/tutorial/essential/environment/sysprop.html
53 NEWLINE = System.getProperty("line.separator");
54 } else {
55 NEWLINE = isWindows() ? "\r\n" : "\n";
56 }
57 }
58
59
60 // Debugging function to print a string's non-basic chars in hex
61 // Based on https://stackoverflow.com/questions/923863/converting-a-string-to-hexadecimal-in-java
62 public static String stringToHex(String str) {
63 String result = "";
64 for(int i = 0; i < str.length(); i++) {
65 int charCode = str.codePointAt(i); // unicode codepoint / ASCII code
66
67 // ASCII table: https://cdn.sparkfun.com/assets/home_page_posts/2/1/2/1/ascii_table_black.png
68 // If the unicode character code pt is less than the ASCII code for space and greater than for tilda, let's display the char in hex (x0000 format)
69 if((charCode >= 20 && charCode <= 126) || charCode == 9 || charCode == 10 || charCode == 13) { // space to tilda, TAB, LF, CR are printable
70 result += str.charAt(i);
71 } else {
72 result += "x" + String.format("%04x", charCode);
73 }
74 }
75
76 return result;
77 }
78
79
80 public static void printHash(HashMap map) {
81 Set entries = map.entrySet();
82 Iterator i = entries.iterator();
83 while (i.hasNext()) {
84 Map.Entry m = (Map.Entry)i.next();
85 String name = (String)m.getKey();
86 String value = (String)m.getValue();
87 }
88 }
89
90
91 /**
92 * Handy function to display the list of calling functions by
93 * printing out the stack trace even when you don't have an exception
94 */
95 static public void printStackTrace() {
96 // https://stackoverflow.com/questions/1069066/get-current-stack-trace-in-java
97
98 //Thread.dumpStack(); // looks too much like an exception, though the newlines separating each function call is handy
99 //new Exception().printStackTrace(); // outputs in the format of an exception too
100 //System.err.println("\n@@@@ stacktrace:\n" + Arrays.toString(Thread.currentThread().getStackTrace()) + "\n"); // outputs all in one line
101
102 System.err.println("\n@@@@ stacktrace:");
103 StackTraceElement[] els = new Throwable().getStackTrace(); // starts at index 1, which is this function
104 //StackTraceElement[] els = Thread.currentThread().getStackTrace(); starts at index 0, "java.lang.Thread.getStackTrace()"
105 for(StackTraceElement ste : els) {
106 System.err.println(" " + ste);
107 }
108 }
109
110 /**
111 * Handy function to display the parent of the calling function
112 * (the function that called the function that called printCaller())
113 */
114 static public void printCaller() {
115 int parent = 1;
116 // this function printCaller() itself adds another layer on the callstack since
117 // it calls the overloaded method, so need to add 1 more to parent
118 printCaller(parent++);
119 }
120
121 /**
122 * Handy function to display the nth ancestor of the calling function
123 * where ancestor=0 would be the calling function itself
124 */
125 static public void printCaller(int ancestor) {
126 // https://stackoverflow.com/questions/1069066/get-current-stack-trace-in-java
127
128 // Thread.currentThread().getStackTrace() starts at index 0: "java.lang.Thread.getStackTrace()"
129 // index 1 will be this method (printCaller) and index 2 will be the calling function itself who wants
130 // to know who called it. So need to at least start at index 3 to get informative caller information
131
132 StackTraceElement[] callstack = Thread.currentThread().getStackTrace();
133 StackTraceElement requestor = callstack[2]; // the calling function, the function that called this one
134 StackTraceElement caller_requested = callstack[ancestor+3]; // the function requested
135 System.err.println("@@@ Function " + requestor + " called by:\n "
136 + caller_requested + " at " + ancestor + " ancestors back");
137 }
138
139
140 /** Method to determine if the host system is Windows based.
141 * @return a boolean which is true if the platform is Windows,
142 * false otherwise
143 */
144 public static boolean isWindows() {
145 Properties props = System.getProperties();
146 String os_name = props.getProperty("os.name","");
147 if(os_name.startsWith("Windows")) {
148 return true;
149 }
150 return false;
151 }
152
153 public static boolean isWindows9x() {
154 Properties props = System.getProperties();
155 String os_name = props.getProperty("os.name","");
156 if(os_name.startsWith("Windows") && os_name.indexOf("9") != -1) {
157 return true;
158 }
159 return false;
160 }
161
162 /** Method to determine if the host system is MacOS based.
163 * @return a boolean which is true if the platform is MacOS, false otherwise
164 */
165 public static boolean isMac() {
166 Properties props = System.getProperties();
167 String os_name = props.getProperty("os.name","");
168 if(os_name.startsWith("Mac OS")) {
169 return true;
170 }
171 return false;
172 }
173
174 public static String getGsdlOS() {
175 if (isWindows()) {
176 return "windows";
177 }
178 if (isMac()) {
179 return "darwin";
180 }
181 return "linux";
182 }
183
184 public static boolean isBigEndian() {
185
186 if (System.getProperty("sun.cpu.endian").equals("big")) {
187 return true;
188 }
189 return false;
190
191 }
192
193 public static BufferedReader makeHttpConnection(String url_string)
194 throws java.net.MalformedURLException, java.io.IOException {
195 BufferedReader reader = null;
196 URL url = new URL(url_string);
197 HttpURLConnection connection = (HttpURLConnection)url.openConnection();
198 InputStream input = connection.getInputStream();
199 reader = new BufferedReader(new InputStreamReader(input));
200 return reader;
201 }
202
203 public static void main(String [] args) {
204 isBigEndian();
205 }
206
207}
208
Note: See TracBrowser for help on using the repository browser.