| Sys.java |
1 // Copyright (c) 2000 Just Objects B.V. <just@justobjects.nl>
2 // Distributable under LGPL license. See terms of license at gnu.org.
3
4 package nl.justobjects.pushlet.util;
5
6
7 import java.io.FileInputStream;
8 import java.io.IOException;
9 import java.text.CharacterIterator;
10import java.text.StringCharacterIterator;
11import java.util.Properties;
12
13/**
14 * Utilities that interact with the underlying OS/JVM.
15 *
16 * @author Just van den Broecke
17 * @version $Id: Sys.java,v 1.4 2007/11/10 14:17:18 justb Exp $
18 */
19public class Sys {
20
21 /**
22 * Replace characters having special meaning <em>inside</em> HTML tags
23 * with their escaped equivalents, using character entities such as <tt>'&'</tt>.
24 * <p/>
25 * <P>The escaped characters are :
26 * <ul>
27 * <li> <
28 * <li> >
29 * <li> "
30 * <li> '
31 * <li> \
32 * <li> &
33 * </ul>
34 * <p/>
35 * <P>This method ensures that arbitrary text appearing inside a tag does not "confuse"
36 * the tag. For example, <tt>HREF='Blah.do?Page=1&Sort=ASC'</tt>
37 * does not comply with strict HTML because of the ampersand, and should be changed to
38 * <tt>HREF='Blah.do?Page=1&Sort=ASC'</tt>. This is commonly seen in building
39 * query strings. (In JSTL, the c:url tag performs this task automatically.)
40 */
41 static public String forHTMLTag(String aTagFragment) {
42 final StringBuffer result = new StringBuffer();
43
44 final StringCharacterIterator iterator = new StringCharacterIterator(aTagFragment);
45 char character = iterator.current();
46 while (character != CharacterIterator.DONE) {
47 if (character == '<') {
48 result.append("<");
49 } else if (character == '>') {
50 result.append(">");
51 } else if (character == '\"') {
52 result.append(""");
53 } else if (character == '\'') {
54 result.append("'");
55 } else if (character == '\\') {
56 result.append("\");
57 } else if (character == '&') {
58 result.append("&");
59 } else {
60 //the char is not a special one
61 //add it to the result as is
62 result.append(character);
63 }
64 character = iterator.next();
65 }
66 return result.toString();
67 }
68
69 /**
70 * Load properties file from classpath.
71 */
72 static public Properties loadPropertiesResource(String aResourcePath) throws IOException {
73 try {
74 // Use the class loader that loaded our class.
75 // This is required where for reasons like security
76 // multiple class loaders exist, e.g. BEA WebLogic.
77 // Thanks to Lutz Lennemann 29-aug-2000.
78 ClassLoader classLoader = Sys.class.getClassLoader();
79
80 Properties properties = new Properties();
81
82 // Try loading it.
83 properties.load(classLoader.getResourceAsStream(aResourcePath));
84 return properties;
85 } catch (Throwable t) {
86 throw new IOException("failed loading Properties resource from " + aResourcePath);
87 }
88 }
89
90 /**
91 * Load properties file from file path.
92 */
93 static public Properties loadPropertiesFile(String aFilePath) throws IOException {
94 try {
95
96 Properties properties = new Properties();
97
98 // Try loading it.
99 properties.load(new FileInputStream(aFilePath));
00 return properties;
01 } catch (Throwable t) {
02 throw new IOException("failed loading Properties file from " + aFilePath);
03 }
04 }
05
06 /**
07 * Shorthand for current time.
08 */
09 static public long now() {
10 return System.currentTimeMillis();
11 }
12
13}
14| Sys.java |