예제 #1
0
 // obj: "object with properties" to write
 private void writeObject(Object obj)
     throws NoSuchMethodException, InstantiationException, InvocationTargetException,
         IllegalAccessException, SecurityException {
   // begin bean encoding:
   this.driver.startElement(DTD.ELEMENT_OBJECT);
   Class cls = obj.getClass();
   final String mappedName = this.context.aliasFor(cls);
   this.driver.setAttribute(
       DTD.ATTRIBUTE_CLASS, (mappedName != null ? mappedName : cls.getName()));
   // encode properties:
   while (cls != Object.class) { // process inheritance:
     for (Field f : cls.getDeclaredFields()) { // process composition:
       if (Modifier.isStatic(f.getModifiers())
           || !ReflectionUtil.hasClassFieldProperty(cls, f)
           || this.context.excluded(f)) {
         continue; // skip static or non-property or excluded field.
       }
       // get property field:
       if (!f.isAccessible()) {
         f.setAccessible(true);
       }
       // write property value:
       final String aliasedFieldName = this.context.aliasFor(f, f.getName());
       this.driver.startElement(aliasedFieldName);
       this.write0(f.get(obj));
       this.driver.endElement();
     }
     cls = cls.getSuperclass();
   }
   // end bean encoding:
   this.driver.endElement();
 }
예제 #2
0
 private void write0(Object data)
     throws NoSuchMethodException, InvocationTargetException, InstantiationException,
         IllegalAccessException, SecurityException {
   // simple strategy:
   // nil:
   if (data == null) {
     this.driver.startElement(DTD.ELEMENT_NIL);
     this.driver.endElement();
     return;
   }
   // non-nil:
   final Class cls = data.getClass();
   SimpleStrategy ss = this.simpleStrategies.lookup(cls);
   if (ss != null) {
     this.driver.startElement(ss.name());
     this.driver.writeValue(ss.marshal(data, this.context));
     this.driver.endElement();
     return;
   }
   // composite strategy:
   // check if data was already visited in the object graph:
   final Integer idRef = this.encoded.get(data);
   if (idRef != null) {
     // write object idref for already-visited data:
     this.driver.startElement(DTD.ELEMENT_OBJECT);
     this.driver.setAttribute(DTD.ATTRIBUTE_IDREF, idRef.toString());
     this.driver.endElement();
   } else {
     // mark data as visited:
     final int nextUniqueId = this.encoded.size() + 1;
     this.encoded.put(data, nextUniqueId);
     this.driver.setOneTimeUniqueIdTo(String.valueOf(nextUniqueId));
     // visit data:
     final CompositeStrategy cs = this.compositeStrategies.lookup(cls);
     if (cs != null) {
       cs.marshal(data, this.driver, this.context);
     } else if (cls.isArray()) {
       this.writeArray(data);
     } else {
       this.writeObject(data);
     }
   }
 }