1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
30
31
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
45
46
47
48
49
50
51
52
53
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
75
76
77
78
79
80
81
82
83 public QuadTemplate(TemplateSubject subject, TemplatePredicate predicate, TemplateObject object) {
84 this(subject, predicate, object, null);
85 }
86
87
88
89
90 public TemplateSubject getSubject() {
91 return subject;
92 }
93
94
95
96
97 public TemplatePredicate getPredicate() {
98 return predicate;
99 }
100
101
102
103
104 public TemplateObject getObject() {
105 return object;
106 }
107
108
109
110
111 public TemplateGraph getGraph() {
112 return graph;
113 }
114
115
116
117
118
119
120
121
122
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 }