01: /*
02: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
03: * Distributed under the terms of either:
04: * - the common development and distribution license (CDDL), v1.0; or
05: * - the GNU Lesser General Public License, v2.1 or later
06: * $Id: ExternalValue.java 3634 2007-01-08 21:42:24Z gbevin $
07: */
08: package com.uwyn.rife.template;
09:
10: import com.uwyn.rife.tools.StringUtils;
11: import java.io.IOException;
12: import java.io.OutputStream;
13: import java.util.ArrayList;
14:
15: public class ExternalValue extends ArrayList<CharSequence> implements
16: CharSequence {
17: private static final long serialVersionUID = -7361025452353622788L;
18:
19: private int mSize = 0;
20:
21: public ExternalValue() {
22: super ();
23: }
24:
25: public int length() {
26: return toString().length();
27: }
28:
29: public char charAt(int index) {
30: return toString().charAt(index);
31: }
32:
33: public CharSequence subSequence(int start, int end) {
34: return toString().subSequence(start, end);
35: }
36:
37: public void append(CharSequence value) {
38: mSize += value.length();
39: add(value);
40: }
41:
42: public String toString() {
43: StringBuilder result = new StringBuilder(mSize);
44:
45: for (CharSequence charsequence : this ) {
46: // force JDK 1.4 compatibility by preventing that the append(CharSequence) is used
47: result.append((Object) charsequence);
48: }
49:
50: return result.toString();
51: }
52:
53: public void write(OutputStream out, String charsetName)
54: throws IOException {
55: if (null == charsetName) {
56: charsetName = StringUtils.ENCODING_UTF_8;
57: }
58:
59: for (CharSequence charsequence : this ) {
60: if (charsequence instanceof com.uwyn.rife.template.InternalString) {
61: out.write(((InternalString) charsequence)
62: .getBytes(charsetName));
63: } else if (charsequence instanceof java.lang.String) {
64: out
65: .write(((String) charsequence)
66: .getBytes(charsetName));
67: }
68: }
69: }
70: }
|