View Javadoc

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *  http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17
    18  package org.apache.any23.extractor.html;
    19
    20  import org.apache.any23.extractor.ExtractionException;
    21  import org.apache.any23.extractor.ExtractionResult;
    22  import org.apache.any23.rdf.RDFUtils;
    23  import org.eclipse.rdf4j.model.BNode;
    24  import org.w3c.dom.Node;
    25
    26  import java.util.List;
    27
    28  /**
    29   * Base class for microformat extractors based on entities.
    30   *
    31   * @author Gabriele Renzi
    32   */
    33  public abstract class EntityBasedMicroformatExtractor extends MicroformatExtractor {
    34
    35      /**
    36       * Returns the base class name for the extractor.
    37       *
    38       * @return a string containing the base of the extractor.
    39       */
    40      protected abstract String getBaseClassName();
    41
    42      /**
    43       * Resets the internal status of the extractor to prepare it to a new extraction section.
    44       */
    45      protected abstract void resetExtractor();
    46
    47      /**
    48       * Extracts an entity from a <i>DOM</i> node.
    49       *
    50       * @param node
    51       *            the DOM node.
    52       * @param out
    53       *            the extraction result collector.
    54       *
    55       * @return <code>true</code> if the extraction has produces something, <code>false</code> otherwise.
    56       *
    57       * @throws ExtractionException
    58       *             if there is an error during extraction
    59       */
    60      protected abstract boolean extractEntity(Node node, ExtractionResult out) throws ExtractionException;
    61
    62      @Override
    63      public boolean extract() throws ExtractionException {
    64          List<Node> nodes = DomUtils.findAllByClassName(getHTMLDocument().getDocument(), getBaseClassName());
    65          boolean foundAny = false;
    66          int count = 1;
    67          for (Node node : nodes) {
    68              resetExtractor();
    69              String contextID = Integer.toString(count);
    70              ExtractionResult subResult = openSubResult(getExtractionContext().copy(contextID));
    71              foundAny |= extractEntity(node, subResult);
    72              subResult.close();
    73              count++;
    74          }
    75          return foundAny;
    76      }
    77
    78      /**
    79       * @param node
    80       *            a DOM node representing a blank node
    81       *
    82       * @return an RDF blank node corresponding to that DOM node, by using a blank node ID like "MD5 of
    83       *         http://doc-uri/#xpath/to/node"
    84       */
    85      protected BNode getBlankNodeFor(Node node) {
    86          return RDFUtils.getBNode(getDocumentIRI() + "#" + DomUtils.getXPathForNode(node));
    87      }
    88
    89  }