KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > modfact > jmi > xmi > CharTranslator


1 package org.objectweb.modfact.jmi.xmi;
2
3 import java.util.*;
4
5 public class CharTranslator {
6
7     Collection set = new Vector();
8     
9     public CharTranslator() {
10         set.add(new TranslationEntry("<","&lt;"));
11         set.add(new TranslationEntry(">","&gt;"));
12     }
13     
14     
15     public String JavaDoc encode(String JavaDoc s) {
16         Iterator it = set.iterator();
17         while(it.hasNext()) {
18             TranslationEntry entry = (TranslationEntry) it.next();
19             s = replaceAll(s, entry.view ,entry.encode);
20         }
21         return s;
22     }
23         
24     public String JavaDoc view(String JavaDoc s) {
25         Iterator it = set.iterator();
26         while(it.hasNext()) {
27             TranslationEntry entry = (TranslationEntry) it.next();
28             s = replaceAll(s, entry.encode ,entry.view);
29         }
30         return s;
31     }
32     
33     
34     public static String JavaDoc replaceAll(String JavaDoc in, String JavaDoc replace, String JavaDoc by) {
35         int i;
36         while((i=in.indexOf(replace))>=0) {
37           in = in.substring(0, i)
38             + by
39             + in.substring(i + replace.length());
40         }
41         return in;
42     }
43     
44     
45     static class TranslationEntry {
46         String JavaDoc view;
47         String JavaDoc encode;
48         TranslationEntry(String JavaDoc v, String JavaDoc en) {
49           view = v;
50           encode = en;
51         }
52     }
53     
54 }
55
Popular Tags