01: /*
02: * $Id: PDFStringHelper.java,v 1.1.1.1 2001/10/29 19:51:08 ezb Exp $
03: *
04: * $Date: 2001/10/29 19:51:08 $
05: *
06: * Copyright (C) 2001 Eric Z. Beard, ericzbeard@hotmail.com
07: *
08: *
09: * This library is free software; you can redistribute it and/or
10: * modify it under the terms of the GNU Lesser General Public
11: * License as published by the Free Software Foundation; either
12: * version 2.1 of the License, or (at your option) any later version.
13: *
14: * This library is distributed in the hope that it will be useful,
15: * but WITHOUT ANY WARRANTY; without even the implied warranty of
16: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17: * Lesser General Public License for more details.
18: *
19: * You should have received a copy of the GNU Lesser General Public
20: * License along with this library; if not, write to the Free Software
21: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22: *
23: */
24:
25: package gnu.jpdf;
26:
27: /**
28: * String manipulation methods
29: *
30: * @author Eric Z. Beard, ericzbeard@hotmail.com
31: * @author $Author: ezb $
32: * @version $Revision: 1.1.1.1 $, $Date: 2001/10/29 19:51:08 $
33: *
34: */
35: public class PDFStringHelper {
36: /**
37: * This converts a string into PDF. It prefixes ( or ) with \
38: * and wraps the string in a ( ) pair.
39: * @param s String to convert
40: * @return String that can be placed in a PDF (or Postscript) stream
41: */
42: public static String makePDFString(String s) {
43: if (s.indexOf("(") > -1)
44: s = replace(s, "(", "\\(");
45:
46: if (s.indexOf(")") > -1)
47: s = replace(s, ")", "\\)");
48:
49: return "(" + s + ")";
50: }
51:
52: /**
53: * Helper method for toString()
54: * @param s source string
55: * @param f string to remove
56: * @param t string to replace f
57: * @return string with f replaced by t
58: */
59: private static String replace(String source, String removeThis,
60: String replaceWith) {
61: StringBuffer b = new StringBuffer();
62: int p = 0, c = 0;
63:
64: while (c > -1) {
65: if ((c = source.indexOf(removeThis, p)) > -1) {
66: b.append(source.substring(p, c));
67: b.append(replaceWith);
68: p = c + 1;
69: }
70: }
71:
72: // include any remaining text
73: if (p < source.length())
74: b.append(source.substring(p));
75:
76: return b.toString();
77: }
78: } // end class PDFStringHelper
|