2 * Copyright (C) 2001, 2002 The Mir-coders group
\r
4 * This file is part of Mir.
\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
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
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
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
30 package mircoders.localizer.basic;
\r
32 import java.util.Collections;
\r
33 import java.util.List;
\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
43 public class MirBasicUtilityFunctions {
\r
44 public String encodeXML(Object anObject) throws Exception {
\r
45 return HTMLRoutines.encodeXML(StringRoutines.interpretAsString(anObject));
\r
48 public String encodeHTML(Object aString) throws Exception {
\r
49 return HTMLRoutines.encodeHTML(StringRoutines.interpretAsString(aString));
\r
52 public String prettyEncodeHTML(Object aString) throws Exception {
\r
53 return HTMLRoutines.prettyEncodeHTML(StringRoutines.interpretAsString(aString));
\r
56 public String encodeURI(Object aString) throws Exception {
\r
57 return HTMLRoutines.encodeURL(StringRoutines.interpretAsString(aString));
\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
66 public String subString(Object aString, Object aFrom) throws Exception {
\r
67 return StringRoutines.interpretAsString(aString).substring(StringRoutines.interpretAsInteger(aFrom));
\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
77 return target.substring(StringRoutines.interpretAsInteger(aFrom), length);
\r
80 public String escapeJDBCString(Object aString) throws Exception {
\r
81 return JDBCStringRoutines.escapeStringLiteral(StringRoutines.interpretAsString(aString));
\r
84 public String constructString(Object aString) throws Exception {
\r
86 return StructuredContentParser.constructStringLiteral("");
\r
88 return StructuredContentParser.constructStringLiteral(StringRoutines.interpretAsString(aString));
\r
91 public Object parseStructuredString(Object aString) throws Exception {
\r
95 return StructuredContentParser.parse(StringRoutines.interpretAsString(aString));
\r
98 public boolean isOdd(Object anInteger) throws Exception {
\r
99 return (StringRoutines.interpretAsInteger(anInteger) & 1) == 1;
\r
102 public int increment(Object anInteger) throws Exception {
\r
103 final Integer ONE = new Integer(1);
\r
105 return increment(anInteger, ONE);
\r
108 public int increment(Object anInteger, Object anIncrement) throws Exception {
\r
109 return StringRoutines.interpretAsInteger(anInteger) +
\r
110 StringRoutines.interpretAsInteger(anIncrement);
\r
113 public Object subList(Object aList, Object aSkip) throws Exception {
\r
114 return subList(aList, aSkip, new Integer(-1));
\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
121 if (aList instanceof RewindableIterator)
\r
122 return new SubsetIterator((RewindableIterator) aList, skip, maxSize);
\r
124 List list = (List) aList;
\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
131 return list.subList(skip, skip+maxSize);
\r
135 public int listSize(RewindableIterator anIterator) {
\r
136 anIterator.rewind();
\r
139 while (anIterator.hasNext()) {
\r
144 anIterator.rewind();
\r
149 public int listSize(List aList) {
\r
150 return aList.size();
\r
153 public Object evaluate(Object aTarget, String anExpression) throws Exception {
\r
154 return ParameterExpander.expandExpression(aTarget, anExpression);
\r
157 public String regexpreplace(String aString, String anExpression, String aReplacement) {
\r
158 return StringRoutines.performRegularExpressionReplacement(aString, anExpression, aReplacement);
\r
161 public boolean regexpmatch(String aString, String anExpression) {
\r
162 return StringRoutines.performRegularExpressionSearch(aString, anExpression);
\r