DlgpWriter extends AbstractGraalWriter { public DlgpWriter write(Directive d) throws IOException { switch (d.getType()) { case UNA: this.write("@una"); break; case BASE: if(!d.getValue().toString().isEmpty()) { this.base = d.getValue().toString(); this.write("@base "); this.write('<'); this.write(encode(this.base)); this.write('>'); } break; case TOP: if (d.getValue() instanceof Predicate) { this.top = (Predicate) d.getValue(); } else { this.top = new Predicate(d.getValue().toString(), 1); } this.write("@top "); this.writePredicate(this.top); break; case COMMENT: this.write("%% "); this.write(d.getValue()); break; } this.write("\n"); return this; } DlgpWriter();  DlgpWriter(OutputStream out);  DlgpWriter(Writer out);  DlgpWriter(File file);  DlgpWriter(String path); DlgpWriter write(Directive d); @Override DlgpWriter writeComment(String comment); @Override DlgpWriter write(AtomSet atomset); @Override DlgpWriter write(Atom atom); @Override DlgpWriter write(Rule rule); @Override DlgpWriter write(NegativeConstraint constraint); @Override DlgpWriter write(ConjunctiveQuery query); @Override DlgpWriter write(Prefix prefix); static String writeToString(Object o);  }
@Test public void writeConstant() throws IOException { Term a = DefaultTermFactory.instance().createConstant("A"); ByteArrayOutputStream os = new ByteArrayOutputStream(); DlgpWriter writer = new DlgpWriter(os); writer.write(new DefaultAtom(predicat, a)); writer.flush(); String s = new String(os.toByteArray(),"UTF-8"); writer.close(); char c = s.charAt(s.indexOf("(") + 1); Assert.assertTrue("Constant label does not begin with lower case.", Character.isLowerCase(c) || c == '<'); }
@Test public void writeVariable() throws IOException { Term x = DefaultTermFactory.instance().createVariable("x"); ByteArrayOutputStream os = new ByteArrayOutputStream(); DlgpWriter writer = new DlgpWriter(os); writer.write(new DefaultAtom(predicat, x)); writer.flush(); String s = new String(os.toByteArray(),"UTF-8"); writer.close(); Assert.assertTrue("Variable label does not begin with upper case.", Character.isUpperCase(s.charAt(s.indexOf("(") + 1))); }
@Test public void writeURIForbiddenChars() throws IOException { Predicate p = new Predicate(new DefaultURI("/< >\"{}|^`\\/"), 1); ByteArrayOutputStream os = new ByteArrayOutputStream(); DlgpWriter writer = new DlgpWriter(os); writer.write(new DefaultAtom(p, cst)); writer.flush(); String s = new String(os.toByteArray(),"UTF-8"); writer.close(); System.out.println(s); Assert.assertEquals("</\\u003c\\u0020\\u003e\\u0022\\u007b\\u007d\\u007c\\u005e\\u0060\\u005c/>(<A>).\n", s); }
@Test public void writeValideURI() throws IOException { String uri = "/a "abcdefghijklmnopqrstuvwxyz" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "´áéíóúýàèìòùỳ¨äëïöüÿâêîôûŷ" + "?¿.:,;!¡~'" + "0123456789₀₁₂₃₄₅₆₇₈₉₍₎₊₋₌ₐₑₒₓₔ⁰¹⁴⁵⁶⁷⁸⁹⁺⁽⁾⁼⁻" + "+-*%=()[]«»_—–#" + "ßæœçåÅøØĐħĦ" + "€$¤£" + "¶¦¬©®™ªº♯♮♭" + "ΑΒΔΕΦΓΗΙΘΚΛΜΝΟΠΧΡΣΤΥΩΞΨΖαβδεφγηιθκλμνοπχρστυωξψζ" + "‰≃≠≮≯≤≥≰≱≲≳Ω¼½¾ƒℓ⅓⅔⅛⅜⅝⅞⩽⩾←↑→↓↔↦⇒⇔∂∙∏∑∆∇√∞∫≈≡∀∃∈∉∪∩⊂⊃♀♂ℝℂℚℕℤℍ⊥‖∧∨⟦⟧⟨⟩∘" + " " + " Predicate p = new Predicate(new DefaultURI(uri), 1); ByteArrayOutputStream os = new ByteArrayOutputStream(); DlgpWriter writer = new DlgpWriter(os); writer.write(new DefaultAtom(p, cst)); writer.flush(); String s = new String(os.toByteArray(),"UTF-8"); writer.close(); Assert.assertTrue(s.contains("<" + uri + ">(<A>).")); }
@Test public void bug76() throws IOException { Prefix p1 = new Prefix("a", "http: Prefix p2 = new Prefix("b", "http: Predicate p = new Predicate(new DefaultURI("http: ByteArrayOutputStream os = new ByteArrayOutputStream(); DlgpWriter writer = new DlgpWriter(os); writer.write(p1); writer.write(p2); writer.write(new DefaultAtom(p, cst)); writer.flush(); String s = new String(os.toByteArray(),"UTF-8"); writer.close(); Assert.assertTrue(s.contains("b:toto(<A>).")); }
@Test public void bugIllegalCharInPrefixedName() throws IOException { Prefix p1 = new Prefix("a", "http: Predicate p = new Predicate(new DefaultURI("http: ByteArrayOutputStream os = new ByteArrayOutputStream(); DlgpWriter writer = new DlgpWriter(os); writer.write(p1); writer.write(new DefaultAtom(p, cst)); writer.flush(); String s = new String(os.toByteArray(),"UTF-8"); writer.close(); Assert.assertTrue(s.contains("<http: }
@Test public void bugDoubleQuoteInStringLiteral() throws IOException { Literal l = DefaultTermFactory.instance().createLiteral(URIUtils.XSD_STRING, "test\"test"); ByteArrayOutputStream os = new ByteArrayOutputStream(); DlgpWriter writer = new DlgpWriter(os); writer.write(new DefaultAtom(predicat, l)); writer.flush(); String s = new String(os.toByteArray(),"UTF-8"); writer.close(); Assert.assertTrue(s.contains("\"test\\\"test\"")); }
@Test public void emptyAtomSet() throws IOException { ByteArrayOutputStream os = new ByteArrayOutputStream(); DlgpWriter writer = new DlgpWriter(os); writer.write(new LinkedListAtomSet()); writer.write(new LinkedListAtomSet()); writer.flush(); String s = new String(os.toByteArray(),"UTF-8"); writer.close(); Assert.assertFalse(s.contains(".")); }