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 com.fasterxml.jackson.core.JsonFactory;
21  import com.fasterxml.jackson.core.JsonParser;
22  import com.github.jsonldjava.utils.JsonUtils;
23  import org.apache.any23.extractor.ExtractionContext;
24  import org.apache.any23.extractor.ExtractionResult;
25  import org.apache.any23.extractor.ExtractorDescription;
26  import org.eclipse.rdf4j.rio.RDFParser;
27  
28  import java.lang.reflect.Field;
29  
30  /**
31   * Concrete implementation of {@link org.apache.any23.extractor.Extractor.ContentExtractor}
32   * handling <a href="http://www.w3.org/TR/json-ld/">JSON-LD</a> format.
33   *
34   */
35  public class JSONLDExtractor extends BaseRDFExtractor {
36  
37      static {
38          //See https://issues.apache.org/jira/browse/ANY23-336
39          try {
40              //This field was introduced in jsonld-java version 0.12.0
41              if ((Object)JsonUtils.JSONLD_JAVA_USER_AGENT instanceof Void) {
42                  throw new Error("This error will never be thrown.");
43              }
44          } catch (NoSuchFieldError th) {
45              throw new AssertionError("You have an outdated version of jsonld-java on the classpath. " +
46                      "Upgrade to at least version 0.12.0. See: https://issues.apache.org/jira/browse/ANY23-336", th);
47          }
48  
49          JsonFactory JSON_FACTORY;
50          try {
51              Field field = JsonUtils.class.getDeclaredField("JSON_FACTORY");
52              field.setAccessible(true);
53              JSON_FACTORY = (JsonFactory)field.get(null);
54          } catch (Exception e) {
55              throw new AssertionError(e);
56          }
57  
58          JSON_FACTORY.enable(JsonParser.Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER);
59          JSON_FACTORY.disable(JsonParser.Feature.ALLOW_COMMENTS); //handled by JsonCleaningInputStream
60          JSON_FACTORY.disable(JsonParser.Feature.ALLOW_MISSING_VALUES); //handled by JsonCleaningInputStream
61          JSON_FACTORY.enable(JsonParser.Feature.ALLOW_NON_NUMERIC_NUMBERS);
62          JSON_FACTORY.enable(JsonParser.Feature.ALLOW_NUMERIC_LEADING_ZEROS);
63          JSON_FACTORY.disable(JsonParser.Feature.ALLOW_SINGLE_QUOTES); //handled by JsonCleaningInputStream
64          JSON_FACTORY.disable(JsonParser.Feature.ALLOW_TRAILING_COMMA); //handled by JsonCleaningInputStream
65          JSON_FACTORY.enable(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS);
66          JSON_FACTORY.enable(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES);
67          JSON_FACTORY.disable(JsonParser.Feature.ALLOW_YAML_COMMENTS); //handled by JsonCleaningInputStream
68          JSON_FACTORY.enable(JsonParser.Feature.IGNORE_UNDEFINED);
69          JSON_FACTORY.enable(JsonParser.Feature.INCLUDE_SOURCE_IN_LOCATION);
70          JSON_FACTORY.disable(JsonParser.Feature.STRICT_DUPLICATE_DETECTION);
71      }
72  
73  
74      public JSONLDExtractor(boolean verifyDataType, boolean stopAtFirstError) {
75          super(verifyDataType, stopAtFirstError);
76      }
77  
78      public JSONLDExtractor() {
79          this(false, false);
80      }
81  
82      @Override
83      public ExtractorDescription getDescription() {
84          return JSONLDExtractorFactory.getDescriptionInstance();
85      }
86  
87      @Override
88      protected RDFParser getParser(ExtractionContext extractionContext, ExtractionResult extractionResult) {
89          return RDFParserFactory.getInstance().getJSONLDParser(
90                  isVerifyDataType(), isStopAtFirstError(), extractionContext, extractionResult
91          );
92      }
93  }