KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javolution > xml > stream > EntitiesImpl


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.stream;
10
11 import j2me.lang.CharSequence;
12 import j2me.util.Collection;
13 import j2me.util.Iterator;
14 import j2me.util.Map;
15 import javolution.lang.Reusable;
16 import javolution.text.CharArray;
17 import javolution.util.FastCollection;
18 import javolution.util.FastCollection.Record;
19
20 /**
21  * This class holds defined entities while parsing.
22  *
23  * @author <a HREF="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
24  * @version 4.0, June 16, 2006
25  */

26 final class EntitiesImpl implements Reusable {
27
28     /**
29      * Holds maximum length.
30      */

31     private int _maxLength = 1;
32
33     /**
34      * Holds the user defined entities mapping.
35      */

36     private Map _entitiesMapping;
37
38     /**
39      * Default constructor.
40      */

41     EntitiesImpl() {
42     }
43
44     /**
45      * Returns the length of the largest entity.
46      *
47      * @return the length largest entity.
48      */

49     public int getMaxLength() {
50         return _maxLength;
51     }
52
53     /**
54      * Replaces the entity at the specified position.
55      * The five predefined XML entities "&#38;lt;", "&#38;gt;", "&#38;apos;",
56      * "&#38;quot;", "&#38;amp;" as well as character refererences
57      * (decimal or hexadecimal) are always recognized.
58      *
59      * @param buffer the data buffer.
60      * @param start the index of entity first character (index of '&')
61      * @return the length of the replacement entity (including ';')
62      * @throws XMLStreamException if the entity is not recognized.
63      */

64     public int replaceEntity(char[] buffer, int start, int length)
65             throws XMLStreamException {
66
67         // Checks for character references.
68
if (buffer[start + 1] == '#') {
69             char c = buffer[start + 2];
70             int base = (c == 'x') ? 16 : 10;
71             int i = (c == 'x') ? 3 : 2;
72             int charValue = 0;
73             for (; i < length; i++) {
74                 c = buffer[start + i];
75                 charValue *= base;
76                 charValue += (c <= '9') ? (c - '0') : (c <= 'Z') ? c - 'A'
77                         : c - 'a';
78             }
79             buffer[start] = (char) charValue;
80             return 1;
81         }
82
83         if ((buffer[start + 1] == 'l') && (buffer[start + 2] == 't')
84                 && (buffer[start + 3] == ';')) {
85             buffer[start] = '<';
86             return 1;
87         }
88
89         if ((buffer[start + 1] == 'g') && (buffer[start + 2] == 't')
90                 && (buffer[start + 3] == ';')) {
91             buffer[start] = '>';
92             return 1;
93         }
94
95         if ((buffer[start + 1] == 'a') && (buffer[start + 2] == 'p')
96                 && (buffer[start + 3] == 'o') && (buffer[start + 4] == 's')
97                 && (buffer[start + 5] == ';')) {
98             buffer[start] = '\'';
99             return 1;
100         }
101
102         if ((buffer[start + 1] == 'q') && (buffer[start + 2] == 'u')
103                 && (buffer[start + 3] == 'o') && (buffer[start + 4] == 't')
104                 && (buffer[start + 5] == ';')) {
105             buffer[start] = '"';
106             return 1;
107         }
108
109         if ((buffer[start + 1] == 'a') && (buffer[start + 2] == 'm')
110                 && (buffer[start + 3] == 'p') && (buffer[start + 4] == ';')) {
111             buffer[start] = '&';
112             return 1;
113         }
114
115         // Searches user defined entities.
116
_tmp.setArray(buffer, start + 1, length - 2);
117         CharSequence JavaDoc replacementText = (_entitiesMapping != null) ?
118                 (CharSequence JavaDoc) _entitiesMapping.get(_tmp) : null;
119         if (replacementText == null)
120             throw new XMLStreamException("Entity " + _tmp + " not recognized");
121         int replacementTextLength = replacementText.length();
122         for (int i = 0; i < replacementTextLength; i++) {
123             buffer[start + i] = replacementText.charAt(i);
124         }
125         return replacementTextLength;
126     }
127
128     private CharArray _tmp = new CharArray();
129
130     /**
131      * Defines a custom entity mapping.
132      *
133      * @param entityToReplacementText the entity (e.g. "copy") to replacement
134      * text (e.g. "©") mapping (both CharSequence).
135      */

136     public void setEntitiesMapping(Map entityToReplacementText) {
137         // Sets the maximum length for replacement text.
138
Collection values = entityToReplacementText.values();
139         if (values instanceof FastCollection) { // Avoids allocating iterators.
140
FastCollection fc = (FastCollection) values;
141              for (Record r=fc.head(), t=fc.tail(); (r = r.getNext())!= t;) {
142                  CharSequence JavaDoc value = (CharSequence JavaDoc) fc.valueOf(r);
143                  if (_maxLength < value.length()) {
144                      _maxLength = value.length();
145                  }
146              }
147         } else {
148             for (Iterator i=values.iterator(); i.hasNext();) {
149                 CharSequence JavaDoc value = (CharSequence JavaDoc) i.next();
150                 if (_maxLength < value.length()) {
151                     _maxLength = value.length();
152                 }
153             }
154         }
155         _entitiesMapping = entityToReplacementText;
156     }
157
158     /**
159      * Returns the custom entity mapping.
160      *
161      * @return the entity (e.g. "copy") to replacement text (e.g. "©") mapping
162      * (both CharSequence).
163      */

164     public Map getEntitiesMapping() {
165         return _entitiesMapping;
166     }
167     
168     // Implements Reusable.
169
public void reset() {
170         _maxLength = 1;
171         _entitiesMapping = null;
172     }
173
174 }
175
Popular Tags