KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > jpdf > PDFStringHelper


1 /*
2  * $Id: PDFStringHelper.java,v 1.1.1.1 2001/10/29 19:51:08 ezb Exp $
3  *
4  * $Date: 2001/10/29 19:51:08 $
5  *
6  * Copyright (C) 2001 Eric Z. Beard, ericzbeard@hotmail.com
7  *
8  *
9  * 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   /**
38    * This converts a string into PDF. It prefixes ( or ) with \
39    * and wraps the string in a ( ) pair.
40    * @param s String to convert
41    * @return String that can be placed in a PDF (or Postscript) stream
42    */

43   public static String JavaDoc makePDFString(String JavaDoc s) {
44     if(s.indexOf("(")>-1)
45       s = replace(s,"(","\\(");
46     
47     if(s.indexOf(")")>-1)
48       s = replace(s,")","\\)");
49     
50     return "("+s+")";
51   }
52   
53   /**
54    * Helper method for toString()
55    * @param s source string
56    * @param f string to remove
57    * @param t string to replace f
58    * @return string with f replaced by t
59    */

60   private static String JavaDoc replace(String JavaDoc source,
61                                 String JavaDoc removeThis,
62                                 String JavaDoc replaceWith) {
63     StringBuffer JavaDoc b = new StringBuffer JavaDoc();
64     int p = 0, c=0;
65     
66     while(c>-1) {
67       if((c = source.indexOf(removeThis,p)) > -1) {
68         b.append(source.substring(p,c));
69         b.append(replaceWith);
70         p=c+1;
71       }
72     }
73     
74     // include any remaining text
75
if(p<source.length())
76       b.append(source.substring(p));
77     
78     return b.toString();
79   }
80 } // end class PDFStringHelper
81
Popular Tags