Child: [r4] (diff)

Download this file

html.java    333 lines (291 with data), 10.7 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/*
* SPDXVersion: SPDX-1.1
* Creator: Person: Nuno Brito (nuno.brito@triplecheck.de)
* Creator: Organization: TripleCheck (contact@triplecheck.de)
* Created: 2013-11-01T00:00:00Z
* LicenseName: NOASSERTION
* FileName: html.java
* FileType: SOURCE
* FileCopyrightText: <text> Copyright 2013 Nuno Brito, TripleCheck </text>
* FileComment: <text> </text>
*/
package utils;
import definitions.is;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import main.core;
import script.log;
import www.RequestOrigin;
import www.WebRequest;
/**
*
* @author Nuno Brito, 20th of September 2013 in Darmstadt, Germany.
* nuno.brito@triplecheck.de | http://nunobrito.eu
*/
public class html {
public static final String
br = "\n<br>",
_div = "</div>\n",
space = "&nbsp",
divider = space + "|" + space;
public static String getIcon(String iconName, WebRequest request){
String result = iconName;
if(request.requestOrigin == RequestOrigin.BROWSER){
result =
"<img align=\"middle;\" hspace=\"3\" src=\""
+ iconName
+ "\"> "
;
return result;
}
File icon = new File(request.BaseFolder, iconName);
String iconPath = icon.getAbsolutePath();
iconPath = "file:///" + iconPath.replace("\\", "/");
result =
"<img align=\"middle;\" hspace=\"3\" src=\""
+ iconPath
+ "\"> "
;
return result;
}
/**
* Provides the HTML text necessary to display an image on our text box
* @param iconName the icon from the collection of icons
* @return An HTML valid string to be placed on web pages
*/
public static String getCommonFolderIcon(String iconName){
// create the icon image
File icon = core.getIcon(iconName);
String iconPath = icon.getAbsolutePath();
iconPath = "file:///" + iconPath.replace("\\", "/");
String result =
"<img align=\"middle;\" hspace=\"3\" src=\""
+ iconPath
+ "\"> "
;
return result;
}
/**
* HTML string that shows text on a given color
* @param text text to be displayed
* @return An HTML text prepared to be displayed on a web page
*/
public static String textGrey(String text){
return "<span style=\"color: rgb("
+ "102, "
+ "102, "
+ "102);\">"
+ text
+ "</span>";
}
/**
* Adds an HTML link that allows selecting a specific tree node with a given
* ID text
* @param title Title displayed to the reader
* @param link the action
* @return the HTML text ready to be placed on the text editor/web page
*/
public static String linkNode(String title, String link){
return "<a href=\"nodeId://" + link + "\">" + title + "</a>";
}
/**
* Allows to run a script at our choice
* @param title The link title that is visible to the user
* @param script The script file that where the source code is located
* @param method The specific method that we want to call
* @return the HTML code ready to be placed on a web page
*/
public static String linkScript(String title, File script, String method){
return "<a href=\"script://" + script.getAbsolutePath()
+ ">>"+method
+ "\">" + title + "</a>";
}
/**
* Adds a plain normal HTTP link that will open a web page on the default
* browser
* @param title Title for the link that is read by end-user
* @param link URL that is used for opening the site location
* @return
*/
public static String link(String title, String link){
return "<a href=\"" + link + "\">" + title + "</a>";
}
/**
* Search for a given term or expression. It will fill the search box
* with the selected term and then perform the search operation, displaying
* the found results.
* @param title The title for the link that is read by the end-user
* @param what The expression or term that will be used on the search box
* @return
*/
public static String linkSearch(String title, String what){
return "<a href=\"search://" + what + "\">" + title + "</a>";
}
/**
* Adds a text with an advance of 10 pixels
* @return
*/
public static String div(){
return "<div style=\"margin-left: 10px;\">";
}
/**
* Adds a text with an advance of a given width in pixels
* @param width width in pixels
* @return an HTML text with the DIV code
*/
public static String div(int width){
return "<div style=\"margin-left: " + width + "px;\">";
}
/**
* Search a term using the Yandex engine
* @param what the search term to be found
* @return The HTML text prepared to be added on a web page
*/
public static String linkToSearchYandex(String what){
return html.link("Yandex", "http://www.yandex.com/yandsearch?text="
+ what);
}
/**
* Search a term using the Google engine
* @param what the search term to be found
* @return The HTML text prepared to be added on a web page
*/
public static String linkToSearchGoogle(String what){
return html.link("Google", "https://www.google.de/?#q=" + what);
}
// public static String linkToSearchGoogle(String what){
// return html.link("Google", "https://www.google.com/?q=\""
// + what+"\"#q=\"" + what +"\"");
// }
/**
* Search a term using the VirusTotal engine
* @param what the search term to be found
* @return The HTML text prepared to be added on a web page
*/
public static String linkToSearchVirusTotal(String what){
return html.link("VirusTotal", "https://www.virustotal.com/en/file/"
+what
+"/analysis/");
}
/**
* Search a term using the Ohloh engine
* @param what the search term to be found
* @return The HTML text prepared to be added on a web page
*/
public static String linkToSearchOhloh(String what){
return html.link("Ohloh", "http://code.ohloh.net/search?s="
+what
);
}
/**
* Creates a link for an HTML page inside the computer that will be displayed
* on the text box
* @param title The title that is visible to the user
* @param htmlPage The file pointer to an HTML file
* @return The HMTL code ready to be inserted on the web page
*/
public static String linkInside(String title, File htmlPage){
//String url = htmlPage.getAbsolutePath().replace("\\", "/");
return link(title, htmlPage.getAbsolutePath());
}
public static String linkToSearchAntepedia(String what) {
return html.link("Antepedia", "http://www.antepedia.com/search.html?q="
+what
);
}
public static String linkToSearchGitHub(String what) {
return html.link("GitHub", "https://github.com/search?q="
//+ "%22"
+ what
//+ "%22"
+ "&type=Code&ref=searchresults"
);
}
/**
* Returns a text formatted as an H2 title
* @param title title of the text
* @return the HTML formatted text
*/
public static String h2(String title){
return "<h2>" + title + "</h2>";
}
/**
* Returns a text formatted as an H3 title
* @param title title of the text
* @return the HTML formatted text
*/
public static String h3(String title){
return "<h3>" + title + "</h3>";
}
/**
* This method picks a sentence of text containing several parameters and
* then provides
* @param data in the format: test=123&test2=567
* @return
*/
public static ArrayList<String[]> cleanParameters(String data){
ArrayList<String[]> result = new ArrayList();
// split each of the parameters into something we can process
String[] params = data.split("&");
// go through each parameter
for (String param : params) {
String[] pair = param.split("=");
String value = "";
String key = "";
if (pair.length > 1) {
try {
key = java.net.URLDecoder.decode(pair[0], "UTF-8");
value = java.net.URLDecoder.decode(pair[1], "UTF-8");
//value = pair[1];
} catch (UnsupportedEncodingException er) {
log.write(is.ERROR,
"Failed to read: %1", data);
return null;
}
} else if (pair.length == 1) {
try {
key = java.net.URLDecoder.decode(pair[0], "UTF-8");
//value = pair[1];
} catch (UnsupportedEncodingException er) {
log.write(is.ERROR,
"Failed to read: %1", data);
return null;
}
}
// add this parameter on our list
result.add(new String[]{key, value});
}// for
// all done
return result;
}
/**
* This method provides the HTML code to automatically redirect to a
* different web page
* @param URL the target URL
* @param delay hoe many seconds to wait
* @return the HTML code that is provided to the end user
*/
public static String redirect(String URL, int delay){
return "<html><head>"
+ "<meta http-equiv=\"refresh\" CONTENT=\""
+ delay
+"; URL="
+ URL
+ "\">"
+ "</head><body></body></html>";
}
/**
* Provides just the meta tag for the HTML portion of the request
* @param URL the page to load
* @param delay the time it will take until activated
* @return an HTML portion properly formatted for a web page
*/
public static String redirectMetaTag(String URL, int delay){
return "<meta http-equiv=\"refresh\" CONTENT=\""
+ delay
+"; URL="
+ URL
+ "\">";
}
}