KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > chaperon > common > Decoder


1 /*
2  * Copyright (C) Chaperon. All rights reserved.
3  * -------------------------------------------------------------------------
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE file.
7  */

8
9 package net.sourceforge.chaperon.common;
10
11 /**
12  * The class maskes special characters
13  *
14  * @author <a HREF="mailto:stephan@apache.org">Stephan Michels</a>
15  * @version CVS $Id: Decoder.java,v 1.9 2003/12/10 16:30:35 benedikta Exp $
16  */

17 public class Decoder
18 {
19   public static final String JavaDoc STRING = "\"";
20   public static final String JavaDoc CHAR = "\'";
21   public static final String JavaDoc CLASS = "[^- ]";
22   public static final String JavaDoc REGEX = "+*?^$() ";
23
24   /**
25    * Mask special characters
26    *
27    * @param string String
28    *
29    * @return Masked string
30    */

31   public static String JavaDoc toString(String JavaDoc string)
32   {
33     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
34
35     buffer.append("\"");
36     for (int i = 0; i<string.length(); i++)
37       buffer.append(decode(string.charAt(i), STRING));
38
39     buffer.append("\"");
40     return buffer.toString();
41   }
42
43   public static String JavaDoc toChar(char character)
44   {
45     return "'"+decode(character, CHAR)+"'";
46   }
47
48   public static String JavaDoc toClass(char minimum, char maximum)
49   {
50     if (minimum==maximum)
51       return toChar(minimum);
52     else
53     {
54       StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
55       buffer.append("[");
56       buffer.append(decode(minimum, CLASS));
57       buffer.append("-");
58       buffer.append(decode(maximum, CLASS));
59       buffer.append("]");
60       return buffer.toString();
61     }
62   }
63
64   public static String JavaDoc toNegativeClass(char minimum, char maximum)
65   {
66     if (minimum==maximum)
67     {
68       StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
69       buffer.append("[^");
70       buffer.append(decode(minimum, CLASS));
71       buffer.append("]");
72       return buffer.toString();
73     }
74     else
75     {
76       StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
77       buffer.append("[^");
78       buffer.append(decode(minimum, CLASS));
79       buffer.append("-");
80       buffer.append(decode(maximum, CLASS));
81       buffer.append("]");
82       return buffer.toString();
83     }
84   }
85
86   public static String JavaDoc decode(char character, String JavaDoc masked)
87   {
88     if (character=='\n')
89       return "\\n";
90     else if (character=='\t')
91       return "\\t";
92     else if (character=='\r')
93       return "\\r";
94     else if (character=='\\')
95       return "\\\\";
96     else if ((character>=' ') && (character<='~') && (masked.indexOf(character)<0))
97       return String.valueOf(character);
98     else if ((character>=' ') && (character<='~'))
99       return "\\"+character;
100
101     String JavaDoc hexstring = Integer.toHexString(character);
102     String JavaDoc zeros = "0000";
103
104     return "\\u"+zeros.substring(hexstring.length())+hexstring;
105   }
106
107   public static String JavaDoc decode(String JavaDoc string, String JavaDoc masked)
108   {
109     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
110
111     for (int i = 0; i<string.length(); i++)
112       buffer.append(decode(string.charAt(i), masked));
113
114     return buffer.toString();
115   }
116 }
117
Popular Tags