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.rdf;
19  
20  import org.apache.any23.extractor.ExtractionContext;
21  import org.apache.any23.extractor.ExtractionException;
22  import org.apache.any23.extractor.ExtractionParameters;
23  import org.apache.any23.extractor.ExtractionResult;
24  import org.apache.any23.extractor.Extractor;
25  import org.apache.any23.extractor.ExtractorDescription;
26  import org.openrdf.rio.RDFHandlerException;
27  import org.openrdf.rio.RDFParseException;
28  import org.openrdf.rio.RDFParser;
29  import org.openrdf.rio.helpers.RDFParserBase;
30  
31  import java.io.IOException;
32  import java.io.InputStream;
33  
34  /**
35   * Base class for a generic <i>RDF</i>
36   * {@link org.apache.any23.extractor.Extractor.ContentExtractor}.
37   *
38   * @author Michele Mostarda (mostarda@fbk.eu)
39   */
40  public abstract class BaseRDFExtractor implements Extractor.ContentExtractor {
41  
42      private boolean verifyDataType;
43      private boolean stopAtFirstError;
44  
45      /**
46       * Constructor, allows to specify the validation and error handling policies.
47       *
48       * @param verifyDataType if <code>true</code> the data types will be verified,
49       *         if <code>false</code> will be ignored.
50       * @param stopAtFirstError if <code>true</code> the parser will stop at first parsing error,
51       *        if <code>false</code> will ignore non blocking errors.
52       */
53      public BaseRDFExtractor(boolean verifyDataType, boolean stopAtFirstError) {
54          this.verifyDataType = verifyDataType;
55          this.stopAtFirstError = stopAtFirstError;
56      }
57  
58      public abstract ExtractorDescription getDescription();
59  
60      protected abstract RDFParser getParser(
61              ExtractionContext extractionContext,
62              ExtractionResult extractionResult
63      );
64  
65      public BaseRDFExtractor() {
66          this(false, false);
67      }
68  
69      public boolean isVerifyDataType() {
70          return verifyDataType;
71      }
72  
73      public void setVerifyDataType(boolean verifyDataType) {
74          this.verifyDataType = verifyDataType;
75      }
76  
77      public boolean isStopAtFirstError() {
78          return stopAtFirstError;
79      }
80  
81      public void setStopAtFirstError(boolean b) {
82          stopAtFirstError = b;
83      }
84  
85      public void run(
86              ExtractionParameters extractionParameters,
87              ExtractionContext extractionContext,
88              InputStream in,
89              ExtractionResult extractionResult
90      ) throws IOException, ExtractionException {
91          try {
92              final RDFParser parser = getParser(extractionContext, extractionResult);
93              parser.parse(in, extractionContext.getDocumentURI().stringValue());
94          } catch (RDFHandlerException ex) {
95              throw new IllegalStateException("Unexpected exception.", ex);
96          } catch (RDFParseException ex) {
97              throw new ExtractionException("Error while parsing RDF document.", ex, extractionResult);
98          }
99      }
100 
101 }