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