private Node[] processIndexNode( final Node theNode, final Document theTargetDocument, final IndexEntryFoundListener theIndexEntryFoundListener) { theNode.normalize(); boolean ditastyle = false; String textNode = null; final NodeList childNodes = theNode.getChildNodes(); final StringBuilder textBuf = new StringBuilder(); final List<Node> contents = new ArrayList<Node>(); for (int i = 0; i < childNodes.getLength(); i++) { final Node child = childNodes.item(i); if (checkElementName(child)) { ditastyle = true; break; } else if (child.getNodeType() == Node.ELEMENT_NODE) { textBuf.append(XMLUtils.getStringValue((Element) child)); contents.add(child); } else if (child.getNodeType() == Node.TEXT_NODE) { textBuf.append(child.getNodeValue()); contents.add(child); } } textNode = IndexStringProcessor.normalizeTextValue(textBuf.toString()); if (textNode.length() == 0) { textNode = null; } if (theNode.getAttributes().getNamedItem(elIndexRangeStartName) != null || theNode.getAttributes().getNamedItem(elIndexRangeEndName) != null) { ditastyle = true; } final ArrayList<Node> res = new ArrayList<Node>(); if ((ditastyle)) { final IndexEntry[] indexEntries = indexDitaProcessor.processIndexDitaNode(theNode, ""); for (final IndexEntry indexEntrie : indexEntries) { theIndexEntryFoundListener.foundEntry(indexEntrie); } final Node[] nodes = transformToNodes(indexEntries, theTargetDocument, null); for (final Node node : nodes) { res.add(node); } } else if (textNode != null) { final Node[] nodes = processIndexString(textNode, contents, theTargetDocument, theIndexEntryFoundListener); for (final Node node : nodes) { res.add(node); } } else { return new Node[0]; } return (Node[]) res.toArray(new Node[res.size()]); }
/** * Processes curr node. Copies node to the target document if its is not a text node of index * entry element. Otherwise it process it and creates nodes with "prefix" in given "namespace_url" * from the parsed index entry text. * * @param theNode node to process * @param theTargetDocument target document used to import and create nodes * @param theIndexEntryFoundListener listener to notify that new index entry was found * @return the array of nodes after processing input node */ private Node[] processCurrNode( final Node theNode, final Document theTargetDocument, final IndexEntryFoundListener theIndexEntryFoundListener) { final NodeList childNodes = theNode.getChildNodes(); if (checkElementName(theNode)) { return processIndexNode(theNode, theTargetDocument, theIndexEntryFoundListener); } else { final Node result = theTargetDocument.importNode(theNode, false); for (int i = 0; i < childNodes.getLength(); i++) { final Node[] processedNodes = processCurrNode(childNodes.item(i), theTargetDocument, theIndexEntryFoundListener); for (final Node node : processedNodes) { result.appendChild(node); } } return new Node[] {result}; } }
public void createAndAddIndexGroups( final IndexEntry[] theIndexEntries, final IndexConfiguration theConfiguration, final Document theDocument, final Locale theLocale) { final IndexComparator indexEntryComparator = new IndexComparator(theLocale); final IndexGroup[] indexGroups = indexGroupProcessor.process(theIndexEntries, theConfiguration, theLocale); final Element rootElement = theDocument.getDocumentElement(); final Element indexGroupsElement = theDocument.createElementNS(namespace_url, "index.groups"); indexGroupsElement.setPrefix(prefix); for (final IndexGroup group : indexGroups) { // Create group element final Node groupElement = theDocument.createElementNS(namespace_url, "index.group"); groupElement.setPrefix(prefix); // Create group label element and index entry childs final Element groupLabelElement = theDocument.createElementNS(namespace_url, "label"); groupLabelElement.setPrefix(prefix); groupLabelElement.appendChild(theDocument.createTextNode(group.getLabel())); groupElement.appendChild(groupLabelElement); final Node[] entryNodes = transformToNodes(group.getEntries(), theDocument, indexEntryComparator); for (final Node entryNode : entryNodes) { groupElement.appendChild(entryNode); } indexGroupsElement.appendChild(groupElement); } rootElement.appendChild(indexGroupsElement); }
/** * Creates nodes from index entries * * @param theIndexEntries index entries * @param theTargetDocument target document * @param theIndexEntryComparator comparator to sort the index entries. if it is null the index * entries will be unsorted * @return nodes for the target document */ private Node[] transformToNodes( final IndexEntry[] theIndexEntries, final Document theTargetDocument, final Comparator<IndexEntry> theIndexEntryComparator) { if (null != theIndexEntryComparator) { Arrays.sort(theIndexEntries, theIndexEntryComparator); } final List<Element> result = new ArrayList<Element>(); for (final IndexEntry indexEntry : theIndexEntries) { final Element indexEntryNode = createElement(theTargetDocument, "index.entry"); final Element formattedStringElement = createElement(theTargetDocument, "formatted-value"); if (indexEntry.getContents() != null) { for (final Iterator<Node> i = indexEntry.getContents().iterator(); i.hasNext(); ) { final Node child = i.next(); final Node clone = theTargetDocument.importNode(child, true); if (!i.hasNext() && clone.getNodeType() == Node.TEXT_NODE) { final Text t = (Text) clone; t.setData(t.getData().replaceAll("[\\s\\n]+$", "")); } formattedStringElement.appendChild(clone); } } else { final Text textNode = theTargetDocument.createTextNode(indexEntry.getFormattedString()); textNode.normalize(); formattedStringElement.appendChild(textNode); } indexEntryNode.appendChild(formattedStringElement); final String[] refIDs = indexEntry.getRefIDs(); for (final String refID : refIDs) { final Element referenceIDElement = createElement(theTargetDocument, "refID"); referenceIDElement.setAttribute("value", refID); indexEntryNode.appendChild(referenceIDElement); } final String val = indexEntry.getValue(); if (null != val) { indexEntryNode.setAttribute("value", val); } final String sort = indexEntry.getSortString(); if (null != sort) { indexEntryNode.setAttribute("sort-string", sort); } if (indexEntry.isStartingRange()) { indexEntryNode.setAttribute("start-range", "true"); } else if (indexEntry.isEndingRange()) { indexEntryNode.setAttribute("end-range", "true"); } if (indexEntry.isSuppressesThePageNumber()) { indexEntryNode.setAttribute("no-page", "true"); } else if (indexEntry.isRestoresPageNumber()) { indexEntryNode.setAttribute("single-page", "true"); } final IndexEntry[] childIndexEntries = indexEntry.getChildIndexEntries(); final Node[] nodes = transformToNodes(childIndexEntries, theTargetDocument, theIndexEntryComparator); for (final Node node : nodes) { indexEntryNode.appendChild(node); } final IndexEntry[] seeChildIndexEntries = indexEntry.getSeeChildIndexEntries(); if (seeChildIndexEntries != null) { final Element seeElement = createElement(theTargetDocument, "see-childs"); final Node[] seeNodes = transformToNodes(seeChildIndexEntries, theTargetDocument, theIndexEntryComparator); for (final Node node : seeNodes) { seeElement.appendChild(node); } indexEntryNode.appendChild(seeElement); } final IndexEntry[] seeAlsoChildIndexEntries = indexEntry.getSeeAlsoChildIndexEntries(); if (seeAlsoChildIndexEntries != null) { final Element seeAlsoElement = createElement(theTargetDocument, "see-also-childs"); final Node[] seeAlsoNodes = transformToNodes(seeAlsoChildIndexEntries, theTargetDocument, theIndexEntryComparator); for (final Node node : seeAlsoNodes) { seeAlsoElement.appendChild(node); } indexEntryNode.appendChild(seeAlsoElement); } result.add(indexEntryNode); } return (Node[]) result.toArray(new Node[result.size()]); }