2828import java .util .HashMap ;
2929import java .util .List ;
3030import java .util .Map ;
31+ import java .util .Optional ;
3132import java .util .stream .Collectors ;
3233import java .util .stream .Stream ;
3334
4041import org .eclipse .rdf4j .model .impl .LinkedHashModel ;
4142import org .eclipse .rdf4j .model .impl .SimpleValueFactory ;
4243import org .eclipse .rdf4j .model .util .Models ;
44+ import org .eclipse .rdf4j .model .vocabulary .CONFIG ;
4345import org .eclipse .rdf4j .model .vocabulary .RDF ;
4446import org .eclipse .rdf4j .repository .RepositoryReadOnlyException ;
4547import org .eclipse .rdf4j .repository .config .ConfigTemplate ;
@@ -143,12 +145,12 @@ private String getUserTemplates() {
143145 */
144146 private String getBuiltinTemplates () {
145147 // assume the templates are all located in the same jar "directory" as the RepositoryConfig class
146- Class cl = RepositoryConfig .class ;
148+ Class < RepositoryConfig > cl = RepositoryConfig .class ;
147149
148150 try {
149151 URI dir = cl .getResource (cl .getSimpleName () + ".class" ).toURI ();
150152 if (dir .getScheme ().equals ("jar" )) {
151- try (FileSystem fs = FileSystems .newFileSystem (dir , Collections .EMPTY_MAP , null )) {
153+ try (FileSystem fs = FileSystems .newFileSystem (dir , Collections .emptyMap () , null )) {
152154 // turn package structure into directory structure
153155 String pkg = cl .getPackage ().getName ().replaceAll ("\\ ." , "/" );
154156 return getOrderedTemplates (fs .getPath (pkg ));
@@ -187,15 +189,9 @@ private void createRepository(final String templateName) {
187189 boolean eof = inputParameters (valueMap , variableMap , configTemplate .getMultilineMap ());
188190 if (!eof ) {
189191 final String configString = configTemplate .render (valueMap );
190- final Model graph = new LinkedHashModel ( );
192+ final Model graph = parseConfigIntoModel ( configString );
191193
192- final RDFParser rdfParser = Rio .createParser (RDFFormat .TURTLE , SimpleValueFactory .getInstance ());
193- rdfParser .setRDFHandler (new StatementCollector (graph ));
194- rdfParser .parse (new StringReader (configString ), RepositoryConfigSchema .NAMESPACE );
195-
196- final Resource repositoryNode = Models
197- .subject (graph .getStatements (null , RDF .TYPE , RepositoryConfigSchema .REPOSITORY ))
198- .orElseThrow (() -> new RepositoryConfigException ("missing repository node" ));
194+ final Resource repositoryNode = findRepositoryNode (graph );
199195
200196 final RepositoryConfig repConfig = RepositoryConfig .create (graph , repositoryNode );
201197 repConfig .validate ();
@@ -232,6 +228,25 @@ private void createRepository(final String templateName) {
232228 }
233229 }
234230
231+ private Resource findRepositoryNode (final Model graph ) {
232+ Optional <Resource > modern = Models
233+ .subject (graph .getStatements (null , RDF .TYPE , CONFIG .Rep .Repository ));
234+ Resource repositoryNode = modern
235+ .orElseGet (() -> Models
236+ .subject (graph .getStatements (null , RDF .TYPE , RepositoryConfigSchema .REPOSITORY ))
237+ .orElseThrow (() -> new RepositoryConfigException ("missing repository node" )));
238+ return repositoryNode ;
239+ }
240+
241+ private Model parseConfigIntoModel (final String configString ) throws IOException {
242+ final Model graph = new LinkedHashModel ();
243+
244+ final RDFParser rdfParser = Rio .createParser (RDFFormat .TURTLE , SimpleValueFactory .getInstance ());
245+ rdfParser .setRDFHandler (new StatementCollector (graph ));
246+ rdfParser .parse (new StringReader (configString ), CONFIG .NAMESPACE );
247+ return graph ;
248+ }
249+
235250 /**
236251 * Ask user to specify values for the template variables
237252 *
@@ -251,23 +266,7 @@ private boolean inputParameters(final Map<String, String> valueMap, final Map<St
251266 final String var = entry .getKey ();
252267 final List <String > values = entry .getValue ();
253268
254- StringBuilder sb = new StringBuilder ();
255- sb .append (var );
256-
257- if (values .size () > 1 ) {
258- sb .append (" (" );
259- for (int i = 0 ; i < values .size (); i ++) {
260- if (i > 0 ) {
261- sb .append ("|" );
262- }
263- sb .append (values .get (i ));
264- }
265- sb .append (")" );
266- }
267- if (!values .isEmpty ()) {
268- sb .append (" [" + values .get (0 ) + "]" );
269- }
270- String prompt = sb .append (": " ).toString ();
269+ String prompt = buildPromptString (values , var );
271270 String value = multilineInput .containsKey (var ) ? consoleIO .readMultiLineInput (prompt )
272271 : consoleIO .readln (prompt );
273272 eof = (value == null );
@@ -284,6 +283,25 @@ private boolean inputParameters(final Map<String, String> valueMap, final Map<St
284283 return eof ;
285284 }
286285
286+ private String buildPromptString (final List <String > values , String var ) {
287+ StringBuilder sb = new StringBuilder ();
288+ sb .append (var );
289+ if (values .size () > 1 ) {
290+ sb .append (" (" );
291+ for (int i = 0 ; i < values .size (); i ++) {
292+ if (i > 0 ) {
293+ sb .append ("|" );
294+ }
295+ sb .append (values .get (i ));
296+ }
297+ sb .append (")" );
298+ }
299+ if (!values .isEmpty ()) {
300+ sb .append (" [" + values .get (0 ) + "]" );
301+ }
302+ return sb .append (": " ).toString ();
303+ }
304+
287305 /**
288306 * Create input stream from a template file in the specified file directory. If the file cannot be found, try to
289307 * read it from the embedded java resources instead.
0 commit comments