-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathConstructForClass.java
More file actions
144 lines (127 loc) · 5.1 KB
/
Copy pathConstructForClass.java
File metadata and controls
144 lines (127 loc) · 5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
* Copyright 2020 Martynas Jusevičius <martynas@atomgraph.com>.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.atomgraph.client.writer.function;
import com.atomgraph.client.util.Constructor;
import com.atomgraph.client.vocabulary.AC;
import static com.atomgraph.client.writer.ModelXSLTWriter.checkURI;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import javax.xml.transform.stream.StreamSource;
import net.sf.saxon.s9api.ExtensionFunction;
import net.sf.saxon.s9api.ItemType;
import net.sf.saxon.s9api.OccurrenceIndicator;
import net.sf.saxon.s9api.Processor;
import net.sf.saxon.s9api.QName;
import net.sf.saxon.s9api.SaxonApiException;
import net.sf.saxon.s9api.SequenceType;
import net.sf.saxon.s9api.XdmValue;
import com.atomgraph.client.util.jena.PrefixGraphRepository;
import org.apache.jena.ontapi.OntModelFactory;
import org.apache.jena.ontapi.OntSpecification;
import org.apache.jena.ontapi.model.OntClass;
import org.apache.jena.ontapi.model.OntModel;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.riot.RDFFormat;
import org.apache.jena.riot.RDFWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* <code>ac:construct()</code> XSLT function that constructs instances for given classes from their constructors.
* Plugs into the Saxon processor.
*
* @author Martynas Jusevičius {@literal <martynas@atomgraph.com>}
* @see <a href="http://www.saxonica.com/documentation/#!extensibility/integratedfunctions">Integrated extension functions</a>
*/
public class ConstructForClass implements ExtensionFunction
{
private static final Logger log = LoggerFactory.getLogger(ConstructForClass.class);
private final Processor processor;
private final PrefixGraphRepository repository;
public ConstructForClass(Processor processor, PrefixGraphRepository repository)
{
this.processor = processor;
this.repository = repository;
}
@Override
public QName getName()
{
return new QName(AC.NS, "construct");
}
@Override
public SequenceType getResultType()
{
return SequenceType.makeSequenceType(ItemType.DOCUMENT_NODE, OccurrenceIndicator.ZERO_OR_MORE);
}
@Override
public SequenceType[] getArgumentTypes()
{
return new SequenceType[]
{
SequenceType.makeSequenceType(ItemType.ANY_URI, OccurrenceIndicator.ONE),
SequenceType.makeSequenceType(ItemType.ANY_URI, OccurrenceIndicator.ZERO_OR_MORE),
SequenceType.makeSequenceType(ItemType.ANY_URI, OccurrenceIndicator.ONE),
};
}
@Override
public XdmValue call(XdmValue[] arguments) throws SaxonApiException
{
try
{
String ontology = arguments[0].itemAt(0).getStringValue();
String base = arguments[2].itemAt(0).getStringValue();
Model instances = ModelFactory.createDefaultModel();
try
{
OntModel ontModel = OntModelFactory.createModel(getRepository().get(ontology), OntSpecification.OWL2_FULL_MEM, getRepository());
arguments[1].stream().
<OntClass>map(forClass -> ontModel.getOntClass(checkURI(forClass.getStringValue()).toString())).
filter(forClass -> forClass != null).
forEach(forClass -> new Constructor().construct(forClass, instances, base));
}
catch (RuntimeException ex) // ontology or one of its imports could not be loaded — return empty result instead of failing the transform
{
if (log.isWarnEnabled()) log.warn("Could not construct instances for ontology '{}': {}", ontology, ex.toString());
}
return getProcessor().newDocumentBuilder().build(getSource(instances));
}
catch (IOException ex)
{
throw new SaxonApiException(ex);
}
}
public StreamSource getSource(Model model) throws IOException
{
if (model == null) throw new IllegalArgumentException("Model cannot be null");
try (ByteArrayOutputStream stream = new ByteArrayOutputStream())
{
RDFWriter.create().
format(RDFFormat.RDFXML_PLAIN).
source(model).
output(stream);
return new StreamSource(new ByteArrayInputStream(stream.toByteArray()));
}
}
public Processor getProcessor()
{
return processor;
}
public PrefixGraphRepository getRepository()
{
return repository;
}
}