PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/RDFSlice/src/main/java/org/rdfslice/RDFSliceRDFInputStreamInterator.java

https://bitbucket.org/emarx/rdfslice
Java | 143 lines | 116 code | 26 blank | 1 comment | 23 complexity | 655262f82ba6ffb037adfe8e46a08a5d MD5 | raw file
  1. package org.rdfslice;
  2. import java.io.BufferedInputStream;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.InputStreamReader;
  6. import java.io.LineNumberReader;
  7. import java.nio.charset.Charset;
  8. import java.util.HashMap;
  9. import java.util.Iterator;
  10. import java.util.Map;
  11. import org.apache.log4j.Logger;
  12. import org.rdfslice.model.IStatement;
  13. import org.rdfslice.model.ModelFactory;
  14. import org.rdfslice.model.Statement;
  15. import org.rdfslice.model.Triple;
  16. import org.rdfslice.util.RDFUtil;
  17. import org.rdfslice.util.SystemUtil;
  18. public class RDFSliceRDFInputStreamInterator implements Iterator<Statement> {
  19. static Logger logger = Logger.getLogger(RDFSliceRDFInputStreamInterator.class);
  20. String currentLine = null; // current line of the iterator
  21. InputStream is = null; // RDF iterator file
  22. LineNumberReader br;
  23. BufferedInputStream bIS;
  24. Statement triple;
  25. Statement nextStatement;
  26. Map<String, String> prefixTable;
  27. public RDFSliceRDFInputStreamInterator(InputStream is) throws Exception {
  28. if(is == null) {
  29. throw new Exception("InputStream could not be Null.");
  30. }
  31. this.is = is;
  32. init(is);
  33. }
  34. private void init(InputStream is) throws Exception {
  35. br = new LineNumberReader(new InputStreamReader(is, Charset.forName("UTF-8")));
  36. bIS = new BufferedInputStream(is);
  37. prefixTable = new HashMap<String, String>();
  38. }
  39. @Override
  40. public boolean hasNext() {
  41. try {
  42. return (nextStatement!=null
  43. || readLine()!=null);
  44. } catch (IOException e) {
  45. throw new RuntimeException(e);
  46. }
  47. }
  48. private String readLine() throws IOException {
  49. while((currentLine = br.readLine())!=null && !isValid(currentLine)){}
  50. return currentLine;
  51. }
  52. private boolean isValid(String currentLine) throws IOException {
  53. if(currentLine.length() <= 6)
  54. return false;
  55. boolean comment = (currentLine.charAt(0)=='#');
  56. boolean error = (currentLine.charAt(currentLine.length()-1)!='.');
  57. if(!comment && !error && (currentLine.charAt(currentLine.length()-1)!=IStatement.NTRIPLE_SEPARATOR)) { // to avoid extra empty space
  58. int pos = RDFUtil.getNTripleTeminatorPos(currentLine);
  59. if(pos == -1) {
  60. return false;
  61. }
  62. error = (currentLine.charAt(pos) != IStatement.NTRIPLE_TERMINATOR);
  63. }
  64. if(!comment && error)
  65. logger.warn("Invalid line " + br.getLineNumber() + ": " + currentLine);
  66. return !comment && !error;
  67. }
  68. @Override
  69. public Statement next() {
  70. Statement currentStatement;
  71. if(this.nextStatement!=null) {
  72. currentStatement = this.nextStatement;
  73. this.nextStatement = null;
  74. }
  75. else {
  76. currentStatement = getNextStatement();
  77. }
  78. Statement statement = currentStatement;
  79. while(hasNext() && currentStatement.getSubject().equals((this.nextStatement = getNextStatement()).getSubject())) {
  80. statement.add(nextStatement.get(0));
  81. this.nextStatement = null;
  82. }
  83. return statement;
  84. }
  85. public Statement getNextStatement() {
  86. String line="";
  87. try {
  88. while((currentLine != null && !RDFUtil.isASimpleStatement(currentLine))) {
  89. line += (currentLine) + SystemUtil.getInstance().getLineSeparator();
  90. currentLine = readLine();
  91. }
  92. if(currentLine == null)
  93. throw new Exception("Line could not be null");
  94. line += currentLine;
  95. Triple triple = ModelFactory.getNTriple(line);
  96. Statement statement = new Statement();
  97. statement.add(triple);
  98. if(statement.isPrefix()) {
  99. Triple prefix = (Triple) statement.get(0);
  100. prefixTable.put(prefix.getPredicate(), prefix.getObject());
  101. } else
  102. statement.replacePrefixes(prefixTable);
  103. return statement;
  104. } catch (Exception e) {
  105. logger.error("Error parsing the line: " + line, e);
  106. if(hasNext())
  107. return getNextStatement();
  108. return null;
  109. }
  110. }
  111. @Override
  112. public void remove() {
  113. // for security reasons not supported
  114. }
  115. public void reset() throws IOException {
  116. }
  117. }