1 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 26 final class EntitiesImpl implements Reusable { 27 28 31 private int _maxLength = 1; 32 33 36 private Map _entitiesMapping; 37 38 41 EntitiesImpl() { 42 } 43 44 49 public int getMaxLength() { 50 return _maxLength; 51 } 52 53 64 public int replaceEntity(char[] buffer, int start, int length) 65 throws XMLStreamException { 66 67 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 _tmp.setArray(buffer, start + 1, length - 2); 117 CharSequence replacementText = (_entitiesMapping != null) ? 118 (CharSequence ) _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 136 public void setEntitiesMapping(Map entityToReplacementText) { 137 Collection values = entityToReplacementText.values(); 139 if (values instanceof FastCollection) { FastCollection fc = (FastCollection) values; 141 for (Record r=fc.head(), t=fc.tail(); (r = r.getNext())!= t;) { 142 CharSequence value = (CharSequence ) 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 value = (CharSequence ) i.next(); 150 if (_maxLength < value.length()) { 151 _maxLength = value.length(); 152 } 153 } 154 } 155 _entitiesMapping = entityToReplacementText; 156 } 157 158 164 public Map getEntitiesMapping() { 165 return _entitiesMapping; 166 } 167 168 public void reset() { 170 _maxLength = 1; 171 _entitiesMapping = null; 172 } 173 174 } 175 | Popular Tags |