* @param lookUpTable
* @param revLookUpTable
*/
private TableOfContentsEntry generateTableOfContents(Document doc, HashMap<Node, TableOfContentsEntry> lookUpTable, HashMap<TableOfContentsEntry, Node> revLookUpTable){
// create toc object
TableOfContentsEntry toc = new TableOfContentsEntry("root", "root");
// store root in lut
lookUpTable.put(doc.getDocumentElement(), toc);
// elements where we should use the first x words
String[] elementsUsingFirstWords = PropertiesProvider.getInstance().getElementsForTOCUsingBeginningOfSection();
int numberOfFirstWords = PropertiesProvider.getInstance().getNumberOfFirstWordsForTOC();
// create the list of
XPathFactory xpf = XPathFactory.newInstance();
XPath xpath = xpf.newXPath();
xpath.setNamespaceContext(Utils.getTEINamespaceContext());
try {
String finalLevel = PropertiesProvider.getInstance().getFinalTOCLevel();
XPathExpression xpexp = xpath.compile("//tei:" + finalLevel +
"[count(ancestor::tei:" + finalLevel + ")=0]");
NodeList list = (NodeList) xpexp.evaluate(doc, XPathConstants.NODESET);
for(int i = 0; i < list.getLength(); i++){
Node parent = list.item(i);
TableOfContentsEntry cEntry = getTocEntry((Element)parent, elementsUsingFirstWords, numberOfFirstWords);
lookUpTable.put(parent, cEntry);
revLookUpTable.put(cEntry, parent);
while((parent = parent.getParentNode()) != null){
TableOfContentsEntry pEntry;
if(! lookUpTable.containsKey(parent)){
pEntry = getTocEntry((Element)parent, elementsUsingFirstWords, numberOfFirstWords);
lookUpTable.put(parent, pEntry);
revLookUpTable.put(pEntry, parent);
} else
pEntry = lookUpTable.get(parent);
// if we are not in there yet
boolean found = false;
for(int j = 0; j < pEntry.getChildCount(); j++ ){
if(pEntry.getChild(j) == cEntry){
found = true;
break;
}
}
if(!found)
pEntry.add(cEntry);
else
break;
// switch p and c
cEntry = pEntry;