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