// create data structures for fast lookup
Map entriesByKeyword = new HashMap();
Set topicHrefs = new HashSet();
IUAElement[] childrenA = a.getChildren();
for (int i=0;i<childrenA.length;++i) {
UAElement childA = (UAElement)childrenA[i];
if (childA instanceof IndexEntry) {
entriesByKeyword.put(childA.getAttribute(IndexEntry.ATTRIBUTE_KEYWORD), childA);
}
else if (childA instanceof Topic) {
topicHrefs.add(childA.getAttribute(Topic.ATTRIBUTE_HREF));
}
}
// now do the merge
IUAElement[] childrenB = b.getChildren();
for (int i=0;i<childrenB.length;++i) {
UAElement childB = (UAElement)childrenB[i];
if (childB instanceof IndexEntry) {
String keyword = childB.getAttribute(IndexEntry.ATTRIBUTE_KEYWORD);
if (entriesByKeyword.containsKey(keyword)) {
// duplicate keyword; merge children
mergeChildren((IndexEntry)entriesByKeyword.get(keyword), childB);
}
else {
// wasn't a duplicate
a.appendChild(childB);
entriesByKeyword.put(keyword, childB);
}
}
else if (childB instanceof Topic) {
String href = childB.getAttribute(Topic.ATTRIBUTE_HREF);
if (!topicHrefs.contains(href)) {
// add topic only if href doesn't exist yet
a.appendChild(childB);
topicHrefs.add(href);
}