Newer
Older
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
/*
* Copyright 2020 ETSI
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package fr.emse.gitlab.saref.managers;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.io.IOUtils;
import org.apache.jena.atlas.io.IndentedWriter;
import org.apache.jena.graph.NodeFactory;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.sparql.core.Var;
import org.apache.jena.sparql.engine.binding.Binding;
import org.apache.jena.sparql.engine.binding.BindingHashMap;
import org.apache.jena.sparql.util.Context;
import org.eclipse.jgit.api.Git;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import fr.emse.ci.sparqlext.SPARQLExt;
import fr.emse.ci.sparqlext.engine.PlanFactory;
import fr.emse.ci.sparqlext.engine.RootPlan;
import fr.emse.ci.sparqlext.stream.LookUpRequest;
import fr.emse.ci.sparqlext.stream.SPARQLExtStreamManager;
import fr.emse.ci.sparqlext.utils.ContextUtils;
import fr.emse.ci.sparqlext.utils.VarUtils;
import fr.emse.gitlab.saref.SAREF;
import fr.emse.gitlab.saref.SAREFErrorLogger;
import fr.emse.gitlab.saref.SAREFPipeline;
import fr.emse.gitlab.saref.SAREFPipeline.Mode;
import fr.emse.gitlab.saref.SAREFPipelineException;
import fr.emse.gitlab.saref.utils.StreamManagerFactory;
public class SiteManager extends SAREFErrorLogger {
private static final Logger LOG = LoggerFactory.getLogger(SiteManager.class);
private static final String DOC_BASE = SAREF.BASE + "documentation/";
private static final String TERM_QUERY = DOC_BASE + "term/main.rqg";
private static final Var VAR_TERM = VarUtils.allocVar("term");
private static final String ONTO_QUERY = DOC_BASE + "ontology/main.rqg";
private static final Var VAR_VERSION_IRI = VarUtils.allocVar("versionIRI");
private static final String EXAMPLE_QUERY = DOC_BASE + "example/main.rqg";
private static final Var VAR_EXAMPLE = VarUtils.allocVar("example");
private static final String NAME_SITE = "site";
private static final String NAME_STATIC = "static";
private static final String NAME_REPORT_HTML = "report.html";
private static final SPARQLExtStreamManager STREAM_MANAGER_BASE = StreamManagerFactory.get();
private final File siteDir;
private final File staticDir;
private final File reportFileHtml;
private final RootPlan planForTerm;
private final RootPlan planForOntologyVersion;
private final RootPlan planForExample;
private static enum MESSAGE {
prepare_error;
}
public SiteManager(SAREFPipeline pipeline, Logger errorLogger) throws IOException {
super(pipeline, errorLogger);
siteDir = new File(pipeline.targetDir, NAME_SITE);
staticDir = new File(siteDir, NAME_STATIC);
reportFileHtml = new File(siteDir, NAME_REPORT_HTML);
String query = IOUtils.toString(STREAM_MANAGER_BASE.open(new LookUpRequest(TERM_QUERY, SPARQLExt.MEDIA_TYPE)),
StandardCharsets.UTF_8);
planForTerm = PlanFactory.create(query, DOC_BASE);
query = IOUtils.toString(STREAM_MANAGER_BASE.open(new LookUpRequest(ONTO_QUERY, SPARQLExt.MEDIA_TYPE)),
StandardCharsets.UTF_8);
planForOntologyVersion = PlanFactory.create(query, DOC_BASE);
query = IOUtils.toString(STREAM_MANAGER_BASE.open(new LookUpRequest(TERM_QUERY, SPARQLExt.MEDIA_TYPE)),
StandardCharsets.UTF_8);
// planForExample = PlanFactory.create(query, DOC_BASE);
planForExample = null;
}
/**
* Prepare the site folder and clone the saref-portal-static project repository.
*/
public void prepareSite() throws SAREFPipelineException {
if (pipeline.ignoreSite) {
return;
}
try (Git git = Git.cloneRepository().setURI(SAREF.SAREF_PORTAL_STATIC_GIT).setDirectory(siteDir).call()) {
} catch (Exception ex) {
try (Git git = Git.open(siteDir)) {
git.pull().call();
} catch (Exception e) {
String msg = getMessage(MESSAGE.prepare_error, SAREF.SAREF_PORTAL_STATIC_GIT);
log(msg, e, Mode.DEVELOP, Mode.RELEASE, Mode.PORTAL);
throw new SAREFPipelineException(msg, e);
}
}
}
public boolean writeReport(StringWriter sw) throws IOException {
}
File reportHTML = new File(siteDir, "report.html");
try (IndentedWriter writer = new IndentedWriter(new FileOutputStream(reportHTML));) {
boolean debugTemplate = pipeline.mode == Mode.DEVELOP;
Context context = ContextUtils.build(writer).setBase(SAREF.BASE).setDebugTemplate(debugTemplate)
.setStreamManager(STREAM_MANAGER_BASE).build();
BindingHashMap binding = new BindingHashMap();
Var varTestSuites = VarUtils.allocVar("testsuites");
binding.add(varTestSuites, NodeFactory.createLiteral(sw.toString()));
List<Binding> bindings = new ArrayList<>();
bindings.add(binding);
String query = IOUtils.toString(
STREAM_MANAGER_BASE.open(new LookUpRequest("documentation/report/main.rqg", SPARQLExt.MEDIA_TYPE)),
StandardCharsets.UTF_8);
RootPlan reportPlan = PlanFactory.create(query, DOC_BASE);
reportPlan.execTemplateStream(bindings, context);
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
}
}
public void generateSite() {
if (pipeline.ignoreSite) {
return;
}
LOG.info("Generate static files for the portal");
// DatasetManager datasetManager = pipeline.getDatasetManager();
// Dataset dataset = datasetManager.getDataset();
// dataset.begin(ReadWrite.READ);
// Model config = datasetManager.getConfigModel();
//
// File configFile = new File(siteDir, "config.ttl");
// try (FileOutputStream fos = new FileOutputStream(configFile)) {
// config.write(fos, "TTL");
// } catch (IOException ex) {
// errorLogger.error("Unexpected exception while writing configuration file", ex);
// }
//
// for (ResIterator it = config.listResourcesWithProperty(RDF.type, EX.OntologyVersion); it.hasNext();) {
// Resource resource = it.next();// https://saref.etsi.org/core/master/
// String uri = resource.getURI();
// String ontoDirPath = uri.substring(SAREF.BASE.length(), uri.length() - 1); // core/master
// try {
// File ontoDir = new File(siteDir, ontoDirPath);
// FileUtils.forceMkdir(ontoDir);
// String repoName = resource.getRequiredProperty(EX.repositoryName).getString(); // saref-core
// String ontologyFileName = repoName.equals("saref-core") ? "saref" : repoName; // saref
// Model model = dataset.getNamedModel(resource.getURI());
// for (Languages l : Languages.values()) {
// File file = new File(ontoDir, String.format("%s.%s", ontologyFileName, l.getExt()));
// try (FileOutputStream fos = new FileOutputStream(file)) {
// model.write(fos, l.getLang());
// }
// }
// File htmlFile = new File(ontoDir, String.format("%s.html", ontologyFileName));
//
//
// // attention, le directory change selon le graph à generer.
// StreamManager streamManager = StreamManagerFactory.get(directory);
//
//
// try (IndentedWriter writer = new IndentedWriter(new FileOutputStream(htmlFile));) {
// Context context = ContextUtils.build(writer).setBase(REGEX.BASE)
// .setDebugTemplate(pipeline.mode == Mode.DEVELOP).setInputDataset(dataset)
// .setStreamManager(streamManager).build();
// generateHTML(planForOntologyVersion, VAR_VERSION_IRI, resource, context);
// }
//
//
//
// } catch (Exception ex) {
// errorLogger.error(String.format("Unexpected exception while writing static files for %s", resource),
// ex);
// }
// try {
// File diagramDir = new File(siteDir, ontoDirPath + File.separator + "diagrams");
// FileUtils.copyDirectory(new File(directory, "diagrams"), diagramDir);
// } catch (IOException ex) {
// errorLogger.warn("Could not copy diagrams directory", ex);
// }
// }
//
// if (!ignoreTerms) {
// for (ResIterator it = config.listResourcesWithProperty(RDF.type, RDFP.Resource); it.hasNext();) {
// Resource resource = it.next();// https://saref.etsi.org/core/master/ --
// // https://saref.etsi.org/core/Command
// String uri = resource.getURI();
// try {
// String termPath = uri.substring(SAREF.BASE.length()); // core/Command
// File termDir = new File(siteDir, termPath).getParentFile(); // core
// FileUtils.forceMkdir(termDir);
// String termName = resource.getRequiredProperty(EX.localName).getString(); // Command
// Model model = dataset.getNamedModel(resource.getURI());
// for (Languages l : Languages.values()) {
// File file = new File(termDir, String.format("%s.%s", termName, l.getExt()));
// try (FileOutputStream fos = new FileOutputStream(file)) {
// model.write(fos, l.getLang());
// }
// }
// File htmlFile = new File(termDir, String.format("%s.html", termName));
// try (IndentedWriter writer = new IndentedWriter(new FileOutputStream(htmlFile));) {
// Context context = ContextUtils.build(writer).setBase(REGEX.BASE).setDebugTemplate(verbose)
// .setInputDataset(dataset).setStreamManager(streamManager).build();
// generateHTML(planForTerm, VAR_TERM, resource, context);
// }
// } catch (Exception ex) {
// errorLogger.error(String.format("Unexpected exception while writing static files for %s", resource),
// ex);
// }
// }
// }
//
// if (!ignoreExamples) {
// for (ResIterator it = config.listResourcesWithProperty(RDF.type, EX.Example); it.hasNext();) {
// Resource resource = it.next();// https://saref.etsi.org/core/master/example/doorswitch
// String uri = resource.getURI();
// try {
// String exPath = uri.substring(REGEX.BASE.length()); // core/master/example/doorswitch
// File exDir = new File(siteDir, exPath).getParentFile(); // core/master/example
// FileUtils.forceMkdir(exDir);
// String exName = resource.getRequiredProperty(EX.localName).getString(); // doorswitch
// Model model = dataset.getNamedModel(resource.getURI());
// for (Languages l : Languages.values()) {
// File file = new File(exDir, String.format("%s.%s", exName, l.getExt()));
// try (FileOutputStream fos = new FileOutputStream(file)) {
// model.write(fos, l.getLang());
// }
// }
// // File htmlFile = new File(exDir, String.format("%s.html", exName));
// // try (IndentedWriter writer = new IndentedWriter(new
// // FileOutputStream(htmlFile));) {
// // Context context = ContextUtils.build(writer).setBase(Constants.BASE)
// // .setDebugTemplate(!Constants.PRODUCTION).setInputDataset(dataset)
// // .setStreamManager(streamManager).build();
// // generateHTML(planForExample, VAR_EXAMPLE, resource, context);
// // }
// } catch (Exception ex) {
// errorLogger.error(String.format("Unexpected exception while writing static files for %s", resource),
// ex);
// }
// }
// }
//
// dataset.end();
}
private void generateHTML(RootPlan plan, Var var, Resource resource, Context context) {
BindingHashMap binding = new BindingHashMap();
binding.add(var, resource.asNode());
List<Binding> bindings = new ArrayList<>();
bindings.add(binding);
plan.execTemplateStream(bindings, context);
}
}