[Bedework-commit] calendarapi r690 - in trunk: annotations/src/org/bedework/calfacade/annotations calFacade/src/org/bedework/calfacade calFacade/src/org/bedework/calfacade/base

svnadmin at bedework.org svnadmin at bedework.org
Tue Aug 26 15:44:15 EDT 2008


Author: douglm
Date: 2008-08-26 15:44:13 -0400 (Tue, 26 Aug 2008)
New Revision: 690

Added:
   trunk/annotations/src/org/bedework/calfacade/annotations/Dump.java
   trunk/annotations/src/org/bedework/calfacade/annotations/NoDump.java
   trunk/calFacade/src/org/bedework/calfacade/base/DumpEntity.java
Modified:
   trunk/calFacade/src/org/bedework/calfacade/BwSystem.java
   trunk/calFacade/src/org/bedework/calfacade/base/BwDbentity.java
Log:
Start of changes to use annotations to control the dump process.

Started with an easy one and annotated the BwSystem class.

Added: trunk/annotations/src/org/bedework/calfacade/annotations/Dump.java
===================================================================
--- trunk/annotations/src/org/bedework/calfacade/annotations/Dump.java	                        (rev 0)
+++ trunk/annotations/src/org/bedework/calfacade/annotations/Dump.java	2008-08-26 19:44:13 UTC (rev 690)
@@ -0,0 +1,45 @@
+/* **********************************************************************
+    Copyright 2008 Rensselaer Polytechnic Institute. All worldwide rights reserved.
+
+    Redistribution and use of this distribution in source and binary forms,
+    with or without modification, are permitted provided that:
+       The above copyright notice and this permission notice appear in all
+        copies and supporting documentation;
+
+        The name, identifiers, and trademarks of Rensselaer Polytechnic
+        Institute are not used in advertising or publicity without the
+        express prior written permission of Rensselaer Polytechnic Institute;
+
+    DISCLAIMER: The software is distributed" AS IS" without any express or
+    implied warranty, including but not limited to, any implied warranties
+    of merchantability or fitness for a particular purpose or any warrant)'
+    of non-infringement of any current or pending patent rights. The authors
+    of the software make no representations about the suitability of this
+    software for any particular purpose. The entire risk as to the quality
+    and performance of the software is with the user. Should the software
+    prove defective, the user assumes the cost of all necessary servicing,
+    repair or correction. In particular, neither Rensselaer Polytechnic
+    Institute, nor the authors of the software are liable for any indirect,
+    special, consequential, or incidental damages related to the software,
+    to the maximum extent the law permits.
+*/
+
+package org.bedework.calfacade.annotations;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/** This is used to annotate a method for which we should generate dump output.
+ *
+ * @author Mike Douglass
+ *
+ */
+ at Retention(RetentionPolicy.RUNTIME)
+ at Target({ElementType.METHOD, ElementType.TYPE})
+public @interface Dump {
+  /** Name of element to use in xml dump
+   */
+  String elementName() default "";
+}

Added: trunk/annotations/src/org/bedework/calfacade/annotations/NoDump.java
===================================================================
--- trunk/annotations/src/org/bedework/calfacade/annotations/NoDump.java	                        (rev 0)
+++ trunk/annotations/src/org/bedework/calfacade/annotations/NoDump.java	2008-08-26 19:44:13 UTC (rev 690)
@@ -0,0 +1,50 @@
+/* **********************************************************************
+    Copyright 2008 Rensselaer Polytechnic Institute. All worldwide rights reserved.
+
+    Redistribution and use of this distribution in source and binary forms,
+    with or without modification, are permitted provided that:
+       The above copyright notice and this permission notice appear in all
+        copies and supporting documentation;
+
+        The name, identifiers, and trademarks of Rensselaer Polytechnic
+        Institute are not used in advertising or publicity without the
+        express prior written permission of Rensselaer Polytechnic Institute;
+
+    DISCLAIMER: The software is distributed" AS IS" without any express or
+    implied warranty, including but not limited to, any implied warranties
+    of merchantability or fitness for a particular purpose or any warrant)'
+    of non-infringement of any current or pending patent rights. The authors
+    of the software make no representations about the suitability of this
+    software for any particular purpose. The entire risk as to the quality
+    and performance of the software is with the user. Should the software
+    prove defective, the user assumes the cost of all necessary servicing,
+    repair or correction. In particular, neither Rensselaer Polytechnic
+    Institute, nor the authors of the software are liable for any indirect,
+    special, consequential, or incidental damages related to the software,
+    to the maximum extent the law permits.
+*/
+
+package org.bedework.calfacade.annotations;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/** This is used to annotate a method which we should not dump.
+ *
+ * @author Mike Douglass
+ *
+ */
+ at Retention(RetentionPolicy.RUNTIME)
+ at Target({ElementType.METHOD, ElementType.TYPE})
+public @interface NoDump {
+  /** This is valid only for a class. If specified without any values means
+   * do not dump the class. If specified with a list means skip those methods.
+   *
+   * <p>Allows skipping of inherited methods.
+   *
+   * @return String names of methods to skip
+   */
+  String[] value() default {};
+}

Modified: trunk/calFacade/src/org/bedework/calfacade/BwSystem.java
===================================================================
--- trunk/calFacade/src/org/bedework/calfacade/BwSystem.java	2008-08-25 19:48:36 UTC (rev 689)
+++ trunk/calFacade/src/org/bedework/calfacade/BwSystem.java	2008-08-26 19:44:13 UTC (rev 690)
@@ -54,6 +54,8 @@
 
 package org.bedework.calfacade;
 
+import org.bedework.calfacade.annotations.Dump;
+import org.bedework.calfacade.annotations.NoDump;
 import org.bedework.calfacade.base.BwDbentity;
 
 import java.util.Comparator;
@@ -64,6 +66,8 @@
  *
  * @author Mike Douglass       douglm at rpi.edu
  */
+ at Dump(elementName="system")
+ at NoDump({"byteSize"})
 public class BwSystem extends BwDbentity implements Comparator {
   /* A name for the system */
   private String name;

Modified: trunk/calFacade/src/org/bedework/calfacade/base/BwDbentity.java
===================================================================
--- trunk/calFacade/src/org/bedework/calfacade/base/BwDbentity.java	2008-08-25 19:48:36 UTC (rev 689)
+++ trunk/calFacade/src/org/bedework/calfacade/base/BwDbentity.java	2008-08-26 19:44:13 UTC (rev 690)
@@ -26,6 +26,7 @@
 package org.bedework.calfacade.base;
 
 import org.bedework.calfacade.CalFacadeDefs;
+import org.bedework.calfacade.annotations.NoDump;
 import org.bedework.calfacade.annotations.NoWrap;
 
 import java.io.Serializable;
@@ -40,7 +41,7 @@
  *
  * @param <T>
  */
-public class BwDbentity<T> implements Comparable<T>, Serializable {
+public class BwDbentity<T> extends DumpEntity<T> implements Comparable<T>, Serializable {
   private int id = CalFacadeDefs.unsavedItemKey;
 
   /* Hibernate does not implicitly delete db entities during update or
@@ -143,6 +144,7 @@
   /**
    * @return deleted entities or null
    */
+  @NoDump
   public Collection<BwDbentity<?>> getDeletedEntities() {
     return deletedEntities;
   }

Added: trunk/calFacade/src/org/bedework/calfacade/base/DumpEntity.java
===================================================================
--- trunk/calFacade/src/org/bedework/calfacade/base/DumpEntity.java	                        (rev 0)
+++ trunk/calFacade/src/org/bedework/calfacade/base/DumpEntity.java	2008-08-26 19:44:13 UTC (rev 690)
@@ -0,0 +1,218 @@
+/* **********************************************************************
+    Copyright 2008 Rensselaer Polytechnic Institute. All worldwide rights reserved.
+
+    Redistribution and use of this distribution in source and binary forms,
+    with or without modification, are permitted provided that:
+       The above copyright notice and this permission notice appear in all
+        copies and supporting documentation;
+
+        The name, identifiers, and trademarks of Rensselaer Polytechnic
+        Institute are not used in advertising or publicity without the
+        express prior written permission of Rensselaer Polytechnic Institute;
+
+    DISCLAIMER: The software is distributed" AS IS" without any express or
+    implied warranty, including but not limited to, any implied warranties
+    of merchantability or fitness for a particular purpose or any warrant)'
+    of non-infringement of any current or pending patent rights. The authors
+    of the software make no representations about the suitability of this
+    software for any particular purpose. The entire risk as to the quality
+    and performance of the software is with the user. Should the software
+    prove defective, the user assumes the cost of all necessary servicing,
+    repair or correction. In particular, neither Rensselaer Polytechnic
+    Institute, nor the authors of the software are liable for any indirect,
+    special, consequential, or incidental damages related to the software,
+    to the maximum extent the law permits.
+*/
+package org.bedework.calfacade.base;
+
+import org.bedework.calfacade.annotations.Dump;
+import org.bedework.calfacade.annotations.NoDump;
+import org.bedework.calfacade.exc.CalFacadeException;
+
+import edu.rpi.sss.util.xml.XmlEmit;
+
+import javax.xml.namespace.QName;
+
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.Collection;
+
+/** An entity which can be dumped..
+ *
+ * @author Mike Douglass
+ * @version 1.0
+ *
+ * @param <T>
+ */
+public class DumpEntity<T> {
+  /** Dump this entity as xml.
+   *
+   * @param xml
+   * @throws CalFacadeException
+   */
+  public void dump(XmlEmit xml) throws CalFacadeException {
+    NoDump nd = (NoDump)getClass().getAnnotation(NoDump.class);
+
+    ArrayList<String> noDumpMethods = null;
+
+    if (nd != null) {
+      if (nd.value().length == 0) {
+        return;
+      }
+
+      noDumpMethods = new ArrayList<String>();
+      for (String m: nd.value()) {
+        noDumpMethods.add(m);
+      }
+    }
+
+    QName qn = startElement(xml, getClass(),
+                            (Dump)getClass().getAnnotation(Dump.class));
+
+    Collection<Method> ms = findGetters();
+
+    for (Method m: ms) {
+      if (noDumpMethods.contains(fieldName(m.getName()))) {
+        continue;
+      }
+
+      try {
+        Object methVal = m.invoke(this, (Object[])null);
+
+        if (methVal instanceof DumpEntity) {
+          QName mqn = startElement(xml, m,
+                                   (Dump)m.getAnnotation(Dump.class));
+
+          ((DumpEntity)methVal).dump(xml);
+          closeElement(xml, mqn);
+        } else {
+          property(xml, m, (Dump)m.getAnnotation(Dump.class), methVal);
+        }
+      } catch (Throwable t) {
+        throw new CalFacadeException(t);
+      }
+
+    }
+
+    closeElement(xml, qn);
+  }
+
+  /* ====================================================================
+   *                   Private methods
+   * ==================================================================== */
+
+  private QName startElement(XmlEmit xml, Class c, Dump d) throws CalFacadeException {
+    try {
+      QName qn;
+
+      if (d == null) {
+        qn = new QName(c.getName());
+      } else {
+        qn = new QName(d.elementName());
+      }
+
+      xml.openTag(qn);
+      return qn;
+    } catch (Throwable t) {
+      throw new CalFacadeException(t);
+    }
+  }
+
+  private QName startElement(XmlEmit xml, Method m, Dump d) throws CalFacadeException {
+    try {
+      QName qn;
+
+      if (d == null) {
+        String methodName = m.getName();
+
+        qn = new QName(fieldName(methodName));
+      } else {
+        qn = new QName(d.elementName());
+      }
+
+      xml.openTag(qn);
+      return qn;
+    } catch (Throwable t) {
+      throw new CalFacadeException(t);
+    }
+  }
+
+  private void property(XmlEmit xml, Method m,
+                        Dump d, Object p) throws CalFacadeException {
+    if (p == null) {
+      return;
+    }
+
+    try {
+      QName qn;
+
+      if (d == null) {
+        String methodName = m.getName();
+
+        qn = new QName(fieldName(methodName));
+      } else {
+        qn = new QName(d.elementName());
+      }
+
+      xml.property(qn, String.valueOf(p));
+    } catch (Throwable t) {
+      throw new CalFacadeException(t);
+    }
+  }
+
+  private void closeElement(XmlEmit xml, QName qn) throws CalFacadeException {
+    try {
+      xml.closeTag(qn);
+    } catch (Throwable t) {
+      throw new CalFacadeException(t);
+    }
+  }
+
+  private Collection<Method> findGetters() throws CalFacadeException {
+    Method[] meths = getClass().getMethods();
+    Collection<Method> getters = new ArrayList<Method>();
+
+    for (int i = 0; i < meths.length; i++) {
+      Method m = meths[i];
+
+      String mname = m.getName();
+
+      if (mname.length() < 4) {
+        continue;
+      }
+
+      /* Name must start with get */
+      if (!mname.startsWith("get")) {
+        continue;
+      }
+
+      /* Don't want getClass */
+      if (mname.equals("getClass")) {
+        continue;
+      }
+
+      /* No parameters */
+      Class[] parClasses = m.getParameterTypes();
+      if (parClasses.length != 0) {
+        continue;
+      }
+
+      /* Not annotated with NoDump */
+      if (m.getAnnotation(NoDump.class) != null) {
+        continue;
+      }
+
+      getters.add(m);
+    }
+
+    return getters;
+  }
+
+  private String fieldName(String val) {
+    if (val.length() < 4) {
+      return null;
+    }
+
+    return val.substring(3, 4).toLowerCase() + val.substring(4);
+  }
+}



More information about the Bedework-commit mailing list