001: /**
002: * $Id: Edit.java,v 1.4 2003/01/10 02:53:28 rakeshn Exp $
003: * Copyright 2002 Sun Microsystems, Inc. All
004: * rights reserved. Use of this product is subject
005: * to license terms. Federal Acquisitions:
006: * Commercial Software -- Government Users
007: * Subject to Standard License Terms and
008: * Conditions.
009: *
010: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
011: * are trademarks or registered trademarks of Sun Microsystems,
012: * Inc. in the United States and other countries.
013: */package com.sun.portal.wireless.taglibs.base;
014:
015: import com.sun.portal.desktop.encode.Encoder;
016: import com.sun.portal.desktop.encode.EncoderException;
017:
018: public class Edit {
019:
020: public static final int ESCAPE_STYLE_NONE = 0;
021: public static final int ESCAPE_STYLE_XML = 1;
022: public static final int ESCAPE_STYLE_WML = 2;
023:
024: private int truncation = -1;
025: private boolean escape = false;
026: private String from = null;
027: private int fromLength = 0;
028: private String to = "";
029: private int toLength = 0;
030: private String escapeClassName;
031:
032: /**
033: * Edit constructor...
034: *
035: */
036: public Edit(String escapeClassNameParam) {
037: this .escapeClassName = escapeClassNameParam;
038: }
039:
040: /**
041: * String substitution...
042: *
043: */
044: private String substituteString(String string) {
045: int matchIndex = string.indexOf(from);
046:
047: if (matchIndex == -1)
048: return string;
049:
050: return string.substring(0, matchIndex)
051: + to
052: + substituteString(string.substring(matchIndex
053: + fromLength));
054: }
055:
056: /**
057: * Process string according to
058: * current edit rules.
059: *
060: * @return processed string
061: */
062: public String editString(String string) {
063: int length = 0;
064:
065: if (string != null) {
066: length = string.length();
067: }
068:
069: if (length > 0) {
070: // Perform case conversion
071: //
072: if (toLower) {
073: string = string.toLowerCase();
074: } else if (toUpper) {
075: string = string.toUpperCase();
076: }
077:
078: // Perform substitution...
079: if (fromLength != 0) {
080: string = substituteString(string);
081: }
082:
083: // Compute truncation...
084: length = string.length();
085: int trunc = truncation;
086: if (trunc < 0) {
087: trunc = length;
088: } else {
089: trunc = (trunc > length) ? length : trunc;
090: }
091:
092: // Truncate, escape, and output...
093: if (escape) {
094: try {
095: return Encoder.encode(escapeClassName, string
096: .substring(0, trunc));
097: } catch (EncoderException ex) {
098: return string.substring(0, trunc);
099: }
100: } else {
101: return string.substring(0, trunc);
102: }
103: }
104:
105: return string;
106: }
107:
108: /**
109: * Get from...
110: *
111: * @return the from value.
112: */
113: public String getFrom() {
114: return from;
115: }
116:
117: /**
118: * Set from...
119: *
120: * @return none
121: */
122: public void setFrom(String s) {
123: from = s;
124: fromLength = from.length();
125: return;
126: }
127:
128: /**
129: * Get to...
130: *
131: * @return the from value.
132: */
133: public String getTo() {
134: return to;
135: }
136:
137: /**
138: * Set from...
139: *
140: * @return none
141: */
142: public void setTo(String s) {
143: to = s;
144: toLength = to.length();
145: return;
146: }
147:
148: /**
149: * Get truncation...
150: *
151: * @return the truncation value.
152: */
153: public int getTruncation() {
154: return truncation;
155: }
156:
157: /**
158: * Set truncation...
159: *
160: * @return none
161: */
162: public void setTruncation(int v) {
163: truncation = v;
164: }
165:
166: /**
167: * Get escape...
168: *
169: * @return the escape value.
170: */
171: public boolean getEscape() {
172: return escape;
173: }
174:
175: /**
176: * Set escape...
177: *
178: * @return none
179: */
180: public void setEscape(boolean b) {
181: escape = b;
182: }
183:
184: /**
185: * Specify if the property is to be returned upper/lowercase
186: */
187:
188: private boolean toLower = false;
189: private boolean toUpper = false;
190:
191: /**
192: * Set the case.
193: */
194: public void setCase(String type) {
195: String caseType = null;
196:
197: if (type != null) {
198: caseType = type.trim();
199: if (caseType.equals("")) // throw away empty strings
200: caseType = null;
201: }
202:
203: if (caseType != null) {
204: if (caseType.equalsIgnoreCase("lower"))
205: toLower = true;
206: else if (caseType.equalsIgnoreCase("upper"))
207: toUpper = true;
208: }
209: }
210:
211: }
|