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.rdfa;
19  
20  import org.apache.any23.extractor.ExtractionResult;
21  import org.apache.any23.extractor.IssueReport;
22  import org.eclipse.rdf4j.model.Resource;
23  import org.eclipse.rdf4j.model.Value;
24  import org.eclipse.rdf4j.model.ValueFactory;
25  
26  /**
27   * @author Hans Brende (hansbrende@apache.org)
28   */
29  final class SemarglSink implements org.semarglproject.sink.TripleSink, org.semarglproject.rdf.ProcessorGraphHandler {
30  
31      private static final String BNODE_PREFIX = org.semarglproject.vocab.RDF.BNODE_PREFIX;
32  
33      private final ExtractionResult handler;
34      private final ValueFactory valueFactory;
35  
36      SemarglSink(ExtractionResult handler, ValueFactory valueFactory) {
37          this.handler = handler;
38          this.valueFactory = valueFactory;
39      }
40  
41      private Resource createResource(String arg) {
42          if (arg.startsWith(BNODE_PREFIX)) {
43              return valueFactory.createBNode(arg.substring(BNODE_PREFIX.length()));
44          }
45          return valueFactory.createIRI(arg);
46      }
47  
48      private void writeTriple(String s, String p, Value o) {
49          handler.writeTriple(createResource(s), valueFactory.createIRI(p), o);
50      }
51  
52      @Override
53      public final void addNonLiteral(String s, String p, String o) {
54          writeTriple(s, p, createResource(o));
55      }
56  
57      @Override
58      public final void addPlainLiteral(String s, String p, String o, String lang) {
59          writeTriple(s, p, lang == null ? valueFactory.createLiteral(o) : valueFactory.createLiteral(o, lang));
60      }
61  
62      @Override
63      public final void addTypedLiteral(String s, String p, String o, String type) {
64          writeTriple(s, p, valueFactory.createLiteral(o, valueFactory.createIRI(type)));
65      }
66  
67      @Override
68      public void startStream() {
69  
70      }
71  
72      @Override
73      public void endStream() {
74      }
75  
76      @Override
77      public boolean setProperty(String key, Object value) {
78          return false;
79      }
80  
81      @Override
82      public void setBaseUri(String baseUri) {
83      }
84  
85      @Override
86      public void info(String infoClass, String message) {
87  
88      }
89  
90      @Override
91      public void warning(String warningClass, String message) {
92          handler.notifyIssue(IssueReport.IssueLevel.WARNING, message, -1, -1);
93      }
94  
95      @Override
96      public void error(String errorClass, String message) {
97          handler.notifyIssue(IssueReport.IssueLevel.ERROR, message, -1, -1);
98      }
99  }