~ubuntu-branches/ubuntu/vivid/icu4j-4.4/vivid

« back to all changes in this revision

Viewing changes to main/classes/core/src/com/ibm/icu/impl/duration/impl/XMLRecordWriter.java

  • Committer: Bazaar Package Importer
  • Author(s): Niels Thykier
  • Date: 2011-08-02 15:50:33 UTC
  • Revision ID: james.westby@ubuntu.com-20110802155033-itjzsl21y2lqdonn
Tags: upstream-4.4.2
ImportĀ upstreamĀ versionĀ 4.4.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 ******************************************************************************
 
3
 * Copyright (C) 2007-2010, International Business Machines Corporation and   *
 
4
 * others. All Rights Reserved.                                               *
 
5
 ******************************************************************************
 
6
 */
 
7
 
 
8
package com.ibm.icu.impl.duration.impl;
 
9
 
 
10
import java.io.IOException;
 
11
import java.io.Writer;
 
12
import java.util.ArrayList;
 
13
import java.util.List;
 
14
 
 
15
import com.ibm.icu.lang.UCharacter;
 
16
 
 
17
public class XMLRecordWriter implements RecordWriter {
 
18
    private Writer w;
 
19
    private List<String> nameStack;
 
20
 
 
21
    public XMLRecordWriter(Writer w) {
 
22
        this.w = w;
 
23
        this.nameStack = new ArrayList<String>();
 
24
    }
 
25
 
 
26
    public boolean open(String title) {
 
27
        newline();
 
28
        writeString("<" + title + ">");
 
29
        nameStack.add(title);
 
30
        return true;
 
31
    }
 
32
 
 
33
    public boolean close() {
 
34
        int ix = nameStack.size() - 1;
 
35
        if (ix >= 0) {
 
36
            String name = nameStack.remove(ix);
 
37
            newline();
 
38
            writeString("</" + name + ">");
 
39
            return true;
 
40
        }
 
41
        return false;
 
42
    }
 
43
 
 
44
    public void flush() {
 
45
        try {
 
46
            w.flush();
 
47
        } catch (IOException e) {
 
48
        }
 
49
    }
 
50
 
 
51
    public void bool(String name, boolean value) {
 
52
        internalString(name, String.valueOf(value));
 
53
    }
 
54
 
 
55
    public void boolArray(String name, boolean[] values) {
 
56
        if (values != null) {
 
57
            String[] stringValues = new String[values.length];
 
58
            for (int i = 0; i < values.length; ++i) {
 
59
                stringValues[i] = String.valueOf(values[i]);
 
60
            }
 
61
            stringArray(name, stringValues);
 
62
        }
 
63
    }
 
64
 
 
65
    private static String ctos(char value) {
 
66
        if (value == '<') {
 
67
            return "&lt;";
 
68
        }
 
69
        if (value == '&') {
 
70
            return "&amp;";
 
71
        }
 
72
        return String.valueOf(value);
 
73
    }
 
74
 
 
75
    public void character(String name, char value) {
 
76
        if (value != '\uffff') {
 
77
            internalString(name, ctos(value));
 
78
        }
 
79
    }
 
80
 
 
81
    public void characterArray(String name, char[] values) {
 
82
        if (values != null) {
 
83
            String[] stringValues = new String[values.length];
 
84
            for (int i = 0; i < values.length; ++i) {
 
85
                char value = values[i];
 
86
                if (value == '\uffff') {
 
87
                    stringValues[i] = NULL_NAME;
 
88
                } else {
 
89
                    stringValues[i] = ctos(value);
 
90
                }
 
91
            }
 
92
            internalStringArray(name, stringValues);
 
93
        }
 
94
    }
 
95
 
 
96
    public void namedIndex(String name, String[] names, int value) {
 
97
        if (value >= 0) {
 
98
            internalString(name, names[value]);
 
99
        }
 
100
    }
 
101
 
 
102
    public void namedIndexArray(String name, String[] names, byte[] values) {
 
103
        if (values != null) {
 
104
            String[] stringValues = new String[values.length];
 
105
            for (int i = 0; i < values.length; ++i) {
 
106
                int value = values[i];
 
107
                if (value < 0) {
 
108
                    stringValues[i] = NULL_NAME;
 
109
                } else {
 
110
                    stringValues[i] = names[value];
 
111
                }
 
112
            }
 
113
            internalStringArray(name, stringValues);
 
114
        }
 
115
    }
 
116
 
 
117
    public static String normalize(String str) {
 
118
        if (str == null) {
 
119
            return null;
 
120
        }
 
121
        StringBuilder sb = null;
 
122
        boolean inWhitespace = false;
 
123
        char c = '\0';
 
124
        boolean special = false;
 
125
        for (int i = 0; i < str.length(); ++i) {
 
126
            c = str.charAt(i);
 
127
            if (UCharacter.isWhitespace(c)) {
 
128
                if (sb == null && (inWhitespace || c != ' ')) {
 
129
                    sb = new StringBuilder(str.substring(0, i));
 
130
                }
 
131
                if (inWhitespace) {
 
132
                    continue;
 
133
                }
 
134
                inWhitespace = true;
 
135
                special = false;
 
136
                c = ' ';
 
137
            } else {
 
138
                inWhitespace = false;
 
139
                special = c == '<' || c == '&';
 
140
                if (special && sb == null) {
 
141
                    sb = new StringBuilder(str.substring(0, i));
 
142
                }
 
143
            }
 
144
            if (sb != null) {
 
145
                if (special) {
 
146
                    sb.append(c == '<' ? "&lt;" : "&amp;");
 
147
                } else {
 
148
                    sb.append(c);
 
149
                }
 
150
            }
 
151
        }
 
152
        if (sb != null) {
 
153
            /*
 
154
             * if (c == ' ') { int len = sb.length(); if (len == 0) { return
 
155
             * " "; } if (len > 1 && c == ' ') { sb.deleteCharAt(len - 1); } }
 
156
             */
 
157
            return sb.toString();
 
158
        }
 
159
        return str;
 
160
    }
 
161
 
 
162
    private void internalString(String name, String normalizedValue) {
 
163
        if (normalizedValue != null) {
 
164
            newline();
 
165
            writeString("<" + name + ">" + normalizedValue + "</" + name + ">");
 
166
        }
 
167
    }
 
168
 
 
169
    private void internalStringArray(String name, String[] normalizedValues) {
 
170
        if (normalizedValues != null) {
 
171
            push(name + "List");
 
172
            for (int i = 0; i < normalizedValues.length; ++i) {
 
173
                String value = normalizedValues[i];
 
174
                if (value == null) {
 
175
                    value = NULL_NAME;
 
176
                }
 
177
                string(name, value);
 
178
            }
 
179
            pop();
 
180
        }
 
181
    }
 
182
 
 
183
    public void string(String name, String value) {
 
184
        internalString(name, normalize(value));
 
185
    }
 
186
 
 
187
    public void stringArray(String name, String[] values) {
 
188
        if (values != null) {
 
189
            push(name + "List");
 
190
            for (int i = 0; i < values.length; ++i) {
 
191
                String value = normalize(values[i]);
 
192
                if (value == null) {
 
193
                    value = NULL_NAME;
 
194
                }
 
195
                internalString(name, value);
 
196
            }
 
197
            pop();
 
198
        }
 
199
    }
 
200
 
 
201
    public void stringTable(String name, String[][] values) {
 
202
        if (values != null) {
 
203
            push(name + "Table");
 
204
            for (int i = 0; i < values.length; ++i) {
 
205
                String[] rowValues = values[i];
 
206
                if (rowValues == null) {
 
207
                    internalString(name + "List", NULL_NAME);
 
208
                } else {
 
209
                    stringArray(name, rowValues);
 
210
                }
 
211
            }
 
212
            pop();
 
213
        }
 
214
    }
 
215
 
 
216
    private void push(String name) {
 
217
        newline();
 
218
        writeString("<" + name + ">");
 
219
        nameStack.add(name);
 
220
    }
 
221
 
 
222
    private void pop() {
 
223
        int ix = nameStack.size() - 1;
 
224
        String name = nameStack.remove(ix);
 
225
        newline();
 
226
        writeString("</" + name + ">");
 
227
    }
 
228
 
 
229
    private void newline() {
 
230
        writeString("\n");
 
231
        for (int i = 0; i < nameStack.size(); ++i) {
 
232
            writeString(INDENT);
 
233
        }
 
234
    }
 
235
 
 
236
    private void writeString(String str) {
 
237
        if (w != null) {
 
238
            try {
 
239
                w.write(str);
 
240
            } catch (IOException e) {
 
241
                // if there's a problem, record it and stop writing
 
242
                System.err.println(e.getMessage());
 
243
                w = null;
 
244
            }
 
245
        }
 
246
    }
 
247
 
 
248
    static final String NULL_NAME = "Null";
 
249
    private static final String INDENT = "    ";
 
250
}