2 * Copyright (C) 2001, 2002 The Mir-coders group
4 * This file is part of Mir.
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.
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.
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
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.
30 package mircoders.localizer.basic;
32 import java.util.Collections;
33 import java.util.List;
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;
43 public class MirBasicUtilityFunctions {
44 public String encodeXML(Object anObject) throws Exception {
45 return HTMLRoutines.encodeXML(StringRoutines.interpretAsString(anObject));
48 public String encodeHTML(Object aString) throws Exception {
49 return HTMLRoutines.encodeHTML(StringRoutines.interpretAsString(aString));
52 public String prettyEncodeHTML(Object aString) throws Exception {
53 return HTMLRoutines.prettyEncodeHTML(StringRoutines.interpretAsString(aString));
56 public String encodeURI(Object aString) throws Exception {
57 return HTMLRoutines.encodeURL(StringRoutines.interpretAsString(aString));
60 public String encodeURI(Object aString, Object anEncoding) throws Exception {
61 return HTMLRoutines.encodeURL(
62 StringRoutines.interpretAsString(aString),
63 StringRoutines.interpretAsString(anEncoding));
66 public String subString(Object aString, Object aFrom) throws Exception {
67 return StringRoutines.interpretAsString(aString).substring(StringRoutines.interpretAsInteger(aFrom));
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();
77 return target.substring(StringRoutines.interpretAsInteger(aFrom), length);
80 public String escapeJDBCString(Object aString) throws Exception {
81 return JDBCStringRoutines.escapeStringLiteral(StringRoutines.interpretAsString(aString));
84 public String constructString(Object aString) throws Exception {
86 return StructuredContentParser.constructStringLiteral("");
87 return StructuredContentParser.constructStringLiteral(StringRoutines.interpretAsString(aString));
90 public Object parseStructuredString(Object aString) throws Exception {
93 return StructuredContentParser.parse(StringRoutines.interpretAsString(aString));
96 public boolean isOdd(Object anInteger) throws Exception {
97 return (StringRoutines.interpretAsInteger(anInteger) & 1) == 1;
100 public int increment(Object anInteger) throws Exception {
101 final Integer ONE = new Integer(1);
103 return increment(anInteger, ONE);
106 public int increment(Object anInteger, Object anIncrement) throws Exception {
107 return StringRoutines.interpretAsInteger(anInteger) +
108 StringRoutines.interpretAsInteger(anIncrement);
111 public Object subList(Object aList, Object aSkip) throws Exception {
112 return subList(aList, aSkip, new Integer(-1));
115 public Object subList(Object aList, Object aSkip, Object aMaxSize) throws Exception {
116 int skip = StringRoutines.interpretAsInteger(aSkip);
117 int maxSize = StringRoutines.interpretAsInteger(aMaxSize);
119 if (aList instanceof RewindableIterator)
120 return new SubsetIterator((RewindableIterator) aList, skip, maxSize);
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);
130 public int listSize(RewindableIterator anIterator) {
134 while (anIterator.hasNext()) {
144 public int listSize(List aList) {
148 public Object evaluate(Object aTarget, String anExpression) throws Exception {
149 return ParameterExpander.expandExpression(aTarget, anExpression);
152 public String regexpreplace(String aString, String anExpression, String aReplacement) {
153 return StringRoutines.performRegularExpressionReplacement(aString, anExpression, aReplacement);