View Javadoc
1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements. See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership. The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.commons.rdf.simple;
19  
20  import java.io.IOException;
21  import java.util.UUID;
22  import java.util.function.Consumer;
23  
24  import org.apache.commons.rdf.api.IRI;
25  import org.apache.commons.rdf.api.Quad;
26  import org.apache.commons.rdf.api.RDF;
27  import org.apache.commons.rdf.experimental.RDFParser;
28  import org.apache.commons.rdf.simple.experimental.AbstractRDFParser;
29  import org.apache.commons.rdf.simple.experimental.RDFParseException;
30  
31  /**
32   * For test purposes - a {@link RDFParser} that inserts information about what
33   * it has been asked to parse instead of actually parsing anything.
34   * <p>
35   * This always insert at least the triple equivalent to:
36   *
37   * <pre>
38   *    &lt;urn:uuid:b7ac3fcc-4d86-4d28-8358-a1cd094974a6&gt; &lt;http://example.com/greeting&gt; "Hello world" .
39   * </pre>
40   *
41   * Additional triples match the corresponding getter in AbstractRDFParser, e.g.:
42   *
43   * <pre>
44   *   &lt;urn:uuid:b7ac3fcc-4d86-4d28-8358-a1cd094974a6&gt; &lt;http://example.com/base&gt; &lt;http://www.example.org/&gt; .
45   *   &lt;urn:uuid:b7ac3fcc-4d86-4d28-8358-a1cd094974a6&gt; &lt;http://example.com/sourceFile&gt; "/tmp/file.ttl" .
46   * </pre>
47   *
48   *
49   */
50  public class DummyRDFParserBuilder extends AbstractRDFParser<DummyRDFParserBuilder> {
51  
52      @Override
53      protected void parseSynchronusly() throws IOException, IllegalStateException, RDFParseException {
54          // From parseSynchronusly both of these are always present
55          final RDF factory = getRdfTermFactory().get();
56          final Consumer<Quad> t = getTarget();
57  
58          // well - each parsing is unique. This should hopefully
59          // catch any accidental double parsing
60          final IRI parsing = factory.createIRI("urn:uuid:" + UUID.randomUUID());
61          t.accept(factory.createQuad(null, parsing, factory.createIRI("http://example.com/greeting"),
62                  factory.createLiteral("Hello world")));
63  
64          // Now we'll expose the finalized AbstractRDFParser settings
65          // so they can be inspected by the junit test
66  
67          if (getSourceIri().isPresent()) {
68              t.accept(factory.createQuad(null, parsing, factory.createIRI("http://example.com/source"),
69                      getSourceIri().get()));
70          }
71          if (getSourceFile().isPresent()) {
72              t.accept(factory.createQuad(null, parsing, factory.createIRI("http://example.com/source"),
73                      factory.createIRI(getSourceFile().get().toUri().toString())));
74          }
75          if (getSourceInputStream().isPresent()) {
76              t.accept(factory.createQuad(null, parsing, factory.createIRI("http://example.com/source"),
77                      factory.createBlankNode()));
78          }
79  
80          if (getBase().isPresent()) {
81              t.accept(factory.createQuad(null, parsing, factory.createIRI("http://example.com/base"), getBase().get()));
82          }
83          if (getContentType().isPresent()) {
84              t.accept(factory.createQuad(null, parsing, factory.createIRI("http://example.com/contentType"),
85                      factory.createLiteral(getContentType().get())));
86          }
87          if (getContentTypeSyntax().isPresent()) {
88              t.accept(factory.createQuad(null, parsing, factory.createIRI("http://example.com/contentTypeSyntax"),
89                      factory.createLiteral(getContentTypeSyntax().get().name())));
90          }
91      }
92  
93  }