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.xpath;
19  
20  import org.apache.any23.extractor.ExtractionResult;
21  import org.eclipse.rdf4j.model.Resource;
22  import org.eclipse.rdf4j.model.IRI;
23  import org.eclipse.rdf4j.model.Value;
24  
25  import java.util.Locale;
26  import java.util.Map;
27  
28  /**
29   * This class models a <i>NQuads</i> template, that is a quadruple in which any component can be a variable.
30   *
31   * @author Michele Mostarda (mostarda@fbk.eu)
32   */
33  public class QuadTemplate {
34  
35      private final TemplateSubject subject;
36  
37      private final TemplatePredicate predicate;
38  
39      private final TemplateObject object;
40  
41      private final TemplateGraph graph;
42  
43      /**
44       * Constructor.
45       *
46       * @param subject
47       *            not <code>null</code> subject template.
48       * @param predicate
49       *            not <code>null</code> predicate template.
50       * @param object
51       *            not <code>null</code> object template.
52       * @param graph
53       *            graph template, can be <code>null</code>.
54       */
55      public QuadTemplate(TemplateSubject subject, TemplatePredicate predicate, TemplateObject object,
56              TemplateGraph graph) {
57          if (subject == null) {
58              throw new NullPointerException("subject term cannot be null.");
59          }
60          if (predicate == null) {
61              throw new NullPointerException("predicate term cannot be null.");
62          }
63          if (object == null) {
64              throw new NullPointerException("object term cannot be null.");
65          }
66  
67          this.subject = subject;
68          this.predicate = predicate;
69          this.object = object;
70          this.graph = graph;
71      }
72  
73      /**
74       * Constructor for template with no graph.
75       *
76       * @param subject
77       *            a populated {@link org.apache.any23.extractor.xpath.TemplateSubject}
78       * @param predicate
79       *            a populated {@link org.apache.any23.extractor.xpath.TemplatePredicate}
80       * @param object
81       *            a populated {@link org.apache.any23.extractor.xpath.TemplateObject}
82       */
83      public QuadTemplate(TemplateSubject subject, TemplatePredicate predicate, TemplateObject object) {
84          this(subject, predicate, object, null);
85      }
86  
87      /**
88       * @return the template subject.
89       */
90      public TemplateSubject getSubject() {
91          return subject;
92      }
93  
94      /**
95       * @return the template predicate.
96       */
97      public TemplatePredicate getPredicate() {
98          return predicate;
99      }
100 
101     /**
102      * @return the template object.
103      */
104     public TemplateObject getObject() {
105         return object;
106     }
107 
108     /**
109      * @return the template graph, can be <code>null</code>.
110      */
111     public TemplateGraph getGraph() {
112         return graph;
113     }
114 
115     /**
116      * Prints out this quad template in the given {@link org.apache.any23.extractor.ExtractionResult}, using the passed
117      * <i>variableAssignment</i> to expand variables.
118      *
119      * @param er
120      *            extraction result instance on which write the quad produced by this template.
121      * @param variableAssignment
122      *            the assignment used to expand variables.
123      */
124     public void printOut(ExtractionResult er, Map<String, String> variableAssignment) {
125         final Resource s = subject.getValue(variableAssignment);
126         final IRI p = predicate.getValue(variableAssignment);
127         @SuppressWarnings("unchecked")
128         final Value o = object.getValue(variableAssignment);
129         if (graph != null) {
130             final IRI g = graph.getValue(variableAssignment);
131             er.writeTriple(s, p, o, g);
132         } else {
133             er.writeTriple(s, p, o);
134         }
135     }
136 
137     @Override
138     public String toString() {
139         return String.format(Locale.ROOT, "%s %s %s %s", subject, predicate, object, graph);
140     }
141 
142 }