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