363b997214aa5a5926d6be1413c480520181d9d7
[mir.git] / source / mircoders / localizer / basic / MirBasicUtilityFunctions.java
1 /*\r
2  * Copyright (C) 2001, 2002 The Mir-coders group\r
3  *\r
4  * This file is part of Mir.\r
5  *\r
6  * Mir is free software; you can redistribute it and/or modify\r
7  * it under the terms of the GNU General Public License as published by\r
8  * the Free Software Foundation; either version 2 of the License, or\r
9  * (at your option) any later version.\r
10  *\r
11  * Mir is distributed in the hope that it will be useful,\r
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14  * GNU General Public License for more details.\r
15  *\r
16  * You should have received a copy of the GNU General Public License\r
17  * along with Mir; if not, write to the Free Software\r
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
19  *\r
20  * In addition, as a special exception, The Mir-coders gives permission to link\r
21  * the code of this program with  any library licensed under the Apache Software License,\r
22  * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library\r
23  * (or with modified versions of the above that use the same license as the above),\r
24  * and distribute linked combinations including the two.  You must obey the\r
25  * GNU General Public License in all respects for all of the code used other than\r
26  * the above mentioned libraries.  If you modify this file, you may extend this\r
27  * exception to your version of the file, but you are not obligated to do so.\r
28  * If you do not wish to do so, delete this exception statement from your version.\r
29  */\r
30 package mircoders.localizer.basic;\r
31 \r
32 import mir.util.*;\r
33 import mir.config.MirPropertiesConfiguration;\r
34 \r
35 import java.util.Collections;\r
36 import java.util.List;\r
37 \r
38 public class MirBasicUtilityFunctions {\r
39   public String encodeXML(Object anObject) throws Exception {\r
40     return HTMLRoutines.encodeXML(StringRoutines.interpretAsString(anObject));\r
41   }\r
42 \r
43   public String encodeHTML(Object aString) throws Exception {\r
44     return HTMLRoutines.encodeHTML(StringRoutines.interpretAsString(aString));\r
45   }\r
46 \r
47   public String prettyEncodeHTML(Object aString) throws Exception {\r
48     return HTMLRoutines.prettyEncodeHTML(StringRoutines.interpretAsString(aString));\r
49   }\r
50 \r
51   public String encodeURI(Object aString) throws Exception {\r
52     return HTMLRoutines.encodeURL(StringRoutines.interpretAsString(aString));\r
53   }\r
54 \r
55   public String encodeURI(Object aString, Object anEncoding) throws Exception {\r
56     return HTMLRoutines.encodeURL(\r
57         StringRoutines.interpretAsString(aString),\r
58         StringRoutines.interpretAsString(anEncoding));\r
59   }\r
60 \r
61   public String subString(Object aString, Object aFrom) throws Exception {\r
62     return StringRoutines.interpretAsString(aString).substring(StringRoutines.interpretAsInteger(aFrom));\r
63   }\r
64 \r
65   public String subString(Object aString, Object aFrom, Object aLength) throws Exception {\r
66     int length = StringRoutines.interpretAsInteger(aLength);\r
67     String target = StringRoutines.interpretAsString(aString);\r
68     if (length<0 || length>target.length()) {\r
69       length=target.length();\r
70     }\r
71 \r
72     return target.substring(StringRoutines.interpretAsInteger(aFrom), length);\r
73   }\r
74 \r
75   public String escapeJDBCString(Object aString) throws Exception {\r
76     return JDBCStringRoutines.escapeStringLiteral(StringRoutines.interpretAsString(aString));\r
77   }\r
78 \r
79   public String constructString(Object aString) throws Exception {\r
80     if (aString==null)\r
81       return StructuredContentParser.constructStringLiteral("");\r
82     else\r
83       return StructuredContentParser.constructStringLiteral(StringRoutines.interpretAsString(aString));\r
84   }\r
85 \r
86   public Object parseStructuredString(Object aString) throws Exception {\r
87     if (aString==null)\r
88       return null;\r
89     else\r
90       return StructuredContentParser.parse(StringRoutines.interpretAsString(aString));\r
91   }\r
92 \r
93   public boolean isOdd(Object anInteger) throws Exception {\r
94     return (StringRoutines.interpretAsInteger(anInteger) & 1) == 1;\r
95   }\r
96 \r
97   public int increment(Object anInteger) throws Exception {\r
98     final Integer ONE = new Integer(1);\r
99 \r
100     return increment(anInteger, ONE);\r
101   }\r
102 \r
103   public int increment(Object anInteger, Object anIncrement) throws Exception {\r
104     return StringRoutines.interpretAsInteger(anInteger) +\r
105            StringRoutines.interpretAsInteger(anIncrement);\r
106   }\r
107 \r
108   public Object subList(Object aList, Object aSkip) throws Exception {\r
109     return subList(aList, aSkip, new Integer(-1));\r
110   }\r
111 \r
112   public Object subList(Object aList, Object aSkip, Object aMaxSize) throws Exception {\r
113     int skip = StringRoutines.interpretAsInteger(aSkip);\r
114     int maxSize = StringRoutines.interpretAsInteger(aMaxSize);\r
115 \r
116     if (aList instanceof RewindableIterator)\r
117       return new SubsetIterator((RewindableIterator) aList, skip, maxSize);\r
118     else {\r
119       List list = (List) aList;\r
120 \r
121       if (skip>=list.size())\r
122         return Collections.EMPTY_LIST;\r
123       if (maxSize<0 || (skip+maxSize)>=list.size())\r
124         return list.subList(skip, list.size());\r
125       else\r
126         return list.subList(skip, skip+maxSize);\r
127     }\r
128   }\r
129 \r
130   public int listSize(RewindableIterator anIterator) {\r
131     anIterator.rewind();\r
132     int result=0;\r
133 \r
134     while (anIterator.hasNext()) {\r
135       result++;\r
136       anIterator.next();\r
137     }\r
138 \r
139     anIterator.rewind();\r
140 \r
141     return result;\r
142   }\r
143 \r
144   public int listSize(List aList) {\r
145     return aList.size();\r
146   }\r
147 \r
148   public Object evaluate(Object aTarget, String anExpression) throws Exception {\r
149     return ParameterExpander.expandExpression(aTarget, anExpression);\r
150   }\r
151 \r
152   public String regexpreplace(String aString, String anExpression, String aReplacement) {\r
153     return StringRoutines.performRegularExpressionReplacement(aString, anExpression, aReplacement);\r
154   }\r
155 \r
156   public boolean regexpmatch(String aString, String anExpression) {\r
157     return StringRoutines.performRegularExpressionSearch(aString, anExpression);\r
158   }\r
159 }\r