/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.any23.extractor.rdf;
import org.apache.any23.extractor.ExtractionContext;
import org.apache.any23.extractor.ExtractionException;
import org.apache.any23.extractor.ExtractionParameters;
import org.apache.any23.extractor.ExtractionResult;
import org.apache.any23.extractor.Extractor;
import org.apache.any23.extractor.ExtractorDescription;
import org.openrdf.rio.RDFHandlerException;
import org.openrdf.rio.RDFParseException;
import org.openrdf.rio.RDFParser;
import org.openrdf.rio.helpers.RDFParserBase;
import java.io.IOException;
import java.io.InputStream;
/**
* Base class for a generic <i>RDF</i>
* {@link org.apache.any23.extractor.Extractor.ContentExtractor}.
*
* @author Michele Mostarda (mostarda@fbk.eu)
*/
public abstract class BaseRDFExtractor implements Extractor.ContentExtractor {
private boolean verifyDataType;
private boolean stopAtFirstError;
/**
* Constructor, allows to specify the validation and error handling policies.
*
* @param verifyDataType if <code>true</code> the data types will be verified,
* if <code>false</code> will be ignored.
* @param stopAtFirstError if <code>true</code> the parser will stop at first parsing error,
* if <code>false</code> will ignore non blocking errors.
*/
public BaseRDFExtractor(boolean verifyDataType, boolean stopAtFirstError) {
this.verifyDataType = verifyDataType;
this.stopAtFirstError = stopAtFirstError;
}
public abstract ExtractorDescription getDescription();
protected abstract RDFParser getParser(
ExtractionContext extractionContext,
ExtractionResult extractionResult
);
public BaseRDFExtractor() {
this(false, false);
}
public boolean isVerifyDataType() {
return verifyDataType;
}
public void setVerifyDataType(boolean verifyDataType) {
this.verifyDataType = verifyDataType;
}
public boolean isStopAtFirstError() {
return stopAtFirstError;
}
public void setStopAtFirstError(boolean b) {
stopAtFirstError = b;
}
public void run(
ExtractionParameters extractionParameters,
ExtractionContext extractionContext,
InputStream in,
ExtractionResult extractionResult
) throws IOException, ExtractionException {
try {
final RDFParser parser = getParser(extractionContext, extractionResult);
parser.parse(in, extractionContext.getDocumentURI().stringValue());
} catch (RDFHandlerException ex) {
throw new IllegalStateException("Unexpected exception.", ex);
} catch (RDFParseException ex) {
throw new ExtractionException("Error while parsing RDF document.", ex, extractionResult);
}
}
}