HDTParser extends AbstractRDFParser { @Override public synchronized void parse(InputStream in, String baseURI) throws IOException, RDFParseException, RDFHandlerException { if (in == null) { throw new IllegalArgumentException("Input stream must not be 'null'"); } if (in instanceof FileInputStream) { } HDTDictionarySection shared = null; HDTDictionarySection subjects = null; HDTDictionarySection predicates = null; HDTDictionarySection objects = null; HDTTriplesSection section = null; CountingInputStream bis = new CountingInputStream(in); try { reportLocation(0, -1); HDTGlobal global = new HDTGlobal(); global.parse(bis); Map<String, String> globalProps = global.getProperties(); String base = globalProps.getOrDefault(HDTGlobal.GLOBAL_BASEURI, ""); if (!base.isEmpty()) { setBaseURI(base); } reportLocation(bis.getByteCount(), -1); HDTHeader header = new HDTHeader(); header.parse(bis); reportLocation(bis.getByteCount(), -1); new HDTDictionary().parse(bis); long dpos = bis.getByteCount(); reportLocation(dpos, -1); shared = HDTDictionarySectionFactory.parse(bis, "S+O", dpos); shared.parse(bis); dpos = bis.getByteCount(); reportLocation(dpos, -1); subjects = HDTDictionarySectionFactory.parse(bis, "S", dpos); subjects.parse(bis); dpos = bis.getByteCount(); reportLocation(dpos, -1); predicates = HDTDictionarySectionFactory.parse(bis, "P", dpos); predicates.parse(bis); dpos = bis.getByteCount(); reportLocation(dpos, -1); objects = HDTDictionarySectionFactory.parse(bis, "O", dpos); objects.parse(bis); reportLocation(bis.getByteCount(), -1); HDTTriples triples = new HDTTriples(); triples.parse(bis); reportLocation(bis.getByteCount(), -1); section = HDTTriplesSectionFactory.parse(new String(HDTTriples.FORMAT_BITMAP)); section.parse(bis, triples.getOrder()); } catch (IOException ioe) { reportFatalError(ioe.getMessage(), bis.getCount(), -1); } finally { bis.close(); } if (rdfHandler != null) { rdfHandler.startRDF(); } int cnt = 0; int size = shared.size(); while (section.hasNext()) { int[] t = section.next(); byte[] s = getSO(t[0], size, shared, subjects); byte[] p = predicates.get(t[1]); byte[] o = getSO(t[2], size, shared, objects); Statement stmt = valueFactory.createStatement(createSubject(s), createPredicate(p), createObject(o)); if (rdfHandler != null) { rdfHandler.handleStatement(stmt); } } if (rdfHandler != null) { rdfHandler.endRDF(); } } HDTParser();  HDTParser(ValueFactory valueFactory); @Override RDFFormat getRDFFormat(); @Override Collection<RioSetting<?>> getSupportedSettings(); @Override synchronized void parse(InputStream in, String baseURI); @Override synchronized void parse(Reader reader, String baseURI);  }
@Test public void parseSimpleSPO() { Model orig = new LinkedHashModel(); try (InputStream is = HDTParserTest.class.getResourceAsStream("/test-orig.nt")) { RDFParser nt = Rio.createParser(RDFFormat.NTRIPLES); nt.setRDFHandler(new StatementCollector(orig)); nt.parse(is, ""); } catch (Exception e) { fail(e.getMessage()); } Model m = new LinkedHashModel(); try (InputStream is = HDTParserTest.class.getResourceAsStream("/test.hdt")) { parser.setRDFHandler(new StatementCollector(m)); parser.parse(is, ""); assertEquals("Number of statements does not match", 43, m.size()); } catch (Exception e) { fail(e.getMessage()); } orig.removeAll(m); assertEquals("HDT model does not match original NT file", 0, orig.size()); }
@Test public void parseSimplePOS() { Model m = new LinkedHashModel(); try (InputStream is = HDTParserTest.class.getResourceAsStream("/test-pos.hdt")) { parser.setRDFHandler(new StatementCollector(m)); parser.parse(is, ""); assertEquals("Number of statements does not match", 43, m.size()); fail("Unsupported not caught"); } catch (Exception e) { assertEquals(e.getMessage(), "Triples section: order 4, but only SPO order is supported"); } }