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;
    19
    20  import org.eclipse.rdf4j.model.IRI;
    21
    22  /**
    23   * This class provides the context for the processing of a single {@link Extractor}.
    24   */
    25  public class ExtractionContext {
    26
    27      public static final String ROOT_EXTRACTION_RESULT_ID = "root-extraction-result-id";
    28
    29      /**
    30       * Name of the extractor.
    31       */
    32      private final String extractorName;
    33
    34      /**
    35       * IRI of the document.
    36       */
    37      private final IRI documentIRI;
    38
    39      /**
    40       * The document default language.
    41       */
    42      private String defaultLanguage;
    43
    44      /**
    45       * ID identifying the document.
    46       */
    47      private final String uniqueID;
    48
    49      public ExtractionContext(String extractorName, IRI documentIRI, String defaultLanguage, String localID) {
    50          checkNotNull(extractorName, "extractor name");
    51          checkNotNull(documentIRI, "document IRI");
    52          this.extractorName = extractorName;
    53          this.documentIRI = documentIRI;
    54          this.defaultLanguage = defaultLanguage;
    55          this.uniqueID = "urn:x-any23:" + extractorName + ":" + (localID == null ? "" : localID) + ":" + documentIRI;
    56      }
    57
    58      public ExtractionContext(String extractorName, IRI documentIRI, String defaultLanguage) {
    59          this(extractorName, documentIRI, defaultLanguage, ROOT_EXTRACTION_RESULT_ID);
    60      }
    61
    62      public ExtractionContext(String extractorName, IRI documentIRI) {
    63          this(extractorName, documentIRI, null);
    64      }
    65
    66      public ExtractionContext copy(String localID) {
    67          return new ExtractionContext(getExtractorName(), getDocumentIRI(), getDefaultLanguage(), localID);
    68      }
    69
    70      public String getExtractorName() {
    71          return extractorName;
    72      }
    73
    74      public IRI getDocumentIRI() {
    75          return documentIRI;
    76      }
    77
    78      public String getDefaultLanguage() {
    79          return defaultLanguage;
    80      }
    81
    82      public String getUniqueID() {
    83          return uniqueID;
    84      }
    85
    86      public int hashCode() {
    87          return uniqueID.hashCode();
    88      }
    89
    90      public boolean equals(Object other) {
    91          if (!(other instanceof ExtractionContext))
    92              return false;
    93          return ((ExtractionContext) other).uniqueID.equals(uniqueID);
    94      }
    95
    96      public String toString() {
    97          return "ExtractionContext(" + uniqueID + ")";
    98      }
    99
    100     private void checkNotNull(Object data, String desc) {
    101         if (data == null)
    102             throw new NullPointerException(desc + " cannot be null.");
    103     }
    104
    105 }