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