1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 package complex.sfx2; 25 26 // import complexlib.ComplexTestCase; 27 import com.sun.star.beans.Pair; 28 import com.sun.star.rdf.Literal; 29 import com.sun.star.rdf.XLiteral; 30 import com.sun.star.rdf.XNamedGraph; 31 import com.sun.star.rdf.BlankNode; 32 import com.sun.star.rdf.XQuerySelectResult; 33 import com.sun.star.rdf.XNode; 34 import com.sun.star.rdf.XDocumentRepository; 35 import com.sun.star.rdf.XMetadatable; 36 import com.sun.star.rdf.Statement; 37 import com.sun.star.rdf.FileFormat; 38 import com.sun.star.rdf.URIs; 39 import com.sun.star.rdf.URI; 40 import com.sun.star.rdf.XDocumentMetadataAccess; 41 import com.sun.star.rdf.XRepositorySupplier; 42 import com.sun.star.rdf.XRepository; 43 import com.sun.star.rdf.XBlankNode; 44 import com.sun.star.rdf.XURI; 45 import helper.StreamSimulator; 46 47 import com.sun.star.uno.UnoRuntime; 48 import com.sun.star.uno.XComponentContext; 49 import com.sun.star.lang.XMultiServiceFactory; 50 import com.sun.star.lang.XComponent; 51 52 import com.sun.star.lang.XServiceInfo; 53 import com.sun.star.lang.IllegalArgumentException; 54 import com.sun.star.lang.WrappedTargetException; 55 import com.sun.star.lang.WrappedTargetRuntimeException; 56 import com.sun.star.beans.XPropertySet; 57 import com.sun.star.beans.PropertyValue; 58 import com.sun.star.beans.StringPair; 59 import com.sun.star.container.XEnumerationAccess; 60 import com.sun.star.container.XEnumeration; 61 import com.sun.star.io.XInputStream; 62 import com.sun.star.util.XCloseable; 63 import com.sun.star.frame.XStorable; 64 import com.sun.star.text.XTextDocument; 65 import com.sun.star.text.XTextRange; 66 import com.sun.star.text.XText; 67 import complex.sfx2.tools.TestDocument; 68 import lib.TestParameters; 69 70 71 import org.junit.After; 72 import org.junit.AfterClass; 73 import org.junit.Before; 74 import org.junit.BeforeClass; 75 import org.junit.Test; 76 import org.openoffice.test.OfficeConnection; 77 import static org.junit.Assert.*; 78 79 /** 80 * Test case for interface com.sun.star.rdf.XDocumentMetadataAccess 81 * Currently, this service is implemented in 82 * sfx2/source/doc/DocumentMetadataAccess.cxx 83 * 84 * Actually, this is not a service, so we need to create a document and 85 * go from there... 86 * 87 * @author mst 88 */ 89 public class DocumentMetadataAccess 90 { 91 XMultiServiceFactory xMSF; 92 XComponentContext xContext; 93 String tempDir; 94 95 String nsRDF = "http://www.w3.org/1999/02/22-rdf-syntax-ns#"; 96 String nsRDFS = "http://www.w3.org/2000/01/rdf-schema#"; 97 String nsPkg="http://docs.oasis-open.org/opendocument/meta/package/common#"; 98 String nsODF ="http://docs.oasis-open.org/opendocument/meta/package/odf#"; 99 100 XURI foo; 101 XURI bar; 102 XURI baz; 103 104 static XURI rdf_type; 105 static XURI rdfs_label; 106 static XURI pkg_Document; 107 static XURI pkg_hasPart; 108 static XURI pkg_MetadataFile; 109 static XURI odf_ContentFile; 110 static XURI odf_StylesFile; 111 static XURI odf_Element; 112 static XBlankNode blank1; 113 static XBlankNode blank2; 114 static XBlankNode blank3; 115 static XBlankNode blank4; 116 static String manifestPath = "manifest.rdf"; 117 static String contentPath = "content.xml"; 118 static String stylesPath = "styles.xml"; 119 static String fooPath = "foo.rdf"; 120 static String fooBarPath = "meta/foo/bar.rdf"; 121 122 XRepository xRep; 123 XRepositorySupplier xRS; 124 XDocumentMetadataAccess xDMA; 125 126 // public String[] getTestMethodNames () 127 // { 128 // return new String[] { "check", "checkRDFa" }; 129 // } 130 /** 131 * The test parameters 132 */ 133 private static TestParameters param = null; 134 before()135 @Before public void before() 136 { 137 try { 138 139 xMSF = getMSF(); 140 param = new TestParameters(); 141 param.put("ServiceFactory", xMSF); // important for param.getMSF() 142 143 assertNotNull("could not create MultiServiceFactory.", xMSF); 144 XPropertySet xPropertySet = UnoRuntime.queryInterface(XPropertySet.class, xMSF); 145 Object defaultCtx = xPropertySet.getPropertyValue("DefaultContext"); 146 xContext = UnoRuntime.queryInterface(XComponentContext.class, defaultCtx); 147 assertNotNull("could not get component context.", xContext); 148 149 tempDir = util.utils.getOfficeTemp/*Dir*/(xMSF); 150 System.out.println("tempdir: " + tempDir); 151 152 foo = URI.create(xContext, "uri:foo"); 153 assertNotNull("foo", foo); 154 bar = URI.create(xContext, "uri:bar"); 155 assertNotNull("bar", bar); 156 baz = URI.create(xContext, "uri:baz"); 157 assertNotNull("baz", baz); 158 159 blank1 = BlankNode.create(xContext, "_:1"); 160 assertNotNull("blank1", blank1); 161 blank2 = BlankNode.create(xContext, "_:2"); 162 assertNotNull("blank2", blank2); 163 blank3 = BlankNode.create(xContext, "_:3"); 164 assertNotNull("blank3", blank3); 165 blank4 = BlankNode.create(xContext, "_:4"); 166 assertNotNull("blank4", blank4); 167 rdf_type = URI.createKnown(xContext, URIs.RDF_TYPE); 168 assertNotNull("rdf_type", rdf_type); 169 rdfs_label = URI.createKnown(xContext, URIs.RDFS_LABEL); 170 assertNotNull("rdfs_label", rdfs_label); 171 pkg_Document = URI.createKnown(xContext, URIs.PKG_DOCUMENT); 172 assertNotNull("pkg_Document", pkg_Document); 173 pkg_hasPart = URI.createKnown(xContext, URIs.PKG_HASPART); 174 assertNotNull("pkg_hasPart", pkg_hasPart); 175 pkg_MetadataFile = URI.createKnown(xContext, URIs.PKG_METADATAFILE); 176 assertNotNull("pkg_MetadataFile", pkg_MetadataFile); 177 odf_ContentFile = URI.createKnown(xContext, URIs.ODF_CONTENTFILE); 178 assertNotNull("odf_ContentFile", odf_ContentFile); 179 odf_StylesFile = URI.createKnown(xContext, URIs.ODF_STYLESFILE); 180 assertNotNull("odf_StylesFile", odf_StylesFile); 181 odf_Element = URI.createKnown(xContext, URIs.ODF_ELEMENT); 182 assertNotNull("odf_Element", odf_Element); 183 184 } catch (Exception e) { 185 report(e); 186 } 187 } 188 after()189 @After public void after() 190 { 191 xRep = null; 192 xRS = null; 193 xDMA = null; 194 } 195 check()196 @Test public void check() 197 { 198 XComponent xComp = null; 199 XComponent xComp2 = null; 200 try { 201 XEnumeration xStmtsEnum; 202 XNamedGraph xManifest; 203 204 System.out.println("Creating document with Repository..."); 205 206 // we cannot create a XDMA directly, we must create 207 // a document and get it from there :( 208 // create document 209 PropertyValue[] loadProps = new PropertyValue[1]; 210 loadProps[0] = new PropertyValue(); 211 loadProps[0].Name = "Hidden"; 212 loadProps[0].Value = true; 213 xComp = util.DesktopTools.openNewDoc(xMSF, "swriter", loadProps); 214 XTextDocument xText = UnoRuntime.queryInterface(XTextDocument.class, xComp); 215 216 XRepositorySupplier xRepoSupplier = UnoRuntime.queryInterface(XRepositorySupplier.class, xComp); 217 assertNotNull("xRS null", xRepoSupplier); 218 XDocumentMetadataAccess xDocMDAccess = UnoRuntime.queryInterface(XDocumentMetadataAccess.class, xRepoSupplier); 219 assertNotNull("xDMA null", xDocMDAccess); 220 xRep = xRepoSupplier.getRDFRepository(); 221 assertNotNull("xRep null", xRep); 222 223 System.out.println("...done"); 224 225 System.out.println("Checking that new repository is initialized..."); 226 227 XURI xBaseURI = (XURI) xDocMDAccess; 228 String baseURI = xBaseURI.getStringValue(); 229 assertNotNull("new: baseURI", xBaseURI ); 230 assertTrue("new: baseURI", !xBaseURI.getStringValue().equals("")); 231 232 assertTrue("new: # graphs", 1 == xRep.getGraphNames().length); 233 XURI manifest = URI.createNS(xContext, xBaseURI.getStringValue(), 234 manifestPath); 235 xManifest = xRep.getGraph(manifest); 236 assertTrue("new: manifest graph", null != xManifest); 237 238 Statement[] manifestStmts = getManifestStmts(xBaseURI); 239 xStmtsEnum = xRep.getStatements(null, null, null); 240 assertTrue("new: manifest graph", eq(xStmtsEnum, manifestStmts)); 241 242 System.out.println("...done"); 243 244 System.out.println("Checking some invalid args..."); 245 246 String content = "behold, for i am the content."; 247 XTextRange xTR = new TestRange(content); 248 XMetadatable xM = (XMetadatable) xTR; 249 250 try { 251 xDocMDAccess.getElementByURI(null); 252 fail("getElementByURI: null allowed"); 253 } catch (IllegalArgumentException e) { 254 // ignore 255 } 256 try { 257 xDocMDAccess.getMetadataGraphsWithType(null); 258 fail("getMetadataGraphsWithType: null URI allowed"); 259 } catch (IllegalArgumentException e) { 260 // ignore 261 } 262 try { 263 xDocMDAccess.addMetadataFile("", new XURI[0]); 264 fail("addMetadataFile: empty filename allowed"); 265 } catch (IllegalArgumentException e) { 266 // ignore 267 } 268 try { 269 xDocMDAccess.addMetadataFile("/foo", new XURI[0]); 270 fail("addMetadataFile: absolute filename allowed"); 271 } catch (IllegalArgumentException e) { 272 // ignore 273 } 274 try { 275 xDocMDAccess.addMetadataFile("fo\"o", new XURI[0]); 276 fail("addMetadataFile: invalid filename allowed"); 277 } catch (IllegalArgumentException e) { 278 // ignore 279 } 280 try { 281 xDocMDAccess.addMetadataFile("../foo", new XURI[0]); 282 fail("addMetadataFile: filename with .. allowed"); 283 } catch (IllegalArgumentException e) { 284 // ignore 285 } 286 try { 287 xDocMDAccess.addMetadataFile("foo/../../bar", new XURI[0]); 288 fail("addMetadataFile: filename with nest .. allowed"); 289 } catch (IllegalArgumentException e) { 290 // ignore 291 } 292 try { 293 xDocMDAccess.addMetadataFile("foo/././bar", new XURI[0]); 294 fail("addMetadataFile: filename with nest . allowed"); 295 } catch (IllegalArgumentException e) { 296 // ignore 297 } 298 try { 299 xDocMDAccess.addMetadataFile("content.xml", new XURI[0]); 300 fail("addMetadataFile: content.xml allowed"); 301 } catch (IllegalArgumentException e) { 302 // ignore 303 } 304 try { 305 xDocMDAccess.addMetadataFile("styles.xml", new XURI[0]); 306 fail("addMetadataFile: styles.xml allowed"); 307 } catch (IllegalArgumentException e) { 308 // ignore 309 } 310 try { 311 xDocMDAccess.addMetadataFile("meta.xml", new XURI[0]); 312 fail("addMetadataFile: meta.xml allowed"); 313 } catch (IllegalArgumentException e) { 314 // ignore 315 } 316 try { 317 xDocMDAccess.addMetadataFile("settings.xml", new XURI[0]); 318 fail("addMetadataFile: settings.xml allowed"); 319 } catch (IllegalArgumentException e) { 320 // ignore 321 } 322 try { 323 xDocMDAccess.importMetadataFile(FileFormat.RDF_XML, null, "foo", 324 foo, new XURI[0]); 325 fail("importMetadataFile: null stream allowed"); 326 } catch (IllegalArgumentException e) { 327 // ignore 328 } 329 330 final String sEmptyRDF = TestDocument.getUrl("empty.rdf"); 331 try { 332 XInputStream xFooIn = new StreamSimulator(sEmptyRDF, true, param); 333 xDocMDAccess.importMetadataFile(FileFormat.RDF_XML, xFooIn, "", 334 foo, new XURI[0]); 335 fail("importMetadataFile: empty filename allowed"); 336 } catch (IllegalArgumentException e) { 337 // ignore 338 } 339 try { 340 XInputStream xFooIn = 341 new StreamSimulator(sEmptyRDF, true, param); 342 xDocMDAccess.importMetadataFile(FileFormat.RDF_XML, xFooIn, "meta.xml", 343 foo, new XURI[0]); 344 fail("importMetadataFile: meta.xml filename allowed"); 345 } catch (IllegalArgumentException e) { 346 // ignore 347 } 348 try { 349 XInputStream xFooIn = 350 new StreamSimulator(sEmptyRDF, true, param); 351 xDocMDAccess.importMetadataFile(FileFormat.RDF_XML, 352 xFooIn, "foo", null, new XURI[0]); 353 fail("importMetadataFile: null base URI allowed"); 354 } catch (IllegalArgumentException e) { 355 // ignore 356 } 357 try { 358 XInputStream xFooIn = 359 new StreamSimulator(sEmptyRDF, true, param); 360 xDocMDAccess.importMetadataFile(FileFormat.RDF_XML, 361 xFooIn, "foo", rdf_type, new XURI[0]); 362 fail("importMetadataFile: non-absolute base URI allowed"); 363 } catch (IllegalArgumentException e) { 364 // ignore 365 } 366 try { 367 xDocMDAccess.removeMetadataFile(null); 368 fail("removeMetadataFile: null URI allowed"); 369 } catch (IllegalArgumentException e) { 370 // ignore 371 } 372 try { 373 xDocMDAccess.addContentOrStylesFile(""); 374 fail("addContentOrStylesFile: empty filename allowed"); 375 } catch (IllegalArgumentException e) { 376 // ignore 377 } 378 try { 379 xDocMDAccess.addContentOrStylesFile("/content.xml"); 380 fail("addContentOrStylesFile: absolute filename allowed"); 381 } catch (IllegalArgumentException e) { 382 // ignore 383 } 384 try { 385 xDocMDAccess.addContentOrStylesFile("foo.rdf"); 386 fail("addContentOrStylesFile: invalid filename allowed"); 387 } catch (IllegalArgumentException e) { 388 // ignore 389 } 390 try { 391 xDocMDAccess.removeContentOrStylesFile(""); 392 fail("removeContentOrStylesFile: empty filename allowed"); 393 } catch (IllegalArgumentException e) { 394 // ignore 395 } 396 try { 397 xDocMDAccess.loadMetadataFromStorage(null, foo, null); 398 fail("loadMetadataFromStorage: null storage allowed"); 399 } catch (IllegalArgumentException e) { 400 // ignore 401 } 402 try { 403 xDocMDAccess.storeMetadataToStorage(null/*, base*/); 404 fail("storeMetadataToStorage: null storage allowed"); 405 } catch (IllegalArgumentException e) { 406 // ignore 407 } 408 try { 409 xDocMDAccess.loadMetadataFromMedium(new PropertyValue[0]); 410 fail("loadMetadataFromMedium: empty medium allowed"); 411 } catch (IllegalArgumentException e) { 412 // ignore 413 } 414 try { 415 xDocMDAccess.storeMetadataToMedium(new PropertyValue[0]); 416 fail("storeMetadataToMedium: empty medium allowed"); 417 } catch (IllegalArgumentException e) { 418 // ignore 419 } 420 421 System.out.println("...done"); 422 423 System.out.println("Checking file addition/removal..."); 424 425 xDocMDAccess.removeContentOrStylesFile(contentPath); 426 xStmtsEnum = xManifest.getStatements(null, null, null); 427 assertTrue("removeContentOrStylesFile (content)", 428 eq(xStmtsEnum, new Statement[] { 429 manifestStmts[0], manifestStmts[2], manifestStmts[4] 430 })); 431 432 xDocMDAccess.addContentOrStylesFile(contentPath); 433 xStmtsEnum = xManifest.getStatements(null, null, null); 434 assertTrue("addContentOrStylesFile (content)", 435 eq(xStmtsEnum, manifestStmts)); 436 437 xDocMDAccess.removeContentOrStylesFile(stylesPath); 438 xStmtsEnum = xManifest.getStatements(null, null, null); 439 assertTrue("removeContentOrStylesFile (styles)", 440 eq(xStmtsEnum, new Statement[] { 441 manifestStmts[0], manifestStmts[1], manifestStmts[3] 442 })); 443 444 xDocMDAccess.addContentOrStylesFile(stylesPath); 445 xStmtsEnum = xManifest.getStatements(null, null, null); 446 assertTrue("addContentOrStylesFile (styles)", 447 eq(xStmtsEnum, manifestStmts)); 448 449 XURI xFoo = URI.createNS(xContext, xBaseURI.getStringValue(), 450 fooPath); 451 Statement xM_BaseHaspartFoo = 452 new Statement(xBaseURI, pkg_hasPart, xFoo, manifest); 453 Statement xM_FooTypeMetadata = 454 new Statement(xFoo, rdf_type, pkg_MetadataFile, manifest); 455 Statement xM_FooTypeBar = 456 new Statement(xFoo, rdf_type, bar, manifest); 457 xDocMDAccess.addMetadataFile(fooPath, new XURI[] { bar }); 458 xStmtsEnum = xManifest.getStatements(null, null, null); 459 assertTrue("addMetadataFile", 460 eq(xStmtsEnum, merge(manifestStmts, new Statement[] { 461 xM_BaseHaspartFoo, xM_FooTypeMetadata, xM_FooTypeBar 462 }))); 463 464 XURI[] graphsBar = xDocMDAccess.getMetadataGraphsWithType(bar); 465 assertTrue("getMetadataGraphsWithType", 466 graphsBar.length == 1 && eq(graphsBar[0], xFoo)); 467 468 469 xDocMDAccess.removeMetadataFile(xFoo); 470 xStmtsEnum = xManifest.getStatements(null, null, null); 471 assertTrue("removeMetadataFile", 472 eq(xStmtsEnum, manifestStmts)); 473 474 System.out.println("...done"); 475 476 System.out.println("Checking mapping..."); 477 478 XEnumerationAccess xTextEnum = UnoRuntime.queryInterface(XEnumerationAccess.class, xText.getText()); 479 Object o = xTextEnum.createEnumeration().nextElement(); 480 XMetadatable xMeta1 = UnoRuntime.queryInterface(XMetadatable.class, o); 481 482 XURI uri; 483 XMetadatable xMeta; 484 xMeta = xDocMDAccess.getElementByURI(xMeta1); 485 assertTrue("getElementByURI: null", null != xMeta); 486 String XmlId = xMeta.getMetadataReference().Second; 487 String XmlId1 = xMeta1.getMetadataReference().Second; 488 assertTrue("getElementByURI: no xml id", !XmlId.equals("")); 489 assertTrue("getElementByURI: different xml id", XmlId.equals(XmlId1)); 490 491 System.out.println("...done"); 492 493 System.out.println("Checking storing and loading..."); 494 495 XURI xFoobar = URI.createNS(xContext, xBaseURI.getStringValue(), 496 fooBarPath); 497 Statement[] metadataStmts = getMetadataFileStmts(xBaseURI, 498 fooBarPath); 499 xDocMDAccess.addMetadataFile(fooBarPath, new XURI[0]); 500 xStmtsEnum = xRep.getStatements(null, null, null); 501 assertTrue("addMetadataFile", 502 eq(xStmtsEnum, merge(manifestStmts, metadataStmts ))); 503 504 Statement xFoobar_FooBarFoo = 505 new Statement(foo, bar, foo, xFoobar); 506 xRep.getGraph(xFoobar).addStatement(foo, bar, foo); 507 xStmtsEnum = xRep.getStatements(null, null, null); 508 assertTrue("addStatement", 509 eq(xStmtsEnum, merge(manifestStmts, merge(metadataStmts, 510 new Statement[] { xFoobar_FooBarFoo })))); 511 512 PropertyValue noMDNoContentFile = new PropertyValue(); 513 noMDNoContentFile.Name = "URL"; 514 noMDNoContentFile.Value = TestDocument.getUrl("CUSTOM.odt"); 515 PropertyValue noMDFile = new PropertyValue(); 516 noMDFile.Name = "URL"; 517 noMDFile.Value = TestDocument.getUrl("TEST.odt"); 518 PropertyValue file = new PropertyValue(); 519 file.Name = "URL"; 520 file.Value = tempDir + "TESTDMA.odt"; 521 /* 522 PropertyValue baseURL = new PropertyValue(); 523 baseURL.Name = "DocumentBaseURL"; 524 baseURL.Value = tempDir + "TMP.odt"; 525 */ 526 PropertyValue mimetype = new PropertyValue(); 527 mimetype.Name = "MediaType"; 528 mimetype.Value = "application/vnd.oasis.opendocument.text"; 529 PropertyValue[] argsEmptyNoContent = { mimetype, noMDNoContentFile}; 530 PropertyValue[] argsEmpty = { mimetype, noMDFile }; 531 PropertyValue[] args = { mimetype, file }; 532 533 xStmtsEnum = xRep.getStatements(null, null, null); 534 XURI[] graphs = xRep.getGraphNames(); 535 536 xDocMDAccess.storeMetadataToMedium(args); 537 538 // this should re-init 539 xDocMDAccess.loadMetadataFromMedium(argsEmptyNoContent); 540 xRep = xRepoSupplier.getRDFRepository(); 541 assertTrue("xRep null", null != xRep); 542 assertTrue("baseURI still tdoc?", 543 !baseURI.equals(xDocMDAccess.getStringValue())); 544 Statement[] manifestStmts2 = getManifestStmts((XURI) xDocMDAccess); 545 xStmtsEnum = xRep.getStatements(null, null, null); 546 // there is no content or styles file in here, so we have just 547 // the package stmt 548 assertTrue("loadMetadataFromMedium (no metadata, no content)", 549 eq(xStmtsEnum, new Statement[] { manifestStmts2[0] })); 550 551 // this should re-init 552 xDocMDAccess.loadMetadataFromMedium(argsEmpty); 553 xRep = xRepoSupplier.getRDFRepository(); 554 assertTrue("xRep null", null != xRep); 555 assertTrue("baseURI still tdoc?", 556 !baseURI.equals(xDocMDAccess.getStringValue())); 557 Statement[] manifestStmts3 = getManifestStmts((XURI) xDocMDAccess); 558 559 xStmtsEnum = xRep.getStatements(null, null, null); 560 assertTrue("loadMetadataFromMedium (no metadata)", 561 eq(xStmtsEnum, manifestStmts3)); 562 563 xDocMDAccess.loadMetadataFromMedium(args); 564 xRep = xRepoSupplier.getRDFRepository(); 565 assertTrue("xRep null", null != xRep); 566 Statement[] manifestStmts4 = getManifestStmts((XURI) xDocMDAccess); 567 Statement[] metadataStmts4 = getMetadataFileStmts((XURI) xDocMDAccess, 568 fooBarPath); 569 570 xStmtsEnum = xRep.getStatements(null, null, null); 571 assertTrue("some graph(s) not reloaded", 572 graphs.length == xRep.getGraphNames().length); 573 574 XURI xFoobar4 = URI.createNS(xContext, xDocMDAccess.getStringValue(), 575 fooBarPath); 576 Statement xFoobar_FooBarFoo4 = 577 new Statement(foo, bar, foo, xFoobar4); 578 assertTrue("loadMetadataFromMedium (re-load)", 579 eq(xStmtsEnum, merge(manifestStmts4, merge(metadataStmts4, 580 new Statement[] { xFoobar_FooBarFoo4 })))); 581 582 System.out.println("...done"); 583 584 System.out.println("Checking storing and loading via model..."); 585 586 String f = tempDir + "TESTPARA.odt"; 587 588 XStorable xStor = UnoRuntime.queryInterface(XStorable.class, xRepoSupplier); 589 590 xStor.storeToURL(f, new PropertyValue[0]); 591 592 xComp2 = util.DesktopTools.loadDoc(xMSF, f, loadProps); 593 594 XDocumentMetadataAccess xDMA2 = UnoRuntime.queryInterface(XDocumentMetadataAccess.class, xComp2); 595 assertTrue("xDMA2 null", null != xDMA2); 596 597 XRepositorySupplier xRS2 = UnoRuntime.queryInterface(XRepositorySupplier.class, xComp2); 598 assertTrue("xRS2 null", null != xRS2); 599 600 XRepository xRep2 = xRS2.getRDFRepository(); 601 assertTrue("xRep2 null", null != xRep2); 602 603 Statement[] manifestStmts5 = getManifestStmts((XURI) xDMA2); 604 Statement[] metadataStmts5 = getMetadataFileStmts((XURI) xDMA2, 605 fooBarPath); 606 XURI xFoobar5 = URI.createNS(xContext, xDMA2.getStringValue(), 607 fooBarPath); 608 Statement xFoobar_FooBarFoo5 = 609 new Statement(foo, bar, foo, xFoobar5); 610 xStmtsEnum = xRep.getStatements(null, null, null); 611 XEnumeration xStmtsEnum2 = xRep2.getStatements(null, null, null); 612 assertTrue("load: repository differs", 613 eq(xStmtsEnum2, merge(manifestStmts5, merge(metadataStmts5, 614 new Statement[] { xFoobar_FooBarFoo5 })))); 615 616 System.out.println("...done"); 617 618 } catch (Exception e) { 619 report(e); 620 } finally { 621 close(xComp); 622 close(xComp2); 623 } 624 } 625 checkRDFa()626 @Test public void checkRDFa() 627 { 628 XComponent xComp = null; 629 try { 630 final String file = TestDocument.getUrl("TESTRDFA.odt"); 631 xComp = loadRDFa(file); 632 if (xComp != null) 633 { 634 final String sNewFile = tempDir + "TESTRDFA.odt"; 635 storeRDFa(xComp, sNewFile); 636 close(xComp); 637 638 xComp = loadRDFa(sNewFile); 639 } 640 } finally { 641 close(xComp); 642 } 643 } 644 storeRDFa(XComponent xComp, String file)645 private void storeRDFa(XComponent xComp, String file) 646 { 647 try { 648 649 System.out.println("Storing test document..."); 650 651 XStorable xStor = UnoRuntime.queryInterface(XStorable.class, xComp); 652 653 xStor.storeToURL(file, new PropertyValue[0]); 654 655 System.out.println("...done"); 656 657 } catch (Exception e) { 658 report(e); 659 } 660 } 661 loadRDFa(String file)662 private XComponent loadRDFa(String file) 663 { 664 XComponent xComp = null; 665 try { 666 667 System.out.println("Loading test document..."); 668 669 PropertyValue[] loadProps = new PropertyValue[1]; 670 loadProps[0] = new PropertyValue(); 671 loadProps[0].Name = "Hidden"; 672 loadProps[0].Value = true; 673 674 675 676 xComp = util.DesktopTools.loadDoc(xMSF, file, loadProps); 677 678 XRepositorySupplier xRepoSupplier = UnoRuntime.queryInterface(XRepositorySupplier.class, xComp); 679 assertTrue("xRS null", null != xRepoSupplier); 680 681 XDocumentRepository xDocRepository = UnoRuntime.queryInterface(XDocumentRepository.class, xRepoSupplier.getRDFRepository()); 682 assertTrue("xRep null", null != xDocRepository); 683 684 XTextDocument xTextDoc = UnoRuntime.queryInterface(XTextDocument.class, xComp); 685 686 XText xText = xTextDoc.getText(); 687 688 XEnumerationAccess xEA = UnoRuntime.queryInterface(XEnumerationAccess.class, xText); 689 XEnumeration xEnum = xEA.createEnumeration(); 690 691 System.out.println("...done"); 692 693 System.out.println("Checking RDFa in loaded test document..."); 694 695 XMetadatable xPara; 696 Pair<Statement[], Boolean> result; 697 698 Statement x_FooBarLit1 = new Statement(foo, bar, mkLit("1"), null); 699 xPara = UnoRuntime.queryInterface(XMetadatable.class, xEnum.nextElement()); 700 result = xDocRepository.getStatementRDFa(xPara); 701 assertTrue("RDFa: 1", 702 !result.Second && 703 eq(result.First, new Statement[] { 704 x_FooBarLit1 705 })); 706 707 Statement x_FooBarLit2 = new Statement(foo, bar, mkLit("2"), null); 708 xPara = UnoRuntime.queryInterface(XMetadatable.class, xEnum.nextElement()); 709 result = xDocRepository.getStatementRDFa(xPara); 710 assertTrue("RDFa: 2", 711 !result.Second && 712 eq(result.First, new Statement[] { 713 x_FooBarLit2 714 })); 715 716 Statement x_BlankBarLit3 = 717 new Statement(blank1, bar, mkLit("3"), null); 718 xPara = UnoRuntime.queryInterface(XMetadatable.class, xEnum.nextElement()); 719 result = xDocRepository.getStatementRDFa(xPara); 720 assertTrue("RDFa: 3", 721 !result.Second && 722 eq(result.First, new Statement[] { 723 x_BlankBarLit3 724 })); 725 XBlankNode b3 = UnoRuntime.queryInterface(XBlankNode.class, result.First[0].Subject); 726 727 Statement x_BlankBarLit4 = 728 new Statement(blank2, bar, mkLit("4"), null); 729 xPara = UnoRuntime.queryInterface(XMetadatable.class, xEnum.nextElement()); 730 result = xDocRepository.getStatementRDFa(xPara); 731 assertTrue("RDFa: 4", 732 !result.Second && 733 eq(result.First, new Statement[] { 734 x_BlankBarLit4 735 })); 736 XBlankNode b4 = UnoRuntime.queryInterface(XBlankNode.class, result.First[0].Subject); 737 738 Statement x_BlankBarLit5 = 739 new Statement(blank1, bar, mkLit("5"), null); 740 xPara = UnoRuntime.queryInterface(XMetadatable.class, xEnum.nextElement()); 741 result = xDocRepository.getStatementRDFa(xPara); 742 assertTrue("RDFa: 5", 743 !result.Second && 744 eq(result.First, new Statement[] { 745 x_BlankBarLit5 746 })); 747 XBlankNode b5 = UnoRuntime.queryInterface(XBlankNode.class, result.First[0].Subject); 748 749 assertTrue("RDFa: 3 != 4", 750 !b3.getStringValue().equals(b4.getStringValue())); 751 assertTrue("RDFa: 3 == 5", 752 b3.getStringValue().equals(b5.getStringValue())); 753 754 Statement x_FooBarLit6 = new Statement(foo, bar, mkLit("6"), null); 755 Statement x_FooBazLit6 = new Statement(foo, baz, mkLit("6"), null); 756 xPara = UnoRuntime.queryInterface(XMetadatable.class, xEnum.nextElement()); 757 result = xDocRepository.getStatementRDFa(xPara); 758 assertTrue("RDFa: 6", 759 !result.Second && 760 eq(result.First, new Statement[] { 761 x_FooBarLit6, x_FooBazLit6 762 })); 763 764 Statement x_FooBarLit7 = new Statement(foo, bar, mkLit("7"), null); 765 Statement x_FooBazLit7 = new Statement(foo, baz, mkLit("7"), null); 766 Statement x_FooFooLit7 = new Statement(foo, foo, mkLit("7"), null); 767 xPara = UnoRuntime.queryInterface(XMetadatable.class, xEnum.nextElement()); 768 result = xDocRepository.getStatementRDFa(xPara); 769 assertTrue("RDFa: 7", 770 !result.Second && 771 eq(result.First, new Statement[] { 772 x_FooBarLit7, x_FooBazLit7, x_FooFooLit7 773 })); 774 775 XNode lit = mkLit("a fooish bar"); 776 XNode lit_type= mkLit("a fooish bar", bar); 777 Statement x_FooBarLit = new Statement(foo, bar, lit, null); 778 Statement x_FooBarLittype = new Statement(foo, bar, lit_type, null); 779 780 xPara = UnoRuntime.queryInterface(XMetadatable.class, xEnum.nextElement()); 781 result = xDocRepository.getStatementRDFa(xPara); 782 assertTrue("RDFa: 8", 783 result.Second && 784 eq(result.First, new Statement[] { 785 x_FooBarLit 786 })); 787 788 xPara = UnoRuntime.queryInterface(XMetadatable.class, xEnum.nextElement()); 789 result = xDocRepository.getStatementRDFa(xPara); 790 assertTrue("RDFa: 9", 791 result.Second && 792 eq(result.First, new Statement[] { 793 x_FooBarLit 794 })); 795 796 xPara = UnoRuntime.queryInterface(XMetadatable.class, xEnum.nextElement()); 797 result = xDocRepository.getStatementRDFa(xPara); 798 assertTrue("RDFa: 10", 799 result.Second && 800 eq(result.First, new Statement[] { 801 x_FooBarLittype 802 })); 803 804 Statement x_FooBarLit11 805 = new Statement(foo, bar, mkLit("11", bar), null); 806 xPara = UnoRuntime.queryInterface(XMetadatable.class, xEnum.nextElement()); 807 result = xDocRepository.getStatementRDFa(xPara); 808 assertTrue("RDFa: 11", 809 !result.Second && 810 eq(result.First, new Statement[] { 811 x_FooBarLit11 812 })); 813 814 XURI xFile = URI.createNS(xContext, file, "/" + contentPath); 815 Statement x_FileBarLit12 = 816 new Statement(xFile, bar, mkLit("12"), null); 817 xPara = UnoRuntime.queryInterface(XMetadatable.class, xEnum.nextElement()); 818 result = xDocRepository.getStatementRDFa(xPara); 819 assertTrue("RDFa: 12", 820 !result.Second && 821 eq(result.First, new Statement[] { 822 x_FileBarLit12 823 })); 824 825 xPara = UnoRuntime.queryInterface(XMetadatable.class, xEnum.nextElement()); 826 result = xDocRepository.getStatementRDFa(xPara); 827 assertTrue("RDFa: 13", 828 result.Second && 829 eq(result.First, new Statement[] { 830 x_FooBarLit 831 })); 832 833 Statement x_FooLabelLit14 = 834 new Statement(foo, rdfs_label, mkLit("14"), null); 835 xPara = UnoRuntime.queryInterface(XMetadatable.class, xEnum.nextElement()); 836 result = xDocRepository.getStatementRDFa(xPara); 837 assertTrue("RDFa: 14", 838 result.Second && 839 eq(result.First, new Statement[] { 840 /* x_FooLabelLit14 */ x_FooBarLit 841 })); 842 843 xPara = UnoRuntime.queryInterface(XMetadatable.class, xEnum.nextElement()); 844 result = xDocRepository.getStatementRDFa(xPara); 845 assertTrue("RDFa: 15", eq(result.First, new Statement[] { } )); 846 847 xPara = UnoRuntime.queryInterface(XMetadatable.class, xEnum.nextElement()); 848 result = xDocRepository.getStatementRDFa(xPara); 849 assertTrue("RDFa: 16", eq(result.First, new Statement[] { } )); 850 851 xPara = UnoRuntime.queryInterface(XMetadatable.class, xEnum.nextElement()); 852 result = xDocRepository.getStatementRDFa(xPara); 853 assertTrue("RDFa: 17", eq(result.First, new Statement[] { } )); 854 855 xPara = UnoRuntime.queryInterface(XMetadatable.class, xEnum.nextElement()); 856 result = xDocRepository.getStatementRDFa(xPara); 857 assertTrue("RDFa: 18", eq(result.First, new Statement[] { } )); 858 859 xPara = UnoRuntime.queryInterface(XMetadatable.class, xEnum.nextElement()); 860 result = xDocRepository.getStatementRDFa(xPara); 861 assertTrue("RDFa: 19", eq(result.First, new Statement[] { } )); 862 863 xPara = UnoRuntime.queryInterface( 864 XMetadatable.class, xEnum.nextElement()); 865 result = xDocRepository.getStatementRDFa(xPara); 866 assertTrue("RDFa: 20", eq(result.First, new Statement[] { } )); 867 868 xPara = UnoRuntime.queryInterface( 869 XMetadatable.class, xEnum.nextElement()); 870 result = xDocRepository.getStatementRDFa(xPara); 871 assertTrue("RDFa: 21", eq(result.First, new Statement[] { } )); 872 873 System.out.println("...done"); 874 875 } catch (Exception e) { 876 report(e); 877 close(xComp); 878 } 879 return xComp; 880 } 881 882 883 // utilities ------------------------------------------------------------- 884 report2(Exception e)885 public void report2(Exception e) 886 { 887 if (e instanceof WrappedTargetException) 888 { 889 System.out.println("Cause:"); 890 Exception cause = (Exception) 891 (((WrappedTargetException)e).TargetException); 892 System.out.println(cause.toString()); 893 report2(cause); 894 } else if (e instanceof WrappedTargetRuntimeException) { 895 System.out.println("Cause:"); 896 Exception cause = (Exception) 897 (((WrappedTargetRuntimeException)e).TargetException); 898 System.out.println(cause.toString()); 899 report2(cause); 900 } 901 } 902 report(Exception e)903 public void report(Exception e) { 904 System.out.println("Exception occurred:"); 905 e.printStackTrace(System.out); 906 report2(e); 907 fail(); 908 } 909 close(XComponent i_comp)910 static void close(XComponent i_comp) 911 { 912 try { 913 XCloseable xClos = UnoRuntime.queryInterface(XCloseable.class, i_comp); 914 if (xClos != null) 915 { 916 xClos.close(true); 917 } 918 } catch (Exception e) { 919 } 920 } 921 mkLit(String i_content)922 XLiteral mkLit(String i_content) 923 { 924 return Literal.create(xContext, i_content); 925 } 926 mkLit(String i_content, XURI i_uri)927 XLiteral mkLit(String i_content, XURI i_uri) 928 { 929 return Literal.createWithType(xContext, i_content, i_uri); 930 } 931 merge(Statement[] i_A1, Statement[] i_A2)932 static Statement[] merge(Statement[] i_A1, Statement[] i_A2) 933 { 934 // bah, java sucks... 935 Statement[] ret = new Statement[i_A1.length + i_A2.length]; 936 for (int i = 0; i < i_A1.length; ++i) { 937 ret[i] = i_A1[i]; 938 } 939 for (int i = 0; i < i_A2.length; ++i) { 940 ret[i+i_A1.length] = i_A2[i]; 941 } 942 return ret; 943 } 944 toS(XNode n)945 public static String toS(XNode n) { 946 if (null == n) 947 { 948 return "< null >"; 949 } 950 return n.getStringValue(); 951 } 952 isBlank(XNode i_node)953 static boolean isBlank(XNode i_node) 954 { 955 XBlankNode blank = UnoRuntime.queryInterface(XBlankNode.class, i_node); 956 return blank != null; 957 } 958 959 /* 960 static class Statement implements XStatement 961 { 962 XResource m_Subject; 963 XResource m_Predicate; 964 XNode m_Object; 965 XURI m_Graph; 966 967 Statement(XResource i_Subject, XResource i_Predicate, XNode i_Object, 968 XURI i_Graph) 969 { 970 m_Subject = i_Subject; 971 m_Predicate = i_Predicate; 972 m_Object = i_Object; 973 m_Graph = i_Graph; 974 } 975 976 public XResource getSubject() { return m_Subject; } 977 public XResource getPredicate() { return m_Predicate; } 978 public XNode getObject() { return m_Object; } 979 public XURI getGraph() { return m_Graph; } 980 } 981 */ 982 toSeq(XEnumeration i_Enum)983 static Statement[] toSeq(XEnumeration i_Enum) throws Exception 984 { 985 java.util.Collection c = new java.util.Vector(); 986 while (i_Enum.hasMoreElements()) { 987 Statement s = (Statement) i_Enum.nextElement(); 988 //System.out.println("toSeq: " + s.getSubject().getStringValue() + " " + s.getPredicate().getStringValue() + " " + s.getObject().getStringValue() + "."); 989 c.add(s); 990 } 991 // return (Statement[]) c.toArray(); 992 // java sucks 993 Object[] arr = c.toArray(); 994 Statement[] ret = new Statement[arr.length]; 995 for (int i = 0; i < arr.length; ++i) { 996 ret[i] = (Statement) arr[i]; 997 } 998 return ret; 999 } 1000 toSeqs(XEnumeration i_Enum)1001 static XNode[][] toSeqs(XEnumeration i_Enum) throws Exception 1002 { 1003 java.util.Collection c = new java.util.Vector(); 1004 while (i_Enum.hasMoreElements()) { 1005 XNode[] s = (XNode[]) i_Enum.nextElement(); 1006 c.add(s); 1007 } 1008 // return (XNode[][]) c.toArray(); 1009 Object[] arr = c.toArray(); 1010 XNode[][] ret = new XNode[arr.length][]; 1011 for (int i = 0; i < arr.length; ++i) { 1012 ret[i] = (XNode[]) arr[i]; 1013 } 1014 return ret; 1015 } 1016 1017 static class BindingComp implements java.util.Comparator 1018 { compare(Object i_Left, Object i_Right)1019 public int compare(Object i_Left, Object i_Right) 1020 { 1021 XNode[] left = (XNode[]) i_Left; 1022 XNode[] right = (XNode[]) i_Right; 1023 if (left.length != right.length) 1024 { 1025 throw new RuntimeException(); 1026 } 1027 for (int i = 0; i < left.length; ++i) { 1028 int eq = (left[i].getStringValue().compareTo( 1029 right[i].getStringValue())); 1030 if (eq != 0) 1031 { 1032 return eq; 1033 } 1034 } 1035 return 0; 1036 } 1037 } 1038 1039 static class StmtComp implements java.util.Comparator 1040 { compare(Object i_Left, Object i_Right)1041 public int compare(Object i_Left, Object i_Right) 1042 { 1043 int eq; 1044 Statement left = (Statement) i_Left; 1045 Statement right = (Statement) i_Right; 1046 if ((eq = cmp(left.Graph, right.Graph )) != 0) return eq; 1047 if ((eq = cmp(left.Subject, right.Subject )) != 0) return eq; 1048 if ((eq = cmp(left.Predicate, right.Predicate)) != 0) return eq; 1049 if ((eq = cmp(left.Object, right.Object )) != 0) return eq; 1050 return 0; 1051 } 1052 cmp(XNode i_Left, XNode i_Right)1053 public int cmp(XNode i_Left, XNode i_Right) 1054 { 1055 if (isBlank(i_Left)) { 1056 return isBlank(i_Right) ? 0 : 1; 1057 } else { 1058 if (isBlank(i_Right)) { 1059 return -1; 1060 } else { 1061 return toS(i_Left).compareTo(toS(i_Right)); 1062 } 1063 } 1064 } 1065 } 1066 eq(Statement i_Left, Statement i_Right)1067 static boolean eq(Statement i_Left, Statement i_Right) 1068 { 1069 XURI lG = i_Left.Graph; 1070 XURI rG = i_Right.Graph; 1071 if (!eq(lG, rG)) { 1072 System.out.println("Graphs differ: " + toS(lG) + " != " + toS(rG)); 1073 return false; 1074 } 1075 if (!eq(i_Left.Subject, i_Right.Subject)) { 1076 System.out.println("Subjects differ: " + 1077 i_Left.Subject.getStringValue() + " != " + 1078 i_Right.Subject.getStringValue()); 1079 return false; 1080 } 1081 if (!eq(i_Left.Predicate, i_Right.Predicate)) { 1082 System.out.println("Predicates differ: " + 1083 i_Left.Predicate.getStringValue() + " != " + 1084 i_Right.Predicate.getStringValue()); 1085 return false; 1086 } 1087 if (!eq(i_Left.Object, i_Right.Object)) { 1088 System.out.println("Objects differ: " + 1089 i_Left.Object.getStringValue() + " != " + 1090 i_Right.Object.getStringValue()); 1091 return false; 1092 } 1093 return true; 1094 } 1095 eq(Statement[] i_Result, Statement[] i_Expected)1096 static boolean eq(Statement[] i_Result, Statement[] i_Expected) 1097 { 1098 if (i_Result.length != i_Expected.length) { 1099 System.out.println("eq: different lengths: " + i_Result.length + " " + 1100 i_Expected.length); 1101 return false; 1102 } 1103 Statement[] expected = (Statement[]) 1104 java.util.Arrays.asList(i_Expected).toArray(); 1105 java.util.Arrays.sort(i_Result, new StmtComp()); 1106 java.util.Arrays.sort(expected, new StmtComp()); 1107 for (int i = 0; i < expected.length; ++i) 1108 { 1109 // This is better for debug! 1110 final Statement a = i_Result[i]; 1111 final Statement b = expected[i]; 1112 final boolean cond = eq(a, b); 1113 if (!cond) return false; 1114 } 1115 return true; 1116 } 1117 eq(XEnumeration i_Enum, Statement[] i_Expected)1118 static boolean eq(XEnumeration i_Enum, Statement[] i_Expected) 1119 throws Exception 1120 { 1121 Statement[] current = toSeq(i_Enum); 1122 return eq(current, i_Expected); 1123 } 1124 eq(XNode i_Left, XNode i_Right)1125 static boolean eq(XNode i_Left, XNode i_Right) 1126 { 1127 if (i_Left == null) { 1128 return (i_Right == null); 1129 } else { 1130 return (i_Right != null) && 1131 (i_Left.getStringValue().equals(i_Right.getStringValue()) 1132 // FIXME: hack: blank nodes considered equal 1133 || (isBlank(i_Left) && isBlank(i_Right))); 1134 } 1135 } 1136 eq(XQuerySelectResult i_Result, String[] i_Vars, XNode[][] i_Bindings)1137 static boolean eq(XQuerySelectResult i_Result, 1138 String[] i_Vars, XNode[][] i_Bindings) throws Exception 1139 { 1140 String[] vars = i_Result.getBindingNames(); 1141 XEnumeration iter = (XEnumeration) i_Result; 1142 XNode[][] bindings = toSeqs(iter); 1143 if (vars.length != i_Vars.length) { 1144 System.out.println("var lengths differ"); 1145 return false; 1146 } 1147 if (bindings.length != i_Bindings.length) { 1148 System.out.println("binding lengths differ: " + i_Bindings.length + 1149 " vs " + bindings.length ); 1150 return false; 1151 } 1152 java.util.Arrays.sort(bindings, new BindingComp()); 1153 java.util.Arrays.sort(i_Bindings, new BindingComp()); 1154 for (int i = 0; i < i_Bindings.length; ++i) { 1155 if (i_Bindings[i].length != i_Vars.length) { 1156 System.out.println("TEST ERROR!"); 1157 throw new Exception(); 1158 } 1159 if (bindings[i].length != i_Vars.length) { 1160 System.out.println("binding length and var length differ"); 1161 return false; 1162 } 1163 for (int j = 0; j < i_Vars.length; ++j) { 1164 if (!eq(bindings[i][j], i_Bindings[i][j])) { 1165 System.out.println("bindings differ: " + 1166 toS(bindings[i][j]) + " != " + toS(i_Bindings[i][j])); 1167 return false; 1168 } 1169 } 1170 } 1171 for (int i = 0; i < i_Vars.length; ++i) { 1172 if (!vars[i].equals(i_Vars[i])) { 1173 System.out.println("variable names differ: " + 1174 vars[i] + " != " + i_Vars[i]); 1175 return false; 1176 } 1177 } 1178 return true; 1179 } 1180 eq(StringPair i_Left, StringPair i_Right)1181 static boolean eq(StringPair i_Left, StringPair i_Right) 1182 { 1183 return ((i_Left.First).equals(i_Right.First)) && 1184 ((i_Left.Second).equals(i_Right.Second)); 1185 } 1186 mkNamespace(String i_prefix, String i_namespace)1187 static String mkNamespace(String i_prefix, String i_namespace) 1188 { 1189 return "PREFIX " + i_prefix + ": <" + i_namespace + ">\n"; 1190 } 1191 mkNss()1192 static String mkNss() 1193 { 1194 String namespaces = mkNamespace("rdf", 1195 "http://www.w3.org/1999/02/22-rdf-syntax-ns#"); 1196 namespaces += mkNamespace("pkg", 1197 "http://docs.oasis-open.org/opendocument/meta/package/common#"); 1198 namespaces += mkNamespace("odf", 1199 "http://docs.oasis-open.org/opendocument/meta/package/odf#"); 1200 return namespaces; 1201 } 1202 getManifestStmts(XURI xBaseURI)1203 Statement[] getManifestStmts(XURI xBaseURI) throws Exception 1204 { 1205 XURI xManifest = URI.createNS(xContext, xBaseURI.getStringValue(), 1206 manifestPath); 1207 XURI xContent = URI.createNS(xContext, xBaseURI.getStringValue(), 1208 contentPath); 1209 XURI xStyles = URI.createNS(xContext, xBaseURI.getStringValue(), 1210 stylesPath); 1211 Statement xM_BaseTypeDoc = 1212 new Statement(xBaseURI, rdf_type, pkg_Document, xManifest); 1213 Statement xM_BaseHaspartContent = 1214 new Statement(xBaseURI, pkg_hasPart, xContent, xManifest); 1215 Statement xM_BaseHaspartStyles = 1216 new Statement(xBaseURI, pkg_hasPart, xStyles, xManifest); 1217 Statement xM_ContentTypeContent = 1218 new Statement(xContent, rdf_type, odf_ContentFile, xManifest); 1219 Statement xM_StylesTypeStyles = 1220 new Statement(xStyles, rdf_type, odf_StylesFile, xManifest); 1221 return new Statement[] { 1222 xM_BaseTypeDoc, xM_BaseHaspartContent, xM_BaseHaspartStyles, 1223 xM_ContentTypeContent, xM_StylesTypeStyles 1224 }; 1225 } 1226 getMetadataFileStmts(XURI xBaseURI, String Path)1227 Statement[] getMetadataFileStmts(XURI xBaseURI, String Path) 1228 throws Exception 1229 { 1230 XURI xManifest = URI.createNS(xContext, xBaseURI.getStringValue(), 1231 manifestPath); 1232 XURI xGraph = URI.createNS(xContext, xBaseURI.getStringValue(), Path); 1233 Statement xM_BaseHaspartGraph = 1234 new Statement(xBaseURI, pkg_hasPart, xGraph, xManifest); 1235 Statement xM_GraphTypeMetadata = 1236 new Statement(xGraph, rdf_type, pkg_MetadataFile, xManifest); 1237 return new Statement[] { xM_BaseHaspartGraph, xM_GraphTypeMetadata }; 1238 } 1239 1240 class TestRange implements XTextRange, XMetadatable, XServiceInfo 1241 { 1242 String m_Stream; 1243 String m_XmlId; 1244 String m_Text; TestRange(String i_Str)1245 TestRange(String i_Str) { m_Text = i_Str; } 1246 getStringValue()1247 public String getStringValue() { return ""; } getNamespace()1248 public String getNamespace() { return ""; } getLocalName()1249 public String getLocalName() { return ""; } 1250 getMetadataReference()1251 public StringPair getMetadataReference() 1252 { 1253 return new StringPair(m_Stream, m_XmlId); 1254 } setMetadataReference(StringPair i_Ref)1255 public void setMetadataReference(StringPair i_Ref) 1256 throws IllegalArgumentException 1257 { 1258 m_Stream = i_Ref.First; 1259 m_XmlId = i_Ref.Second; 1260 } ensureMetadataReference()1261 public void ensureMetadataReference() 1262 { 1263 m_Stream = "content.xml"; 1264 m_XmlId = "42"; 1265 } 1266 getImplementationName()1267 public String getImplementationName() { return null; } getSupportedServiceNames()1268 public String[] getSupportedServiceNames() { return null; } supportsService(String i_Svc)1269 public boolean supportsService(String i_Svc) 1270 { 1271 return i_Svc.equals("com.sun.star.text.Paragraph"); 1272 } 1273 getText()1274 public XText getText() { return null; } getStart()1275 public XTextRange getStart() { return null; } getEnd()1276 public XTextRange getEnd() { return null; } getString()1277 public String getString() { return m_Text; } setString(String i_Str)1278 public void setString(String i_Str) { m_Text = i_Str; } 1279 } 1280 1281 1282 getMSF()1283 private XMultiServiceFactory getMSF() 1284 { 1285 final XMultiServiceFactory xMSF1 = UnoRuntime.queryInterface(XMultiServiceFactory.class, connection.getComponentContext().getServiceManager()); 1286 return xMSF1; 1287 } 1288 1289 // setup and close connections setUpConnection()1290 @BeforeClass public static void setUpConnection() throws Exception { 1291 System.out.println( "------------------------------------------------------------" ); 1292 System.out.println( "starting class: " + DocumentMetadataAccess.class.getName() ); 1293 System.out.println( "------------------------------------------------------------" ); 1294 connection.setUp(); 1295 } 1296 tearDownConnection()1297 @AfterClass public static void tearDownConnection() 1298 throws InterruptedException, com.sun.star.uno.Exception 1299 { 1300 System.out.println( "------------------------------------------------------------" ); 1301 System.out.println( "finishing class: " + DocumentMetadataAccess.class.getName() ); 1302 System.out.println( "------------------------------------------------------------" ); 1303 connection.tearDown(); 1304 } 1305 1306 private static final OfficeConnection connection = new OfficeConnection(); 1307 1308 } 1309 1310