1 9 package javolution.xml; 10 11 import j2me.lang.CharSequence; 12 import javolution.Javolution; 13 import javolution.lang.Reusable; 14 import javolution.text.CharArray; 15 import javolution.text.TextBuilder; 16 import javolution.util.FastComparator; 17 import javolution.util.FastMap; 18 import javolution.util.FastTable; 19 import javolution.util.Index; 20 import javolution.xml.stream.XMLStreamException; 21 22 33 public class XMLReferenceResolver implements Reusable { 34 35 38 private FastMap _objectToId = new FastMap() 39 .setKeyComparator(FastComparator.IDENTITY); 40 41 44 private FastTable _idToObject = new FastTable(); 45 46 49 private int _counter; 50 51 54 private String _idName = "id"; 55 56 59 private String _idURI = null; 60 61 64 private String _refName = "ref"; 65 66 69 private String _refURI = null; 70 71 74 public XMLReferenceResolver() { 75 } 76 77 84 public void setIdentifierAttribute(String name) { 85 setIdentifierAttribute(name, null); 86 } 87 88 95 public void setIdentifierAttribute(String localName, String uri) { 96 _idName = localName; 97 _idURI = uri; 98 } 99 100 107 public void setReferenceAttribute(String name) { 108 setReferenceAttribute(name, null); 109 } 110 111 118 public void setReferenceAttribute(String localName, String uri) { 119 _refName = localName; 120 _refURI = uri; 121 } 122 123 134 public boolean writeReference(Object obj, XMLFormat.OutputElement xml) 135 throws XMLStreamException { 136 Index id = (Index) _objectToId.get(obj); 137 if (id == null) { id = Index.valueOf(_counter++); 139 _objectToId.put(obj, id); 140 _tmp.clear().append(id.intValue()); 141 if (_idURI == null) { 142 xml.getStreamWriter().writeAttribute(toCsq(_idName), 143 _tmp); 144 } else { 145 xml.getStreamWriter().writeAttribute(toCsq(_idURI), 146 toCsq(_idName), _tmp); 147 } 148 return false; 149 } 150 _tmp.clear().append(id.intValue()); 151 if (_refURI == null) { 152 xml._writer 153 .writeAttribute(toCsq(_refName), _tmp); 154 } else { 155 xml._writer.writeAttribute(toCsq(_refURI), 156 toCsq(_refName), _tmp); 157 } 158 return true; 159 } 160 161 private TextBuilder _tmp = new TextBuilder(); 162 163 172 public Object readReference(XMLFormat.InputElement xml) 173 throws XMLStreamException { 174 CharArray value = xml._reader.getAttributeValue( 175 toCsq(_refURI), toCsq(_refName)); 176 if (value == null) 177 return null; 178 int ref = value.toInt(); 179 if (ref >= _idToObject.size()) 180 throw new XMLStreamException("Reference: " + value + " not found"); 181 return _idToObject.get(ref); 182 } 183 184 193 public void createReference(Object obj, XMLFormat.InputElement xml) 194 throws XMLStreamException { 195 CharArray value = xml._reader.getAttributeValue( 196 toCsq(_idURI), toCsq(_idName)); 197 if (value == null) 198 return; 199 int i = value.toInt(); 200 if (_idToObject.size() != i) 201 throw new XMLStreamException("Identifier discontinuity detected " 202 + "(expected " + _idToObject.size() + " found " + i + ")"); 203 _idToObject.add(obj); 204 } 205 206 public void reset() { 208 _idName = "id"; 209 _idURI = null; 210 _refName = "ref"; 211 _refURI = null; 212 _idToObject.clear(); 213 _objectToId.clear(); 214 _counter = 0; 215 } 216 217 private static CharSequence toCsq(Object str) { 218 return Javolution.j2meToCharSeq(str); 219 } 220 221 } 222 | Popular Tags |