View Javadoc

1   /*
2    ORDI - Ontology Repository and Data Integration
3   
4    Copyright (c) 2004-2007, OntoText Lab. / SIRMA
5   
6    This library is free software; you can redistribute it and/or modify it under
7    the terms of the GNU Lesser General Public License as published by the Free
8    Software Foundation; either version 2.1 of the License, or (at your option)
9    any later version.
10   This library is distributed in the hope that it will be useful, but WITHOUT
11   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12   FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13   details.
14   You should have received a copy of the GNU Lesser General Public License along
15   with this library; if not, write to the Free Software Foundation, Inc.,
16   59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17   */
18  package com.ontotext.ordi.compatibilitytests;
19  
20  import java.io.BufferedReader;
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.io.InputStreamReader;
24  import java.util.regex.Matcher;
25  import java.util.regex.Pattern;
26  
27  import org.openrdf.model.Statement;
28  import org.openrdf.model.ValueFactory;
29  import org.openrdf.model.impl.ValueFactoryImpl;
30  
31  /**
32   * This class is helper parser to process the owl file in the resource
33   * directory. It'll not process arbitrary .nt files! Use a generic .nt parser
34   * instead!
35   * 
36   * @author vassil
37   * 
38   */
39  class NTParser {
40  
41      private BufferedReader reader;
42      private ValueFactory factory;
43      private Pattern pattern;
44  
45      public NTParser(InputStream stream) {
46          if (stream == null) {
47              throw new IllegalArgumentException();
48          }
49          this.reader = new BufferedReader(new InputStreamReader(stream));
50          this.factory = new ValueFactoryImpl();
51          this.pattern = Pattern
52                  .compile("\\s*<(.+)>\\s*<(.+)>\\s*(?:<|\")(.+)(?:>|\")\\s*\\.");
53      }
54  
55      /**
56       * Reads the next available statement in NT file format.
57       * 
58       * @return next statement or null
59       */
60      Statement next() {
61          try {
62              String line = reader.readLine();
63              if (line == null) {
64                  reader.close();
65                  return null;
66              }
67              Matcher match = pattern.matcher(line);
68              if (match.matches() == false || match.groupCount() != 3) {
69                  throw new RuntimeException(
70                          "The input file format is not supported!");
71              }
72              return factory.createStatement(factory.createURI(match.group(1)),
73                      factory.createURI(match.group(2)), match.group(3)
74                              .startsWith("http://") ? factory.createURI(match
75                              .group(3)) : factory.createLiteral(match.group(3)));
76          } catch (IOException e) {
77              throw new RuntimeException("Could not read input stream!", e);
78          }
79      }
80  
81  }