Search in sources :

Example 1 with Dump

use of org.bedework.calfacade.annotations.Dump in project bw-calendar-engine by Bedework.

the class DumpEntity method dump.

/**
 * Dump the entire entity into the given file.
 *
 * @param f - the file
 * @throws CalFacadeException
 */
@NoWrap
public void dump(final File f) throws CalFacadeException {
    Dump dCl = getClass().getAnnotation(Dump.class);
    if (dCl.format() == DumpFormat.xml) {
        Writer wtr = null;
        try {
            XmlEmit xml = new XmlEmit();
            wtr = new FileWriter(f);
            xml.startEmit(wtr);
            dump(xml, DumpType.def, false);
            xml.flush();
            return;
        } catch (CalFacadeException cfe) {
            throw cfe;
        } catch (Throwable t) {
            throw new CalFacadeException(t);
        } finally {
            if (wtr != null) {
                try {
                    wtr.close();
                } catch (Throwable t) {
                    throw new CalFacadeException(t);
                }
            }
        }
    }
    if (dCl.format() == DumpFormat.vCard) {
        Writer wtr = null;
        try {
            VCard vc = new VCard();
            dump(vc, DumpType.def);
            String vcStr = vc.toString();
            wtr = new FileWriter(f);
            wtr.append(vcStr);
            return;
        } catch (CalFacadeException cfe) {
            throw cfe;
        } catch (Throwable t) {
            throw new CalFacadeException(t);
        } finally {
            if (wtr != null) {
                try {
                    wtr.close();
                } catch (Throwable t) {
                    throw new CalFacadeException(t);
                }
            }
        }
    }
    throw new CalFacadeException("Unsupported dump format " + dCl.format());
}
Also used : NoDump(org.bedework.calfacade.annotations.NoDump) Dump(org.bedework.calfacade.annotations.Dump) FileWriter(java.io.FileWriter) XmlEmit(org.bedework.util.xml.XmlEmit) VCard(net.fortuna.ical4j.vcard.VCard) FileWriter(java.io.FileWriter) Writer(java.io.Writer) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException) NoWrap(org.bedework.calfacade.annotations.NoWrap)

Example 2 with Dump

use of org.bedework.calfacade.annotations.Dump in project bw-calendar-engine by Bedework.

the class DumpEntity method dump.

/* ====================================================================
   *                   Private Vcard methods
   * ==================================================================== */
/**
 * Dump this entity as a vcard.
 *
 * @param vc
 * @param dtype
 * @throws CalFacadeException
 */
@NoWrap
private void dump(final VCard vc, final DumpType dtype) throws CalFacadeException {
    if (!hasDumpValue()) {
        return;
    }
    NoDump ndCl = getClass().getAnnotation(NoDump.class);
    Dump dCl = getClass().getAnnotation(Dump.class);
    /* If dumpKeyFields is true we are dumping a field which is referred to by
     * a key field - for example, a principal is addressed by the principal
     * href.
     */
    boolean dumpKeyFields = dtype == DumpType.reference;
    ArrayList<String> noDumpMethods = null;
    ArrayList<String> firstMethods = null;
    try {
        if (ndCl != null) {
            if (ndCl.value().length == 0) {
                return;
            }
            noDumpMethods = new ArrayList<String>();
            for (String m : ndCl.value()) {
                noDumpMethods.add(m);
            }
        }
        if (!dumpKeyFields && (dCl != null) && (dCl.firstFields().length != 0)) {
            firstMethods = new ArrayList<String>();
            for (String f : dCl.firstFields()) {
                firstMethods.add(methodName(f));
            }
        }
        QName qn = null;
    /*
      if (dtype != DumpType.compound) {
        qn = startElement(xml, getClass(), dCl);
      }

      Collection<ComparableMethod> ms = findGetters(dCl, dtype);

      if (firstMethods != null) {
        doFirstMethods:
        for (String methodName: firstMethods) {
          for (ComparableMethod cm: ms) {
            Method m = cm.m;

            if (methodName.equals(m.getName())) {
              Dump d = m.getAnnotation(Dump.class);

              dumpValue(xml, m, d, m.invoke(this, (Object[])null), fromCollection);

              continue doFirstMethods;
            }
          }

          error("Listed first field has no corresponding getter: " + methodName);
        }
      }

      for (ComparableMethod cm: ms) {
        Method m = cm.m;

        if ((noDumpMethods != null) &&
            noDumpMethods.contains(fieldName(m.getName()))) {
          continue;
        }

        if ((firstMethods != null) &&
            firstMethods.contains(m.getName())) {
          continue;
        }

        Dump d = m.getAnnotation(Dump.class);

        dumpValue(xml, m, d, m.invoke(this, (Object[])null), fromCollection);
      }

      if (qn != null) {
        closeElement(xml, qn);
      }
      */
    // } catch (CalFacadeException cfe) {
    // throw cfe;
    } catch (Throwable t) {
        throw new CalFacadeException(t);
    }
}
Also used : NoDump(org.bedework.calfacade.annotations.NoDump) NoDump(org.bedework.calfacade.annotations.NoDump) Dump(org.bedework.calfacade.annotations.Dump) QName(javax.xml.namespace.QName) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException) NoWrap(org.bedework.calfacade.annotations.NoWrap)

Example 3 with Dump

use of org.bedework.calfacade.annotations.Dump in project bw-calendar-engine by Bedework.

the class DumpEntity method dump.

/* ====================================================================
   *                   Private XML methods
   * ==================================================================== */
/**
 * Dump this entity as xml.
 *
 * @param xml
 * @param dtype
 * @param fromCollection  true if the value is a member of a collection
 * @throws CalFacadeException
 */
@NoWrap
private void dump(final XmlEmit xml, final DumpType dtype, final boolean fromCollection) throws CalFacadeException {
    if (!hasDumpValue()) {
        return;
    }
    NoDump ndCl = getClass().getAnnotation(NoDump.class);
    Dump dCl = getClass().getAnnotation(Dump.class);
    boolean dumpKeyFields = dtype == DumpType.reference;
    ArrayList<String> noDumpMethods = null;
    ArrayList<String> firstMethods = null;
    try {
        if (ndCl != null) {
            if (ndCl.value().length == 0) {
                return;
            }
            noDumpMethods = new ArrayList<String>();
            for (String m : ndCl.value()) {
                noDumpMethods.add(m);
            }
        }
        if (!dumpKeyFields && (dCl != null) && (dCl.firstFields().length != 0)) {
            firstMethods = new ArrayList<String>();
            for (String f : dCl.firstFields()) {
                firstMethods.add(methodName(f));
            }
        }
        QName qn = null;
        if (fromCollection || (dtype != DumpType.compound)) {
            qn = startElement(xml, getClass(), dCl);
        }
        Collection<ComparableMethod> ms = findGetters(dCl, dtype);
        if (firstMethods != null) {
            doFirstMethods: for (String methodName : firstMethods) {
                for (ComparableMethod cm : ms) {
                    Method m = cm.m;
                    if (methodName.equals(m.getName())) {
                        Dump d = m.getAnnotation(Dump.class);
                        dumpValue(xml, m, d, m.invoke(this, (Object[]) null), fromCollection);
                        continue doFirstMethods;
                    }
                }
                error("Listed first field has no corresponding getter: " + methodName);
            }
        }
        for (ComparableMethod cm : ms) {
            Method m = cm.m;
            if ((noDumpMethods != null) && noDumpMethods.contains(fieldName(m.getName()))) {
                continue;
            }
            if ((firstMethods != null) && firstMethods.contains(m.getName())) {
                continue;
            }
            Dump d = m.getAnnotation(Dump.class);
            dumpValue(xml, m, d, m.invoke(this, (Object[]) null), fromCollection);
        }
        if (qn != null) {
            closeElement(xml, qn);
        }
    } catch (CalFacadeException cfe) {
        throw cfe;
    } catch (Throwable t) {
        throw new CalFacadeException(t);
    }
}
Also used : NoDump(org.bedework.calfacade.annotations.NoDump) NoDump(org.bedework.calfacade.annotations.NoDump) Dump(org.bedework.calfacade.annotations.Dump) QName(javax.xml.namespace.QName) Method(java.lang.reflect.Method) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException) NoWrap(org.bedework.calfacade.annotations.NoWrap)

Aggregations

Dump (org.bedework.calfacade.annotations.Dump)3 NoDump (org.bedework.calfacade.annotations.NoDump)3 NoWrap (org.bedework.calfacade.annotations.NoWrap)3 CalFacadeException (org.bedework.calfacade.exc.CalFacadeException)3 QName (javax.xml.namespace.QName)2 FileWriter (java.io.FileWriter)1 Writer (java.io.Writer)1 Method (java.lang.reflect.Method)1 VCard (net.fortuna.ical4j.vcard.VCard)1 XmlEmit (org.bedework.util.xml.XmlEmit)1