KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > yagga > util > StringManip


1 /*
2  * This file is part of MiniInstaller, a self installer builder for Java
3  * Copyright (C) 2002 Walter Gamba
4  * mailto:walter@yagga.net
5  * http://www.yagga.net/java/miniinstaller
6  *
7  * MiniInstaller is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * MiniInstaller is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  *
21  * As the time of writing, the GNU General Public Licene can be
22  * found at http://www.gnu.org/licenses/gpl.txt
23  *
24  */

25
26 package net.yagga.util;
27
28 import java.util.*;
29 /**
30   class for manipulating strings........
31 */

32
33 public class StringManip {
34
35   static public void main(String JavaDoc[] argv){
36     HashMap subst=new HashMap();
37     subst.put("<NICK>","__NIC__");
38     //with no trailing "\"
39
subst.put("<SERVER_DIR>","__SERVER_DIR__");
40     subst.put("<VERSION_NAME>","__VER__");
41     subst.put("<FILE_VERSION>","__VERNUM__");
42     subst.put("<OS>","win" );
43
44    /*String initConf=Ut.readFile("test.txt");
45     initConf=StringManip.makeSubsts(initConf,subst);
46     System.out.println(initConf);*/

47     String JavaDoc sr="aasd a da\\n sa\\ads asads\\tasda\\nas c:\\\\temp dsda";
48      System.out.println(sr);
49      System.out.println(escape(sr,true));
50   }
51
52   public StringManip() {
53   }
54
55   /**
56     does a global subst of the given pairs in the original string. Returns the changed string.
57     The pairs assoc array contains values whose key is:
58     search1->subst1
59   */

60   public static String JavaDoc makeSubsts(String JavaDoc orig, HashMap pairs){
61     Iterator it=pairs.keySet().iterator();
62     String JavaDoc ret=orig;
63     while(it.hasNext()){
64         String JavaDoc search=(String JavaDoc)it.next();
65       ret=subst(ret,search,(String JavaDoc)pairs.get(search),true);
66     }
67     return ret;
68   }
69
70   /**
71    * search an exact string in the given one, and if found, subst it with replace
72    * does a global replace if globa=true
73    * $return_value=$orig+~s/search/subst/global
74    */

75   public static String JavaDoc subst(String JavaDoc orig, String JavaDoc search, String JavaDoc replace, boolean global)
76   {
77     int i=-1;
78     int len1=search.length();
79     int len2=replace.length();
80
81     String JavaDoc _subst=orig;
82     if(orig==null)
83       return null;
84     i=_subst.indexOf(search);
85     //System.err.println("i="+i);
86
while(i>=0){
87       //do replace
88
//System.err.println("befrepl="+_subst);
89
_subst=_subst.substring(0,i)+replace+_subst.substring(i+len1,_subst.length());
90     //System.err.println("aftrepl="+_subst);
91

92       i=_subst.indexOf(search,i+len2);
93     //System.err.println("i="+i);
94
if(!global)
95         break;
96     }
97     //System.err.println("Ex");
98
return _subst;
99   }
100
101     /**
102      * Escape most usual characters sequences: \n \r \t.
103      * Treat \+(any other char) as -> (any other char), so \b -> b and \\ -> \
104      * @param str the string where to search escapes
105      * @param unix tell if system is unix like or not. If unix-like treat \r as empty string
106      * so \r\n -> \n and \r -> ''
107      * @return string with escape substituted
108      */

109   public static String JavaDoc escape(String JavaDoc orig, boolean unix){
110     if(orig==null)
111       return null;
112     String JavaDoc str=orig;
113     StringBuffer JavaDoc sb=new StringBuffer JavaDoc();
114     int len=str.length();
115     int i=0, s=0;
116     while(s<len){
117       i=str.indexOf("\\",s);
118       if(i!=-1){
119         int ch=-1;
120         String JavaDoc esc=null;
121         if(i<len-1)
122           ch=str.charAt(i+1);
123         switch(ch){
124           case 't': esc="\t"; break;
125           case 'r':
126             if(unix)
127               esc="";
128             else
129               esc="\r";
130             break;
131           case 'n': esc="\n"; break;
132           default: Character JavaDoc c=new Character JavaDoc((char)ch); esc=c.toString();
133         }
134         sb.append(str.substring(s,i));
135         sb.append(esc);
136         s=i+2;
137         //System.out.println("S="+s+",I="+i+", sb="+sb);
138
}
139       else{ //i==-1{
140
sb.append(str.substring(s));
141         break;
142       }
143
144     }
145     //System.out.println("ORIG<"+orig+"> ESC=<"+sb+">");
146
return sb.toString();
147   }
148
149 }
150
Popular Tags