private void processColumn(
      Element reltable,
      int column,
      LoadedDocuments loadedDocs,
      List<Entry> entryList,
      List<Entry[]> cellList) {
    Node child = reltable.getFirstChild();
    while (child != null) {
      if (child.getNodeType() == Node.ELEMENT_NODE) {
        Element childElement = (Element) child;

        if (DITAUtil.hasClass(childElement, "map/relrow")) {
          Element relcell = DOMUtil.getNthChildElement(childElement, column);
          if (relcell != null && DITAUtil.hasClass(relcell, "map/relcell")) {
            entryList.clear();
            processCell(relcell, loadedDocs, entryList);

            Entry[] entries = new Entry[entryList.size()];
            entryList.toArray(entries);
            cellList.add(entries);
          }
        }
      }

      child = child.getNextSibling();
    }
  }
  private void processHierarchy(
      Element topicrefOrMap, LoadedDocuments loadedDocs, List<Entry> childList) {
    CollectionType collectionType = null;
    String type = DITAUtil.getNonEmptyAttribute(topicrefOrMap, null, "collection-type");
    if (type != null) {
      collectionType = CollectionType.fromString(type);
    }
    if (collectionType == null) {
      collectionType = CollectionType.UNORDERED;
    }

    processCollection(topicrefOrMap, collectionType, loadedDocs, childList);

    // Recurse ---

    // LIMITATION: @collection-type is ignored inside reltables.

    Node child = topicrefOrMap.getFirstChild();
    while (child != null) {
      if (child.getNodeType() == Node.ELEMENT_NODE) {
        Element childElement = (Element) child;

        if (DITAUtil.hasClass(childElement, "map/topicref")
            &&
            // Does not make sense inside frontmatter/backmatter.
            // For example, inside a glossary.
            !DITAUtil.hasClass(childElement, "bookmap/frontmatter", "bookmap/backmatter")) {
          processHierarchy(childElement, loadedDocs, childList);
        }
      }

      child = child.getNextSibling();
    }
  }
  private void processRows(
      Element reltable, LoadedDocuments loadedDocs, Map<Element, Entry[]> collected) {
    ArrayList<Entry[]> cellList = new ArrayList<Entry[]>();

    Node child = reltable.getFirstChild();
    while (child != null) {
      if (child.getNodeType() == Node.ELEMENT_NODE) {
        Element childElement = (Element) child;

        if (DITAUtil.hasClass(childElement, "map/relrow")) {
          cellList.clear();
          processRow(childElement, loadedDocs, cellList);

          addRow(cellList, collected);
        }
      }

      child = child.getNextSibling();
    }
  }
  private void processCell(Element relcell, LoadedDocuments loadedDocs, List<Entry> entryList) {
    Node child = relcell.getFirstChild();
    while (child != null) {
      if (child.getNodeType() == Node.ELEMENT_NODE) {
        Element childElement = (Element) child;

        String href;
        if (DITAUtil.hasClass(childElement, "map/topicref")
            && (href = DITAUtil.getNonEmptyAttribute(childElement, null, "href")) != null) {
          Entry entry = createEntry(childElement, href, loadedDocs);
          if (entry != null) {
            entryList.add(entry);
          }
        }

        processCell(childElement, loadedDocs, entryList);
      }

      child = child.getNextSibling();
    }
  }
  private void processRow(Element relrow, LoadedDocuments loadedDocs, List<Entry[]> cellList) {
    ArrayList<Entry> entryList = new ArrayList<Entry>();

    Node child = relrow.getFirstChild();
    while (child != null) {
      if (child.getNodeType() == Node.ELEMENT_NODE) {
        Element childElement = (Element) child;

        if (DITAUtil.hasClass(childElement, "map/relcell")) {
          entryList.clear();
          processCell(childElement, loadedDocs, entryList);

          Entry[] entries = new Entry[entryList.size()];
          entryList.toArray(entries);
          cellList.add(entries);
        }
      }

      child = child.getNextSibling();
    }
  }
  private void processColumns(
      Element reltable, LoadedDocuments loadedDocs, Map<Element, Entry[]> collected) {
    Element relheader = DITAUtil.findChildByClass(reltable, "map/relheader");
    if (relheader == null) {
      return;
    }

    int column = 0;
    ArrayList<Entry[]> cellList = new ArrayList<Entry[]>();
    ArrayList<Entry> entryList = new ArrayList<Entry>();

    Node child = relheader.getFirstChild();
    while (child != null) {
      if (child.getNodeType() == Node.ELEMENT_NODE) {
        Element childElement = (Element) child;

        if (DITAUtil.hasClass(childElement, "map/relcolspec")) {
          cellList.clear();

          entryList.clear();
          processCell(childElement, loadedDocs, entryList);

          if (entryList.size() > 0) {
            Entry[] entries = new Entry[entryList.size()];
            entryList.toArray(entries);
            cellList.add(entries);

            processColumn(reltable, column, loadedDocs, entryList, cellList);

            addColumn(cellList, collected);
          }
          // Otherwise the relcolspec does not contain topicrefs.

          ++column;
        }
      }

      child = child.getNextSibling();
    }
  }
  public void processMap(Element map, URL mapURL, LoadedDocuments loadedDocs) {
    // Process @collection-type (but not inside reltables) ---

    mapHref = mapURL.toExternalForm();
    ArrayList<Entry> childList = new ArrayList<Entry>();

    processHierarchy(map, loadedDocs, childList);

    // Process reltables ---

    IdentityHashMap<Element, Entry[]> collected = new IdentityHashMap<Element, Entry[]>();

    Node child = map.getFirstChild();
    while (child != null) {
      if (child.getNodeType() == Node.ELEMENT_NODE) {
        Element childElement = (Element) child;

        if (DITAUtil.hasClass(childElement, "map/reltable")) {
          processColumns(childElement, loadedDocs, collected);

          processRows(childElement, loadedDocs, collected);
        }
      }

      child = child.getNextSibling();
    }

    Iterator<Map.Entry<Element, Entry[]>> iter = collected.entrySet().iterator();
    while (iter.hasNext()) {
      Map.Entry<Element, Entry[]> e = iter.next();

      Element topic = e.getKey();
      Entry[] entries = e.getValue();

      addRelatedLinks(topic, entries);
    }
  }
  private void processCollection(
      Element topicrefOrMap,
      CollectionType collectionType,
      LoadedDocuments loadedDocs,
      List<Entry> childList) {
    Entry parent = null;

    String href = DITAUtil.getNonEmptyAttribute(topicrefOrMap, null, "href");
    if (href != null) {
      parent = createEntry(topicrefOrMap, href, loadedDocs);
      if (parent != null && parent.topic == null) {
        parent = null;
      }
    }

    childList.clear();

    Node child = topicrefOrMap.getFirstChild();
    while (child != null) {
      if (child.getNodeType() == Node.ELEMENT_NODE) {
        Element childElement = (Element) child;

        if (DITAUtil.hasClass(childElement, "map/topicref")
            && (href = DITAUtil.getNonEmptyAttribute(childElement, null, "href")) != null) {
          Entry entry = createEntry(childElement, href, loadedDocs);
          if (entry != null && entry.topic == null) {
            entry = null;
          }
          if (entry != null) {
            childList.add(entry);
          }
        }
      }

      child = child.getNextSibling();
    }

    int childCount = childList.size();
    if (childCount > 0) {
      if (parent != null) {
        linkParentToChildren(parent, childList, childCount, collectionType);
      }

      switch (collectionType) {
        case FAMILY:
          {
            for (int i = 0; i < childCount; ++i) {
              Entry entry = childList.get(i);

              linkFamilyMembers(entry, parent, childList, childCount, i);
            }
          }
          break;
        case SEQUENCE:
          {
            Entry previous = null;
            for (int i = 0; i < childCount; ++i) {
              Entry entry = childList.get(i);

              Entry next = (i + 1 < childCount) ? childList.get(i + 1) : null;
              linkSequenceMembers(entry, parent, previous, next);
              previous = entry;
            }
          }
          break;
        default:
          {
            for (int i = 0; i < childCount; ++i) {
              Entry entry = childList.get(i);

              linkMembers(entry, parent, collectionType);
            }
          }
      }
    }
  }