media handling fixes, gotten rid of StorageObject, set the default method for blots...
[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 java.util.Collections;\r
33 import java.util.List;\r
34 \r
35 import mir.util.HTMLRoutines;\r
36 import mir.util.JDBCStringRoutines;\r
37 import mir.util.ParameterExpander;\r
38 import mir.util.RewindableIterator;\r
39 import mir.util.StringRoutines;\r
40 import mir.util.StructuredContentParser;\r
41 import mir.util.SubsetIterator;\r
42 \r
43 public class MirBasicUtilityFunctions {\r
44   public String encodeXML(Object anObject) throws Exception {\r
45     return HTMLRoutines.encodeXML(StringRoutines.interpretAsString(anObject));\r
46   }\r
47 \r
48   public String encodeHTML(Object aString) throws Exception {\r
49     return HTMLRoutines.encodeHTML(StringRoutines.interpretAsString(aString));\r
50   }\r
51 \r
52   public String prettyEncodeHTML(Object aString) throws Exception {\r
53     return HTMLRoutines.prettyEncodeHTML(StringRoutines.interpretAsString(aString));\r
54   }\r
55 \r
56   public String encodeURI(Object aString) throws Exception {\r
57     return HTMLRoutines.encodeURL(StringRoutines.interpretAsString(aString));\r
58   }\r
59 \r
60   public String encodeURI(Object aString, Object anEncoding) throws Exception {\r
61     return HTMLRoutines.encodeURL(\r
62         StringRoutines.interpretAsString(aString),\r
63         StringRoutines.interpretAsString(anEncoding));\r
64   }\r
65 \r
66   public String subString(Object aString, Object aFrom) throws Exception {\r
67     return StringRoutines.interpretAsString(aString).substring(StringRoutines.interpretAsInteger(aFrom));\r
68   }\r
69 \r
70   public String subString(Object aString, Object aFrom, Object aLength) throws Exception {\r
71     int length = StringRoutines.interpretAsInteger(aLength);\r
72     String target = StringRoutines.interpretAsString(aString);\r
73     if (length<0 || length>target.length()) {\r
74       length=target.length();\r
75     }\r
76 \r
77     return target.substring(StringRoutines.interpretAsInteger(aFrom), length);\r
78   }\r
79 \r
80   public String escapeJDBCString(Object aString) throws Exception {\r
81     return JDBCStringRoutines.escapeStringLiteral(StringRoutines.interpretAsString(aString));\r
82   }\r
83 \r
84   public String constructString(Object aString) throws Exception {\r
85     if (aString==null)\r
86       return StructuredContentParser.constructStringLiteral("");\r
87     else\r
88       return StructuredContentParser.constructStringLiteral(StringRoutines.interpretAsString(aString));\r
89   }\r
90 \r
91   public Object parseStructuredString(Object aString) throws Exception {\r
92     if (aString==null)\r
93       return null;\r
94     else\r
95       return StructuredContentParser.parse(StringRoutines.interpretAsString(aString));\r
96   }\r
97 \r
98   public boolean isOdd(Object anInteger) throws Exception {\r
99     return (StringRoutines.interpretAsInteger(anInteger) & 1) == 1;\r
100   }\r
101 \r
102   public int increment(Object anInteger) throws Exception {\r
103     final Integer ONE = new Integer(1);\r
104 \r
105     return increment(anInteger, ONE);\r
106   }\r
107 \r
108   public int increment(Object anInteger, Object anIncrement) throws Exception {\r
109     return StringRoutines.interpretAsInteger(anInteger) +\r
110            StringRoutines.interpretAsInteger(anIncrement);\r
111   }\r
112 \r
113   public Object subList(Object aList, Object aSkip) throws Exception {\r
114     return subList(aList, aSkip, new Integer(-1));\r
115   }\r
116 \r
117   public Object subList(Object aList, Object aSkip, Object aMaxSize) throws Exception {\r
118     int skip = StringRoutines.interpretAsInteger(aSkip);\r
119     int maxSize = StringRoutines.interpretAsInteger(aMaxSize);\r
120 \r
121     if (aList instanceof RewindableIterator)\r
122       return new SubsetIterator((RewindableIterator) aList, skip, maxSize);\r
123     else {\r
124       List list = (List) aList;\r
125 \r
126       if (skip>=list.size())\r
127         return Collections.EMPTY_LIST;\r
128       if (maxSize<0 || (skip+maxSize)>=list.size())\r
129         return list.subList(skip, list.size());\r
130       else\r
131         return list.subList(skip, skip+maxSize);\r
132     }\r
133   }\r
134 \r
135   public int listSize(RewindableIterator anIterator) {\r
136     anIterator.rewind();\r
137     int result=0;\r
138 \r
139     while (anIterator.hasNext()) {\r
140       result++;\r
141       anIterator.next();\r
142     }\r
143 \r
144     anIterator.rewind();\r
145 \r
146     return result;\r
147   }\r
148 \r
149   public int listSize(List aList) {\r
150     return aList.size();\r
151   }\r
152 \r
153   public Object evaluate(Object aTarget, String anExpression) throws Exception {\r
154     return ParameterExpander.expandExpression(aTarget, anExpression);\r
155   }\r
156 \r
157   public String regexpreplace(String aString, String anExpression, String aReplacement) {\r
158     return StringRoutines.performRegularExpressionReplacement(aString, anExpression, aReplacement);\r
159   }\r
160 \r
161   public boolean regexpmatch(String aString, String anExpression) {\r
162     return StringRoutines.performRegularExpressionSearch(aString, anExpression);\r
163   }\r
164 }\r