~ubuntu-branches/ubuntu/vivid/herold/vivid

« back to all changes in this revision

Viewing changes to java/org/dbdoclet/service/UnicodeServices.java

  • Committer: Package Import Robot
  • Author(s): Mathieu Malaterre
  • Date: 2012-09-20 10:00:14 UTC
  • Revision ID: package-import@ubuntu.com-20120920100014-5pcwbw2err6on8yg
Tags: upstream-6.0.1
Import upstream version 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.dbdoclet.service;
 
2
 
 
3
import java.util.regex.Matcher;
 
4
import java.util.regex.Pattern;
 
5
 
 
6
/**
 
7
 * Die Klasse <code>UnicodeServices</code> stellt eine Sammlung statischer
 
8
 * Methoden zur Bearbeitung von Unicode zur Verfügung.
 
9
 *
 
10
 * @author <a href="mailto:michael.fuchs@unico-group.com">Michael Fuchs</a>
 
11
 * @version 1.0
 
12
 */
 
13
public class UnicodeServices {
 
14
 
 
15
    private static Pattern pattern = Pattern.compile("\\\\u[0-9a-fA-F]{4}");
 
16
 
 
17
    public static String escape(char c) {
 
18
 
 
19
        char[] v = new char[1];
 
20
        v[0] = c;
 
21
 
 
22
        return escape(new String(v));
 
23
    }
 
24
 
 
25
    /**
 
26
     * Die Methode <code>escape</code> wandelt die Zeichenkette
 
27
     * <code>str</code> in eine 7-bit ASCII Darstellung um.
 
28
     *
 
29
     * @param str <code>String</code>
 
30
     * @return <code>String</code>
 
31
     */
 
32
    public static String escape(String str) {
 
33
 
 
34
        if (str == null) {
 
35
            return "";
 
36
        }
 
37
        
 
38
        StringBuffer buffer = new StringBuffer();
 
39
        
 
40
        for (int i = 0; i < str.length(); i++) {
 
41
            
 
42
            char c = str.charAt(i);
 
43
            int n = c;
 
44
 
 
45
            if (c == '\r') {
 
46
                continue;
 
47
            }
 
48
 
 
49
            if (n >= 0 && n < 128 && c != '\n' && c != '\\') {
 
50
 
 
51
               buffer.append(c);
 
52
 
 
53
            } else {
 
54
 
 
55
                buffer.append("\\u");
 
56
                String hex = Integer.toHexString(n);
 
57
 
 
58
                for (int j = 0; j < 4 - hex.length(); j++) {
 
59
                    buffer.append("0");
 
60
                }
 
61
 
 
62
                buffer.append(hex);
 
63
            }
 
64
        }
 
65
 
 
66
        return buffer.toString();
 
67
    }
 
68
 
 
69
    public static String unescape(String str) {
 
70
 
 
71
        Matcher matcher = pattern.matcher(str);
 
72
 
 
73
        char[] c = new char[1];
 
74
        String escape;
 
75
        String buffer;
 
76
        String part1;
 
77
        String part2;
 
78
 
 
79
        while (matcher.find()) {
 
80
 
 
81
            escape = matcher.group();
 
82
            c[0] = (char) Integer.parseInt(escape.substring(2), 16);
 
83
            buffer = new String(c);
 
84
            // System.out.println("buffer = " + buffer);
 
85
 
 
86
            part1 = str.substring(0, matcher.start());
 
87
            // System.out.println("part1 = " + part1);
 
88
 
 
89
            part2 = str.substring(matcher.end());
 
90
            // System.out.println("part2 = " + part2);
 
91
            
 
92
            str = part1 + buffer + part2;
 
93
            // System.out.println("str = " + str);
 
94
 
 
95
            matcher.reset(str);
 
96
        }
 
97
 
 
98
        return str;
 
99
    }
 
100
 
 
101
    public static String removeUndefinedCharacters(String text) {
 
102
        
 
103
        if (text == null || text.length() == 0) {
 
104
            return "";
 
105
        }
 
106
 
 
107
        StringBuffer textBuffer = new StringBuffer(text);
 
108
        StringBuffer buffer = new StringBuffer();
 
109
        
 
110
        char c;
 
111
        
 
112
        for (int i = 0; i < textBuffer.length(); i++) {
 
113
 
 
114
            c = textBuffer.charAt(i);
 
115
            
 
116
            if (c == '\n' || c == '\t') {
 
117
                buffer.append(c);
 
118
                continue;
 
119
            }
 
120
 
 
121
            if (Character.isISOControl(c) == false 
 
122
                && Character.isDefined(c) == true) {
 
123
 
 
124
                buffer.append(c);
 
125
            }
 
126
        }
 
127
 
 
128
        return buffer.toString();
 
129
    }
 
130
}