1 17 18 package org.objectweb.jac.aspects.export; 19 20 import java.io.File ; 21 import java.io.IOException ; 22 import java.io.InputStream ; 23 import java.io.Reader ; 24 import java.util.HashSet ; 25 import java.util.Hashtable ; 26 import java.util.LinkedList ; 27 import java.util.List ; 28 import java.util.Map ; 29 import java.util.Set ; 30 import java.util.Vector ; 31 import org.apache.log4j.Logger; 32 import org.apache.xerces.parsers.SAXParser; 33 import org.objectweb.jac.aspects.integrity.IntegrityAC; 34 import org.objectweb.jac.aspects.persistence.ValueConverter; 35 import org.objectweb.jac.core.ACManager; 36 import org.objectweb.jac.core.NameRepository; 37 import org.objectweb.jac.core.Naming; 38 import org.objectweb.jac.core.rtti.ClassItem; 39 import org.objectweb.jac.core.rtti.ClassRepository; 40 import org.objectweb.jac.core.rtti.CollectionItem; 41 import org.objectweb.jac.core.rtti.FieldItem; 42 import org.objectweb.jac.util.Files; 43 import org.objectweb.jac.util.Strings; 44 import org.xml.sax.Attributes ; 45 import org.xml.sax.ContentHandler ; 46 import org.xml.sax.InputSource ; 47 import org.xml.sax.SAXException ; 48 import org.xml.sax.XMLReader ; 49 import org.xml.sax.helpers.DefaultHandler ; 50 51 54 public class Importer extends DefaultHandler implements ContentHandler { 55 static Logger logger = Logger.getLogger("import"); 56 57 NameRepository nr; 58 ClassRepository cr; 59 XMLReader xmlReader; 60 61 boolean firstPass = true; 62 63 public Importer() { 64 nr = (NameRepository)NameRepository.get(); 65 cr = ClassRepository.get(); 66 xmlReader = new SAXParser(); 67 xmlReader.setContentHandler(this); 68 } 69 70 Hashtable counters = new Hashtable (); 71 72 81 public void importObjects(File file) throws IOException , SAXException 82 { 83 importObjects(file,ExportAC.DEFAULT_ENCODING); 84 } 85 86 96 public void importObjects(File file, String encoding) throws IOException , SAXException { 97 try { 99 firstPass = true; 100 Reader reader = Files.autoDecompressReader(file, encoding); 101 IntegrityAC.disableRoleUpdates(); 102 try { 103 xmlReader.parse(new InputSource (reader)); 104 ACManager.getACM().updateNameCounters(counters); 105 } finally { 106 reader.close(); 107 } 108 109 firstPass = false; 111 reader = Files.autoDecompressReader(file, encoding); 112 try { 113 xmlReader.parse(new InputSource (reader)); 114 } finally { 115 reader.close(); 116 } 117 } finally { 118 IntegrityAC.enableRoleUpdates(); 119 newObjects.clear(); 120 } 121 } 122 123 public void startDocument() { 124 if (firstPass) 125 logger.info("FIRST PASS"); 126 else 127 logger.info("SECOND PASS"); 128 } 129 130 public void endDocument() { 131 if (firstPass) 132 logger.info("FIRST PASS DONE."); 133 else 134 logger.info("SECOND PASS DONE"); 135 } 136 137 Object current = null; 139 String currentName; 141 ClassItem cl = null; 143 FieldItem field = null; 144 CollectionItem collection = null; 145 boolean reference = false; 146 boolean key = false; 147 boolean value = false; 148 boolean nameCounter = false; 149 Object keyObject = null; 151 Object valueObject = null; 153 String name = null; 155 Long counter = null; 157 158 Set currentSet = new HashSet (); 159 Map currentMap = new Hashtable (); 160 List currentList = new Vector (); 161 162 List newObjects = new LinkedList (); 165 166 public void startElement(String uri, String localName, String qName, Attributes attributes) { 167 if (localName.equals("object")) { 168 String name = attributes.getValue("name"); 169 currentName = name; 170 cl = cr.getClass(attributes.getValue("class")); 171 logger.info("<object name=\""+name+"\" class=\""+cl.getName()+"\">"); 172 current = nr.getObject(name); 173 if (current==null) { 174 if (firstPass) { 175 logger.debug(" No such object "+name); 176 try { 177 Naming.setName(name); 178 current = cl.newInstance(); 179 logger.debug(" instanciated "+Strings.hex(current)); 180 if (!nr.getName(current).equals(name)) 181 logger.error("Instanciated object is named "+ 182 nr.getName(current)+" instead of "+name); 183 newObjects.add(current); 184 } catch (Exception e) { 185 logger.error("Instanciation of "+cl.getName()+" failed:"+e); 186 } 187 } else { 188 logger.error("Object "+name+" was not instanciated during first pass?"); 189 } 190 } 191 } else if (localName.equals("field")) { 192 if (!firstPass) { 193 String name = attributes.getValue("name"); 194 logger.debug(" <field name=\""+name+"\">"); 195 if (current==null || cl==null) { 196 logger.error("No current object or class for <field class=\""+name+"\"> element"); 197 } else { 198 field = cl.getField(name); 199 if (field instanceof CollectionItem) { 200 collection = (CollectionItem)field; 201 } 203 } 204 } 205 } else if (localName.equals("reference")) { 206 if (!firstPass) { 207 logger.debug(" <reference>"); 208 if (field!=null) { 209 reference = true; 210 buf.setLength(0); 211 } 212 } 213 } else if (localName.equals("primitive_value")) { 214 if (!firstPass) { 215 logger.debug(" <primitive_value>"); 216 if (field!=null) { 217 buf.setLength(0); 218 } 219 } 220 } else if (localName.equals("list")) { 221 if (!firstPass) { 222 if (collection!=null) { 223 currentList.clear(); 224 } else { 225 logger.error("No current collection field for <list>"); 226 } 227 } 228 } else if (localName.equals("set")) { 229 if (!firstPass) { 230 if (collection!=null) { 231 currentSet.clear(); 232 } else { 233 logger.error("No current collection field for <set>"); 234 } 235 } 236 } else if (localName.equals("map")) { 237 if (!firstPass) { 238 if (collection!=null) { 239 currentMap.clear(); 240 } else { 241 logger.error("No current collection field for <map>"); 242 } 243 } 244 } else if (localName.equals("entry")) { 245 } else if (localName.equals("value")) { 246 value = true; 247 } else if (localName.equals("key")) { 248 key = true; 249 } else if (localName.equals("nameCounter")) { 250 nameCounter = true; 251 } else if (localName.equals("name")) { 252 if (firstPass) { 253 logger.debug(" <name>"); 254 buf.setLength(0); 255 } 256 } else if (localName.equals("counter")) { 257 if (firstPass) { 258 logger.debug(" <counter>"); 259 buf.setLength(0); 260 } 261 } 262 } 263 264 public void endElement(String uri, String localName, String qName) { 265 try { 266 if (localName.equals("object")) { 267 current = null; 268 currentName = null; 269 cl = null; 270 } else if (localName.equals("field")) { 271 field = null; 272 collection = null; 273 } else if (localName.equals("reference")) { 274 if (!firstPass) { 275 String name = Strings.unslashify(buf.toString()); 276 Object ref = name.equals("null") ? null : nr.getObject(name); 277 logger.debug(" read reference "+name+" -> "+Strings.hex(ref)); 278 if (key) { 279 keyObject = ref; 280 } else if (value) { 281 valueObject = ref; 282 } else if (field!=null) { 283 if (collection!=null) { 284 if (collection.isSet()) 285 currentSet.add(ref); 286 else 287 currentList.add(ref); 288 } else { 289 if (field.getThroughAccessor(current)!=ref) { 290 logger.info(" Updating "+currentName+"."+field.getName()); 291 field.setThroughWriter(current,ref); 292 } 293 } 294 } 295 reference = false; 296 } 297 } else if (localName.equals("primitive_value")) { 298 if (!firstPass) { 299 Object val = 300 ValueConverter.stringToObject( 301 null,Strings.unslashify(buf.toString())); 302 if (key) { 303 keyObject = val; 304 } else if (value) { 305 valueObject = val; 306 } else if (field!=null) { 307 if (collection!=null) { 308 if (collection.isSet()) 309 currentSet.add(val); 310 else 311 currentList.add(val); 312 } else { 313 Object currentValue = field.getThroughAccessor(current); 314 if ((currentValue==null && val!=null) || 315 (currentValue!=null && !currentValue.equals(val))) { 316 logger.info(" Updating "+currentName+"."+field.getName()); 317 field.setThroughWriter(current,val); 318 } 319 } 320 } 321 } 322 } else if (localName.equals("list")) { 323 if (!firstPass) { 324 List list = (List )collection.getThroughAccessor(current); 325 if (!currentList.equals(list)) { 326 logger.info(" Updating list "+currentName+"."+field.getName()); 327 list.clear(); 328 list.addAll(currentList); 329 } else { 330 logger.debug(" List has not changed"); 331 } 332 currentList.clear(); 333 } 334 } else if (localName.equals("set")) { 335 if (!firstPass) { 336 Set set = (Set )collection.getThroughAccessor(current); 337 if (!currentSet.equals(set)) { 338 logger.info(" Updating set "+currentName+"."+field.getName()); 339 set.clear(); 340 set.addAll(currentSet); 341 } else { 342 logger.debug(" Set has not changed"); 343 } 344 currentSet.clear(); 345 } 346 } else if (localName.equals("map")) { 347 if (!firstPass) { 348 Map map = (Map )collection.getThroughAccessor(current); 349 if (!currentMap.equals(map)) { 350 logger.info(" Updating map "+currentName+"."+field.getName()); 351 map.clear(); 352 map.putAll(currentMap); 353 } else { 354 logger.debug(" Map has not changed"); 355 } 356 currentMap.clear(); 357 } 358 } else if (localName.equals("entry")) { 359 if (!firstPass) { 360 if (collection!=null) { 361 if (collection.isMap()) 362 currentMap.put(keyObject,valueObject); 363 else 364 logger.error("Field is not a Map: "+field); 365 } else { 366 logger.error("Field is not a collection: "+field); 367 } 368 } 369 } else if (localName.equals("value")) { 370 value = false; 371 } else if (localName.equals("key")) { 372 key = false; 373 } else if (localName.equals("nameCounter")) { 374 if (firstPass) { 375 counters.put(name,counter); 376 logger.info("Namecounter "+name+" -> "+counter); 377 nameCounter = false; 378 } 379 } else if (localName.equals("name")) { 380 if (firstPass) { 381 name = buf.toString(); 382 } 383 } else if (localName.equals("counter")) { 384 if (firstPass) { 385 counter = new Long (buf.toString()); 386 } 387 } 388 } catch (Exception e) { 389 logger.error("endElement("+uri+", "+localName+", "+qName+") failed"); 390 logger.error(" class="+cl); 391 logger.error(" field="+field); 392 logger.error(" current="+current); 393 logger.error(" buf="+buf,e); 394 } 395 } 396 397 StringBuffer buf = new StringBuffer (); 398 public void characters(char chars[], int start, int length) { 399 if (buf!=null) { 400 buf.append(chars,start,length); 401 } else { 402 } 404 } 405 406 public void ignorableWhitespace(char chars[], int start, int length) { 407 characters(chars,start,length); 408 } 409 410 protected Object readValue(String s) throws IOException { 411 int colon = s.indexOf(':'); 412 if (colon==-1) { 413 return nr.getObject(s); 414 } else { 415 return ValueConverter.stringToObject(null,s); 416 } 417 } 418 } 419 | Popular Tags |