/**
  * Clones an object only if it hasn't been already cloned. Returns the clone.
  *
  * @param sourceObj
  * @return The clone of the source object.
  * @throws Exception
  */
 public InDesignComponent cloneIfNew(InDesignObject sourceObj, InDesignComponent targetParent)
     throws Exception {
   Map<String, InDesignObject> cloneMap = getCloneMapForDoc(sourceObj.getDocument());
   if (cloneMap.containsKey(sourceObj.getId())) return cloneMap.get(sourceObj.getId());
   InDesignComponent clone = this.clone(sourceObj);
   if (targetParent != null) targetParent.addChild(clone);
   return clone;
 }
 /**
  * Register a new object with the document so it can maintain a master index of objects by ID.
  *
  * @param object
  */
 public void registerObject(InDesignObject object) {
   String id = object.getId();
   if (id == null || this.objectsById.containsKey(id)) {
     id = assignObjectId();
     object.setId(id);
   }
   this.objectsById.put(object.getId(), object);
   object.setDocument(this);
 }
 /**
  * Unconditionally clone an InDesign Object.
  *
  * @param sourceObj
  * @return The clone of the object.
  * @throws Exception
  */
 public InDesignComponent clone(InDesignObject sourceObj) throws Exception {
   logger.debug(
       "Cloning object "
           + sourceObj.getClass().getSimpleName()
           + " ["
           + sourceObj.getId()
           + "]...");
   InDesignObject clone = sourceObj.getClass().newInstance();
   assignIdAndRegister(clone);
   // Now remember that we've cloned this source object so we can not
   // clone it again if we don't want to:
   Map<String, InDesignObject> cloneMap = getCloneMapForDoc(sourceObj.getDocument());
   cloneMap.put(sourceObj.getId(), clone);
   clone.loadObject(sourceObj, clone.getId());
   return clone;
 }
 /** @return */
 public String reportChildObjects() {
   StringBuilder report = new StringBuilder("Child Objects::\n");
   for (InDesignComponent comp : this.getChildren()) {
     String dsName = comp.getInxTagName();
     if (comp instanceof InDesignObject) {
       InDesignObject obj = (InDesignObject) comp;
       report.append("[").append(obj.getId()).append("] ");
     }
     report
         .append(comp.getClass().getSimpleName())
         .append(", <")
         .append(dsName)
         .append(">")
         .append("\n");
   }
   return report.toString();
 }
 /**
  * Constructs a new component, which may be an InDesignComponent or an InDesignObject.
  *
  * @param child
  * @return
  * @throws Exception
  */
 public InDesignComponent newInDesignComponent(Element child) throws Exception {
   logger.debug(
       "newInDesignObject(): Creating new object or component from <"
           + child.getNodeName()
           + ">...");
   if (child.hasAttribute(PROP_SELF)) {
     Class<? extends InDesignObject> objectClass = tagToObjectClassMap.get(child.getNodeName());
     if (objectClass == null) objectClass = DefaultInDesignObject.class;
     InDesignObject newObject = (InDesignObject) newObject(objectClass, child);
     logger.debug("newInDesignObject():   New object has ID [" + newObject.getId() + "]");
     return newObject;
   } else {
     Class<? extends InDesignComponent> objectClass =
         tagToComponentClassMap.get(child.getNodeName());
     if (objectClass == null) objectClass = DefaultInDesignComponent.class;
     return newComponent(objectClass, child);
   }
 }
  /**
   * @param class1
   * @param dataSource
   * @return
   * @throws Exception
   * @throws InstantiationException
   */
  private InDesignComponent newObject(Class<? extends InDesignObject> clazz, Element dataSource)
      throws Exception {
    logger.debug("newObject(): Creating new " + clazz.getSimpleName() + "...");
    InDesignObject obj = (InDesignObject) clazz.newInstance();
    obj.setDocument(this);
    if (dataSource != null) {
      logger.debug("newObject():   From element " + dataSource.getNodeName() + "...");
      obj.loadObject(dataSource);
      String selfValue = dataSource.getAttribute(PROP_SELF);
      if (selfValue != null && !"".equals(selfValue.trim())) {
        String id = InxHelper.decodeRawValueToSingleString(selfValue);
        obj.setId(id);
        this.registerObject(obj);
      } else {
        assignIdAndRegister(obj);
      }

    } else {
      assignIdAndRegister(obj);
    }
    logger.debug("newObject(): New object has ID [" + obj.getId() + "]");
    return obj;
  }