예제 #1
0
  /**
   * Process index terms.
   *
   * @param theInput input document
   * @return read index terms
   * @throws ProcessException if processing index terms failed
   */
  public IndexPreprocessResult process(final Document theInput) throws ProcessException {
    final DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder documentBuilder = null;
    try {
      documentBuilder = documentBuilderFactory.newDocumentBuilder();
    } catch (final ParserConfigurationException e) {
      throw new RuntimeException("Unable to create a document builder: " + e.getMessage(), e);
    }
    final Document doc = documentBuilder.newDocument();

    final Node rootElement = theInput.getDocumentElement();

    final ArrayList<IndexEntry> indexes = new ArrayList<IndexEntry>();

    final IndexEntryFoundListener listener =
        new IndexEntryFoundListener() {
          public void foundEntry(final IndexEntry theEntry) {
            indexes.add(theEntry);
          }
        };

    final Node node = processCurrNode(rootElement, doc, listener)[0];

    doc.appendChild(node);

    doc.getDocumentElement().setAttribute(XMLNS_ATTRIBUTE + ":" + this.prefix, this.namespace_url);

    return new IndexPreprocessResult(doc, (IndexEntry[]) indexes.toArray(new IndexEntry[0]));
  }
예제 #2
0
  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);
  }