KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javolution > xml > CharacterData


1 /*
2  * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
3  * Copyright (C) 2005 - Javolution (http://javolution.org/)
4  * All rights reserved.
5  *
6  * Permission to use, copy, modify, and distribute this software is
7  * freely granted, provided that this notice is preserved.
8  */

9 package javolution.xml;
10
11 import javolution.lang.Text;
12 import javolution.realtime.RealtimeObject;
13 import javolution.util.FastComparator;
14 import j2me.io.Serializable;
15 import j2me.lang.CharSequence;
16
17 /**
18  * <p> This class represents a text that is not markup and
19  * constitutes the "Character Data" of a XML document.</p>
20  *
21  * <p> During deserialization, instances of this class are generated for
22  * character data containing at least one non-whitespace character.</p>
23  *
24  * <p> During serialization, instances of this class are written in a
25  * "CDATA" section (<code>&lt;![CDATA[...]]&gt;</code>).</p>
26  *
27  * @author <a HREF="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
28  * @version 3.3, May 13, 2005
29  */

30 public final class CharacterData extends RealtimeObject implements
31         Serializable, CharSequence {
32
33     /**
34      * Holds the object factory.
35      */

36     private static final Factory FACTORY = new Factory() {
37         protected Object create() {
38             return new CharacterData();
39         }
40         protected void cleanup(Object obj) {
41             CharacterData charData = (CharacterData) obj;
42             charData._csq = null;
43         }
44     };
45
46     /**
47      * Holds the character sequence being wrapped.
48      */

49     private CharSequence _csq;
50
51     /**
52      * Default constructor.
53      */

54     private CharacterData() {
55     }
56
57     /**
58      * Returns the character data wrapping the specified character sequence.
59      *
60      * @param csq the character sequence being wrapped.
61      * @return a new, preallocated or recycled instance.
62      */

63     public static CharacterData valueOf(CharSequence csq) {
64         CharacterData charData = (CharacterData) FACTORY.object();
65         charData._csq = csq;
66         return charData;
67     }
68
69     /**
70      * Returns the length of this character data.
71      *
72      * @return the number of characters.
73      */

74     public int length() {
75         return _csq.length();
76     }
77
78     /**
79      * Returns the character at the specified index.
80      *
81      * @param index the index of the character.
82      * @return the character at the specified index.
83      * @throws IndexOutOfBoundsException if index is negative, or index
84      * is equal or greater than <code>this.length()</code>.
85      */

86     public char charAt(int index) {
87         return _csq.charAt(index);
88     }
89
90     /**
91      * Returns a subsequence of this character data.
92      *
93      * @param start the index of the first character inclusive.
94      * @param end the index of the last character exclusive.
95      * @return a character data subsequence of this one.
96      * @throws IndexOutOfBoundsException if <code>(start < 0) || (end < 0) ||
97      * (start > end) || (end > this.length())</code>
98      */

99     public CharSequence subSequence(int start, int end) {
100         return CharacterData.valueOf(_csq.subSequence(start, end));
101     }
102
103     /**
104      * Compares this character data against the specified object for equality.
105      * Returns <code>true</code> if the specified object are both character
106      * data having the same character content.
107      *
108      * @param obj the object to compare with or <code>null</code>.
109      * @return <code>true</code> if that is a character data with the same
110      * character content as this one; <code>false</code> otherwise.
111      */

112     public final boolean equals(Object obj) {
113         if (this == obj)
114             return true;
115         if (!(obj instanceof CharacterData))
116             return false;
117         final CharacterData that = (CharacterData) obj;
118         return FastComparator.LEXICAL.areEqual(this._csq, that._csq);
119     }
120
121     /**
122      * Returns the hash code for this character data.
123      *
124      * @return the hash code value.
125      */

126     public final int hashCode() {
127         return FastComparator.LEXICAL.hashCodeOf(this._csq);
128     }
129
130     /**
131      * Copies the characters from this character data into the destination
132      * character array.
133      *
134      * @param start the index of the first character to copy.
135      * @param end the index after the last character to copy.
136      * @param dest the destination array.
137      * @param destPos the start offset in the destination array.
138      * @throws IndexOutOfBoundsException if <code>(start < 0) || (end < 0) ||
139      * (start > end) || (end > this.length())</code>
140      */

141     public void getChars(int start, int end, char dest[], int destPos) {
142         if ((end > _csq.length()) || (end < start))
143             throw new IndexOutOfBoundsException();
144         for (int i = start, j = destPos; i < end;) {
145             dest[j++] = _csq.charAt(i++);
146         }
147     }
148
149     // Overrides.
150
public Text toText() {
151         return Text.valueOf(_csq);
152     }
153
154 }
Popular Tags