001: /*
002: * ============================================================================
003: * GNU Lesser General Public License
004: * ============================================================================
005: *
006: * JasperReports - Free Java report-generating library.
007: * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
008: *
009: * This library is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU Lesser General Public
011: * License as published by the Free Software Foundation; either
012: * version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: * Lesser General Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
022: *
023: * JasperSoft Corporation
024: * 303 Second Street, Suite 450 North
025: * San Francisco, CA 94107
026: * http://www.jaspersoft.com
027: */
028:
029: /*
030: * Contributors:
031: * Gaganis Giorgos - gaganis@users.sourceforge.net
032: */
033: package net.sf.jasperreports.engine.util;
034:
035: /**
036: * @author Teodor Danciu (teodord@users.sourceforge.net)
037: * @version $Id: JRStringUtil.java 1681 2007-03-29 15:33:50Z teodord $
038: */
039: public class JRStringUtil {
040:
041: /**
042: *
043: */
044: public static String xmlEncode(String text) {
045: int length = text.length();
046: if (text != null && length > 0) {
047: StringBuffer ret = new StringBuffer(length * 12 / 10);
048:
049: int last = 0;
050: for (int i = 0; i < length; i++) {
051: char c = text.charAt(i);
052: switch (c) {
053: // case ' ' : ret.append(" "); break;
054: case '&':
055: if (last < i) {
056: ret.append(text.substring(last, i));
057: }
058: last = i + 1;
059:
060: ret.append("&");
061: break;
062: case '>':
063: if (last < i) {
064: ret.append(text.substring(last, i));
065: }
066: last = i + 1;
067:
068: ret.append(">");
069: break;
070: case '<':
071: if (last < i) {
072: ret.append(text.substring(last, i));
073: }
074: last = i + 1;
075:
076: ret.append("<");
077: break;
078: case '\"':
079: if (last < i) {
080: ret.append(text.substring(last, i));
081: }
082: last = i + 1;
083:
084: ret.append(""");
085: break;
086: case '\'':
087: if (last < i) {
088: ret.append(text.substring(last, i));
089: }
090: last = i + 1;
091:
092: ret.append("'");
093: break;
094:
095: default:
096: break;
097: }
098: }
099:
100: if (last < length) {
101: ret.append(text.substring(last));
102: }
103:
104: return ret.toString();
105: }
106:
107: return text;
108: }
109:
110: /**
111: *
112: */
113: public static String htmlEncode(String text) {
114: int length = text.length();
115: if (text != null && length > 0) {
116: StringBuffer ret = new StringBuffer(length * 12 / 10);
117:
118: boolean isEncodeSpace = true;
119: int last = 0;
120: for (int i = 0; i < length; i++) {
121: char c = text.charAt(i);
122: switch (c) {
123: case ' ':
124: if (isEncodeSpace) {
125: if (last < i) {
126: ret.append(text.substring(last, i));
127: }
128: last = i + 1;
129:
130: ret.append(" ");
131: isEncodeSpace = false;
132: } else {
133: isEncodeSpace = true;
134: }
135: break;
136: case '&':
137: if (last < i) {
138: ret.append(text.substring(last, i));
139: }
140: last = i + 1;
141:
142: ret.append("&");
143: isEncodeSpace = false;
144: break;
145: case '>':
146: if (last < i) {
147: ret.append(text.substring(last, i));
148: }
149: last = i + 1;
150:
151: ret.append(">");
152: isEncodeSpace = false;
153: break;
154: case '<':
155: if (last < i) {
156: ret.append(text.substring(last, i));
157: }
158: last = i + 1;
159:
160: ret.append("<");
161: isEncodeSpace = false;
162: break;
163: case '\"':
164: if (last < i) {
165: ret.append(text.substring(last, i));
166: }
167: last = i + 1;
168:
169: ret.append(""");
170: isEncodeSpace = false;
171: break;
172: // it does not work in IE
173: // case '\'' :
174: // if (last < i)
175: // {
176: // ret.append(text.substring(last, i));
177: // }
178: // last = i + 1;
179: //
180: // ret.append("'");
181: // isEncodeSpace = false;
182: // break;
183: case '\n':
184: if (last < i) {
185: ret.append(text.substring(last, i));
186: }
187: last = i + 1;
188:
189: ret.append("<br/>");
190: isEncodeSpace = false;
191: break;
192:
193: default:
194: isEncodeSpace = false;
195: break;
196: }
197: }
198:
199: if (last < length) {
200: ret.append(text.substring(last));
201: }
202:
203: return ret.toString();
204: }
205:
206: return text;
207: }
208:
209: /**
210: * Takes a name and returns the same if it is a Java identifier;
211: * else it substitutes the illegal characters so that it can be an identifier
212: *
213: * @param name
214: */
215: public static String getLiteral(String name) {
216: if (isValidLiteral(name)) {
217: return name;
218: }
219:
220: StringBuffer buffer = new StringBuffer(name.length() + 5);
221:
222: char[] literalChars = new char[name.length()];
223: name.getChars(0, literalChars.length, literalChars, 0);
224:
225: for (int i = 0; i < literalChars.length; i++) {
226: if (i == 0
227: && !Character
228: .isJavaIdentifierStart(literalChars[i])) {
229: buffer.append((int) literalChars[i]);
230: } else if (i != 0
231: && !Character.isJavaIdentifierPart(literalChars[i])) {
232: buffer.append((int) literalChars[i]);
233: } else {
234: buffer.append(literalChars[i]);
235: }
236: }
237:
238: return buffer.toString();
239: }
240:
241: /**
242: * Checks if the input is a valid Java literal
243: * @param literal
244: * @author Gaganis Giorgos (gaganis@users.sourceforge.net)
245: */
246: private static boolean isValidLiteral(String literal) {
247: boolean result = true;
248:
249: char[] literalChars = new char[literal.length()];
250: literal.getChars(0, literalChars.length, literalChars, 0);
251:
252: for (int i = 0; i < literalChars.length; i++) {
253: if (i == 0
254: && !Character
255: .isJavaIdentifierStart(literalChars[i])) {
256: result = false;
257: break;
258: }
259:
260: if (i != 0
261: && !Character.isJavaIdentifierPart(literalChars[i])) {
262: result = false;
263: break;
264: }
265: }
266:
267: return result;
268: }
269:
270: /**
271: * Replaces DOS end of line (CRLF) with Unix end of line (LF).
272: *
273: * @param text the text
274: * @return the text with CRLF replaced by LF; if no CRLF was found, the same object is returned.
275: */
276: public static String replaceDosEOL(String text) {
277: if (text == null || text.length() < 2) {
278: return text;
279: }
280:
281: int length = text.length();
282: char[] chars = text.toCharArray();
283: int r = 0;
284: for (int i = 0; i < length; ++i) {
285: char ch = chars[i];
286: if (!(ch == '\r' && i + 1 < length && chars[i + 1] == '\n')) {
287: if (r > 0) {
288: chars[i - r] = ch;
289: }
290: } else {
291: ++r;
292: }
293: }
294:
295: return r > 0 ? new String(chars, 0, length - r) : text;
296: }
297: }
|