1 7 8 package com.hp.hpl.jena.xmloutput.test; 9 10 import com.hp.hpl.jena.xmloutput.impl.*; 11 import com.hp.hpl.jena.mem.*; 12 import com.hp.hpl.jena.rdf.model.*; 13 import com.hp.hpl.jena.rdf.model.impl.*; 14 import com.hp.hpl.jena.rdf.model.test.*; 15 import com.hp.hpl.jena.vocabulary.RDF; 16 import com.hp.hpl.jena.rdf.arp.*; 17 import com.hp.hpl.jena.graph.*; 18 import com.hp.hpl.jena.shared.*; 19 20 import junit.framework.*; 21 22 import org.apache.oro.text.awk.AwkCompiler; 23 import org.apache.oro.text.awk.AwkMatcher; 24 import org.apache.oro.text.regex.MalformedPatternException; 25 import java.util.*; 26 27 import java.io.*; 28 29 33 34 public class TestXMLFeatures extends ModelTestBase { 35 static AwkCompiler awk = PrettyWriterTest.awk; 36 static AwkMatcher matcher = PrettyWriterTest.matcher; 37 38 40 42 static private class Change { 43 void code(RDFWriter w) { 44 } 45 void code(Model m){ 46 } 47 void code(Model m, RDFWriter w){ 48 code(m); 49 code(w); 50 } 51 } 52 private String base1 = "http://example/foobar"; 53 private String base2 = "http://example/barfoo"; 54 private String file1 = "testing/abbreviated/namespaces.rdf"; 55 private String lang; 56 TestXMLFeatures(String name, String lang) { 57 super(name); 58 this.lang = lang; 59 } 60 public String toString() { 61 return getName() + " " + lang; 62 } 63 64 public static Test suite() { 65 return new TestSuite(TestXMLFeatures.class); 66 } 67 static SimpleLogger realLogger; 68 static boolean sawErrors; 69 static SimpleLogger falseLogger = new SimpleLogger() { 70 71 public void warn(String s) { 72 sawErrors = true; 73 } 74 75 public void warn(String s, Exception e) { 76 sawErrors = true; 77 } 78 }; 79 static void blockLogger() { 80 realLogger = BaseXMLWriter.setLogger(falseLogger); 81 sawErrors = false; 82 } 83 static boolean unblockLogger() { 84 BaseXMLWriter.setLogger(realLogger); 85 return sawErrors; 86 } 87 88 93 public void testBrokenPrefixing() throws Exception 94 { 95 Model m = ModelFactory.createDefaultModel(); 96 m.add( ModelTestBase.statement( m, "a http://bingle.bongle/booty#PP b" ) ); 97 m.add( ModelTestBase.statement( m, "c http://dingle.dongle/dooty#PP d" ) ); 98 StringWriter sw = new StringWriter(); 99 m.write( sw ); 100 Model m2 = ModelFactory.createDefaultModel(); 101 String written = sw.toString(); 102 m2.read( new StringReader( written ), "" ); 103 StringWriter sw2 = new StringWriter(); 104 m2.write( sw2 ); 105 String s2 = sw2.toString(); 106 int first = s2.indexOf( "xmlns:j.0=" ); 107 int last = s2.lastIndexOf( "xmlns:j.0=" ); 108 assertEquals( first, last ); 109 System.out.println( sw2.toString() ); 110 } 111 112 115 public void testNullBaseWithAbbrev() 116 { 117 ModelFactory.createDefaultModel().write( new StringWriter(), lang, null ); 118 } 119 120 public void testBug696057() throws IOException { 121 File f = File.createTempFile("jena", ".rdf"); 122 String fileName = f.getAbsolutePath(); 123 Model m = createMemModel(); 124 m.read( 125 new FileInputStream("testing/wg/rdfms-syntax-incomplete/test001.rdf"), 126 ""); 127 m.write(new FileWriter(fileName), lang); 128 Model m1 = createMemModel(); 129 m1.read(new FileInputStream(fileName), ""); 130 assertTrue("Use of FileWriter", m.isIsomorphicWith(m1)); 131 f.delete(); 132 } 133 134 public void testXMLBase() throws IOException, MalformedPatternException { 135 check(file1, "xml:base=['\"]" + base2 + "['\"]", new Change() { 137 public void code(RDFWriter writer) { 138 String oldvalue = (String ) writer.setProperty("xmlbase", base1); 139 assertTrue("xmlbase valued non-null", oldvalue == null); 140 141 oldvalue = (String ) writer.setProperty("xmlbase", base2); 142 assertEquals("xmlbase valued incorrect.", base1, oldvalue); 143 } 144 145 }); 146 } 147 148 public void testRelativeURI() { 149 Model m = ModelFactory.createDefaultModel(); 150 m.createResource("foo").addProperty( 151 RDF.value, 152 "bar"); 153 m.write(new OutputStream(){ 154 public void write(int b) throws IOException { 155 }},lang); 156 } 157 public void testPropertyURI() throws IOException { 158 doBadPropTest(lang); 159 } 160 165 private void check(String filename, String regex, Change code) 166 throws IOException, MalformedPatternException { 167 check(filename, regex, null, code); 168 } 169 private void check( 170 String filename, 171 String regexPresent, 172 String regexAbsent, 173 Change code) 174 throws IOException, MalformedPatternException { 175 check(filename, null, regexPresent, regexAbsent, false, code); 176 } 177 178 private void check( 179 String filename, 180 String encoding, 181 String regexPresent, 182 String regexAbsent, 183 Change code) 184 throws IOException, MalformedPatternException { 185 check(filename, encoding, regexPresent, regexAbsent, false, code); 186 } 187 private void check( 188 String filename, 189 String regexAbsent, 190 Change code, 191 String base) 192 throws IOException, MalformedPatternException { 193 check(filename,null,regexAbsent,null,false,new Change(){ 194 public void code(RDFWriter w){} 195 },base); 196 check(filename, null, null, regexAbsent, false, code, base); 197 } 198 private void check( 199 String filename, 200 String encoding, 201 String regexPresent, 202 String regexAbsent, 203 boolean errs, 204 Change code) 205 throws IOException, MalformedPatternException { 206 check(filename, encoding, regexPresent, regexAbsent, errs, code, "file:"+filename); 207 } 208 private void check( 209 String filename, 210 String encoding, 211 String regexPresent, 212 String regexAbsent, 213 boolean errorExpected, 214 Change code, 215 String base) 216 throws IOException, MalformedPatternException { 217 blockLogger(); 219 boolean errorsFound; 220 Model m = createMemModel(); 221 InputStream in = new FileInputStream(filename); 222 m.read(in,base); 223 in.close(); 224 Writer sw; 226 ByteArrayOutputStream bos = null; 227 if (encoding == null) 228 sw = new StringWriter(); 229 else { 230 bos = new ByteArrayOutputStream(); 231 sw = new OutputStreamWriter(bos, encoding); 232 } 233 Properties p = (Properties) System.getProperties().clone(); 234 RDFWriter writer = m.getWriter(lang); 235 code.code( m, writer ); 236 writer.write( m, sw, base ); 237 sw.close(); 238 239 String contents; 240 if (encoding == null) 241 contents = sw.toString(); 242 else { 243 contents = bos.toString(encoding); 244 } 245 try { 246 Model m2 = createMemModel(); 247 m2.read(new StringReader(contents), base); 248 assertTrue("Data got changed.",m.isIsomorphicWith(m2)); 249 if (regexPresent != null) 250 assertTrue( 251 "Should find /" + regexPresent + "/", 252 matcher.contains(contents, awk.compile(regexPresent))); 253 if (regexAbsent != null) 254 assertTrue( 255 "Should not find /" + regexAbsent + "/", 256 !matcher.contains(contents, awk.compile(regexAbsent))); 257 contents = null; 258 } finally { 259 errorsFound = unblockLogger(); 260 System.setProperties(p); 261 if (contents != null) { 262 System.err.println("==================="); 263 System.err.println("Offending content - " + toString()); 264 System.err.println("==================="); 265 System.err.println(contents); 266 System.err.println("==================="); 267 } 268 } 269 assertEquals("Errors (not) detected.", errorExpected, errorsFound); 270 271 } 272 273 void doBadPropTest(String lang) throws IOException { 274 Model m = createMemModel(); 275 m.add( 276 m.createResource(), 277 m.createProperty("http://example/", "foo#"), 278 "foo"); 279 File file = File.createTempFile("rdf", ".xml"); 280 282 FileOutputStream fwriter = new FileOutputStream(file); 283 try { 284 m.write(fwriter, lang); 285 fwriter.close(); 286 fail("Writer did not detect bad property URI"); 287 } catch (InvalidPropertyURIException je) { 288 } 290 file.delete(); 291 } 292 293 public void testUseNamespace() 294 throws IOException, MalformedPatternException { 295 check(file1, "xmlns:eg=['\"]http://example.org/#['\"]", new Change() { 296 public void code(Model m) { 297 m.setNsPrefix("eg", "http://example.org/#"); 298 } 299 }); 300 } 301 302 public void testSingleQuote() 303 throws IOException, MalformedPatternException { 304 check(file1, "'","\"", new Change() { 305 public void code(RDFWriter writer) { 306 writer.setProperty("attributeQuoteChar", "'"); 307 } 308 }); 309 } 310 public void testDoubleQuote() 311 throws IOException, MalformedPatternException { 312 check(file1, "\"","'", new Change() { 313 public void code(RDFWriter writer) { 314 writer.setProperty("attributeQuoteChar", "\""); 315 } 316 }); 317 } 318 319 public void testUseDefaultNamespace() 320 throws IOException, MalformedPatternException { 321 check(file1, "xmlns=['\"]http://example.org/#['\"]", new Change() { 322 public void code( Model m ) { 323 m.setNsPrefix("", "http://example.org/#"); 324 } 325 }); 326 } 327 328 public void testUseUnusedNamespace() 329 throws IOException, MalformedPatternException { 330 check(file1, "xmlns:unused=['\"]http://unused.org/#['\"]", new Change() { 331 public void code( Model m ) { 332 m.setNsPrefix( "unused", "http://unused.org/#"); 333 } 334 }); 335 } 336 337 public void testRDFNamespace() 338 throws IOException, MalformedPatternException { 339 check( 340 file1, 341 "xmlns:r=['\"]" + RDF.getURI() + "['\"]", 342 "rdf:", 343 new Change() { 344 public void code( Model m ) { 345 m.setNsPrefix("r", RDF.getURI()); 346 } 347 }); 348 } 349 350 public void testTab() 351 throws IOException, MalformedPatternException { 352 check( 353 file1, 354 " ", 355 new Change() { 357 public void code(RDFWriter writer) { 358 writer.setProperty("tab", "5"); 359 } 360 }); 361 } 362 public void testNoTab() 363 throws IOException, MalformedPatternException { 364 check( 365 file1, 366 " ", 367 new Change() { 369 public void code(RDFWriter writer) { 370 writer.setProperty("tab", "0"); 371 } 372 }); 373 } 374 public void testNoLiteral() 375 throws IOException, MalformedPatternException { 376 check( 377 "testing/wg/rdfms-xml-literal-namespaces/test001.rdf", 378 "#XMLLiteral", 379 "[\"']Literal[\"']", 380 new Change() { 381 public void code(RDFWriter writer) { 382 writer.setProperty("blockrules", "parseTypeLiteralPropertyElt"); 383 } 384 }); 385 } 386 public void testNoPropAttr() 387 throws IOException, MalformedPatternException { 388 check( 389 file1, 390 null, 391 "prop1=", 392 new Change() { 393 public void code(RDFWriter writer) { 394 writer.setProperty("blockrules", "propertyAttr"); 395 } 396 }); 397 } 398 public void testNoDamlCollection() 399 throws IOException, MalformedPatternException { 400 check( 401 "testing/abbreviated/daml.rdf", 402 null, 403 "[\"']daml:collection[\"']", 404 new Change() { 405 public void code(RDFWriter writer) { 406 writer.setProperty("blockrules", "daml:collection"); 407 } 408 }); 409 } 410 public void testNoRdfCollection() 411 throws IOException, MalformedPatternException { 412 check( 413 "testing/abbreviated/collection.rdf", 414 null, 415 "[\"']Collection[\"']", 416 new Change() { 417 public void code(RDFWriter writer) { 418 writer.setProperty("blockrules", "parseTypeCollectionPropertyElt"); 419 } 420 }); 421 } 422 public void testNoLi() 423 throws IOException, MalformedPatternException { 424 check( 425 "testing/abbreviated/container.rdf", 426 null, 427 "rdf:li", 428 new Change() { 429 public void code(RDFWriter writer) { 430 writer.setProperty("blockrules", "section-List-Expand"); 431 } 432 }); 433 } 434 public void testNoCookUp() 435 throws IOException, MalformedPatternException { 436 check( 437 "testing/abbreviated/cookup.rdf", 438 null, 439 "j.cook.up", 440 new Change() { 441 public void code(RDFWriter writer) { 442 writer.setProperty("blockrules", ""); 443 } 444 }); 445 } 446 public void testNoPropAttrs() 447 throws IOException, MalformedPatternException { 448 check( 449 "testing/abbreviated/namespaces.rdf", 450 null, 451 ":prop0 *=", 452 new Change() { 453 public void code(RDFWriter writer) { 454 } 455 }); 456 } 457 public void testPropAttrs() 458 throws IOException, MalformedPatternException { 459 check( 460 "testing/abbreviated/namespaces.rdf", 461 ":prop0 *=", 462 null, 463 new Change() { 464 public void code(RDFWriter writer) { 465 writer.setProperty("blockrules", ""); 466 } 467 }); 468 } 469 public void testNoID() 470 throws IOException, MalformedPatternException { 471 check( 472 "testing/abbreviated/container.rdf", 473 "rdf:ID", 474 new Change() { 475 public void code(RDFWriter writer) { 476 writer.setProperty("blockrules", "idAttr"); 477 } 478 }, 479 "http://example.org/foo" 480 ); 481 } 482 public void testNoID2() 483 throws IOException, MalformedPatternException { 484 check( 485 "testing/abbreviated/container.rdf", 486 "rdf:ID", 487 new Change() { 488 public void code(RDFWriter writer) { 489 writer.setProperty("blockrules", "idAttr"); 490 } 491 }, 492 "http://example.org/foo#" 493 ); 494 } 495 public void testNoResource() 496 throws IOException, MalformedPatternException { 497 check( 498 "testing/abbreviated/container.rdf", 499 "['\"]Resource[\"']", 500 new Change() { 501 public void code(RDFWriter writer) { 502 writer.setProperty("blockrules", "parseTypeResourcePropertyElt"); 503 } 504 }, 505 "http://example.org/foo#" 506 ); 507 } 508 public void testNoReification() 509 throws IOException, MalformedPatternException { 510 String filename = "testing/abbreviated/reification.rdf"; 512 String base = "http://example.org/foo"; 513 518 check (filename, null, "rdf:subject",null, false, 519 new Change() { 520 public void code(RDFWriter writer) { 521 writer.setProperty("blockrules", "section-Reification"); 522 } 523 }, base); 524 525 } 526 public void testNoStripes() 527 throws IOException, MalformedPatternException { 528 check( 529 "testing/abbreviated/collection.rdf", 530 " <[a-zA-Z][-a-zA-Z0-9._]*:Class", 531 new Change() { 532 public void code(RDFWriter writer) { 533 writer.setProperty("blockrules", "resourcePropertyElt"); 534 } 535 }, 536 "http://example.org/foo" 537 ); 538 } 539 540 public void testRDFDefaultNamespace() 541 throws IOException, MalformedPatternException { 542 check( 543 file1, 544 "xmlns=['\"]" 545 + RDF.getURI() 546 + "['\"].*" 547 + "xmlns:j.cook.up=['\"]" 548 + RDF.getURI() 549 + "['\"]", 550 new Change() { 551 public void code( Model m ) { 552 m.setNsPrefix("", RDF.getURI()); 553 } 554 }); 555 } 556 557 public void testBadPrefixNamespace() 558 throws IOException, MalformedPatternException { 559 } 566 567 public void testDuplicateNamespace() 568 throws IOException, MalformedPatternException { 569 check( 570 file1, 571 "xmlns:eg[12]=['\"]http://example.org/#['\"]", 572 "xmlns:eg[12]=['\"]http://example.org/#['\"].*xmlns:eg[12]=['\"]http://example.org/#['\"]", 573 new Change() { 574 public void code( Model m ) { 575 m.setNsPrefix("eg1", "http://example.org/#"); 576 m.setNsPrefix("eg2", "http://example.org/#"); 577 } 578 }); 579 } 580 581 public void testDuplicatePrefix() 582 throws IOException, MalformedPatternException { 583 check( 584 file1, 585 "xmlns:eg=['\"]http://example.org/file[12]#['\"]", 586 null, 587 new Change() { 588 public void code( Model m ) { 589 m.setNsPrefix("eg", "http://example.org/file1#"); 590 m.setNsPrefix("eg", "http://example.org/file2#"); 591 } 592 }); 593 } 594 595 void setNsPrefixSysProp(String prefix, String uri) { 596 System.setProperty(RDFWriter.NSPREFIXPROPBASE + uri, prefix); 597 } 598 599 public void testUseNamespaceSysProp() 600 throws IOException, MalformedPatternException { 601 check(file1, "xmlns:eg=['\"]http://example.org/#['\"]", new Change() { 602 public void code(RDFWriter writer) { 603 setNsPrefixSysProp("eg", "http://example.org/#"); 604 } 605 }); 606 } 607 608 public void testDefaultNamespaceSysProp() 609 throws IOException, MalformedPatternException { 610 check(file1, "xmlns=['\"]http://example.org/#['\"]", new Change() { 611 public void code(RDFWriter writer) { 612 setNsPrefixSysProp("", "http://example.org/#"); 613 } 614 }); 615 } 616 617 public void testDuplicateNamespaceSysProp() 618 throws IOException, MalformedPatternException { 619 check( 620 file1, 621 "xmlns:eg[12]=['\"]http://example.org/#['\"]", 622 "xmlns:eg[12]=['\"]http://example.org/#['\"].*xmlns:eg[12]=['\"]http://example.org/#['\"]", 623 new Change() { 624 625 public void code(RDFWriter writer) { 626 setNsPrefixSysProp("eg1", "http://example.org/#"); 627 setNsPrefixSysProp("eg2", "http://example.org/#"); 628 } 629 }); 630 } 631 632 public void testDuplicatePrefixSysProp() 633 throws IOException, MalformedPatternException { 634 check( 635 file1, 636 "xmlns:eg=['\"]http://example.org/file[12]#['\"]", 637 null, 638 new Change() { 639 public void code(RDFWriter writer) { 640 setNsPrefixSysProp("eg", "http://example.org/file1#"); 641 setNsPrefixSysProp("eg", "http://example.org/file2#"); 642 } 643 }); 644 } 645 646 public void testDuplicatePrefixSysPropAndExplicit() 647 throws IOException, MalformedPatternException { 648 check( 649 file1, 650 "xmlns:eg=['\"]http://example.org/file[12]#['\"]", 651 null, 652 new Change() { 653 public void code( Model m ) { 654 m.setNsPrefix("eg", "http://example.org/file1#"); 655 setNsPrefixSysProp("eg", "http://example.org/file2#"); 656 } 657 }); 658 } 659 public void testUTF8DeclAbsent() 660 throws IOException, MalformedPatternException { 661 check(file1, "utf-8", null, "<\\?xml", new Change() { 662 public void code(RDFWriter writer) { 663 } 664 }); 665 666 } 667 668 public void testUTF16DeclAbsent() 669 throws IOException, MalformedPatternException { 670 check(file1, "utf-16", null, "<\\?xml", false, new Change() { 671 public void code(RDFWriter writer) { 672 } 673 }); 674 } 675 676 public void testUTF8DeclPresent() 677 throws IOException, MalformedPatternException { 678 check(file1, "utf-8", "<\\?xml", null, new Change() { 679 public void code(RDFWriter writer) { 680 writer.setProperty("showXmlDeclaration", Boolean.TRUE); 681 } 682 }); 683 } 684 685 public void testUTF16DeclPresent() 686 throws IOException, MalformedPatternException { 687 check(file1, "utf-16", "<\\?xml", null, new Change() { 688 public void code(RDFWriter writer) { 689 writer.setProperty("showXmlDeclaration", Boolean.TRUE); 690 } 691 }); 692 } 693 694 public void testISO8859_1_DeclAbsent() 695 throws IOException, MalformedPatternException { 696 check(file1, "iso-8859-1", null, "<\\?xml", new Change() { 697 public void code(RDFWriter writer) { 698 writer.setProperty("showXmlDeclaration", Boolean.FALSE); 699 } 700 }); 701 } 702 703 public void testISO8859_1_DeclPresent() 704 throws IOException, MalformedPatternException { 705 check( 706 file1, 707 "iso-8859-1", 708 "<\\?xml[^?]*ISO-8859-1", 709 null, 710 new Change() { 711 public void code(RDFWriter writer) { 712 } 713 }); 714 } 715 716 public void testStringDeclAbsent() 717 throws IOException, MalformedPatternException { 718 check(file1, null, "<\\?xml", new Change() { 719 public void code(RDFWriter writer) { 720 } 721 }); 722 } 723 724 public void testStringDeclPresent() 725 throws IOException, MalformedPatternException { 726 727 check(file1, "<\\?xml", "encoding", new Change() { 728 public void code(RDFWriter writer) { 729 writer.setProperty("showXmlDeclaration", Boolean.TRUE); 730 } 731 }); 732 } 733 static final int BadPropURI = 1; 734 static final int NoError = 0; 735 static final int ExtraTriples = 2; 736 static final int BadURI = 3; 737 738 public void checkPropURI(String s, String p, Object val, int behaviour) 739 throws IOException { 740 blockLogger(); 745 Node blank = Node.createAnon(); 746 Node prop = Node.createURI(s); 747 Graph g = new GraphMem(); 748 g.add( Triple.create( blank, prop, blank ) ); 749 Model m = ModelFactory.createModelForGraph(g); 751 StringWriter w = new StringWriter(); 753 RDFWriter rw = m.getWriter(lang); 754 if (p != null) 755 rw.setProperty(p, val); 756 try { 757 rw.write(m, w, "http://example.org/"); 758 w.close(); 759 String f = w.toString(); 760 761 switch (behaviour) { 762 case BadPropURI : 763 fail("Bad property URI <" + s + "> was not detected."); 764 case BadURI : 765 fail("Bad URI <" + s + "> was not detected."); 766 } 767 Model m2 = createMemModel(); 769 RDFReader rdr = m2.getReader("RDF/XML"); 770 rdr.setProperty("error-mode","lax"); 771 rdr.read(m2,new StringReader(f), "http://example.org/" ); 772 774 switch (behaviour) { 776 case ExtraTriples : 777 assertTrue("Expecting Brickley behaviour.", m2.size() == 3); 778 break; 779 case NoError : 780 assertTrue( 781 "Comparing Model written out and read in.", 782 m.isIsomorphicWith(m2)); 783 break; 784 785 } 786 } catch (BadURIException e) { 787 if (behaviour == BadURI) return; 788 throw e; 789 } catch (InvalidPropertyURIException je) { 790 if (behaviour == BadPropURI) return; 791 throw je; 792 } catch (JenaException e) { 793 throw e; 794 } finally { 795 unblockLogger(); 798 } 800 } 801 802 public void testBadURIAsProperty1() throws IOException { 803 try 804 { 805 checkPropURI("_:aa", null, null, BadURI); 807 } 808 finally 809 { } 811 } 812 813 public void testBadURIAsProperty2() throws IOException { 814 try 815 { 816 checkPropURI("_:aa", "allowBadURIs", "true", NoError); 818 } 819 finally 820 { } 822 } 823 824 public void testLiAsProperty1() throws IOException { 825 checkPropURI(RDF.getURI()+"li", null, null, BadPropURI); 826 } 827 832 public void testDescriptionAsProperty()throws IOException { 833 checkPropURI(RDF.getURI()+"Description", null, null, BadPropURI); 834 } 835 836 public void testBadProperty1() throws IOException { 837 checkPropURI("http:/a.b/", null, null, BadPropURI); 838 } 839 844 public void testRelativeAPI() { 845 RDFWriter w = createMemModel().getWriter(lang); 846 String old = (String ) w.setProperty("relativeURIs", ""); 847 assertEquals( 848 "default value check", 849 old, 850 "same-document, absolute, relative, parent"); 851 w.setProperty("relativeURIs", "network, grandparent,relative, "); 852 w.setProperty( 853 "relativeURIs", 854 " parent, same-document, network, parent, absolute "); 855 blockLogger(); 857 w.setProperty("relativeURIs", "foo"); assertTrue("A warning should have been generated.", unblockLogger()); 859 } 860 private void relative( 861 String relativeParam, 862 String base, 863 Collection regexesPresent, 864 Collection regexesAbsent) 865 throws IOException, MalformedPatternException { 866 867 Model m = createMemModel(); 868 m.read("file:testing/abbreviated/relative-uris.rdf"); 869 870 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 871 RDFWriter writer = m.getWriter(lang); 872 writer.setProperty("relativeURIs", relativeParam); 873 writer.write(m, bos, base); 874 bos.close(); 875 876 String contents = bos.toString("UTF8"); 877 boolean errorsFound; 878 try { 879 Model m2 = createMemModel(); 880 m2.read(new StringReader(contents), base); 881 assertTrue(m.isIsomorphicWith(m2)); 882 Iterator it = regexesPresent.iterator(); 883 while (it.hasNext()) { 884 String regexPresent = (String ) it.next(); 885 assertTrue( 886 "Looking for /" + regexPresent + "/", 887 matcher.contains( 888 contents, 889 awk.compile( 890 Util.substituteStandardEntities(regexPresent)))); 891 } 892 it = regexesAbsent.iterator(); 893 while (it.hasNext()) { 894 String regexAbsent = (String ) it.next(); 895 assertTrue( 896 "Looking for (not) /" + regexAbsent + "/", 897 !matcher.contains( 898 contents, 899 awk.compile( 900 "[\"']" 901 + Util.substituteStandardEntities(regexAbsent) 902 + "[\"']"))); 903 } 904 contents = null; 905 } finally { 906 if (contents != null) { 907 System.err.println("==================="); 908 System.err.println("Offending content - " + toString()); 909 System.err.println("==================="); 910 System.err.println(contents); 911 System.err.println("==================="); 912 } 913 } 914 } 915 static String rData1[][] = { 916 { 918 "", 919 "http://www.example.org/a/b/c/d/", 920 "http://www.example.org/a/b/c/d/e/f/g/", 921 "http://www.example.org/a/b/C/D", 922 "http://www.example.org/A/B#foo/", 923 "http://www.example.org/a/b/c/d/X#bar", 924 "http://example.com/A", 925 "http://www.example.org/a/b/c/d/z[?]x=a", 926 }, 927 { 928 "same-document", "", null, null, null, null, null, null, }, { 929 "absolute", 930 "/a/b/c/d/", 931 "/a/b/c/d/e/f/g/", 932 "/a/b/C/D", 933 "/A/B#foo/", 934 "/a/b/c/d/X#bar", 935 null, 936 "/a/b/c/d/z[?]x=a", 937 }, 938 { 939 "relative", 940 "[.]", 941 "e/f/g/", 942 null, 943 null, 944 "X#bar", 945 null, 946 "z[?]x=a", 947 }, 948 { 949 "parent", 950 "[.][.]/d/", 951 "[.][.]/d/e/f/g/", 952 null, 953 null, 954 "[.][.]/d/X#bar", 955 null, 956 "[.][.]/d/z[?]x=a", 957 }, 958 { 959 "network", 960 "//www.example.org/a/b/c/d/", 961 "//www.example.org/a/b/c/d/e/f/g/", 962 "//www.example.org/a/b/C/D", 963 "//www.example.org/A/B#foo/", 964 "//www.example.org/a/b/c/d/X#bar", 965 "//example.com/A", 966 "//www.example.org/a/b/c/d/z[?]x=a", 967 }, 968 { 969 "grandparent", 970 "[.][.]/[.][.]/c/d/", 971 "[.][.]/[.][.]/c/d/e/f/g/", 972 "[.][.]/[.][.]/C/D", 973 null, 974 "[.][.]/[.][.]/c/d/X#bar", 975 null, 976 "[.][.]/[.][.]/c/d/z[?]x=a", 977 }, 978 }; 979 static String rData2[][] = { 980 981 { 983 "", 984 "http://www.example.org/a/b/c/d/", 985 "http://www.example.org/a/b/c/d/e/f/g/", 986 "http://www.example.org/a/b/C/D", 987 "http://www.example.org/A/B#foo/", 988 "http://www.example.org/a/b/c/d/X#bar", 989 "http://example.com/A", 990 "http://www.example.org/a/b/c/d/z[?]x=a", 991 }, 992 { 993 "same-document", null, null, null, null, null, null, null, }, { 994 "absolute", 995 "/a/b/c/d/", 996 "/a/b/c/d/e/f/g/", 997 "/a/b/C/D", 998 "/A/B#foo/", 999 "/a/b/c/d/X#bar", 1000 null, 1001 "/a/b/c/d/z[?]x=a", 1002 }, 1003 { 1004 "relative", 1005 "d/", 1006 "d/e/f/g/", 1007 null, 1008 null, 1009 "d/X#bar", 1010 null, 1011 "d/z[?]x=a", 1012 }, 1013 { 1014 "parent", 1015 "[.][.]/c/d/", 1016 "[.][.]/c/d/e/f/g/", 1017 "[.][.]/C/D", 1018 null, 1019 "[.][.]/c/d/X#bar", 1020 null, 1021 "[.][.]/c/d/z[?]x=a", 1022 }, 1023 { 1024 "network", 1025 "//www.example.org/a/b/c/d/", 1026 "//www.example.org/a/b/c/d/e/f/g/", 1027 "//www.example.org/a/b/C/D", 1028 "//www.example.org/A/B#foo/", 1029 "//www.example.org/a/b/c/d/X#bar", 1030 "//example.com/A", 1031 "//www.example.org/a/b/c/d/z[?]x=a", 1032 }, 1033 { 1034 "grandparent", 1035 "[.][.]/[.][.]/b/c/d/", 1036 "[.][.]/[.][.]/b/c/d/e/f/g/", 1037 "[.][.]/[.][.]/b/C/D", 1038 null, 1039 "[.][.]/[.][.]/b/c/d/X#bar", 1040 null, 1041 "[.][.]/[.][.]/b/c/d/z[?]x=a", 1042 }, 1043 }; 1044 static String rData3[][] = { 1045 { 1047 "", 1048 "http://www.example.org/a/b/c/d/", 1049 "http://www.example.org/a/b/c/d/e/f/g/", 1050 "http://www.example.org/a/b/C/D", 1051 "http://www.example.org/A/B#foo/", 1052 "http://www.example.org/a/b/c/d/X#bar", 1053 "http://example.com/A", 1054 "http://www.example.org/a/b/c/d/z[?]x=a", 1055 }, 1056 { 1057 "same-document", null, null, null, "#foo/", null, null, null, }, { 1058 "absolute", 1059 "/a/b/c/d/", 1060 "/a/b/c/d/e/f/g/", 1061 "/a/b/C/D", 1062 "/A/B#foo/", 1063 "/a/b/c/d/X#bar", 1064 null, 1065 "/a/b/c/d/z[?]x=a", 1066 }, 1067 { 1068 "relative", null, null, null, "B#foo/", null, null, null, }, { 1069 "parent", 1070 "[.][.]/a/b/c/d/", 1071 "[.][.]/a/b/c/d/e/f/g/", 1072 "[.][.]/a/b/C/D", 1073 "[.][.]/A/B#foo/", 1074 "[.][.]/a/b/c/d/X#bar", 1075 null, 1076 "[.][.]/a/b/c/d/z[?]x=a", 1077 }, 1078 { 1079 "network", 1080 "//www.example.org/a/b/c/d/", 1081 "//www.example.org/a/b/c/d/e/f/g/", 1082 "//www.example.org/a/b/C/D", 1083 "//www.example.org/A/B#foo/", 1084 "//www.example.org/a/b/c/d/X#bar", 1085 "//example.com/A", 1086 "//www.example.org/a/b/c/d/z[?]x=a", 1087 }, 1088 { 1089 "grandparent", null, null, null, null, null, null, null, }, }; 1090 private void relative(int i, String base, String d[][]) 1091 throws IOException, MalformedPatternException { 1092 Set in = new HashSet(); 1093 Set out = new HashSet(); 1094 for (int j = 1; j < d[i].length; j++) { 1095 1096 in.add(d[i][j] == null ? d[0][j] : d[i][j]); 1097 if (i != 0 && d[i][j] != null) 1098 out.add(d[0][j]); 1099 } 1100 relative(d[i][0], base, in, out); 1102 1103 } 1104 1105 public void testRelative() throws Exception { 1106 for (int i = 0; i < 7; i++) { 1107 relative(i, "http://www.example.org/a/b/c/d/", rData1); 1108 relative(i, "http://www.example.org/a/b/c/d", rData2); 1109 relative(i, "http://www.example.org/A/B#", rData3); 1110 } 1111 } 1112 private static String uris[] = 1113 { 1114 "http://www.example.org/a/b/c/d/", 1115 "http://www.example.org/a/b/c/d/e/f/g/", 1116 "http://www.example.org/a/b/C/D", 1117 "http://www.example.org/A/B#foo/", 1118 "http://www.example.org/a/b/c/d/X#bar", 1119 "http://example.com/A", 1120 "http://www.example.org/a/b/c/d/z?x=a", 1121 }; 1122 1123 static public void main(String args[]) throws Exception { 1124 String b[] = 1125 { 1126 "http://www.example.org/a/b/c/d/", 1127 "http://www.example.org/a/b/c/d", 1128 "http://www.example.org/A/B#", 1129 }; 1130 String n[] = 1131 { 1132 "", 1133 "same-document", 1134 "absolute", 1135 "relative", 1136 "parent", 1137 "network", 1138 "grandparent" }; 1139 for (int k = 0; k < b.length; k++) { 1140 System.out.println("// " + b[k]); 1141 URI bb = new URI(b[k]); 1142 1143 for (int i = 0; i < n.length; i++) { 1144 System.out.print(" { \"" + n[i] + "\", "); 1145 int f = BaseXMLWriter.str2flags(n[i]); 1146 for (int j = 0; j < uris.length; j++) { 1147 String r = bb.relativize(uris[j], f); 1148 System.out.print( 1149 (i != 0 && r.equals(uris[j])) 1150 ? "null, " 1151 : "\"" + r + "\"" + ", "); 1152 } 1153 System.out.println("},"); 1154 } 1155 } 1156 } 1157} 1158 1186 | Popular Tags |