예제 #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
  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()]);
  }