diff options
Diffstat (limited to 'unoxml')
85 files changed, 17089 insertions, 0 deletions
diff --git a/unoxml/prj/build.lst b/unoxml/prj/build.lst new file mode 100644 index 000000000000..cc54f6d3ad28 --- /dev/null +++ b/unoxml/prj/build.lst @@ -0,0 +1,6 @@ +ux unoxml : offuh cppuhelper LIBXML2:libxml2 REDLAND:redland tools NULL +ux unoxml\source\dom nmake - all ux_dom NULL +ux unoxml\source\xpath nmake - all ux_xpath ux_dom NULL +ux unoxml\source\events nmake - all ux_events ux_dom NULL +ux unoxml\source\service nmake - all ux_service ux_dom ux_xpath ux_events NULL +ux unoxml\source\rdf nmake - all ux_librdf NULL diff --git a/unoxml/prj/d.lst b/unoxml/prj/d.lst new file mode 100644 index 000000000000..e2bf5df03b34 --- /dev/null +++ b/unoxml/prj/d.lst @@ -0,0 +1,3 @@ +..\%__SRC%\lib\lib*.so %_DEST%\lib%_EXT%\lib*.so +..\%__SRC%\lib\lib*.dylib %_DEST%\lib%_EXT%\lib*.dylib +..\%__SRC%\bin\*.dll %_DEST%\bin%_EXT%\*.dll diff --git a/unoxml/qa/complex/RDFRepositoryTest.java b/unoxml/qa/complex/RDFRepositoryTest.java new file mode 100644 index 000000000000..5fc100e5d14b --- /dev/null +++ b/unoxml/qa/complex/RDFRepositoryTest.java @@ -0,0 +1,845 @@ +/************************************************************************* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2008 by Sun Microsystems, Inc. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: RDFRepositoryTest.java,v $ + * + * $Revision: 1.2 $ + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + ************************************************************************/ + +package complex.unoxml; + +import complexlib.ComplexTestCase; +import helper.StreamSimulator; + +import com.sun.star.uno.UnoRuntime; +import com.sun.star.uno.XComponentContext; +import com.sun.star.uno.Any; +import com.sun.star.lang.XMultiServiceFactory; +import com.sun.star.lang.XInitialization; +import com.sun.star.lang.XEventListener; +import com.sun.star.lang.XServiceInfo; +import com.sun.star.lang.IllegalArgumentException; +import com.sun.star.lang.WrappedTargetException; +import com.sun.star.lang.WrappedTargetRuntimeException; +import com.sun.star.beans.XPropertySet; +import com.sun.star.beans.PropertyValue; +import com.sun.star.beans.StringPair; +import com.sun.star.container.XEnumeration; +import com.sun.star.container.ElementExistException; +import com.sun.star.container.NoSuchElementException; +import com.sun.star.io.XInputStream; +import com.sun.star.io.XOutputStream; +import com.sun.star.text.XTextRange; +import com.sun.star.text.XText; +import com.sun.star.rdf.*; + +/** + * Test case for service com.sun.star.rdf.Repository + * Currently, this service is implemented in + * unoxml/source/rdf/librdf_repository.cxx + * + * @author mst + */ +public class RDFRepositoryTest extends ComplexTestCase +{ + XComponentContext xContext; + String tempDir; + + XDocumentRepository xRep; + XURI foo; + XURI bar; + XURI baz; + XURI uint; + XURI rdfslabel; + XURI manifest; + XURI uuid; + XURI base; + XBlankNode blank; + XLiteral lit; + XLiteral litlang; + XLiteral littype; + String rdfs = "http://www.w3.org/2000/01/rdf-schema#"; + + public String[] getTestMethodNames () + { + return new String[] { "check", "checkSPARQL", "checkRDFa" }; + } + + public void before() + { + try { + XMultiServiceFactory xMSF = (XMultiServiceFactory) param.getMSF(); + assure("could not create MultiServiceFactory.", xMSF != null); + XPropertySet xPropertySet = (XPropertySet) + UnoRuntime.queryInterface(XPropertySet.class, xMSF); + Object defaultCtx = xPropertySet.getPropertyValue("DefaultContext"); + xContext = (XComponentContext) + UnoRuntime.queryInterface(XComponentContext.class, defaultCtx); + assure("could not get component context.", xContext != null); + + tempDir = util.utils.getOfficeTemp/*Dir*/(xMSF); + log.println("tempdir: " + tempDir); + + foo = URI.create(xContext, "uri:foo"); + assure("foo", null != foo); + bar = URI.create(xContext, "uri:bar"); + assure("bar", null != bar); + baz = URI.create(xContext, "uri:baz"); + assure("baz", null != baz); + uint = URI.create(xContext, "uri:int"); + assure("uint", null != uint); + blank = BlankNode.create(xContext, "_:uno"); + assure("blank", null != blank); + lit = Literal.create(xContext, "i am the literal"); + assure("lit", null != lit); + litlang = Literal.createWithLanguage(xContext, + "i am the literal", "en"); + assure("litlang", null != litlang); + littype = Literal.createWithType(xContext, "42", uint); + assure("littype", null != littype); + + rdfslabel = URI.create(xContext, rdfs + "label"); + assure("rdfslabel", null != rdfslabel); + manifest = URI.create(xContext, "manifest:manifest"); //FIXME + assure("manifest", null != manifest); + uuid = URI.create(xContext, + "urn:uuid:224ab023-77b8-4396-a75a-8cecd85b81e3"); + assure("uuid", null != uuid); + base = URI.create(xContext, "base-uri:"); //FIXME + assure("base", null != base); + } catch (Exception e) { + report(e); + } + } + + public void after() + { + xRep = null; + } + + public void check() + { + try { + + log.println("Creating service Repository..."); + + //FIXME: ? +// xRep = Repository.create(xContext); + xRep = (XDocumentRepository) UnoRuntime.queryInterface( + XDocumentRepository.class, Repository.create(xContext)); + assure("null", null != xRep); + + log.println("...done"); + + log.println("Checking that new repository is really empty..."); + assure("empty: graphs", 0 == xRep.getGraphNames().length); + + XEnumeration stmts; + stmts = xRep.getStatements(null, null, null); + assure("empty: stmts", !stmts.hasMoreElements()); + + log.println("...done"); + + log.println("Checking graph creation..."); + + XNamedGraph xFooGraph = xRep.createGraph(foo); + assure("foo graph", null != xFooGraph); + + try { + xRep.createGraph(foo); + assure("creating duplicate graph was allowed", false); + } catch (ElementExistException e) { + // ignore + } + + try { + xRep.createGraph(null); + assure("invalid graph name was allowed", false); + } catch (IllegalArgumentException e) { + // ignore + } + + XURI[] names = xRep.getGraphNames(); + assure("no foo graph in getGraphNames", + 1 == names.length && eq(names[0], foo)); + assure("no foo graph", null != xRep.getGraph(foo)); + + stmts = xFooGraph.getStatements(null, null, null); + assure("stmts in foo graph", !stmts.hasMoreElements()); + + XOutputStream xFooOut = + new StreamSimulator(tempDir + "empty.rdf", false, param); + xRep.exportGraph(FileFormat.RDF_XML, xFooOut, foo, base); + xFooOut.closeOutput(); + + XInputStream xFooIn = + new StreamSimulator(tempDir + "empty.rdf", true, param); + xRep.importGraph(FileFormat.RDF_XML, xFooIn, bar, base); + assure("no bar graph", null != xRep.getGraph(bar)); + + log.println("...done"); + + log.println("Checking graph manipulation..."); + + XEnumeration xFooEnum; + + Statement xFoo_FooBarBaz = new Statement(foo, bar, baz, foo); + xFooGraph.addStatement(foo, bar, baz); + xFooEnum = xFooGraph.getStatements(null, null, null); + assure("addStatement(foo,bar,baz)", + eq(xFooEnum, new Statement[] { xFoo_FooBarBaz })); + + Statement xFoo_FooBarBlank = new Statement(foo, bar, blank, foo); + xFooGraph.addStatement(foo, bar, blank); + xFooEnum = xFooGraph.getStatements(null, null, null); + assure("addStatement(foo,bar,blank)", + eq(xFooEnum, + new Statement[] { xFoo_FooBarBaz, xFoo_FooBarBlank })); + xFooEnum = xRep.getStatements(null, null, null); + assure("addStatement(foo,bar,blank) (global)", + eq(xFooEnum, + new Statement[] { xFoo_FooBarBaz, xFoo_FooBarBlank })); + + Statement xFoo_BazBarLit = new Statement(baz, bar, lit, foo); + xFooGraph.addStatement(baz, bar, lit); + xFooEnum = xFooGraph.getStatements(null, null, null); + assure("addStatement(baz,bar,lit)", + eq(xFooEnum, new Statement[] { + xFoo_FooBarBaz, xFoo_FooBarBlank, xFoo_BazBarLit })); + xFooEnum = xFooGraph.getStatements(baz, bar, null); + assure("addStatement(baz,bar,lit) (baz,bar)", + eq(xFooEnum, new Statement[] { xFoo_BazBarLit })); + + Statement xFoo_BazBarLitlang = + new Statement(baz, bar, litlang, foo); + xFooGraph.addStatement(baz, bar, litlang); + xFooEnum = xFooGraph.getStatements(null, null, null); + assure("addStatement(baz,bar,litlang)", + eq(xFooEnum, new Statement[] { + xFoo_FooBarBaz, xFoo_FooBarBlank, xFoo_BazBarLit, + xFoo_BazBarLitlang })); + xFooEnum = xFooGraph.getStatements(null, null, baz); + assure("addStatement(baz,bar,litlang) (baz)", + eq(xFooEnum, new Statement[] { xFoo_FooBarBaz })); + + Statement xFoo_BazBarLittype = + new Statement(baz, bar, littype, foo); + xFooGraph.addStatement(baz, bar, littype); + xFooEnum = xFooGraph.getStatements(null, null, null); + assure("addStatement(baz,bar,littype)", + eq(xFooEnum, new Statement[] { xFoo_FooBarBaz, xFoo_FooBarBlank, + xFoo_BazBarLit, xFoo_BazBarLitlang, xFoo_BazBarLittype })); + + xFooGraph.removeStatements(baz, bar, litlang); + xFooEnum = xFooGraph.getStatements(null, null, null); + assure("removeStatement(baz,bar,litlang)", + eq(xFooEnum, new Statement[] { xFoo_FooBarBaz, xFoo_FooBarBlank, + xFoo_BazBarLit, xFoo_BazBarLittype })); + + xFooGraph.removeStatements(foo, bar, null); + xFooEnum = xFooGraph.getStatements(null, null, null); + assure("removeStatement(foo,bar,null)", + eq(xFooEnum, new Statement[] { + xFoo_BazBarLit, xFoo_BazBarLittype })); + + xFooGraph.addStatement(foo, bar, baz); + xFooEnum = xFooGraph.getStatements(null, null, null); + assure("addStatement(foo,bar,baz) (re-add)", + eq(xFooEnum, new Statement[] { xFoo_FooBarBaz, + xFoo_BazBarLit, xFoo_BazBarLittype })); + + xFooGraph.addStatement(foo, bar, baz); + xFooEnum = xFooGraph.getStatements(null, null, null); + assure("addStatement(foo,bar,baz) (duplicate)", + eq(xFooEnum, new Statement[] { xFoo_FooBarBaz, + xFoo_BazBarLit, xFoo_BazBarLittype })); + + xFooGraph.addStatement(xFooGraph, bar, baz); + xFooEnum = xFooGraph.getStatements(null, null, null); + assure("addStatement(foo,bar,baz) (triplicate, as graph)", + eq(xFooEnum, new Statement[] { xFoo_FooBarBaz, + xFoo_BazBarLit, xFoo_BazBarLittype })); + + log.println("...done"); + + log.println("Checking graph import/export..."); + + xFooOut = new StreamSimulator(tempDir + "foo.rdf", false, param); + xRep.exportGraph(FileFormat.RDF_XML, xFooOut, foo, base); + xFooOut.closeOutput(); + + xFooIn = new StreamSimulator(tempDir + "foo.rdf", true, param); + try { + xRep.importGraph(FileFormat.RDF_XML, xFooIn, bar, base); + assure("importing existing graph did not fail", false); + } catch (ElementExistException e) { + // ignore + } + + xFooIn = new StreamSimulator(tempDir + "foo.rdf", true, param); + xRep.importGraph(FileFormat.RDF_XML, xFooIn, baz, base); + XNamedGraph xBazGraph = xRep.getGraph(baz); + assure("no baz graph", null != xBazGraph); + + Statement xBaz_FooBarBaz = new Statement(foo, bar, baz, baz); + Statement xBaz_BazBarLit = new Statement(baz, bar, lit, baz); + Statement xBaz_BazBarLittype = + new Statement(baz, bar, littype, baz); + XEnumeration xBazEnum = xBazGraph.getStatements(null, null, null); + assure("importing exported graph", + eq(xBazEnum, new Statement[] { + xBaz_FooBarBaz, xBaz_BazBarLit, xBaz_BazBarLittype })); + + log.println("...done"); + + log.println("Checking graph deletion..."); + + xFooGraph.clear(); + xFooEnum = xFooGraph.getStatements(null, null, null); + assure("clear", eq(xFooEnum, new Statement[] { })); + + xRep.destroyGraph(baz); + assure("baz graph zombie", null == xRep.getGraph(baz)); + + try { + xBazGraph.clear(); + assure("deleted graph not invalid (clear)", false); + } catch (NoSuchElementException e) { + // ignore + } + + try { + xBazGraph.addStatement(foo, foo, foo); + assure("deleted graph not invalid (add)", false); + } catch (NoSuchElementException e) { + // ignore + } + + try { + xBazGraph.removeStatements(null, null, null); + assure("deleted graph not invalid (remove)", false); + } catch (NoSuchElementException e) { + // ignore + } + + try { + xBazGraph.getStatements(null, null, null); + assure("deleted graph not invalid (get)", false); + } catch (NoSuchElementException e) { + // ignore + } + + log.println("...done"); + + } catch (Exception e) { + report(e); + } + } + + public void checkSPARQL() + { + try { + + log.println("Checking SPARQL queries..."); + + XInputStream xIn = new StreamSimulator( + util.utils.getFullTestDocName("example.rdf"), true, param); + xRep.importGraph(FileFormat.RDF_XML, xIn, manifest, base); + + String query; + query = "SELECT ?p WHERE { ?p rdf:type pkg:Package . }"; + XQuerySelectResult result = xRep.querySelect(mkNss() + query); + assure("query: package-id\n" + query, + eq(result, new String[] { "p" }, + new XNode[][] { { uuid } })); + + query = "SELECT ?part ?path FROM <" + manifest + + "> WHERE { ?pkg rdf:type pkg:Package . ?pkg pkg:hasPart ?part ." + + " ?part pkg:path ?path . ?part rdf:type odf:ContentFile. }"; + result = xRep.querySelect(mkNss() + query); + assure("query: contentfile", + eq(result, new String[] { "part", "path" }, + new XNode[][] { { BlankNode.create(xContext, "whatever"), + Literal.create(xContext, "content.xml") } })); + + query = "SELECT ?pkg ?path FROM <" + toS(manifest) + "> WHERE { " + + "?pkg rdf:type pkg:Package . ?pkg pkg:hasPart ?part . " + + "?part pkg:path ?path . ?part rdf:type odf:ContentFile. }"; + result = xRep.querySelect(mkNss() + query); + assure("query: contentfile\n" + query, + eq(result, new String[] { "pkg", "path" }, + new XNode[][] { { uuid , + Literal.create(xContext, "content.xml") } })); + + query = "SELECT ?part ?path FROM <" + toS(manifest) + "> WHERE { " + + "?pkg rdf:type pkg:Package . ?pkg pkg:hasPart ?part . " + + "?part pkg:path ?path . ?part rdf:type odf:StylesFile. }"; + result = xRep.querySelect(mkNss() + query); + assure("query: stylesfile\n" + query, + eq(result, new String[] { "part", "path" }, + new XNode[][] { })); + + query = "SELECT ?part ?path FROM <" + toS(manifest) + "> WHERE { " + + "?pkg rdf:type pkg:Package . ?pkg pkg:hasPart ?part . " + + "?part pkg:path ?path . ?part rdf:type odf:MetadataFile. }"; + result = xRep.querySelect(mkNss() + query); + assure("query: metadatafile\n" + query, + eq(result, new String[] { "part", "path" }, + new XNode[][] { { + URI.create(xContext, "http://hospital-employee/doctor"), + Literal.create(xContext, + "meta/hospital/doctor.rdf") } })); + +//FIXME redland BUG + String uri = "uri:example-element-2"; + query = "SELECT ?path ?idref FROM <" + toS(manifest) + "> WHERE { " +// + "<" + toS(uuid) + "> rdf:type pkg:Package ; " +// + " pkg:hasPart ?part . " + + "<" + toS(uuid) + "> pkg:hasPart ?part . " + + "?part pkg:path ?path ; " + + " rdf:type ?type ; " + + " pkg:hasPart <" + uri + "> . " +// + "<" + uri + "> rdf:type odf:Element ; " + + "<" + uri + "> " + + " pkg:idref ?idref . " + + " FILTER (?type = odf:ContentFile || ?type = odf:StylesFile)" + + " }"; +//log.println(query); + result = xRep.querySelect(mkNss() + query); + assure("query: example-element-2\n" + query, + eq(result, new String[] { "path", "idref" }, + new XNode[][] { { + Literal.create(xContext, "content.xml"), + Literal.create(xContext, "ID_B") } })); + + // CONSTRUCT result triples have no graph! + Statement x_PkgFooLit = new Statement(uuid, foo, lit, null); + query = "CONSTRUCT { ?pkg <" + toS(foo) + "> \"" + + lit.getStringValue() + "\" } FROM <" + toS(manifest) + + "> WHERE { ?pkg rdf:type pkg:Package . } "; + XEnumeration xResultEnum = xRep.queryConstruct(mkNss() + query); + assure("query: construct\n" + query, + eq(xResultEnum, new Statement[] { x_PkgFooLit })); + + query = "ASK { ?pkg rdf:type pkg:Package . }"; + boolean bResult = xRep.queryAsk(mkNss() + query); + assure("query: ask\n" + query, bResult); + + log.println("...done"); + + } catch (Exception e) { + report(e); + } + } + + public void checkRDFa() + { + try { + log.println("Checking RDFa gunk..."); + + String content = "behold, for i am the content."; + XTextRange xTR = new TestRange(content); + XMetadatable xM = (XMetadatable) xTR; + + Statement[] result = xRep.getStatementRDFa((XMetadatable)xTR); + assure("RDFa: get: not empty (initial)", + 0 == result.length); + + try { + xRep.setStatementRDFa(foo, new XURI[] {}, xM, "", null); + assure("RDFa: set: no predicate", false); + } catch (IllegalArgumentException e) { + // ignore + } + + try { + xRep.setStatementRDFa(foo, new XURI[] {bar}, null, "", null); + assure("RDFa: set: null", false); + } catch (IllegalArgumentException e) { + // ignore + } + + XLiteral trlit = Literal.create(xContext, content); + Statement x_FooBarTRLit = new Statement(foo, bar, trlit, null); + xRep.setStatementRDFa(foo, new XURI[] { bar }, xM, "", null); + + result = xRep.getStatementRDFa((XMetadatable)xTR); + assure("RDFa: get: without content", + 1 == result.length && eq((Statement)result[0], x_FooBarTRLit)); + + //FIXME: do this? + xTR.setString(lit.getStringValue()); +/* + Statement xFooBarLit = new Statement(foo, bar, lit, null); + result = xRep.getStatementRDFa((XMetadatable)xTR); + assure("RDFa: get: change", + eq((Statement)result.First, xFooBarLit) && null == result.Second); +*/ + + Statement x_FooBarLittype = new Statement(foo, bar, littype, null); + Statement x_FooLabelLit = new Statement(foo, rdfslabel, lit, null); + xRep.setStatementRDFa(foo, new XURI[] { bar }, xM, "42", uint); + + result = xRep.getStatementRDFa((XMetadatable)xTR); + assure("RDFa: get: with content", + 2 == result.length && eq((Statement)result[0], x_FooLabelLit) + && eq((Statement)result[1], x_FooBarLittype)); + + //FIXME: do this? + xTR.setString(content); +/* + Statement xFooLabelTRLit = new Statement(foo, rdfslabel, trlit, null); + result = xRep.getStatementRDFa((XMetadatable)xTR); + assure("RDFa: get: change (label)", + eq((Statement)result.First, xFooBarLittype) && + eq((Statement)result.Second, xFooLabelTRLit)); +*/ + + xRep.removeStatementRDFa((XMetadatable)xTR); + + result = xRep.getStatementRDFa((XMetadatable)xTR); + assure("RDFa: get: not empty (removed)", + 0 == result.length); + + xRep.setStatementRDFa(foo, new XURI[] { foo, bar, baz }, xM, + "", null); + + Statement x_FooFooTRLit = new Statement(foo, foo, trlit, null); + Statement x_FooBazTRLit = new Statement(foo, baz, trlit, null); + result = xRep.getStatementRDFa((XMetadatable) xTR); + assure("RDFa: get: without content (multiple predicates, reinsert)", + eq(result, new Statement[] { + x_FooFooTRLit, x_FooBarTRLit, x_FooBazTRLit })); + + xRep.removeStatementRDFa((XMetadatable)xTR); + + result = xRep.getStatementRDFa((XMetadatable) xTR); + assure("RDFa: get: not empty (re-removed)", + 0 == result.length); + + log.println("...done"); + + } catch (Exception e) { + report(e); + } + } + +// utilities ------------------------------------------------------------- + + public void report2(Exception e) + { + if (e instanceof WrappedTargetException) + { + log.println("Cause:"); + Exception cause = (Exception) + (((WrappedTargetException)e).TargetException); + log.println(cause.toString()); + report2(cause); + } else if (e instanceof WrappedTargetRuntimeException) { + log.println("Cause:"); + Exception cause = (Exception) + (((WrappedTargetRuntimeException)e).TargetException); + log.println(cause.toString()); + report2(cause); + } + } + + public void report(Exception e) { + log.println("Exception occurred:"); + e.printStackTrace((java.io.PrintWriter) log); + report2(e); + failed(); + } + + public static String toS(XNode n) { + if (null == n) return "< null >"; + return n.getStringValue(); + } + + static boolean isBlank(XNode i_node) + { + XBlankNode blank = (XBlankNode) UnoRuntime.queryInterface( + XBlankNode.class, i_node); + return blank != null; + } + +/* + static class Statement implements XStatement + { + XResource m_Subject; + XResource m_Predicate; + XNode m_Object; + XURI m_Graph; + + Statement(XResource i_Subject, XResource i_Predicate, XNode i_Object, + XURI i_Graph) + { + m_Subject = i_Subject; + m_Predicate = i_Predicate; + m_Object = i_Object; + m_Graph = i_Graph; + } + + public XResource getSubject() { return m_Subject; } + public XResource getPredicate() { return m_Predicate; } + public XNode getObject() { return m_Object; } + public XURI getGraph() { return m_Graph; } + } +*/ + + static Statement[] toSeq(XEnumeration i_Enum) throws Exception + { + java.util.Collection c = new java.util.Vector(); + while (i_Enum.hasMoreElements()) { + Statement s = (Statement) i_Enum.nextElement(); +//log.println("toSeq: " + s.getSubject().getStringValue() + " " + s.getPredicate().getStringValue() + " " + s.getObject().getStringValue() + "."); + c.add(s); + } +// return (Statement[]) c.toArray(); + // java sucks + Object[] arr = c.toArray(); + Statement[] ret = new Statement[arr.length]; + for (int i = 0; i < arr.length; ++i) { + ret[i] = (Statement) arr[i]; + } + return ret; + } + + static XNode[][] toSeqs(XEnumeration i_Enum) throws Exception + { + java.util.Collection c = new java.util.Vector(); + while (i_Enum.hasMoreElements()) { + XNode[] s = (XNode[]) i_Enum.nextElement(); + c.add(s); + } +// return (XNode[][]) c.toArray(); + Object[] arr = c.toArray(); + XNode[][] ret = new XNode[arr.length][]; + for (int i = 0; i < arr.length; ++i) { + ret[i] = (XNode[]) arr[i]; + } + return ret; + } + + static class BindingComp implements java.util.Comparator + { + public int compare(Object i_Left, Object i_Right) + { + XNode[] left = (XNode[]) i_Left; + XNode[] right = (XNode[]) i_Right; + if (left.length != right.length) throw new RuntimeException(); + for (int i = 0; i < left.length; ++i) { + int eq = (left[i].getStringValue().compareTo( + right[i].getStringValue())); + if (eq != 0) return eq; + } + return 0; + } + } + + static class StmtComp implements java.util.Comparator + { + public int compare(Object i_Left, Object i_Right) + { + int eq; + Statement left = (Statement) i_Left; + Statement right = (Statement) i_Right; + if ((eq = cmp(left.Graph, right.Graph )) != 0) return eq; + if ((eq = cmp(left.Subject, right.Subject )) != 0) return eq; + if ((eq = cmp(left.Predicate, right.Predicate)) != 0) return eq; + if ((eq = cmp(left.Object, right.Object )) != 0) return eq; + return 0; + } + + public int cmp(XNode i_Left, XNode i_Right) + { + if (isBlank(i_Left)) { + return isBlank(i_Right) ? 0 : 1; + } else { + if (isBlank(i_Right)) { + return -1; + } else { + return toS(i_Left).compareTo(toS(i_Right)); + } + } + } + } + + static boolean eq(Statement i_Left, Statement i_Right) + { + XURI lG = i_Left.Graph; + XURI rG = i_Right.Graph; + if (!eq(lG, rG)) { + log.println("Graphs differ: " + toS(lG) + " != " + toS(rG)); + return false; + } + if (!eq(i_Left.Subject, i_Right.Subject)) { + log.println("Subjects differ: " + + i_Left.Subject.getStringValue() + " != " + + i_Right.Subject.getStringValue()); + return false; + } + if (!eq(i_Left.Predicate, i_Right.Predicate)) { + log.println("Predicates differ: " + + i_Left.Predicate.getStringValue() + " != " + + i_Right.Predicate.getStringValue()); + return false; + } + if (!eq(i_Left.Object, i_Right.Object)) { + log.println("Objects differ: " + + i_Left.Object.getStringValue() + " != " + + i_Right.Object.getStringValue()); + return false; + } + return true; + } + + static boolean eq(Statement[] i_Result, Statement[] i_Expected) + { + if (i_Result.length != i_Expected.length) { + log.println("eq: different lengths: " + i_Result.length + " " + + i_Expected.length); + return false; + } + Statement[] expected = (Statement[]) + java.util.Arrays.asList(i_Expected).toArray(); + java.util.Arrays.sort(i_Result, new StmtComp()); + java.util.Arrays.sort(expected, new StmtComp()); + for (int i = 0; i < expected.length; ++i) { + if (!eq(i_Result[i], expected[i])) return false; + } + return true; + } + + static boolean eq(XEnumeration i_Enum, Statement[] i_Expected) + throws Exception + { + Statement[] current = toSeq(i_Enum); + return eq(current, i_Expected); + } + + static boolean eq(XNode i_Left, XNode i_Right) + { + if (i_Left == null) { + return (i_Right == null); + } else { + return (i_Right != null) && + (i_Left.getStringValue().equals(i_Right.getStringValue()) + // FIXME: hack: blank nodes considered equal + || (isBlank(i_Left) && isBlank(i_Right))); + } + } + + static boolean eq(XQuerySelectResult i_Result, + String[] i_Vars, XNode[][] i_Bindings) throws Exception + { + String[] vars = (String[]) i_Result.getBindingNames(); + XEnumeration iter = (XEnumeration) i_Result; + XNode[][] bindings = toSeqs(iter); + if (vars.length != i_Vars.length) { + log.println("var lengths differ"); + return false; + } + if (bindings.length != i_Bindings.length) { + log.println("binding lengths differ: " + i_Bindings.length + + " vs " + bindings.length ); + return false; + } + java.util.Arrays.sort(bindings, new BindingComp()); + java.util.Arrays.sort(i_Bindings, new BindingComp()); + for (int i = 0; i < i_Bindings.length; ++i) { + if (i_Bindings[i].length != i_Vars.length) { + log.println("TEST ERROR!"); + throw new Exception(); + } + if (bindings[i].length != i_Vars.length) { + log.println("binding length and var length differ"); + return false; + } + for (int j = 0; j < i_Vars.length; ++j) { + if (!eq(bindings[i][j], i_Bindings[i][j])) { + log.println("bindings differ: " + + toS(bindings[i][j]) + " != " + toS(i_Bindings[i][j])); + return false; + } + } + } + for (int i = 0; i < i_Vars.length; ++i) { + if (!vars[i].equals(i_Vars[i])) { + log.println("variable names differ: " + + vars[i] + " != " + i_Vars[i]); + return false; + } + } + return true; + } + + static String mkNamespace(String i_prefix, String i_namespace) + { + return "PREFIX " + i_prefix + ": <" + i_namespace + ">\n"; + } + + static String mkNss() + { + String namespaces = mkNamespace("rdf", + "http://www.w3.org/1999/02/22-rdf-syntax-ns#"); + namespaces += mkNamespace("pkg", + "http://docs.oasis-open.org/opendocument/meta/package/common#"); + namespaces += mkNamespace("odf", + "http://docs.oasis-open.org/opendocument/meta/package/odf#"); + return namespaces; + } + + class TestRange implements XTextRange, XMetadatable, XServiceInfo + { + String m_Stream; + String m_XmlId; + String m_Text; + TestRange(String i_Str) { m_Text = i_Str; } + + public String getStringValue() { return ""; } + public String getNamespace() { return ""; } + public String getLocalName() { return ""; } + + public StringPair getMetadataReference() + { return new StringPair(m_Stream, m_XmlId); } + public void setMetadataReference(StringPair i_Ref) + throws IllegalArgumentException + { m_Stream = (String)i_Ref.First; m_XmlId = (String)i_Ref.Second; } + public void ensureMetadataReference() + { m_Stream = "content.xml"; m_XmlId = "42"; } + + public String getImplementationName() { return null; } + public String[] getSupportedServiceNames() { return null; } + public boolean supportsService(String i_Svc) + { return i_Svc.equals("com.sun.star.text.Paragraph"); } + + public XText getText() { return null; } + public XTextRange getStart() { return null; } + public XTextRange getEnd() { return null; } + public String getString() { return m_Text; } + public void setString(String i_Str) { m_Text = i_Str; } + } +} + diff --git a/unoxml/qa/complex/makefile.mk b/unoxml/qa/complex/makefile.mk new file mode 100644 index 000000000000..4a7cd55fd25f --- /dev/null +++ b/unoxml/qa/complex/makefile.mk @@ -0,0 +1,61 @@ +#************************************************************************* +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. +# +# Copyright 2008 by Sun Microsystems, Inc. +# +# OpenOffice.org - a multi-platform office productivity suite +# +# $RCSfile: makefile.mk,v $ +# +# $Revision: 1.2.6.1 $ +# +# This file is part of OpenOffice.org. +# +# OpenOffice.org is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License version 3 +# only, as published by the Free Software Foundation. +# +# OpenOffice.org is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License version 3 for more details +# (a copy is included in the LICENSE file that accompanied this code). +# +# You should have received a copy of the GNU Lesser General Public License +# version 3 along with OpenOffice.org. If not, see +# <http://www.openoffice.org/license.html> +# for a copy of the LGPLv3 License. +#***********************************************************************/ + +PRJ = ..$/.. +TARGET = unoxml_test +PRJNAME = $(TARGET) +PACKAGE = complex$/unoxml + +# --- Settings ----------------------------------------------------- +.INCLUDE: settings.mk + + +#----- compile .java files ----------------------------------------- + +JARFILES = ridl.jar unoil.jar jurt.jar juh.jar java_uno.jar OOoRunner.jar +JAVAFILES = RDFRepositoryTest.java \ + +JAVACLASSFILES = $(foreach,i,$(JAVAFILES) $(CLASSDIR)$/$(PACKAGE)$/$(i:b).class) + +#----- make a jar from compiled files ------------------------------ + +MAXLINELENGTH = 100000 + +JARCLASSDIRS = $(PACKAGE) +JARTARGET = $(TARGET).jar +JARCOMPRESS = TRUE + +# --- Targets ------------------------------------------------------ + +.INCLUDE : target.mk + + +run: + +java -cp $(CLASSPATH) org.openoffice.Runner -TestBase java_complex -sce tests.sce -tdoc $(PWD)$/testdocuments + diff --git a/unoxml/qa/complex/testdocuments/example.rdf b/unoxml/qa/complex/testdocuments/example.rdf new file mode 100644 index 000000000000..07ef7f07b6fd --- /dev/null +++ b/unoxml/qa/complex/testdocuments/example.rdf @@ -0,0 +1,27 @@ +<?xml version="1.0"?> +<rdf:RDF + xmlns:pkg="http://docs.oasis-open.org/opendocument/meta/package/common#" + xmlns:odf="http://docs.oasis-open.org/opendocument/meta/package/odf#" + xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"> +<pkg:Package rdf:about="urn:uuid:224ab023-77b8-4396-a75a-8cecd85b81e3"> + <pkg:hasPart> + <odf:ContentFile pkg:path="content.xml"> + <pkg:hasPart> + <odf:Element rdf:about="uri:example-element-1" + pkg:idref="ID_A"/> + </pkg:hasPart> + <pkg:hasPart> + <odf:Element rdf:about="uri:example-element-2" + pkg:idref="ID_B"/> + </pkg:hasPart> + </odf:ContentFile> + </pkg:hasPart> + <pkg:hasPart> + <odf:MetadataFile rdf:about="http://hospital-employee/doctor" + pkg:path="meta/hospital/doctor.rdf"> + <rdf:type rdf:resource="http://medical-employee/data"/> + <rdf:type rdf:resource="http://www.w3.org/2006/vcard/ns#"/> + </odf:MetadataFile> + </pkg:hasPart> +</pkg:Package> +</rdf:RDF> diff --git a/unoxml/qa/complex/tests.sce b/unoxml/qa/complex/tests.sce new file mode 100644 index 000000000000..20596551f156 --- /dev/null +++ b/unoxml/qa/complex/tests.sce @@ -0,0 +1 @@ +-o complex.unoxml.RDFRepositoryTest diff --git a/unoxml/source/dom/attr.cxx b/unoxml/source/dom/attr.cxx new file mode 100644 index 000000000000..946ed0e68652 --- /dev/null +++ b/unoxml/source/dom/attr.cxx @@ -0,0 +1,156 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2008 by Sun Microsystems, Inc. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: attr.cxx,v $ + * $Revision: 1.10 $ + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#include "attr.hxx" +#include "element.hxx" +#include <com/sun/star/xml/dom/DOMException.hdl> +#include <string.h> + +namespace DOM +{ + CAttr::CAttr(const xmlAttrPtr pAttr) + { + m_aAttrPtr = pAttr; + m_aNodeType = NodeType_ATTRIBUTE_NODE; + init_node((xmlNodePtr)pAttr); + } + + OUString SAL_CALL CAttr::getNodeName() + throw (RuntimeException) + { + return getName(); + } + OUString SAL_CALL CAttr::getNodeValue() + throw (RuntimeException) + { + return getValue(); + } + OUString SAL_CALL CAttr::getLocalName() + throw (RuntimeException) + { + return getName(); + } + + + /** + Returns the name of this attribute. + */ + OUString SAL_CALL CAttr::getName() throw (RuntimeException) + { + OUString aName; + if (m_aAttrPtr != NULL) + { + aName = OUString((char*)m_aAttrPtr->name, strlen((char*)m_aAttrPtr->name), RTL_TEXTENCODING_UTF8); + } + return aName; + } + + /** + The Element node this attribute is attached to or null if this + attribute is not in use. + */ + Reference< XElement > SAL_CALL CAttr::getOwnerElement() + throw (RuntimeException) + { + Reference< XElement > aElement; + if (m_aAttrPtr != NULL && m_aAttrPtr->parent != NULL) + { + aElement = Reference< XElement >(static_cast< CElement* >(CNode::get(m_aAttrPtr->parent))); + } + return aElement; + } + + /** + If this attribute was explicitly given a value in the original + document, this is true; otherwise, it is false. + */ + sal_Bool SAL_CALL CAttr::getSpecified() + throw (RuntimeException) + { + // XXX what is this supposed do exactly? + return sal_False; + } + + /** + On retrieval, the value of the attribute is returned as a string. + */ + OUString SAL_CALL CAttr::getValue() + throw (RuntimeException) + { + OUString aName; + if (m_aAttrPtr != NULL && m_aAttrPtr->children != NULL) + { + aName = OUString((char*)m_aAttrPtr->children->content, strlen((char*)m_aAttrPtr->children->content), + RTL_TEXTENCODING_UTF8); + } + return aName; + } + + /** + Sets the value of the attribute from a string. + */ + void SAL_CALL CAttr::setValue(const OUString& value) + throw (RuntimeException, DOMException) + { + // remember old value (for mutation event) + OUString sOldValue = getValue(); + + OString o1 = OUStringToOString(value, RTL_TEXTENCODING_UTF8); + xmlChar* xValue = (xmlChar*)o1.getStr(); + // xmlChar* xName = OUStringToOString(m_aAttrPtr->name, RTL_TEXTENCODING_UTF8).getStr(); + // this does not work if the attribute was created anew + // xmlNodePtr pNode = m_aAttrPtr->parent; + // xmlSetProp(pNode, m_aAttrPtr->name, xValue); + xmlChar *buffer = xmlEncodeEntitiesReentrant(m_aAttrPtr->doc, xValue); + m_aAttrPtr->children = xmlStringGetNodeList(m_aAttrPtr->doc, buffer); + xmlNodePtr tmp = m_aAttrPtr->children; + while (tmp != NULL) { + tmp->parent = (xmlNodePtr) m_aNodePtr; + tmp->doc = m_aAttrPtr->doc; + if (tmp->next == NULL) + m_aNodePtr->last = tmp; + tmp = tmp->next; + } + + // dispatch DOM events to signal change in attribute value + // dispatch DomAttrModified + DOMSubtreeModified + OUString sEventName( RTL_CONSTASCII_USTRINGPARAM("DOMAttrModified") ); + Reference< XDocumentEvent > docevent(getOwnerDocument(), UNO_QUERY); + Reference< XMutationEvent > event(docevent->createEvent(sEventName),UNO_QUERY); + event->initMutationEvent( + sEventName, sal_True, sal_False, + Reference<XNode>( static_cast<XAttr*>( this ) ), + sOldValue, value, getName(), AttrChangeType_MODIFICATION ); + dispatchEvent(Reference< XEvent >(event, UNO_QUERY)); + dispatchSubtreeModified(); + xmlFree(buffer); + } + +} diff --git a/unoxml/source/dom/attr.hxx b/unoxml/source/dom/attr.hxx new file mode 100644 index 000000000000..394a60357197 --- /dev/null +++ b/unoxml/source/dom/attr.hxx @@ -0,0 +1,213 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2008 by Sun Microsystems, Inc. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: attr.hxx,v $ + * $Revision: 1.10 $ + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#ifndef _ATTR_HXX +#define _ATTR_HXX + +#include <cppuhelper/implbase1.hxx> +#include <com/sun/star/uno/Reference.h> +#include <com/sun/star/uno/Exception.hpp> +#include <com/sun/star/xml/dom/XNode.hpp> +#include <com/sun/star/xml/dom/XAttr.hpp> +#include "node.hxx" +#include <libxml/tree.h> + +using ::rtl::OUString; +using namespace com::sun::star::uno; +using namespace com::sun::star::xml::dom; + +namespace DOM +{ + class CAttr : public cppu::ImplInheritanceHelper1< CNode, XAttr > + { + friend class CNode; + friend class CElement; + private: + xmlAttrPtr m_aAttrPtr; + + protected: + CAttr(const xmlAttrPtr aAttrPtr); + + public: + /** + Returns the name of this attribute. + */ + virtual OUString SAL_CALL getName() throw (RuntimeException); + + /** + The Element node this attribute is attached to or null if this + attribute is not in use. + */ + virtual Reference< XElement > SAL_CALL getOwnerElement() throw (RuntimeException); + + /** + If this attribute was explicitly given a value in the original + document, this is true; otherwise, it is false. + */ + virtual sal_Bool SAL_CALL getSpecified()throw (RuntimeException); + + /** + On retrieval, the value of the attribute is returned as a string. + */ + virtual OUString SAL_CALL getValue() throw (RuntimeException); + + /** + Sets the value of the attribute from a string. + */ + + virtual void SAL_CALL setValue(const OUString& value) throw (RuntimeException, DOMException); + + // resolve uno inheritance problems... + // overrides for XNode base + virtual OUString SAL_CALL getNodeName() + throw (RuntimeException); + virtual OUString SAL_CALL getNodeValue() + throw (RuntimeException); + virtual OUString SAL_CALL getLocalName() + throw (RuntimeException); + + // --- delegation for XNde base. + virtual Reference< XNode > SAL_CALL appendChild(const Reference< XNode >& newChild) + throw (RuntimeException, DOMException) + { + return CNode::appendChild(newChild); + } + virtual Reference< XNode > SAL_CALL cloneNode(sal_Bool deep) + throw (RuntimeException) + { + return CNode::cloneNode(deep); + } + virtual Reference< XNamedNodeMap > SAL_CALL getAttributes() + throw (RuntimeException) + { + return CNode::getAttributes(); + } + virtual Reference< XNodeList > SAL_CALL getChildNodes() + throw (RuntimeException) + { + return CNode::getChildNodes(); + } + virtual Reference< XNode > SAL_CALL getFirstChild() + throw (RuntimeException) + { + return CNode::getFirstChild(); + } + virtual Reference< XNode > SAL_CALL getLastChild() + throw (RuntimeException) + { + return CNode::getLastChild(); + } + virtual OUString SAL_CALL getNamespaceURI() + throw (RuntimeException) + { + return CNode::getNamespaceURI(); + } + virtual Reference< XNode > SAL_CALL getNextSibling() + throw (RuntimeException) + { + return CNode::getNextSibling(); + } + virtual NodeType SAL_CALL getNodeType() + throw (RuntimeException) + { + return CNode::getNodeType(); + } + virtual Reference< XDocument > SAL_CALL getOwnerDocument() + throw (RuntimeException) + { + return CNode::getOwnerDocument(); + } + virtual Reference< XNode > SAL_CALL getParentNode() + throw (RuntimeException) + { + return CNode::getParentNode(); + } + virtual OUString SAL_CALL getPrefix() + throw (RuntimeException) + { + return CNode::getPrefix(); + } + virtual Reference< XNode > SAL_CALL getPreviousSibling() + throw (RuntimeException) + { + return CNode::getPreviousSibling(); + } + virtual sal_Bool SAL_CALL hasAttributes() + throw (RuntimeException) + { + return CNode::hasAttributes(); + } + virtual sal_Bool SAL_CALL hasChildNodes() + throw (RuntimeException) + { + return CNode::hasChildNodes(); + } + virtual Reference< XNode > SAL_CALL insertBefore( + const Reference< XNode >& newChild, const Reference< XNode >& refChild) + throw (RuntimeException, DOMException) + { + return CNode::insertBefore(newChild, refChild); + } + virtual sal_Bool SAL_CALL isSupported(const OUString& feature, const OUString& ver) + throw (RuntimeException) + { + return CNode::isSupported(feature, ver); + } + virtual void SAL_CALL normalize() + throw (RuntimeException) + { + CNode::normalize(); + } + virtual Reference< XNode > SAL_CALL removeChild(const Reference< XNode >& oldChild) + throw (RuntimeException, DOMException) + { + return CNode::removeChild(oldChild); + } + virtual Reference< XNode > SAL_CALL replaceChild( + const Reference< XNode >& newChild, const Reference< XNode >& oldChild) + throw (RuntimeException, DOMException) + { + return CNode::replaceChild(newChild, oldChild); + } + virtual void SAL_CALL setNodeValue(const OUString& nodeValue) + throw (RuntimeException, DOMException) + { + return setValue(nodeValue); + } + virtual void SAL_CALL setPrefix(const OUString& prefix) + throw (RuntimeException, DOMException) + { + return CNode::setPrefix(prefix); + } + + }; +} + +#endif diff --git a/unoxml/source/dom/attributesmap.cxx b/unoxml/source/dom/attributesmap.cxx new file mode 100644 index 000000000000..46990e17bf6d --- /dev/null +++ b/unoxml/source/dom/attributesmap.cxx @@ -0,0 +1,214 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2008 by Sun Microsystems, Inc. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: attributesmap.cxx,v $ + * $Revision: 1.6 $ + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#include "attributesmap.hxx" +#include <string.h> + +namespace DOM +{ + CAttributesMap::CAttributesMap(const CElement* aElement) + : m_pElement(aElement) + { + } + + /** + The number of nodes in this map. + */ + sal_Int32 SAL_CALL CAttributesMap::getLength() throw (RuntimeException) + { + sal_Int32 count = 0; + xmlNodePtr pNode = m_pElement->m_aNodePtr; + if (pNode != NULL) + { + xmlAttrPtr cur = pNode->properties; + while (cur != NULL) + { + count++; + cur = cur->next; + } + } + return count; + + } + + /** + Retrieves a node specified by local name + */ + Reference< XNode > SAL_CALL CAttributesMap::getNamedItem(const OUString& name) throw (RuntimeException) + { + Reference< XNode > aNode; + xmlNodePtr pNode = m_pElement->m_aNodePtr; + if (pNode != NULL) + { + OString o1 = OUStringToOString(name, RTL_TEXTENCODING_UTF8); + xmlChar* xName = (xmlChar*)o1.getStr(); + xmlAttrPtr cur = pNode->properties; + while (cur != NULL) + { + if( strcmp((char*)xName, (char*)cur->name) == 0) + { + aNode = Reference< XNode >(static_cast<CNode*>(CNode::get((xmlNodePtr)cur))); + break; + } + cur = cur->next; + } + } + return aNode; + } + + /** + Retrieves a node specified by local name and namespace URI. + */ + Reference< XNode > SAL_CALL CAttributesMap::getNamedItemNS(const OUString& namespaceURI,const OUString& localName) throw (RuntimeException) + { + Reference< XNode > aNode; + xmlNodePtr pNode = m_pElement->m_aNodePtr; + if (pNode != NULL) + { + OString o1 = OUStringToOString(localName, RTL_TEXTENCODING_UTF8); + xmlChar* xName = (xmlChar*)o1.getStr(); + OString o2 = OUStringToOString(namespaceURI, RTL_TEXTENCODING_UTF8); + xmlChar* xNs = (xmlChar*)o1.getStr(); + xmlNsPtr pNs = xmlSearchNs(pNode->doc, pNode, xNs); + xmlAttrPtr cur = pNode->properties; + while (cur != NULL && pNs != NULL) + { + if( strcmp((char*)xName, (char*)cur->name) == 0 && + cur->ns == pNs) + { + aNode = Reference< XNode >(static_cast< CNode* >(CNode::get((xmlNodePtr)cur))); + break; + } + cur = cur->next; + } + } + return aNode; + } + + /** + Returns the indexth item in the map. + */ + Reference< XNode > SAL_CALL CAttributesMap::item(sal_Int32 index) throw (RuntimeException) + { + Reference< XNode > aNode; + xmlNodePtr pNode = m_pElement->m_aNodePtr; + if (pNode != NULL) + { + xmlAttrPtr cur = pNode->properties; + sal_Int32 count = 0; + while (cur != NULL) + { + if (count == index) + { + aNode = Reference< XNode >(static_cast< CNode* >(CNode::get((xmlNodePtr)cur))); + break; + } + count++; + cur = cur->next; + } + } + return aNode; + + } + + /** + Removes a node specified by name. + */ + Reference< XNode > SAL_CALL CAttributesMap::removeNamedItem(const OUString& name) throw (RuntimeException) + { + Reference< XNode > aNode; + xmlNodePtr pNode = m_pElement->m_aNodePtr; + if (pNode != NULL) + { + OString o1 = OUStringToOString(name, RTL_TEXTENCODING_UTF8); + xmlChar* xName = (xmlChar*)o1.getStr(); + xmlAttrPtr cur = pNode->properties; + while (cur != NULL) + { + if( strcmp((char*)xName, (char*)cur->name) == 0) + { + aNode = Reference< XNode >(static_cast< CNode* >(CNode::get((xmlNodePtr)cur))); + xmlUnlinkNode((xmlNodePtr)cur); + break; + } + cur = cur->next; + } + } + return aNode; + } + + /** + // Removes a node specified by local name and namespace URI. + */ + Reference< XNode > SAL_CALL CAttributesMap::removeNamedItemNS(const OUString& namespaceURI, const OUString& localName) throw (RuntimeException) + { + Reference< XNode > aNode; + xmlNodePtr pNode = m_pElement->m_aNodePtr; + if (pNode != NULL) + { + OString o1 = OUStringToOString(localName, RTL_TEXTENCODING_UTF8); + xmlChar* xName = (xmlChar*)o1.getStr(); + OString o2 = OUStringToOString(namespaceURI, RTL_TEXTENCODING_UTF8); + xmlChar* xNs = (xmlChar*)o1.getStr(); + xmlNsPtr pNs = xmlSearchNs(pNode->doc, pNode, xNs); + xmlAttrPtr cur = pNode->properties; + while (cur != NULL && pNs != NULL) + { + if( strcmp((char*)xName, (char*)cur->name) == 0 && + cur->ns == pNs) + { + aNode = Reference< XNode >(static_cast< CNode* >(CNode::get((xmlNodePtr)cur))); + xmlUnlinkNode((xmlNodePtr)cur); + break; + } + cur = cur->next; + } + } + return aNode; + } + + /** + // Adds a node using its nodeName attribute. + */ + Reference< XNode > SAL_CALL CAttributesMap::setNamedItem(const Reference< XNode >& arg) throw (RuntimeException) + { + return arg; + // return Reference< XNode >(); + } + + /** + Adds a node using its namespaceURI and localName. + */ + Reference< XNode > SAL_CALL CAttributesMap::setNamedItemNS(const Reference< XNode >& arg) throw (RuntimeException) + { + return arg; + // return Reference< XNode >(); + } +} diff --git a/unoxml/source/dom/attributesmap.hxx b/unoxml/source/dom/attributesmap.hxx new file mode 100644 index 000000000000..95badb12985b --- /dev/null +++ b/unoxml/source/dom/attributesmap.hxx @@ -0,0 +1,100 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2008 by Sun Microsystems, Inc. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: attributesmap.hxx,v $ + * $Revision: 1.5 $ + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#ifndef _ATTRIBUTESMAP_HXX +#define _ATTRIBUTESMAP_HXX + +#include <map> +#include <sal/types.h> +#include <cppuhelper/implbase1.hxx> +#include <com/sun/star/uno/Reference.h> +#include <com/sun/star/uno/Exception.hpp> +#include <com/sun/star/xml/dom/XNode.hpp> +#include <com/sun/star/xml/dom/XNamedNodeMap.hpp> +#include "node.hxx" +#include "element.hxx" +#include "attr.hxx" + +using ::rtl::OUString; +using namespace com::sun::star::uno; +using namespace com::sun::star::xml::dom; + +namespace DOM +{ + class CAttributesMap : public cppu::WeakImplHelper1< XNamedNodeMap > + { + private: + const CElement* m_pElement; + public: + CAttributesMap(const CElement* aDocType); + + /** + The number of nodes in this map. + */ + virtual sal_Int32 SAL_CALL getLength() throw (RuntimeException); + + /** + Retrieves a node specified by local name + */ + virtual Reference< XNode > SAL_CALL getNamedItem(const OUString& name) throw (RuntimeException); + + /** + Retrieves a node specified by local name and namespace URI. + */ + virtual Reference< XNode > SAL_CALL getNamedItemNS(const OUString& namespaceURI, const OUString& localName) throw (RuntimeException); + + /** + Returns the indexth item in the map. + */ + virtual Reference< XNode > SAL_CALL item(sal_Int32 index) throw (RuntimeException); + + /** + Removes a node specified by name. + */ + virtual Reference< XNode > SAL_CALL removeNamedItem(const OUString& name) throw (RuntimeException); + + /** + // Removes a node specified by local name and namespace URI. + */ + virtual Reference< XNode > SAL_CALL removeNamedItemNS(const OUString& namespaceURI, const OUString& localName) throw (RuntimeException); + + /** + // Adds a node using its nodeName attribute. + */ + virtual Reference< XNode > SAL_CALL setNamedItem(const Reference< XNode >& arg) throw (RuntimeException); + + /** + Adds a node using its namespaceURI and localName. + */ + virtual Reference< XNode > SAL_CALL setNamedItemNS(const Reference< XNode >& arg) throw (RuntimeException); + }; +} + +#endif diff --git a/unoxml/source/dom/cdatasection.cxx b/unoxml/source/dom/cdatasection.cxx new file mode 100644 index 000000000000..3613f1b68704 --- /dev/null +++ b/unoxml/source/dom/cdatasection.cxx @@ -0,0 +1,63 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2008 by Sun Microsystems, Inc. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: cdatasection.cxx,v $ + * $Revision: 1.6 $ + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#include "cdatasection.hxx" + +#include <com/sun/star/xml/sax/XExtendedDocumentHandler.hpp> + +namespace DOM +{ + CCDATASection::CCDATASection(const xmlNodePtr aNodePtr) + { + m_aNodeType = NodeType_CDATA_SECTION_NODE; + init_text(aNodePtr); + } + + void SAL_CALL CCDATASection::saxify( + const Reference< XDocumentHandler >& i_xHandler) { + if (!i_xHandler.is()) throw RuntimeException(); + Reference< XExtendedDocumentHandler > xExtended(i_xHandler, UNO_QUERY); + if (xExtended.is()) { + xExtended->startCDATA(); + i_xHandler->characters(getData()); + xExtended->endCDATA(); + } + } + + OUString SAL_CALL CCDATASection::getNodeName()throw (RuntimeException) + { + return OUString::createFromAscii("#cdata-section"); + } + + OUString SAL_CALL CCDATASection::getNodeValue() throw (RuntimeException) + { + return CCharacterData::getData(); + } +} diff --git a/unoxml/source/dom/cdatasection.hxx b/unoxml/source/dom/cdatasection.hxx new file mode 100644 index 000000000000..67aedbdbeee2 --- /dev/null +++ b/unoxml/source/dom/cdatasection.hxx @@ -0,0 +1,233 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2008 by Sun Microsystems, Inc. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: cdatasection.hxx,v $ + * $Revision: 1.8 $ + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#ifndef _CDATASECTION_HXX +#define _CDATASECTION_HXX + +#include <com/sun/star/uno/Reference.h> +#include <com/sun/star/uno/Exception.hpp> +#include <com/sun/star/xml/dom/XCDATASection.hpp> + +#include "text.hxx" + +using ::rtl::OUString; +using namespace com::sun::star::uno; +using namespace com::sun::star::xml::dom; + +namespace DOM +{ + class CCDATASection : public cppu::ImplInheritanceHelper1< CText, XCDATASection > + { + friend class CNode; + protected: + CCDATASection(const xmlNodePtr aNodePtr); + + public: + + virtual void SAL_CALL saxify( + const Reference< XDocumentHandler >& i_xHandler); + + virtual Reference< XText > SAL_CALL splitText(sal_Int32 offset) + throw (RuntimeException) + { + return CText::splitText(offset); + } + + // --- delegations for XCharacterData + virtual void SAL_CALL appendData(const OUString& arg) + throw (RuntimeException, DOMException) + { + CCharacterData::appendData(arg); + } + virtual void SAL_CALL deleteData(sal_Int32 offset, sal_Int32 count) + throw (RuntimeException, DOMException) + { + CCharacterData::deleteData(offset, count); + } + virtual OUString SAL_CALL getData() throw (RuntimeException) + { + return CCharacterData::getData(); + } + virtual sal_Int32 SAL_CALL getLength() throw (RuntimeException) + { + return CCharacterData::getLength(); + } + virtual void SAL_CALL insertData(sal_Int32 offset, const OUString& arg) + throw (RuntimeException, DOMException) + { + CCharacterData::insertData(offset, arg); + } + virtual void SAL_CALL replaceData(sal_Int32 offset, sal_Int32 count, const OUString& arg) + throw (RuntimeException, DOMException) + { + CCharacterData::replaceData(offset, count, arg); + } + virtual void SAL_CALL setData(const OUString& data) + throw (RuntimeException, DOMException) + { + CCharacterData::setData(data); + } + virtual OUString SAL_CALL subStringData(sal_Int32 offset, sal_Int32 count) + throw (RuntimeException, DOMException) + { + return CCharacterData::subStringData(offset, count); + } + + + // --- overrides for XNode base + virtual OUString SAL_CALL getNodeName() + throw (RuntimeException); + virtual OUString SAL_CALL getNodeValue() + throw (RuntimeException); + + // --- delegation for XNde base. + virtual Reference< XNode > SAL_CALL appendChild(const Reference< XNode >& newChild) + throw (RuntimeException, DOMException) + { + return CNode::appendChild(newChild); + } + virtual Reference< XNode > SAL_CALL cloneNode(sal_Bool deep) + throw (RuntimeException) + { + return CNode::cloneNode(deep); + } + virtual Reference< XNamedNodeMap > SAL_CALL getAttributes() + throw (RuntimeException) + { + return CNode::getAttributes(); + } + virtual Reference< XNodeList > SAL_CALL getChildNodes() + throw (RuntimeException) + { + return CNode::getChildNodes(); + } + virtual Reference< XNode > SAL_CALL getFirstChild() + throw (RuntimeException) + { + return CNode::getFirstChild(); + } + virtual Reference< XNode > SAL_CALL getLastChild() + throw (RuntimeException) + { + return CNode::getLastChild(); + } + virtual OUString SAL_CALL getLocalName() + throw (RuntimeException) + { + return CNode::getLocalName(); + } + virtual OUString SAL_CALL getNamespaceURI() + throw (RuntimeException) + { + return CNode::getNamespaceURI(); + } + virtual Reference< XNode > SAL_CALL getNextSibling() + throw (RuntimeException) + { + return CNode::getNextSibling(); + } + virtual NodeType SAL_CALL getNodeType() + throw (RuntimeException) + { + return CNode::getNodeType(); + } + virtual Reference< XDocument > SAL_CALL getOwnerDocument() + throw (RuntimeException) + { + return CNode::getOwnerDocument(); + } + virtual Reference< XNode > SAL_CALL getParentNode() + throw (RuntimeException) + { + return CNode::getParentNode(); + } + virtual OUString SAL_CALL getPrefix() + throw (RuntimeException) + { + return CNode::getPrefix(); + } + virtual Reference< XNode > SAL_CALL getPreviousSibling() + throw (RuntimeException) + { + return CNode::getPreviousSibling(); + } + virtual sal_Bool SAL_CALL hasAttributes() + throw (RuntimeException) + { + return CNode::hasAttributes(); + } + virtual sal_Bool SAL_CALL hasChildNodes() + throw (RuntimeException) + { + return CNode::hasChildNodes(); + } + virtual Reference< XNode > SAL_CALL insertBefore( + const Reference< XNode >& newChild, const Reference< XNode >& refChild) + throw (RuntimeException, DOMException) + { + return CNode::insertBefore(newChild, refChild); + } + virtual sal_Bool SAL_CALL isSupported(const OUString& feature, const OUString& ver) + throw (RuntimeException) + { + return CNode::isSupported(feature, ver); + } + virtual void SAL_CALL normalize() + throw (RuntimeException) + { + CNode::normalize(); + } + virtual Reference< XNode > SAL_CALL removeChild(const Reference< XNode >& oldChild) + throw (RuntimeException, DOMException) + { + return CNode::removeChild(oldChild); + } + virtual Reference< XNode > SAL_CALL replaceChild( + const Reference< XNode >& newChild, const Reference< XNode >& oldChild) + throw (RuntimeException, DOMException) + { + return CNode::replaceChild(newChild, oldChild); + } + virtual void SAL_CALL setNodeValue(const OUString& nodeValue) + throw (RuntimeException, DOMException) + { + return CNode::setNodeValue(nodeValue); + } + virtual void SAL_CALL setPrefix(const OUString& prefix) + throw (RuntimeException, DOMException) + { + return CNode::setPrefix(prefix); + } + + }; + +} + +#endif diff --git a/unoxml/source/dom/characterdata.cxx b/unoxml/source/dom/characterdata.cxx new file mode 100644 index 000000000000..586a16b9b76e --- /dev/null +++ b/unoxml/source/dom/characterdata.cxx @@ -0,0 +1,236 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2008 by Sun Microsystems, Inc. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: characterdata.cxx,v $ + * $Revision: 1.11 $ + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#include <com/sun/star/xml/dom/events/XDocumentEvent.hpp> +#include "characterdata.hxx" +#include "../events/mutationevent.hxx" +#include <string.h> + +namespace DOM +{ + + CCharacterData::CCharacterData() + {} + + void CCharacterData::_dispatchEvent(const OUString& prevValue, const OUString& newValue) + { + Reference< XDocumentEvent > docevent(getOwnerDocument(), UNO_QUERY); + Reference< XMutationEvent > event(docevent->createEvent( + OUString::createFromAscii("DOMCharacterDataModified")), UNO_QUERY); + event->initMutationEvent( + OUString::createFromAscii("DOMCharacterDataModified"), + sal_True, sal_False, Reference< XNode >(), + prevValue, newValue, OUString(), (AttrChangeType)0 ); + dispatchEvent(Reference< XEvent >(event, UNO_QUERY)); + dispatchSubtreeModified(); + } + + void CCharacterData::init_characterdata(const xmlNodePtr aNodePtr) + { + init_node(aNodePtr); + } + + /** + Append the string to the end of the character data of the node. + */ + void SAL_CALL CCharacterData::appendData(const OUString& arg) + throw (RuntimeException, DOMException) + { + if (m_aNodePtr != NULL) + { + OUString oldValue((char*)m_aNodePtr->content, strlen((char*)m_aNodePtr->content), RTL_TEXTENCODING_UTF8); + xmlNodeAddContent(m_aNodePtr, (const xmlChar*)(OUStringToOString(arg, RTL_TEXTENCODING_UTF8).getStr())); + OUString newValue((char*)m_aNodePtr->content, strlen((char*)m_aNodePtr->content), RTL_TEXTENCODING_UTF8); + _dispatchEvent(oldValue, newValue); + } + } + + /** + Remove a range of 16-bit units from the node. + */ + void SAL_CALL CCharacterData::deleteData(sal_Int32 offset, sal_Int32 count) + throw (RuntimeException, DOMException) + { + if (m_aNodePtr != NULL) + { + // get current data + OString aData((const sal_Char*)xmlNodeGetContent(m_aNodePtr)); + OUString tmp(aData, aData.getLength(), RTL_TEXTENCODING_UTF8); + if (offset > tmp.getLength() || offset < 0 || count < 0) { + DOMException e; + e.Code = DOMExceptionType_INDEX_SIZE_ERR; + throw e; + } + if ((offset+count) > tmp.getLength()) + count = tmp.getLength() - offset; + + OUString tmp2 = tmp.copy(0, offset); + tmp2 += tmp.copy(offset+count, tmp.getLength() - (offset+count)); + OUString oldValue((char*)m_aNodePtr->content, strlen((char*)m_aNodePtr->content), RTL_TEXTENCODING_UTF8); + xmlNodeSetContent(m_aNodePtr, (const xmlChar*)(OUStringToOString(tmp2, RTL_TEXTENCODING_UTF8).getStr())); + OUString newValue((char*)m_aNodePtr->content, strlen((char*)m_aNodePtr->content), RTL_TEXTENCODING_UTF8); + _dispatchEvent(oldValue, newValue); + + } + } + + + /** + Return the character data of the node that implements this interface. + */ + OUString SAL_CALL CCharacterData::getData() throw (RuntimeException) + { + OUString aData; + if (m_aNodePtr != NULL) + { + OSL_ENSURE(m_aNodePtr->content, "character data node with NULL content, please inform lars.oppermann@sun.com!"); + if (m_aNodePtr->content != NULL) + { + aData = OUString((const sal_Char*)m_aNodePtr->content, strlen((const sal_Char*)m_aNodePtr->content), RTL_TEXTENCODING_UTF8); + } + } + return aData; + } + + /** + The number of 16-bit units that are available through data and the + substringData method below. + */ + sal_Int32 CCharacterData::getLength() throw (RuntimeException) + { + sal_Int32 length = 0; + if (m_aNodePtr != NULL) + { + OUString aData((const sal_Char*)m_aNodePtr->content, strlen((const sal_Char*)m_aNodePtr->content), RTL_TEXTENCODING_UTF8); + length = aData.getLength(); + } + return length; + } + + /** + Insert a string at the specified 16-bit unit offset. + */ + void SAL_CALL CCharacterData::insertData(sal_Int32 offset, const OUString& arg) + throw (RuntimeException, DOMException) + { + if (m_aNodePtr != NULL) + { + // get current data + OString aData((const sal_Char*)xmlNodeGetContent(m_aNodePtr)); + OUString tmp(aData, aData.getLength(), RTL_TEXTENCODING_UTF8); + if (offset > tmp.getLength() || offset < 0) { + DOMException e; + e.Code = DOMExceptionType_INDEX_SIZE_ERR; + throw e; + } + + OUString tmp2 = tmp.copy(0, offset); + tmp2 += arg; + tmp2 += tmp.copy(offset, tmp.getLength() - offset); + OUString oldValue((char*)m_aNodePtr->content, strlen((char*)m_aNodePtr->content), RTL_TEXTENCODING_UTF8); + xmlNodeSetContent(m_aNodePtr, (const xmlChar*)(OUStringToOString(tmp2, RTL_TEXTENCODING_UTF8).getStr())); + OUString newValue((char*)m_aNodePtr->content, strlen((char*)m_aNodePtr->content), RTL_TEXTENCODING_UTF8); + _dispatchEvent(oldValue, newValue); + + } + } + + + /** + Replace the characters starting at the specified 16-bit unit offset + with the specified string. + */ + void SAL_CALL CCharacterData::replaceData(sal_Int32 offset, sal_Int32 count, const OUString& arg) + throw (RuntimeException, DOMException) + { + if (m_aNodePtr != NULL) + { + // get current data + OString aData((const sal_Char*)xmlNodeGetContent(m_aNodePtr)); + OUString tmp(aData, aData.getLength(), RTL_TEXTENCODING_UTF8); + if (offset > tmp.getLength() || offset < 0 || count < 0){ + DOMException e; + e.Code = DOMExceptionType_INDEX_SIZE_ERR; + throw e; + } + if ((offset+count) > tmp.getLength()) + count = tmp.getLength() - offset; + + OUString tmp2 = tmp.copy(0, offset); + tmp2 += arg; + tmp2 += tmp.copy(offset+count, tmp.getLength() - (offset+count)); + OUString oldValue((char*)m_aNodePtr->content, strlen((char*)m_aNodePtr->content), RTL_TEXTENCODING_UTF8); + xmlNodeSetContent(m_aNodePtr, (const xmlChar*)(OUStringToOString(tmp2, RTL_TEXTENCODING_UTF8).getStr())); + OUString newValue((char*)m_aNodePtr->content, strlen((char*)m_aNodePtr->content), RTL_TEXTENCODING_UTF8); + _dispatchEvent(oldValue, newValue); + } + } + + /** + Set the character data of the node that implements this interface. + */ + void SAL_CALL CCharacterData::setData(const OUString& data) + throw (RuntimeException, DOMException) + { + if (m_aNodePtr != NULL) + { + OUString oldValue((char*)m_aNodePtr->content, strlen((char*)m_aNodePtr->content), RTL_TEXTENCODING_UTF8); + xmlNodeSetContent(m_aNodePtr, (const xmlChar*)(OUStringToOString(data, RTL_TEXTENCODING_UTF8).getStr())); + OUString newValue((char*)m_aNodePtr->content, strlen((char*)m_aNodePtr->content), RTL_TEXTENCODING_UTF8); + _dispatchEvent(oldValue, newValue); + + } + } + + /** + Extracts a range of data from the node. + */ + OUString SAL_CALL CCharacterData::subStringData(sal_Int32 offset, sal_Int32 count) + throw (RuntimeException, DOMException) + { + OUString aStr; + if (m_aNodePtr != NULL) + { + // get current data + OString aData((const sal_Char*)xmlNodeGetContent(m_aNodePtr)); + OUString tmp(aData, aData.getLength(), RTL_TEXTENCODING_UTF8); + if (offset > tmp.getLength() || offset < 0 || count < 0) { + DOMException e; + e.Code = DOMExceptionType_INDEX_SIZE_ERR; + throw e; + } + aStr = tmp.copy(offset, count); + } + return aStr; + } + + +} // namspace DOM + diff --git a/unoxml/source/dom/characterdata.hxx b/unoxml/source/dom/characterdata.hxx new file mode 100644 index 000000000000..b32a085ed8e4 --- /dev/null +++ b/unoxml/source/dom/characterdata.hxx @@ -0,0 +1,242 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2008 by Sun Microsystems, Inc. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: characterdata.hxx,v $ + * $Revision: 1.10 $ + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#ifndef _CHARACTERDATA_HXX +#define _CHARACTERDATA_HXX + +#include <sal/types.h> +#include <cppuhelper/implbase1.hxx> +#include <com/sun/star/uno/Reference.h> +#include <com/sun/star/uno/Exception.hpp> +#include <com/sun/star/xml/dom/XNode.hpp> +#include <com/sun/star/xml/dom/XCharacterData.hpp> +#include <com/sun/star/xml/dom/XElement.hpp> +#include <com/sun/star/xml/dom/XDOMImplementation.hpp> +#include <libxml/tree.h> +#include "node.hxx" + +using ::rtl::OUString; +using namespace com::sun::star::uno; +using namespace com::sun::star::xml::dom; + +namespace DOM +{ + class CCharacterData : public cppu::ImplInheritanceHelper1< CNode, XCharacterData > + { + + + protected: + CCharacterData(); + void init_characterdata(const xmlNodePtr aNodePtr); + void _dispatchEvent(const OUString& prevValue, const OUString& newValue); + + public: + /** + Append the string to the end of the character data of the node. + */ + virtual void SAL_CALL appendData(const OUString& arg) + throw (RuntimeException, DOMException); + + /** + Remove a range of 16-bit units from the node. + */ + virtual void SAL_CALL deleteData(sal_Int32 offset, sal_Int32 count) + throw (RuntimeException, DOMException); + + /** + Return the character data of the node that implements this interface. + */ + virtual OUString SAL_CALL getData() throw (RuntimeException); + + /** + The number of 16-bit units that are available through data and the + substringData method below. + */ + virtual sal_Int32 SAL_CALL getLength() throw (RuntimeException); + + /** + Insert a string at the specified 16-bit unit offset. + */ + virtual void SAL_CALL insertData(sal_Int32 offset, const OUString& arg) + throw (RuntimeException, DOMException); + + /** + Replace the characters starting at the specified 16-bit unit offset + with the specified string. + */ + virtual void SAL_CALL replaceData(sal_Int32 offset, sal_Int32 count, const OUString& arg) + throw (RuntimeException, DOMException); + + /** + Set the character data of the node that implements this interface. + */ + virtual void SAL_CALL setData(const OUString& data) + throw (RuntimeException, DOMException); + + /** + Extracts a range of data from the node. + */ + virtual OUString SAL_CALL subStringData(sal_Int32 offset, sal_Int32 count) + throw (RuntimeException, DOMException); + + // --- delegation for XNode base. + virtual Reference< XNode > SAL_CALL appendChild(const Reference< XNode >& newChild) + throw (RuntimeException, DOMException) + { + return CNode::appendChild(newChild); + } + virtual Reference< XNode > SAL_CALL cloneNode(sal_Bool deep) + throw (RuntimeException) + { + return CNode::cloneNode(deep); + } + virtual Reference< XNamedNodeMap > SAL_CALL getAttributes() + throw (RuntimeException) + { + return CNode::getAttributes(); + } + virtual Reference< XNodeList > SAL_CALL getChildNodes() + throw (RuntimeException) + { + return CNode::getChildNodes(); + } + virtual Reference< XNode > SAL_CALL getFirstChild() + throw (RuntimeException) + { + return CNode::getFirstChild(); + } + virtual Reference< XNode > SAL_CALL getLastChild() + throw (RuntimeException) + { + return CNode::getLastChild(); + } + virtual OUString SAL_CALL getLocalName() + throw (RuntimeException) + { + return CNode::getLocalName(); + } + virtual OUString SAL_CALL getNamespaceURI() + throw (RuntimeException) + { + return CNode::getNamespaceURI(); + } + virtual Reference< XNode > SAL_CALL getNextSibling() + throw (RuntimeException) + { + return CNode::getNextSibling(); + } + virtual OUString SAL_CALL getNodeName() + throw (RuntimeException) + { + return CNode::getNodeName(); + } + virtual NodeType SAL_CALL getNodeType() + throw (RuntimeException) + { + return CNode::getNodeType(); + } + virtual OUString SAL_CALL getNodeValue() + throw (RuntimeException) + { + return getData(); + } + virtual Reference< XDocument > SAL_CALL getOwnerDocument() + throw (RuntimeException) + { + return CNode::getOwnerDocument(); + } + virtual Reference< XNode > SAL_CALL getParentNode() + throw (RuntimeException) + { + return CNode::getParentNode(); + } + virtual OUString SAL_CALL getPrefix() + throw (RuntimeException) + { + return CNode::getPrefix(); + } + virtual Reference< XNode > SAL_CALL getPreviousSibling() + throw (RuntimeException) + { + return CNode::getPreviousSibling(); + } + virtual sal_Bool SAL_CALL hasAttributes() + throw (RuntimeException) + { + return CNode::hasAttributes(); + } + virtual sal_Bool SAL_CALL hasChildNodes() + throw (RuntimeException) + { + return CNode::hasChildNodes(); + } + virtual Reference< XNode > SAL_CALL insertBefore( + const Reference< XNode >& newChild, const Reference< XNode >& refChild) + throw (RuntimeException, DOMException) + { + return CNode::insertBefore(newChild, refChild); + } + virtual sal_Bool SAL_CALL isSupported(const OUString& feature, const OUString& ver) + throw (RuntimeException) + { + return CNode::isSupported(feature, ver); + } + virtual void SAL_CALL normalize() + throw (RuntimeException) + { + CNode::normalize(); + } + virtual Reference< XNode > SAL_CALL removeChild(const Reference< XNode >& oldChild) + throw (RuntimeException, DOMException) + { + return CNode::removeChild(oldChild); + } + virtual Reference< XNode > SAL_CALL replaceChild( + const Reference< XNode >& newChild, const Reference< XNode >& oldChild) + throw (RuntimeException, DOMException) + { + return CNode::replaceChild(newChild, oldChild); + } + virtual void SAL_CALL setNodeValue(const OUString& nodeValue) + throw (RuntimeException, DOMException) + { + return setData(nodeValue); + } + virtual void SAL_CALL setPrefix(const OUString& prefix) + throw (RuntimeException, DOMException) + { + return CNode::setPrefix(prefix); + } + + + }; +} + +#endif diff --git a/unoxml/source/dom/childlist.cxx b/unoxml/source/dom/childlist.cxx new file mode 100644 index 000000000000..15efe41d2a05 --- /dev/null +++ b/unoxml/source/dom/childlist.cxx @@ -0,0 +1,75 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2008 by Sun Microsystems, Inc. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: childlist.cxx,v $ + * $Revision: 1.5 $ + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#include "childlist.hxx" +namespace DOM +{ + CChildList::CChildList(const CNode* base) + : m_pNode(base->m_aNodePtr) + { + } + + /** + The number of nodes in the list. + */ + sal_Int32 SAL_CALL CChildList::getLength() throw (RuntimeException) + { + sal_Int32 length = 0; + if (m_pNode != NULL) + { + xmlNodePtr cur = m_pNode->children; + while (cur != NULL) + { + length++; + cur = cur->next; + } + } + return length; + + } + /** + Returns the indexth item in the collection. + */ + Reference< XNode > SAL_CALL CChildList::item(sal_Int32 index) throw (RuntimeException) + { + Reference< XNode >aNode; + if (m_pNode != NULL) + { + xmlNodePtr cur = m_pNode->children; + while (cur != NULL) + { + if (index-- == 0) + aNode = Reference< XNode >(CNode::get(cur)); + cur = cur->next; + } + } + return aNode; + } +} diff --git a/unoxml/source/dom/childlist.hxx b/unoxml/source/dom/childlist.hxx new file mode 100644 index 000000000000..b5ce420ed947 --- /dev/null +++ b/unoxml/source/dom/childlist.hxx @@ -0,0 +1,67 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2008 by Sun Microsystems, Inc. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: childlist.hxx,v $ + * $Revision: 1.5 $ + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#ifndef _CHILDLIST_HXX +#define _CHILDLIST_HXX + +#include <map> +#include <sal/types.h> +#include <cppuhelper/implbase1.hxx> +#include <com/sun/star/uno/Reference.h> +#include <com/sun/star/uno/Exception.hpp> +#include <com/sun/star/xml/dom/XNode.hpp> +#include <com/sun/star/xml/dom/XNodeList.hpp> +#include "node.hxx" +#include "libxml/tree.h" + +using ::rtl::OUString; +using namespace com::sun::star::uno; +using namespace com::sun::star::xml::dom; + +namespace DOM +{ + class CChildList : public cppu::WeakImplHelper1< XNodeList > + { + private: + const xmlNodePtr m_pNode; + public: + CChildList(const CNode* base); + /** + The number of nodes in the list. + */ + virtual sal_Int32 SAL_CALL getLength() throw (RuntimeException); + /** + Returns the indexth item in the collection. + */ + virtual Reference< XNode > SAL_CALL item(sal_Int32 index) throw (RuntimeException); + }; +} + +#endif diff --git a/unoxml/source/dom/comment.cxx b/unoxml/source/dom/comment.cxx new file mode 100644 index 000000000000..1469bb08ab11 --- /dev/null +++ b/unoxml/source/dom/comment.cxx @@ -0,0 +1,61 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2008 by Sun Microsystems, Inc. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: comment.cxx,v $ + * $Revision: 1.6 $ + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#include "comment.hxx" + +#include <com/sun/star/xml/sax/XExtendedDocumentHandler.hpp> + +namespace DOM +{ + CComment::CComment(const xmlNodePtr aNodePtr) + { + m_aNodeType = NodeType_COMMENT_NODE; + init_node(aNodePtr); + } + + void SAL_CALL CComment::saxify( + const Reference< XDocumentHandler >& i_xHandler) { + if (!i_xHandler.is()) throw RuntimeException(); + Reference< XExtendedDocumentHandler > xExtended(i_xHandler, UNO_QUERY); + if (xExtended.is()) { + xExtended->comment(getData()); + } + } + + OUString SAL_CALL CComment::getNodeName()throw (RuntimeException) + { + return OUString::createFromAscii("#comment"); + } + + OUString SAL_CALL CComment::getNodeValue() throw (RuntimeException) + { + return CCharacterData::getData(); + } +} diff --git a/unoxml/source/dom/comment.hxx b/unoxml/source/dom/comment.hxx new file mode 100644 index 000000000000..0b464b2517cf --- /dev/null +++ b/unoxml/source/dom/comment.hxx @@ -0,0 +1,225 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2008 by Sun Microsystems, Inc. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: comment.hxx,v $ + * $Revision: 1.8 $ + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#ifndef _COMMENT_HXX +#define _COMMENT_HXX + +#include <com/sun/star/uno/Reference.h> +#include <com/sun/star/uno/Exception.hpp> +#include <com/sun/star/xml/dom/XComment.hpp> +#include "characterdata.hxx" + +using ::rtl::OUString; +using namespace com::sun::star::uno; +using namespace com::sun::star::xml::dom; + +namespace DOM +{ + class CComment : public cppu::ImplInheritanceHelper1< CCharacterData, XComment > + { + friend class CNode; + protected: + CComment(const xmlNodePtr aNodePtr); + + public: + + virtual void SAL_CALL saxify( + const Reference< XDocumentHandler >& i_xHandler); + + // --- delegations for XCharacterData + virtual void SAL_CALL appendData(const OUString& arg) + throw (RuntimeException, DOMException) + { + CCharacterData::appendData(arg); + } + virtual void SAL_CALL deleteData(sal_Int32 offset, sal_Int32 count) + throw (RuntimeException, DOMException) + { + CCharacterData::deleteData(offset, count); + } + virtual OUString SAL_CALL getData() throw (RuntimeException) + { + return CCharacterData::getData(); + } + virtual sal_Int32 SAL_CALL getLength() throw (RuntimeException) + { + return CCharacterData::getLength(); + } + virtual void SAL_CALL insertData(sal_Int32 offset, const OUString& arg) + throw (RuntimeException, DOMException) + { + CCharacterData::insertData(offset, arg); + } + virtual void SAL_CALL replaceData(sal_Int32 offset, sal_Int32 count, const OUString& arg) + throw (RuntimeException, DOMException) + { + CCharacterData::replaceData(offset, count, arg); + } + virtual void SAL_CALL setData(const OUString& data) + throw (RuntimeException, DOMException) + { + CCharacterData::setData(data); + } + virtual OUString SAL_CALL subStringData(sal_Int32 offset, sal_Int32 count) + throw (RuntimeException, DOMException) + { + return CCharacterData::subStringData(offset, count); + } + + + // --- overrides for XNode base + virtual OUString SAL_CALL getNodeName() + throw (RuntimeException); + virtual OUString SAL_CALL getNodeValue() + throw (RuntimeException); + + // --- delegation for XNde base. + virtual Reference< XNode > SAL_CALL appendChild(const Reference< XNode >& newChild) + throw (RuntimeException, DOMException) + { + return CCharacterData::appendChild(newChild); + } + virtual Reference< XNode > SAL_CALL cloneNode(sal_Bool deep) + throw (RuntimeException) + { + return CCharacterData::cloneNode(deep); + } + virtual Reference< XNamedNodeMap > SAL_CALL getAttributes() + throw (RuntimeException) + { + return CCharacterData::getAttributes(); + } + virtual Reference< XNodeList > SAL_CALL getChildNodes() + throw (RuntimeException) + { + return CCharacterData::getChildNodes(); + } + virtual Reference< XNode > SAL_CALL getFirstChild() + throw (RuntimeException) + { + return CCharacterData::getFirstChild(); + } + virtual Reference< XNode > SAL_CALL getLastChild() + throw (RuntimeException) + { + return CCharacterData::getLastChild(); + } + virtual OUString SAL_CALL getLocalName() + throw (RuntimeException) + { + return CCharacterData::getLocalName(); + } + virtual OUString SAL_CALL getNamespaceURI() + throw (RuntimeException) + { + return CCharacterData::getNamespaceURI(); + } + virtual Reference< XNode > SAL_CALL getNextSibling() + throw (RuntimeException) + { + return CCharacterData::getNextSibling(); + } + virtual NodeType SAL_CALL getNodeType() + throw (RuntimeException) + { + return CCharacterData::getNodeType(); + } + virtual Reference< XDocument > SAL_CALL getOwnerDocument() + throw (RuntimeException) + { + return CCharacterData::getOwnerDocument(); + } + virtual Reference< XNode > SAL_CALL getParentNode() + throw (RuntimeException) + { + return CCharacterData::getParentNode(); + } + virtual OUString SAL_CALL getPrefix() + throw (RuntimeException) + { + return CCharacterData::getPrefix(); + } + virtual Reference< XNode > SAL_CALL getPreviousSibling() + throw (RuntimeException) + { + return CCharacterData::getPreviousSibling(); + } + virtual sal_Bool SAL_CALL hasAttributes() + throw (RuntimeException) + { + return CCharacterData::hasAttributes(); + } + virtual sal_Bool SAL_CALL hasChildNodes() + throw (RuntimeException) + { + return CCharacterData::hasChildNodes(); + } + virtual Reference< XNode > SAL_CALL insertBefore( + const Reference< XNode >& newChild, const Reference< XNode >& refChild) + throw (RuntimeException, DOMException) + { + return CCharacterData::insertBefore(newChild, refChild); + } + virtual sal_Bool SAL_CALL isSupported(const OUString& feature, const OUString& ver) + throw (RuntimeException) + { + return CCharacterData::isSupported(feature, ver); + } + virtual void SAL_CALL normalize() + throw (RuntimeException) + { + CCharacterData::normalize(); + } + virtual Reference< XNode > SAL_CALL removeChild(const Reference< XNode >& oldChild) + throw (RuntimeException, DOMException) + { + return CCharacterData::removeChild(oldChild); + } + virtual Reference< XNode > SAL_CALL replaceChild( + const Reference< XNode >& newChild, const Reference< XNode >& oldChild) + throw (RuntimeException, DOMException) + { + return CCharacterData::replaceChild(newChild, oldChild); + } + virtual void SAL_CALL setNodeValue(const OUString& nodeValue) + throw (RuntimeException, DOMException) + { + return CCharacterData::setNodeValue(nodeValue); + } + virtual void SAL_CALL setPrefix(const OUString& prefix) + throw (RuntimeException, DOMException) + { + return CCharacterData::setPrefix(prefix); + } + + }; +} + +#endif diff --git a/unoxml/source/dom/document.cxx b/unoxml/source/dom/document.cxx new file mode 100644 index 000000000000..9a1216376dcc --- /dev/null +++ b/unoxml/source/dom/document.cxx @@ -0,0 +1,754 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2008 by Sun Microsystems, Inc. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: document.cxx,v $ + * $Revision: 1.15 $ + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#include <com/sun/star/uno/Sequence.h> + +#include "document.hxx" +#include "attr.hxx" +#include "element.hxx" +#include "cdatasection.hxx" +#include "documentfragment.hxx" +#include "text.hxx" +#include "cdatasection.hxx" +#include "comment.hxx" +#include "processinginstruction.hxx" +#include "entityreference.hxx" +#include "documenttype.hxx" +#include "elementlist.hxx" +#include "domimplementation.hxx" + +#include "../events/event.hxx" +#include "../events/mutationevent.hxx" +#include "../events/uievent.hxx" +#include "../events/mouseevent.hxx" + +#include <string.h> + +#include <com/sun/star/xml/sax/FastToken.hpp> +#include <com/sun/star/xml/sax/XExtendedDocumentHandler.hpp> + +namespace DOM +{ + void CDocument::addnode(xmlNodePtr aNode) + { + if (aNode != (xmlNodePtr)m_aDocPtr) + { + Reference< XNode >* nref = new Reference< XNode >(CNode::get(aNode)); + m_aNodeRefList.push_back(nref); + } + } + + CDocument::~CDocument() + { + Reference< XNode >* pRef; + nodereflist_t::const_iterator r = m_aNodeRefList.begin(); + while (r!=m_aNodeRefList.end()) + { + pRef = *r; + delete pRef; + r++; + } + + // get rid of leftover instances, if anybody still holds a + // reference to one of these, it will be invalid! + /* + CNode* aNode = 0; + nodelist_t::const_iterator i = m_aNodeList.begin(); + while (i!=m_aNodeList.end()) + { + aNode = CNode::get(*i, sal_False); + if (aNode != 0) + { + // CNode::remove(*i); + // delete will remove + delete aNode; + } + i++; + } + */ + + xmlFreeDoc(m_aDocPtr); + + } + + CDocument::CDocument(xmlDocPtr aDocPtr): + m_aNodeRefList(), + m_aDocPtr(aDocPtr), + m_streamListeners() + { + // init node base + m_aNodeType = NodeType_DOCUMENT_NODE; + init_node((xmlNodePtr)m_aDocPtr); + } + + void SAL_CALL CDocument::saxify( + const Reference< XDocumentHandler >& i_xHandler) { + i_xHandler->startDocument(); + for (xmlNodePtr pChild = m_aNodePtr->children; + pChild != 0; pChild = pChild->next) { + CNode * pNode = CNode::get(pChild); + OSL_ENSURE(pNode != 0, "CNode::get returned 0"); + pNode->saxify(i_xHandler); + } + i_xHandler->endDocument(); + } + + void SAL_CALL CDocument::fastSaxify( Context& rContext ) { + rContext.mxDocHandler->startDocument(); + for (xmlNodePtr pChild = m_aNodePtr->children; + pChild != 0; pChild = pChild->next) { + CNode * pNode = CNode::get(pChild); + OSL_ENSURE(pNode != 0, "CNode::get returned 0"); + pNode->fastSaxify(rContext); + } + rContext.mxDocHandler->endDocument(); + } + + void SAL_CALL CDocument::addListener(const Reference< XStreamListener >& aListener ) + throw (RuntimeException) + { + m_streamListeners.insert(aListener); + } + + void SAL_CALL CDocument::removeListener(const Reference< XStreamListener >& aListener ) + throw (RuntimeException) + { + m_streamListeners.erase(aListener); + } + + // IO context functions for libxml2 interaction + typedef struct { + Reference< XOutputStream > stream; + bool allowClose; + } IOContext; + + extern "C" { + // write callback + // int xmlOutputWriteCallback (void * context, const char * buffer, int len) + static int writeCallback(void *context, const char* buffer, int len){ + // create a sequence and write it to the stream + IOContext *pContext = static_cast<IOContext*>(context); + Sequence<sal_Int8> bs(reinterpret_cast<const sal_Int8*>(buffer), len); + pContext->stream->writeBytes(bs); + return len; + } + + // clsoe callback + //int xmlOutputCloseCallback (void * context) + static int closeCallback(void *context) + { + IOContext *pContext = static_cast<IOContext*>(context); + if (pContext->allowClose) { + pContext->stream->closeOutput(); + } + return 0; + } + } // extern "C" + + void SAL_CALL CDocument::start() + throw (RuntimeException) + { + if (! m_rOutputStream.is()) return; + + // notify listners about start + listenerlist_t::const_iterator iter1 = m_streamListeners.begin(); + while (iter1 != m_streamListeners.end()) { + Reference< XStreamListener > aListener = *iter1; + aListener->started(); + iter1++; + } + + // setup libxml IO and write data to output stream + IOContext ioctx = {m_rOutputStream, false}; + xmlOutputBufferPtr pOut = xmlOutputBufferCreateIO( + writeCallback, closeCallback, &ioctx, NULL); + xmlSaveFileTo(pOut, m_aNodePtr->doc, NULL); + + // call listeners + listenerlist_t::const_iterator iter2 = m_streamListeners.begin(); + while (iter2 != m_streamListeners.end()) { + Reference< XStreamListener > aListener = *iter2; + aListener->closed(); + iter2++; + } + + } + + void SAL_CALL CDocument::terminate() + throw (RuntimeException) + { + // not supported + } + + void SAL_CALL CDocument::setOutputStream( const Reference< XOutputStream >& aStream ) + throw (RuntimeException) + { + m_rOutputStream = aStream; + } + + Reference< XOutputStream > SAL_CALL CDocument::getOutputStream() throw (RuntimeException) + { + return m_rOutputStream; + } + + // Creates an Attr of the given name. + Reference< XAttr > SAL_CALL CDocument::createAttribute(const OUString& name) + throw (RuntimeException, DOMException) + { + OString o1 = OUStringToOString(name, RTL_TEXTENCODING_UTF8); + xmlChar *xName = (xmlChar*)o1.getStr(); + return Reference< XAttr >(static_cast< CAttr* >( + CNode::get((xmlNodePtr)xmlNewDocProp(m_aDocPtr, xName, NULL)))); + }; + + // Creates an attribute of the given qualified name and namespace URI. + Reference< XAttr > SAL_CALL CDocument::createAttributeNS( + const OUString& ns, const OUString& qname) + throw (RuntimeException, DOMException) + { + + // libxml does not allow a NS definition to be attached to an + // attribute node - which is a good thing, since namespaces are + // only defined as parts of element nodes + // thus, we create a temporary element node which carries the ns definition + // and is removed/merged as soon as the attribute gets append to it's + // actual parent + sal_Int32 i = qname.indexOf(':'); + OString oPrefix, oName, oUri; + xmlChar *xPrefix, *xName, *xUri; + if (i != -1) + { + oPrefix = OUStringToOString(qname.copy(0, i), RTL_TEXTENCODING_UTF8); + xPrefix = (xmlChar*)oPrefix.getStr(); + oName = OUStringToOString(qname.copy(i+1, qname.getLength()-i-1), RTL_TEXTENCODING_UTF8); + } + else + { + xPrefix = (xmlChar*)""; + oName = OUStringToOString(qname, RTL_TEXTENCODING_UTF8); + } + xName = (xmlChar*)oName.getStr(); + oUri = OUStringToOString(ns, RTL_TEXTENCODING_UTF8); + xUri = (xmlChar*)oUri.getStr(); + + // create the carrier node + xmlNodePtr pNode = xmlNewDocNode(m_aDocPtr, NULL, (xmlChar*)"__private", NULL); + xmlNsPtr pNs = xmlNewNs(pNode, xUri, xPrefix); + xmlAttrPtr pAttr = xmlNewNsProp(pNode, pNs, xName, NULL); + return Reference< XAttr >(static_cast< CAttr* >(CNode::get((xmlNodePtr)pAttr))); + }; + + // Creates a CDATASection node whose value is the specified string. + Reference< XCDATASection > SAL_CALL CDocument::createCDATASection(const OUString& data) + throw (RuntimeException) + { + xmlChar *xData = (xmlChar*)OUStringToOString(data, RTL_TEXTENCODING_UTF8).getStr(); + xmlNodePtr pText = xmlNewCDataBlock(m_aDocPtr, xData, strlen((char*)xData)); + return Reference< XCDATASection >(static_cast< CCDATASection* >(CNode::get(pText))); + } + + // Creates a Comment node given the specified string. + Reference< XComment > SAL_CALL CDocument::createComment(const OUString& data) + throw (RuntimeException) + { + OString o1 = OUStringToOString(data, RTL_TEXTENCODING_UTF8); + xmlChar *xData = (xmlChar*)o1.getStr(); + xmlNodePtr pComment = xmlNewDocComment(m_aDocPtr, xData); + return Reference< XComment >(static_cast< CComment* >(CNode::get(pComment))); + } + + //Creates an empty DocumentFragment object. + Reference< XDocumentFragment > SAL_CALL CDocument::createDocumentFragment() + throw (RuntimeException) + { + xmlNodePtr pFrag = xmlNewDocFragment(m_aDocPtr); + return Reference< XDocumentFragment >(static_cast< CDocumentFragment* >(CNode::get(pFrag))); + } + + // Creates an element of the type specified. + Reference< XElement > SAL_CALL CDocument::createElement(const OUString& tagName) + throw (RuntimeException, DOMException) + { + OString o1 = OUStringToOString(tagName, RTL_TEXTENCODING_UTF8); + xmlChar *xName = (xmlChar*)o1.getStr(); + xmlNodePtr aNodePtr = xmlNewDocNode(m_aDocPtr, NULL, xName, NULL); + return Reference< XElement >(static_cast< CElement* >(CNode::get(aNodePtr))); + } + + // Creates an element of the given qualified name and namespace URI. + Reference< XElement > SAL_CALL CDocument::createElementNS( + const OUString& ns, const OUString& qname) + throw (RuntimeException, DOMException) + { + sal_Int32 i = qname.indexOf(':'); + if (ns.getLength() == 0) throw RuntimeException(); + xmlChar *xPrefix; + xmlChar *xName; + OString o1, o2, o3; + if ( i != -1) { + o1 = OUStringToOString(qname.copy(0, i), RTL_TEXTENCODING_UTF8); + xPrefix = (xmlChar*)o1.getStr(); + o2 = OUStringToOString(qname.copy(i+1, qname.getLength()-i-1), RTL_TEXTENCODING_UTF8); + xName = (xmlChar*)o2.getStr(); + } else { + // default prefix + xPrefix = (xmlChar*)""; + o2 = OUStringToOString(qname, RTL_TEXTENCODING_UTF8); + xName = (xmlChar*)o2.getStr(); + } + o3 = OUStringToOString(ns, RTL_TEXTENCODING_UTF8); + xmlChar *xUri = (xmlChar*)o3.getStr(); + + // xmlNsPtr aNsPtr = xmlNewReconciledNs? + // xmlNsPtr aNsPtr = xmlNewGlobalNs? + xmlNodePtr aNodePtr = xmlNewDocNode(m_aDocPtr, NULL, xName, NULL); + xmlNsPtr pNs = xmlNewNs(aNodePtr, xUri, xPrefix); + xmlSetNs(aNodePtr, pNs); + return Reference< XElement >(static_cast< CElement* >(CNode::get(aNodePtr))); + } + + //Creates an EntityReference object. + Reference< XEntityReference > SAL_CALL CDocument::createEntityReference(const OUString& name) + throw (RuntimeException, DOMException) + { + OString o1 = OUStringToOString(name, RTL_TEXTENCODING_UTF8); + xmlChar *xName = (xmlChar*)o1.getStr(); + xmlNodePtr aNodePtr = xmlNewReference(m_aDocPtr, xName); + return Reference< XEntityReference >(static_cast< CEntityReference* >(CNode::get(aNodePtr))); + } + + // Creates a ProcessingInstruction node given the specified name and + // data strings. + Reference< XProcessingInstruction > SAL_CALL CDocument::createProcessingInstruction( + const OUString& target, const OUString& data) + throw (RuntimeException, DOMException) + { + OString o1 = OUStringToOString(target, RTL_TEXTENCODING_UTF8); + xmlChar *xTarget = (xmlChar*)o1.getStr(); + OString o2 = OUStringToOString(data, RTL_TEXTENCODING_UTF8); + xmlChar *xData = (xmlChar*)o2.getStr(); + xmlNodePtr aNodePtr = xmlNewPI(xTarget, xData); + aNodePtr->doc = m_aDocPtr; + return Reference< XProcessingInstruction >(static_cast< CProcessingInstruction* >(CNode::get(aNodePtr))); + } + + // Creates a Text node given the specified string. + Reference< XText > SAL_CALL CDocument::createTextNode(const OUString& data) + throw (RuntimeException) + { + OString o1 = OUStringToOString(data, RTL_TEXTENCODING_UTF8); + xmlChar *xData = (xmlChar*)o1.getStr(); + xmlNodePtr aNodePtr = xmlNewDocText(m_aDocPtr, xData); + return Reference< XText >(static_cast< CText* >(CNode::get(aNodePtr))); + } + + // The Document Type Declaration (see DocumentType) associated with this + // document. + Reference< XDocumentType > SAL_CALL CDocument::getDoctype() + throw (RuntimeException) + { + // find the doc type + xmlNodePtr cur = m_aDocPtr->children; + while (cur != NULL) + { + if (cur->type == XML_DOCUMENT_TYPE_NODE || cur->type == XML_DTD_NODE) + break; + } + return Reference< XDocumentType >(static_cast< CDocumentType* >(CNode::get(cur))); + } + + /// get the pointer to the root element node of the document + static xmlNodePtr SAL_CALL _getDocumentRootPtr(xmlDocPtr i_pDocument) { + // find the document element + xmlNodePtr cur = i_pDocument->children; + while (cur != NULL) + { + if (cur->type == XML_ELEMENT_NODE) + break; + cur = cur->next; + } + return cur; + } + + // This is a convenience attribute that allows direct access to the child + // node that is the root element of the document. + Reference< XElement > SAL_CALL CDocument::getDocumentElement() + throw (RuntimeException) + { + xmlNodePtr cur = _getDocumentRootPtr(m_aDocPtr); + return Reference< XElement >(static_cast< CElement* >(CNode::get(cur))); + } + + static xmlNodePtr _search_element_by_id(const xmlNodePtr cur, const xmlChar* id) + { + + if (cur == NULL) + return NULL; + // look in current node + if (cur->type == XML_ELEMENT_NODE) + { + xmlAttrPtr a = cur->properties; + while (a != NULL) + { + if (a->atype == XML_ATTRIBUTE_ID) { + if (strcmp((char*)a->children->content, (char*)id) == 0) + return cur; + } + a = a->next; + } + } + // look in children + xmlNodePtr result = _search_element_by_id(cur->children, id); + if (result != NULL) + return result; + result = _search_element_by_id(cur->next, id); + return result; + } + + // Returns the Element whose ID is given by elementId. + Reference< XElement > SAL_CALL CDocument::getElementById(const OUString& elementId) + throw (RuntimeException) + { + // search the tree for an element with the given ID + OString o1 = OUStringToOString(elementId, RTL_TEXTENCODING_UTF8); + xmlChar *xId = (xmlChar*)o1.getStr(); + xmlNodePtr pStart = CNode::getNodePtr(getDocumentElement().get()); + xmlNodePtr aNodePtr = _search_element_by_id(pStart, xId); + return Reference< XElement >(static_cast< CElement* >(CNode::get(aNodePtr))); + } + + + Reference< XNodeList > SAL_CALL CDocument::getElementsByTagName(const OUString& tagname) + throw (RuntimeException) + { + // build a list + return Reference< XNodeList >( + new CElementList(static_cast< CElement* >( + this->getDocumentElement().get()), tagname)); + } + + Reference< XNodeList > SAL_CALL CDocument::getElementsByTagNameNS( + const OUString& namespaceURI, const OUString& localName) + throw (RuntimeException) + { + return Reference< XNodeList >( + new CElementList(static_cast< CElement* >( + this->getDocumentElement().get()), namespaceURI, localName)); + } + + Reference< XDOMImplementation > SAL_CALL CDocument::getImplementation() + throw (RuntimeException) + { + // XXX + return Reference< XDOMImplementation >(CDOMImplementation::get()); + } + + // helper function to recall import for siblings + static Reference< XNode > _import_siblings ( + const Reference< XNode > aNode, const Reference< XNode> parent, CDocument* pTarget) + { + Reference< XNode > sibling = aNode; + Reference< XNode > tmp; + Reference< XNode > firstImported; + while (sibling.is()) + { + tmp = pTarget->importNode(sibling, sal_True); + parent->appendChild(tmp); + if (!firstImported.is()) + firstImported = tmp; + sibling = sibling->getNextSibling(); + } + return firstImported; + } + + Reference< XNode > SAL_CALL CDocument::importNode( + const Reference< XNode >& importedNode, sal_Bool deep) + throw (RuntimeException, DOMException) + { + // this node could be from another memory model + // only use uno interfaces to access is!!! + + // allready in doc? + if ( importedNode->getOwnerDocument() == + Reference< XDocument>(static_cast< CDocument* >(CNode::get((xmlNodePtr)m_aDocPtr)))) + return importedNode; + + Reference< XNode > aNode; + NodeType aNodeType = importedNode->getNodeType(); + switch (aNodeType) + { + case NodeType_ATTRIBUTE_NODE: + { + Reference< XAttr > attr(importedNode, UNO_QUERY); + Reference< XAttr > newAttr = createAttribute(attr->getName()); + newAttr->setValue(attr->getValue()); + aNode.set(newAttr, UNO_QUERY); + break; + } + case NodeType_CDATA_SECTION_NODE: + { + Reference< XCDATASection > cdata(importedNode, UNO_QUERY); + Reference< XCDATASection > newCdata = createCDATASection(cdata->getData()); + aNode.set(newCdata, UNO_QUERY); + break; + } + case NodeType_COMMENT_NODE: + { + Reference< XComment > comment(importedNode, UNO_QUERY); + Reference< XComment > newComment = createComment(comment->getData()); + aNode.set(newComment, UNO_QUERY); + break; + } + case NodeType_DOCUMENT_FRAGMENT_NODE: + { + Reference< XDocumentFragment > frag(importedNode, UNO_QUERY); + Reference< XDocumentFragment > newFrag = createDocumentFragment(); + aNode.set(newFrag, UNO_QUERY); + break; + } + case NodeType_ELEMENT_NODE: + { + Reference< XElement > element(importedNode, UNO_QUERY); + OUString aNsUri = importedNode->getNamespaceURI(); + OUString aNsPrefix = importedNode->getPrefix(); + OUString aQName = element->getTagName(); + Reference< XElement > newElement; + if (aNsUri.getLength() > 0) + { + + if (aNsPrefix.getLength() > 0) + aQName = aNsPrefix + OUString::createFromAscii(":") + aQName; + newElement = createElementNS(aNsUri, aQName); + } + else + newElement = createElement(aQName); + + // get attributes + if (element->hasAttributes()) + { + Reference< XNamedNodeMap > attribs = element->getAttributes(); + Reference< XAttr > curAttr; + for (sal_Int32 i = 0; i < attribs->getLength(); i++) + { + curAttr = Reference< XAttr >(attribs->item(i), UNO_QUERY); + OUString aAttrUri = curAttr->getNamespaceURI(); + OUString aAttrPrefix = curAttr->getPrefix(); + OUString aAttrName = curAttr->getName(); + if (aAttrUri.getLength() > 0) + { + if (aAttrPrefix.getLength() > 0) + aAttrName = aAttrPrefix + OUString::createFromAscii(":") + aAttrName; + newElement->setAttributeNS(aAttrUri, aAttrName, curAttr->getValue()); + } + else + newElement->setAttribute(aAttrName, curAttr->getValue()); + } + } + aNode.set(newElement, UNO_QUERY); + break; + } + case NodeType_ENTITY_REFERENCE_NODE: + { + Reference< XEntityReference > ref(importedNode, UNO_QUERY); + Reference< XEntityReference > newRef(createEntityReference(ref->getNodeName())); + aNode.set(newRef, UNO_QUERY); + break; + } + case NodeType_PROCESSING_INSTRUCTION_NODE: + { + Reference< XProcessingInstruction > pi(importedNode, UNO_QUERY); + Reference< XProcessingInstruction > newPi( + createProcessingInstruction(pi->getTarget(), pi->getData())); + aNode.set(newPi, UNO_QUERY); + break; + } + case NodeType_TEXT_NODE: + { + Reference< XText > text(importedNode, UNO_QUERY); + Reference< XText > newText(createTextNode(text->getData())); + aNode.set(newText, UNO_QUERY); + break; + } + case NodeType_ENTITY_NODE: + case NodeType_DOCUMENT_NODE: + case NodeType_DOCUMENT_TYPE_NODE: + case NodeType_NOTATION_NODE: + default: + // can't be imported + throw RuntimeException(); + + } + if (deep) + { + // get children and import them + Reference< XNode > child = importedNode->getFirstChild(); + if (child.is()) + { + _import_siblings(child, aNode, this); + } + } + + /* DOMNodeInsertedIntoDocument + * Fired when a node is being inserted into a document, + * either through direct insertion of the Node or insertion of a + * subtree in which it is contained. This event is dispatched after + * the insertion has taken place. The target of this event is the node + * being inserted. If the Node is being directly inserted the DOMNodeInserted + * event will fire before the DOMNodeInsertedIntoDocument event. + * Bubbles: No + * Cancelable: No + * Context Info: None + */ + if (aNode.is()) + { + Reference< XDocumentEvent > docevent(getOwnerDocument(), UNO_QUERY); + Reference< XMutationEvent > event(docevent->createEvent( + OUString::createFromAscii("DOMNodeInsertedIntoDocument")), UNO_QUERY); + event->initMutationEvent(OUString::createFromAscii("DOMNodeInsertedIntoDocument") + , sal_True, sal_False, Reference< XNode >(), + OUString(), OUString(), OUString(), (AttrChangeType)0 ); + dispatchEvent(Reference< XEvent >(event, UNO_QUERY)); + } + + return aNode; + } + OUString SAL_CALL CDocument::getNodeName()throw (RuntimeException) + { + return OUString::createFromAscii("#document"); + } + OUString SAL_CALL CDocument::getNodeValue() throw (RuntimeException) + { + return OUString(); + } + + Reference< XEvent > SAL_CALL CDocument::createEvent(const OUString& aType) throw (RuntimeException) + { + events::CEvent *pEvent = 0; + if ( + aType.compareToAscii("DOMSubtreeModified") == 0|| + aType.compareToAscii("DOMNodeInserted") == 0|| + aType.compareToAscii("DOMNodeRemoved") == 0|| + aType.compareToAscii("DOMNodeRemovedFromDocument") == 0|| + aType.compareToAscii("DOMNodeInsertedIntoDocument") == 0|| + aType.compareToAscii("DOMAttrModified") == 0|| + aType.compareToAscii("DOMCharacterDataModified") == 0) + { + pEvent = new events::CMutationEvent; + + } else if ( + aType.compareToAscii("DOMFocusIn") == 0|| + aType.compareToAscii("DOMFocusOut") == 0|| + aType.compareToAscii("DOMActivate") == 0) + { + pEvent = new events::CUIEvent; + } else if ( + aType.compareToAscii("click") == 0|| + aType.compareToAscii("mousedown") == 0|| + aType.compareToAscii("mouseup") == 0|| + aType.compareToAscii("mouseover") == 0|| + aType.compareToAscii("mousemove") == 0|| + aType.compareToAscii("mouseout") == 0 ) + { + pEvent = new events::CMouseEvent; + } + else // generic event + { + pEvent = new events::CEvent; + } + return Reference< XEvent >(pEvent); + } + + // ::com::sun::star::xml::sax::XSAXSerializable + void SAL_CALL CDocument::serialize( + const Reference< XDocumentHandler >& i_xHandler, + const Sequence< beans::StringPair >& i_rNamespaces) + throw (RuntimeException, SAXException) + { + // add new namespaces to root node + xmlNodePtr pRoot = _getDocumentRootPtr(m_aDocPtr); + if (0 != pRoot) { + const beans::StringPair * pSeq = i_rNamespaces.getConstArray(); + for (const beans::StringPair *pNsDef = pSeq; + pNsDef < pSeq + i_rNamespaces.getLength(); ++pNsDef) { + OString prefix = OUStringToOString(pNsDef->First, + RTL_TEXTENCODING_UTF8); + OString href = OUStringToOString(pNsDef->Second, + RTL_TEXTENCODING_UTF8); + // this will only add the ns if it does not exist already + xmlNewNs(pRoot, reinterpret_cast<const xmlChar*>(href.getStr()), + reinterpret_cast<const xmlChar*>(prefix.getStr())); + } + // eliminate duplicate namespace declarations + _nscleanup(pRoot->children, pRoot); + } + saxify(i_xHandler); + } + + // ::com::sun::star::xml::sax::XFastSAXSerializable + void SAL_CALL CDocument::fastSerialize( const Reference< XFastDocumentHandler >& i_xHandler, + const Reference< XFastTokenHandler >& i_xTokenHandler, + const Sequence< beans::StringPair >& i_rNamespaces, + const Sequence< beans::Pair< rtl::OUString, sal_Int32 > >& i_rRegisterNamespaces ) + throw (SAXException, RuntimeException) + { + // add new namespaces to root node + xmlNodePtr pRoot = _getDocumentRootPtr(m_aDocPtr); + if (0 != pRoot) { + const beans::StringPair * pSeq = i_rNamespaces.getConstArray(); + for (const beans::StringPair *pNsDef = pSeq; + pNsDef < pSeq + i_rNamespaces.getLength(); ++pNsDef) { + OString prefix = OUStringToOString(pNsDef->First, + RTL_TEXTENCODING_UTF8); + OString href = OUStringToOString(pNsDef->Second, + RTL_TEXTENCODING_UTF8); + // this will only add the ns if it does not exist already + xmlNewNs(pRoot, reinterpret_cast<const xmlChar*>(href.getStr()), + reinterpret_cast<const xmlChar*>(prefix.getStr())); + } + // eliminate duplicate namespace declarations + _nscleanup(pRoot->children, pRoot); + } + + Context aContext(i_xHandler, + i_xTokenHandler); + + // register namespace ids + const beans::Pair<OUString,sal_Int32>* pSeq = i_rRegisterNamespaces.getConstArray(); + for (const beans::Pair<OUString,sal_Int32>* pNs = pSeq; + pNs < pSeq + i_rRegisterNamespaces.getLength(); ++pNs) + { + OSL_ENSURE(pNs->Second >= FastToken::NAMESPACE, + "CDocument::fastSerialize(): invalid NS token id"); + aContext.maNamespaceMap[ pNs->First ] = pNs->Second; + } + + fastSaxify(aContext); + } +} diff --git a/unoxml/source/dom/document.hxx b/unoxml/source/dom/document.hxx new file mode 100644 index 000000000000..9cbf4dac3115 --- /dev/null +++ b/unoxml/source/dom/document.hxx @@ -0,0 +1,369 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2008 by Sun Microsystems, Inc. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: document.hxx,v $ + * $Revision: 1.12 $ + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#ifndef _DOCUMENT_HXX +#define _DOCUMENT_HXX + +#include <list> +#include <set> +#include <sal/types.h> +#include <cppuhelper/implbase6.hxx> +#include <com/sun/star/uno/Reference.h> +#include <com/sun/star/uno/Exception.hpp> +#include <com/sun/star/beans/StringPair.hpp> +#include <com/sun/star/xml/dom/XNode.hpp> +#include <com/sun/star/xml/dom/XAttr.hpp> +#include <com/sun/star/xml/dom/XElement.hpp> +#include <com/sun/star/xml/dom/XDOMImplementation.hpp> +#include <com/sun/star/xml/dom/events/XDocumentEvent.hpp> +#include <com/sun/star/xml/dom/events/XEvent.hpp> +#include <com/sun/star/xml/sax/XSAXSerializable.hpp> +#include <com/sun/star/xml/sax/XFastSAXSerializable.hpp> +#include <com/sun/star/xml/sax/XDocumentHandler.hpp> +#include <com/sun/star/xml/sax/XFastDocumentHandler.hpp> +#include <com/sun/star/io/XActiveDataSource.hpp> +#include <com/sun/star/io/XActiveDataControl.hpp> +#include <com/sun/star/io/XOutputStream.hpp> +#include <com/sun/star/io/XStreamListener.hpp> + +#include "node.hxx" + +#include <libxml/tree.h> + +using namespace std; +using ::rtl::OUString; +using namespace com::sun::star; +using namespace com::sun::star::uno; +using namespace com::sun::star::lang; +using namespace com::sun::star::xml::sax; +using namespace com::sun::star::io; +using namespace com::sun::star::xml::dom; +using namespace com::sun::star::xml::dom::events; + +namespace DOM +{ + + class CDocument : public cppu::ImplInheritanceHelper6< + CNode, XDocument, XDocumentEvent, + XActiveDataControl, XActiveDataSource, XSAXSerializable, XFastSAXSerializable> + { + friend class CNode; + typedef std::list< Reference< XNode >* > nodereflist_t; + typedef set< Reference< XStreamListener > > listenerlist_t; + private: + + nodereflist_t m_aNodeRefList; + + xmlDocPtr m_aDocPtr; + + // datacontrol/source state + listenerlist_t m_streamListeners; + Reference< XOutputStream > m_rOutputStream; + + protected: + CDocument(xmlDocPtr aDocPtr); + + void addnode(xmlNodePtr aNode); + + public: + + virtual ~CDocument(); + + virtual void SAL_CALL saxify( + const Reference< XDocumentHandler >& i_xHandler); + + virtual void SAL_CALL fastSaxify( Context& rContext ); + + /** + Creates an Attr of the given name. + */ + virtual Reference< XAttr > SAL_CALL createAttribute(const OUString& name) + throw (RuntimeException, DOMException); + + /** + Creates an attribute of the given qualified name and namespace URI. + */ + virtual Reference< XAttr > SAL_CALL createAttributeNS(const OUString& namespaceURI, const OUString& qualifiedName) + throw (RuntimeException, DOMException); + + /** + Creates a CDATASection node whose value is the specified string. + */ + virtual Reference< XCDATASection > SAL_CALL createCDATASection(const OUString& data) + throw (RuntimeException); + + /** + Creates a Comment node given the specified string. + */ + virtual Reference< XComment > SAL_CALL createComment(const OUString& data) + throw (RuntimeException); + + /** + Creates an empty DocumentFragment object. + */ + virtual Reference< XDocumentFragment > SAL_CALL createDocumentFragment() + throw (RuntimeException); + + /** + Creates an element of the type specified. + */ + virtual Reference< XElement > SAL_CALL createElement(const OUString& tagName) + throw (RuntimeException, DOMException); + + /** + Creates an element of the given qualified name and namespace URI. + */ + virtual Reference< XElement > SAL_CALL createElementNS(const OUString& namespaceURI, const OUString& qualifiedName) + throw (RuntimeException, DOMException); + + /** + Creates an EntityReference object. + */ + virtual Reference< XEntityReference > SAL_CALL createEntityReference(const OUString& name) + throw (RuntimeException, DOMException); + + /** + Creates a ProcessingInstruction node given the specified name and + data strings. + */ + virtual Reference< XProcessingInstruction > SAL_CALL createProcessingInstruction( + const OUString& target, const OUString& data) + throw (RuntimeException, DOMException); + + /** + Creates a Text node given the specified string. + */ + virtual Reference< XText > SAL_CALL createTextNode(const OUString& data) + throw (RuntimeException); + + /** + The Document Type Declaration (see DocumentType) associated with this + document. + */ + virtual Reference< XDocumentType > SAL_CALL getDoctype() + throw (RuntimeException); + + /** + This is a convenience attribute that allows direct access to the child + node that is the root element of the document. + */ + virtual Reference< XElement > SAL_CALL getDocumentElement() + throw (RuntimeException); + + /** + Returns the Element whose ID is given by elementId. + */ + virtual Reference< XElement > SAL_CALL getElementById(const OUString& elementId) + throw (RuntimeException); + + /** + Returns a NodeList of all the Elements with a given tag name in the + order in which they are encountered in a preorder traversal of the + Document tree. + */ + virtual Reference< XNodeList > SAL_CALL getElementsByTagName(const OUString& tagname) + throw (RuntimeException); + + /** + Returns a NodeList of all the Elements with a given local name and + namespace URI in the order in which they are encountered in a preorder + traversal of the Document tree. + */ + virtual Reference< XNodeList > SAL_CALL getElementsByTagNameNS(const OUString& namespaceURI, const OUString& localName) + throw (RuntimeException); + + /** + The DOMImplementation object that handles this document. + */ + virtual Reference< XDOMImplementation > SAL_CALL getImplementation() + throw (RuntimeException); + + /** + Imports a node from another document to this document. + */ + virtual Reference< XNode > SAL_CALL importNode(const Reference< XNode >& importedNode, sal_Bool deep) + throw (RuntimeException, DOMException); + + // XDocumentEvent + virtual Reference< XEvent > SAL_CALL createEvent(const OUString& eventType) throw (RuntimeException); + + // XActiveDataControl, + // see http://api.openoffice.org/docs/common/ref/com/sun/star/io/XActiveDataControl.html + virtual void SAL_CALL addListener(const Reference< XStreamListener >& aListener ) throw (RuntimeException); + virtual void SAL_CALL removeListener(const Reference< XStreamListener >& aListener ) throw (RuntimeException); + virtual void SAL_CALL start() throw (RuntimeException); + virtual void SAL_CALL terminate() throw (RuntimeException); + + // XActiveDataSource + // see http://api.openoffice.org/docs/common/ref/com/sun/star/io/XActiveDataSource.html + virtual void SAL_CALL setOutputStream( const Reference< XOutputStream >& aStream ) throw (RuntimeException); + virtual Reference< XOutputStream > SAL_CALL getOutputStream() throw (RuntimeException); + + // ---- resolve uno inheritance problems... + // overrides for XNode base + virtual OUString SAL_CALL getNodeName() + throw (RuntimeException); + virtual OUString SAL_CALL getNodeValue() + throw (RuntimeException); + // --- delegation for XNde base. + virtual Reference< XNode > SAL_CALL appendChild(const Reference< XNode >& newChild) + throw (RuntimeException, DOMException) + { + return CNode::appendChild(newChild); + } + virtual Reference< XNode > SAL_CALL cloneNode(sal_Bool deep) + throw (RuntimeException) + { + return CNode::cloneNode(deep); + } + virtual Reference< XNamedNodeMap > SAL_CALL getAttributes() + throw (RuntimeException) + { + return CNode::getAttributes(); + } + virtual Reference< XNodeList > SAL_CALL getChildNodes() + throw (RuntimeException) + { + return CNode::getChildNodes(); + } + virtual Reference< XNode > SAL_CALL getFirstChild() + throw (RuntimeException) + { + return CNode::getFirstChild(); + } + virtual Reference< XNode > SAL_CALL getLastChild() + throw (RuntimeException) + { + return CNode::getLastChild(); + } + virtual OUString SAL_CALL getLocalName() + throw (RuntimeException) + { + return CNode::getLocalName(); + } + virtual OUString SAL_CALL getNamespaceURI() + throw (RuntimeException) + { + return CNode::getNamespaceURI(); + } + virtual Reference< XNode > SAL_CALL getNextSibling() + throw (RuntimeException) + { + return CNode::getNextSibling(); + } + virtual NodeType SAL_CALL getNodeType() + throw (RuntimeException) + { + return CNode::getNodeType(); + } + virtual Reference< XDocument > SAL_CALL getOwnerDocument() + throw (RuntimeException) + { + return CNode::getOwnerDocument(); + } + virtual Reference< XNode > SAL_CALL getParentNode() + throw (RuntimeException) + { + return CNode::getParentNode(); + } + virtual OUString SAL_CALL getPrefix() + throw (RuntimeException) + { + return CNode::getPrefix(); + } + virtual Reference< XNode > SAL_CALL getPreviousSibling() + throw (RuntimeException) + { + return CNode::getPreviousSibling(); + } + virtual sal_Bool SAL_CALL hasAttributes() + throw (RuntimeException) + { + return CNode::hasAttributes(); + } + virtual sal_Bool SAL_CALL hasChildNodes() + throw (RuntimeException) + { + return CNode::hasChildNodes(); + } + virtual Reference< XNode > SAL_CALL insertBefore( + const Reference< XNode >& newChild, const Reference< XNode >& refChild) + throw (DOMException) + { + return CNode::insertBefore(newChild, refChild); + } + virtual sal_Bool SAL_CALL isSupported(const OUString& feature, const OUString& ver) + throw (RuntimeException) + { + return CNode::isSupported(feature, ver); + } + virtual void SAL_CALL normalize() + throw (RuntimeException) + { + CNode::normalize(); + } + virtual Reference< XNode > SAL_CALL removeChild(const Reference< XNode >& oldChild) + throw (RuntimeException, DOMException) + { + return CNode::removeChild(oldChild); + } + virtual Reference< XNode > SAL_CALL replaceChild( + const Reference< XNode >& newChild, const Reference< XNode >& oldChild) + throw (RuntimeException, DOMException) + { + return CNode::replaceChild(newChild, oldChild); + } + virtual void SAL_CALL setNodeValue(const OUString& nodeValue) + throw (RuntimeException, DOMException) + { + return CNode::setNodeValue(nodeValue); + } + virtual void SAL_CALL setPrefix(const OUString& prefix) + throw (RuntimeException, DOMException) + { + return CNode::setPrefix(prefix); + } + + // ::com::sun::star::xml::sax::XSAXSerializable + virtual void SAL_CALL serialize( + const Reference< XDocumentHandler >& i_xHandler, + const Sequence< beans::StringPair >& i_rNamespaces) + throw (RuntimeException, SAXException); + + // ::com::sun::star::xml::sax::XFastSAXSerializable + virtual void SAL_CALL fastSerialize( const Reference< XFastDocumentHandler >& handler, + const Reference< XFastTokenHandler >& tokenHandler, + const Sequence< beans::StringPair >& i_rNamespaces, + const Sequence< beans::Pair< rtl::OUString, sal_Int32 > >& namespaces ) + throw (SAXException, RuntimeException); + }; +} + +#endif diff --git a/unoxml/source/dom/documentbuilder.cxx b/unoxml/source/dom/documentbuilder.cxx new file mode 100644 index 000000000000..7f6777620d87 --- /dev/null +++ b/unoxml/source/dom/documentbuilder.cxx @@ -0,0 +1,432 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2008 by Sun Microsystems, Inc. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: documentbuilder.cxx,v $ + * $Revision: 1.7 $ + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#include "documentbuilder.hxx" +#include "node.hxx" +#include "document.hxx" + +#include <rtl/alloc.h> +#include <rtl/memory.h> +#include <rtl/ustrbuf.hxx> + +#include <cppuhelper/implbase1.hxx> + +#include <libxml/xmlerror.h> + +#include <com/sun/star/xml/sax/SAXParseException.hpp> +#include <com/sun/star/ucb/XCommandEnvironment.hpp> +#include <com/sun/star/task/XInteractionHandler.hpp> +#include <ucbhelper/content.hxx> +#include <ucbhelper/commandenvironment.hxx> + +#include <string.h> +#include <stdio.h> +#include <stdarg.h> + + +using ::rtl::OUStringBuffer; +using ::rtl::OString; +using ::com::sun::star::xml::sax::InputSource; +using namespace ucbhelper; +using namespace ::com::sun::star::ucb; +using ::com::sun::star::task::XInteractionHandler; + + +namespace DOM +{ + extern "C" { + //char *strdup(const char *s); + /* + static char* strdupfunc(const char* s) + { + sal_Int32 len = 0; + while (s[len] != '\0') len++; + char *newStr = (char*)rtl_allocateMemory(len+1); + if (newStr != NULL) + rtl_copyMemory(newStr, s, len+1); + return newStr; + } + */ + } + + + class CDefaultEntityResolver : public cppu::WeakImplHelper1< XEntityResolver > + { + public: + virtual InputSource SAL_CALL resolveEntity( const OUString& sPublicId, const OUString& sSystemId ) + throw (::com::sun::star::uno::RuntimeException) + { + InputSource is; + is.sPublicId = sPublicId; + is.sSystemId = sSystemId; + is.sEncoding = OUString(); + + try { + Reference< XCommandEnvironment > aEnvironment( + new CommandEnvironment(Reference< XInteractionHandler >(), + Reference< XProgressHandler >() )); + Content aContent(sSystemId, aEnvironment); + + is.aInputStream = aContent.openStream(); + } catch (com::sun::star::uno::Exception) { + OSL_ENSURE(sal_False, "exception in default entity resolver"); + is.aInputStream = Reference< XInputStream >(); + } + return is; + } + + }; + + CDocumentBuilder::CDocumentBuilder(const Reference< XMultiServiceFactory >& xFactory) + : m_aFactory(xFactory) + , m_aEntityResolver(Reference< XEntityResolver > (new CDefaultEntityResolver())) + { + // init libxml. libxml will protect itself against multiple + // initializations so there is no problem here if this gets + // called multiple times. + xmlInitParser(); + } + + Reference< XInterface > CDocumentBuilder::_getInstance(const Reference< XMultiServiceFactory >& rSMgr) + { + // XXX + return static_cast< XDocumentBuilder* >(new CDocumentBuilder(rSMgr)); + } + + const char* CDocumentBuilder::aImplementationName = "com.sun.star.comp.xml.dom.DocumentBuilder"; + const char* CDocumentBuilder::aSupportedServiceNames[] = { + "com.sun.star.xml.dom.DocumentBuilder", + NULL + }; + + OUString CDocumentBuilder::_getImplementationName() + { + return OUString::createFromAscii(aImplementationName); + } + Sequence<OUString> CDocumentBuilder::_getSupportedServiceNames() + { + Sequence<OUString> aSequence; + for (int i=0; aSupportedServiceNames[i]!=NULL; i++) { + aSequence.realloc(i+1); + aSequence[i]=(OUString::createFromAscii(aSupportedServiceNames[i])); + } + return aSequence; + } + + Sequence< OUString > SAL_CALL CDocumentBuilder::getSupportedServiceNames() + throw (RuntimeException) + { + return CDocumentBuilder::_getSupportedServiceNames(); + } + + OUString SAL_CALL CDocumentBuilder::getImplementationName() + throw (RuntimeException) + { + return CDocumentBuilder::_getImplementationName(); + } + + sal_Bool SAL_CALL CDocumentBuilder::supportsService(const OUString& aServiceName) + throw (RuntimeException) + { + Sequence< OUString > supported = CDocumentBuilder::_getSupportedServiceNames(); + for (sal_Int32 i=0; i<supported.getLength(); i++) + { + if (supported[i] == aServiceName) return sal_True; + } + return sal_False; + } + + Reference< XDOMImplementation > SAL_CALL CDocumentBuilder::getDOMImplementation() + throw (RuntimeException) + { + + return Reference< XDOMImplementation >(); + } + + sal_Bool SAL_CALL CDocumentBuilder::isNamespaceAware() + throw (RuntimeException) + { + return sal_True; + } + + sal_Bool SAL_CALL CDocumentBuilder::isValidating() + throw (RuntimeException) + { + return sal_False; + } + + Reference< XDocument > SAL_CALL CDocumentBuilder::newDocument() + throw (RuntimeException) + { + // create a new document + xmlDocPtr pDocument = xmlNewDoc((const xmlChar*)"1.0"); + return Reference< XDocument >(static_cast< CDocument* >(CNode::get((xmlNodePtr)pDocument))); + } + + static OUString make_error_message(xmlParserCtxtPtr ctxt) + { + OUStringBuffer buf; + buf.appendAscii(ctxt->lastError.message); + buf.appendAscii("Line: "); + buf.append(static_cast<sal_Int32>(ctxt->lastError.line)); + buf.appendAscii("\nColumn: "); + buf.append(static_cast<sal_Int32>(ctxt->lastError.int2)); + OUString msg = buf.makeStringAndClear(); + return msg; + } + + // -- callbacks and context struct for parsing from stream + // -- c-linkage, so the callbacks can be used by libxml + extern "C" { + + // context struct passed to IO functions + typedef struct context { + CDocumentBuilder *pBuilder; + Reference< XInputStream > rInputStream; + bool close; + bool freeOnClose; + } context_t; + + static int xmlIO_read_func( void *context, char *buffer, int len) + { + // get the context... + context_t *pctx = static_cast<context_t*>(context); + if (!pctx->rInputStream.is()) + return -1; + try { + // try to read the requested number of bytes + Sequence< sal_Int8 > chunk(len); + int nread = pctx->rInputStream->readBytes(chunk, len); + + // copy bytes to the provided buffer + rtl_copyMemory(buffer, chunk.getConstArray(), nread); + return nread; + } catch (com::sun::star::uno::Exception& ex) { + (void) ex; + OSL_ENSURE(sal_False, OUStringToOString(ex.Message, RTL_TEXTENCODING_UTF8).getStr()); + return -1; + } + } + + static int xmlIO_close_func(void* context) + { + // get the context... + context_t *pctx = static_cast<context_t*>(context); + if (!pctx->rInputStream.is()) + return 0; + try + { + if (pctx->close) + pctx->rInputStream->closeInput(); + if (pctx->freeOnClose) + delete pctx; + return 0; + } catch (com::sun::star::uno::Exception& ex) { + (void) ex; + OSL_ENSURE(sal_False, OUStringToOString(ex.Message, RTL_TEXTENCODING_UTF8).getStr()); + return -1; + } + } + + static xmlParserInputPtr resolve_func(void *ctx, + const xmlChar *publicId, + const xmlChar *systemId) + { + // get the CDocumentBuilder object + xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr)ctx; + CDocumentBuilder *builder = static_cast< CDocumentBuilder* >(ctxt->_private); + Reference< XEntityResolver > resolver = builder->getEntityResolver(); + OUString sysid; + if (systemId != 0) + sysid = OUString((sal_Char*)systemId, strlen((char*)systemId), RTL_TEXTENCODING_UTF8); + OUString pubid; + if (publicId != 0) + pubid = OUString((sal_Char*)publicId, strlen((char*)publicId), RTL_TEXTENCODING_UTF8); + + // resolve the entity + InputSource src = resolver->resolveEntity(pubid, sysid); + + // create IO context on heap because this call will no longer be on the stack + // when IO is actually performed through the callbacks. The close function must + // free the memory which is indicated by the freeOnClose field in the context struct + context_t *c = new context_t; + c->pBuilder = builder; + c->rInputStream = src.aInputStream; + c->close = true; + c->freeOnClose = true; + + // set up the inputBuffer and inputPtr for libxml + xmlParserInputBufferPtr pBuffer = + xmlParserInputBufferCreateIO(xmlIO_read_func, xmlIO_close_func, c, XML_CHAR_ENCODING_NONE); + xmlParserInputPtr pInput = + xmlNewIOInputStream(ctxt, pBuffer, XML_CHAR_ENCODING_NONE); + return pInput; + } + + static xmlParserInputPtr external_entity_loader(const char *URL, const char * /*ID*/, xmlParserCtxtPtr ctxt) + { + // just call our resolver function using the URL as systemId + return resolve_func(ctxt, 0, (const xmlChar*)URL); + } + + // default warning handler triggers assertion + static void warning_func(void * ctx, const char * /*msg*/, ...) + { + OUStringBuffer buf(OUString::createFromAscii("libxml2 warning\n")); + buf.append(make_error_message(static_cast< xmlParserCtxtPtr >(ctx))); + OString msg = OUStringToOString(buf.makeStringAndClear(), RTL_TEXTENCODING_ASCII_US); + OSL_ENSURE(sal_False, msg.getStr()); + } + + // default error handler triggers assertion + static void error_func(void * ctx, const char * /*msg*/, ...) + { + OUStringBuffer buf(OUString::createFromAscii("libxml2 error\n")); + buf.append(make_error_message(static_cast< xmlParserCtxtPtr >(ctx))); + OString msg = OUStringToOString(buf.makeStringAndClear(), RTL_TEXTENCODING_ASCII_US); + OSL_ENSURE(sal_False, msg.getStr()); + } + + } // extern "C" + + void throwEx(xmlParserCtxtPtr ctxt) { + OUString msg = make_error_message(ctxt); + xmlFreeParserCtxt(ctxt); + com::sun::star::xml::sax::SAXParseException saxex; + saxex.Message = msg; + saxex.LineNumber = static_cast<sal_Int32>(ctxt->lastError.line); + saxex.ColumnNumber = static_cast<sal_Int32>(ctxt->lastError.int2); + throw saxex; + } + + Reference< XDocument > SAL_CALL CDocumentBuilder::parse(const Reference< XInputStream >& is) + throw (RuntimeException, SAXParseException, IOException) + { + + // encoding... + /* + xmlChar *encstr = (xmlChar*) OUStringToOString(src.sEncoding, RTL_TEXTENCODING_UTF8).getStr(); + xmlCharEncoding enc = xmlParseCharEncoding(encstr); + */ + + xmlParserCtxtPtr ctxt = xmlNewParserCtxt(); + + // register error functions to prevent errors being printed + // on the console + ctxt->_private = this; + ctxt->sax->error = error_func; + ctxt->sax->warning = warning_func; + ctxt->sax->resolveEntity = resolve_func; + + // IO context struct + context_t c; + c.pBuilder = this; + c.rInputStream = is; + // we did not open the stream, thus we do not close it. + c.close = false; + c.freeOnClose = false; + xmlDocPtr pDoc = xmlCtxtReadIO(ctxt, xmlIO_read_func, xmlIO_close_func, &c, + 0, 0, 0); + + if (pDoc == 0) { + throwEx(ctxt); + } + xmlFreeParserCtxt(ctxt); + return Reference< XDocument >(static_cast< CDocument* >(CNode::get((xmlNodePtr)pDoc))); + } + + Reference< XDocument > SAL_CALL CDocumentBuilder::parseSource(const InputSource& is) + throw (RuntimeException, SAXParseException, IOException) + { + // if there is an encoding specified in the input source, use it + xmlCharEncoding enc = XML_CHAR_ENCODING_NONE; + if (is.sEncoding.getLength() > 0) { + OString oEncstr = OUStringToOString(is.sEncoding, RTL_TEXTENCODING_UTF8); + char *encstr = (char*) oEncstr.getStr(); + enc = xmlParseCharEncoding(encstr); + } + + // set up parser context + xmlParserCtxtPtr ctxt = xmlNewParserCtxt(); + // register error functions to prevent errors being printed + // on the console + ctxt->_private = this; + ctxt->sax->error = error_func; + ctxt->sax->warning = warning_func; + + // setup entity resolver binding(s) + ctxt->sax->resolveEntity = resolve_func; + xmlSetExternalEntityLoader(external_entity_loader); + + // if an input stream is provided, use it + + // use the systemID + + return Reference< XDocument >(); + } + + Reference< XDocument > SAL_CALL CDocumentBuilder::parseURI(const OUString& sUri) + throw (RuntimeException, SAXParseException, IOException) + { + xmlParserCtxtPtr ctxt = xmlNewParserCtxt(); + ctxt->_private = this; + ctxt->sax->error = error_func; + ctxt->sax->warning = warning_func; + ctxt->sax->resolveEntity = resolve_func; + // xmlSetExternalEntityLoader(external_entity_loader); + OString oUri = OUStringToOString(sUri, RTL_TEXTENCODING_UTF8); + char *uri = (char*) oUri.getStr(); + xmlDocPtr pDoc = xmlCtxtReadFile(ctxt, uri, 0, 0); + if (pDoc == 0) { + throwEx(ctxt); + } + xmlFreeParserCtxt(ctxt); + return Reference< XDocument >(static_cast< CDocument* >(CNode::get((xmlNodePtr)pDoc))); + } + + void SAL_CALL CDocumentBuilder::setEntityResolver(const Reference< XEntityResolver >& er) + throw (RuntimeException) + { + m_aEntityResolver = er; + } + + Reference< XEntityResolver > SAL_CALL CDocumentBuilder::getEntityResolver() + throw (RuntimeException) + { + return m_aEntityResolver; + } + + + void SAL_CALL CDocumentBuilder::setErrorHandler(const Reference< XErrorHandler >& eh) + throw (RuntimeException) + { + m_aErrorHandler = eh; + } +} diff --git a/unoxml/source/dom/documentbuilder.hxx b/unoxml/source/dom/documentbuilder.hxx new file mode 100644 index 000000000000..43fb543aabe2 --- /dev/null +++ b/unoxml/source/dom/documentbuilder.hxx @@ -0,0 +1,160 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2008 by Sun Microsystems, Inc. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: documentbuilder.hxx,v $ + * $Revision: 1.7 $ + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#ifndef _DOCUMENTBUILDER_HXX +#define _DOCUMENTBUILDER_HXX + +#include <sal/types.h> +#include <cppuhelper/implbase2.hxx> +#include <com/sun/star/uno/Reference.h> +#include <com/sun/star/uno/Sequence.h> + +#include <com/sun/star/uno/XInterface.hpp> +#include <com/sun/star/uno/Exception.hpp> +#include <com/sun/star/xml/dom/XDocumentBuilder.hpp> +#include <com/sun/star/xml/dom/XDocument.hpp> +#include <com/sun/star/xml/dom/XDOMImplementation.hpp> +#include <com/sun/star/xml/sax/XEntityResolver.hpp> +#include <com/sun/star/xml/sax/XErrorHandler.hpp> +#include <com/sun/star/xml/sax/SAXParseException.hpp> +#include <com/sun/star/io/XInputStream.hpp> +#include <com/sun/star/io/IOException.hpp> +#include <com/sun/star/lang/XServiceInfo.hpp> +#include <com/sun/star/lang/XSingleServiceFactory.hpp> +#include <com/sun/star/lang/XMultiServiceFactory.hpp> + +#include "libxml/tree.h" + +using ::rtl::OUString; +using namespace com::sun::star::uno; +using namespace com::sun::star::lang; +using namespace com::sun::star::xml::dom; +using namespace com::sun::star::xml::sax; +using namespace com::sun::star::io; + +namespace DOM +{ + class CDocumentBuilder + : public ::cppu::WeakImplHelper2< XDocumentBuilder, XServiceInfo > + { + private: + Reference< XMultiServiceFactory > m_aFactory; + Reference< XEntityResolver > m_aEntityResolver; + Reference< XErrorHandler > m_aErrorHandler; + + public: + + // ctor + CDocumentBuilder(const Reference< XMultiServiceFactory >& xFactory); + + // call for factory + static Reference< XInterface > getInstance(const Reference < XMultiServiceFactory >& xFactory); + + // static helpers for service info and component management + static const char* aImplementationName; + static const char* aSupportedServiceNames[]; + static OUString _getImplementationName(); + static Sequence< OUString > _getSupportedServiceNames(); + static Reference< XInterface > _getInstance(const Reference< XMultiServiceFactory >& rSMgr); + + // XServiceInfo + virtual OUString SAL_CALL getImplementationName() + throw (RuntimeException); + virtual sal_Bool SAL_CALL supportsService(const OUString& ServiceName) + throw (RuntimeException); + virtual Sequence< OUString > SAL_CALL getSupportedServiceNames () + throw (RuntimeException); + + /** + Obtain an instance of a DOMImplementation object. + */ + virtual Reference< XDOMImplementation > SAL_CALL getDOMImplementation() + throw (RuntimeException); + + /** + Indicates whether or not this parser is configured to understand + namespaces. + */ + virtual sal_Bool SAL_CALL isNamespaceAware() + throw (RuntimeException); + + /** + Indicates whether or not this parser is configured to validate XML + documents. + */ + virtual sal_Bool SAL_CALL isValidating() + throw (RuntimeException); + + /** + Obtain a new instance of a DOM Document object to build a DOM tree + with. + */ + virtual Reference< XDocument > SAL_CALL newDocument() + throw (RuntimeException); + + /** + Parse the content of the given InputStream as an XML document and + return a new DOM Document object. + */ + virtual Reference< XDocument > SAL_CALL parse(const Reference< XInputStream >& is) + throw (RuntimeException, SAXParseException, IOException); + + /** + Parse the content of the given URI as an XML document and return + a new DOM Document object. + */ + virtual Reference< XDocument > SAL_CALL parseURI(const OUString& uri) + throw (RuntimeException, SAXParseException, IOException); + + virtual Reference< XDocument > SAL_CALL parseSource(const InputSource& is) + throw (RuntimeException, SAXParseException, IOException); + + + /** + Specify the EntityResolver to be used to resolve entities present + in the XML document to be parsed. + */ + virtual void SAL_CALL setEntityResolver(const Reference< XEntityResolver >& er) + throw (RuntimeException); + + virtual Reference< XEntityResolver > SAL_CALL getEntityResolver() + throw (RuntimeException); + + + /** + Specify the ErrorHandler to be used to report errors present in + the XML document to be parsed. + */ + virtual void SAL_CALL setErrorHandler(const Reference< XErrorHandler >& eh) + throw (RuntimeException); + }; +} + +#endif diff --git a/unoxml/source/dom/documentfragment.cxx b/unoxml/source/dom/documentfragment.cxx new file mode 100644 index 000000000000..4c01a403c009 --- /dev/null +++ b/unoxml/source/dom/documentfragment.cxx @@ -0,0 +1,48 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2008 by Sun Microsystems, Inc. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: documentfragment.cxx,v $ + * $Revision: 1.5 $ + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#include "documentfragment.hxx" + +namespace DOM +{ + CDocumentFragment::CDocumentFragment(const xmlNodePtr aNodePtr) + { + m_aNodeType = NodeType_DOCUMENT_FRAGMENT_NODE; + init_node(aNodePtr); + } + OUString SAL_CALL CDocumentFragment::getNodeName()throw (RuntimeException) + { + return OUString::createFromAscii("#document-fragment"); + } + OUString SAL_CALL CDocumentFragment::getNodeValue() throw (RuntimeException) + { + return OUString(); + } +} diff --git a/unoxml/source/dom/documentfragment.hxx b/unoxml/source/dom/documentfragment.hxx new file mode 100644 index 000000000000..96b8f2fac4fe --- /dev/null +++ b/unoxml/source/dom/documentfragment.hxx @@ -0,0 +1,182 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2008 by Sun Microsystems, Inc. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: documentfragment.hxx,v $ + * $Revision: 1.7 $ + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#ifndef _DOCUMENTFRAGMENT_HXX +#define _DOCUMENTFRAGMENT_HXX + +#include <com/sun/star/uno/Reference.h> +#include <com/sun/star/uno/Exception.hpp> +#include <com/sun/star/xml/dom/XDocumentFragment.hpp> + +#include "node.hxx" + +using ::rtl::OUString; +using namespace com::sun::star::uno; +using namespace com::sun::star::xml::dom; + +namespace DOM +{ + class CDocumentFragment : public cppu::ImplInheritanceHelper1< CNode, XDocumentFragment > + { + friend class CNode; + protected: + CDocumentFragment(const xmlNodePtr aNodePtr); + + public: + // ---- resolve uno inheritance problems... + // overrides for XNode base + virtual OUString SAL_CALL getNodeName() + throw (RuntimeException); + virtual OUString SAL_CALL getNodeValue() + throw (RuntimeException); + // --- delegation for XNde base. + virtual Reference< XNode > SAL_CALL appendChild(const Reference< XNode >& newChild) + throw (RuntimeException, DOMException) + { + return CNode::appendChild(newChild); + } + virtual Reference< XNode > SAL_CALL cloneNode(sal_Bool deep) + throw (RuntimeException) + { + return CNode::cloneNode(deep); + } + virtual Reference< XNamedNodeMap > SAL_CALL getAttributes() + throw (RuntimeException) + { + return CNode::getAttributes(); + } + virtual Reference< XNodeList > SAL_CALL getChildNodes() + throw (RuntimeException) + { + return CNode::getChildNodes(); + } + virtual Reference< XNode > SAL_CALL getFirstChild() + throw (RuntimeException) + { + return CNode::getFirstChild(); + } + virtual Reference< XNode > SAL_CALL getLastChild() + throw (RuntimeException) + { + return CNode::getLastChild(); + } + virtual OUString SAL_CALL getLocalName() + throw (RuntimeException) + { + return CNode::getLocalName(); + } + virtual OUString SAL_CALL getNamespaceURI() + throw (RuntimeException) + { + return CNode::getNamespaceURI(); + } + virtual Reference< XNode > SAL_CALL getNextSibling() + throw (RuntimeException) + { + return CNode::getNextSibling(); + } + virtual NodeType SAL_CALL getNodeType() + throw (RuntimeException) + { + return CNode::getNodeType(); + } + virtual Reference< XDocument > SAL_CALL getOwnerDocument() + throw (RuntimeException) + { + return CNode::getOwnerDocument(); + } + virtual Reference< XNode > SAL_CALL getParentNode() + throw (RuntimeException) + { + return CNode::getParentNode(); + } + virtual OUString SAL_CALL getPrefix() + throw (RuntimeException) + { + return CNode::getPrefix(); + } + virtual Reference< XNode > SAL_CALL getPreviousSibling() + throw (RuntimeException) + { + return CNode::getPreviousSibling(); + } + virtual sal_Bool SAL_CALL hasAttributes() + throw (RuntimeException) + { + return CNode::hasAttributes(); + } + virtual sal_Bool SAL_CALL hasChildNodes() + throw (RuntimeException) + { + return CNode::hasChildNodes(); + } + virtual Reference< XNode > SAL_CALL insertBefore( + const Reference< XNode >& newChild, const Reference< XNode >& refChild) + throw (RuntimeException, DOMException) + { + return CNode::insertBefore(newChild, refChild); + } + virtual sal_Bool SAL_CALL isSupported(const OUString& feature, const OUString& ver) + throw (RuntimeException) + { + return CNode::isSupported(feature, ver); + } + virtual void SAL_CALL normalize() + throw (RuntimeException) + { + CNode::normalize(); + } + virtual Reference< XNode > SAL_CALL removeChild(const Reference< XNode >& oldChild) + throw (RuntimeException, DOMException) + { + return CNode::removeChild(oldChild); + } + virtual Reference< XNode > SAL_CALL replaceChild( + const Reference< XNode >& newChild, const Reference< XNode >& oldChild) + throw (RuntimeException, DOMException) + { + return CNode::replaceChild(newChild, oldChild); + } + virtual void SAL_CALL setNodeValue(const OUString& nodeValue) + throw (RuntimeException, DOMException) + { + return CNode::setNodeValue(nodeValue); + } + virtual void SAL_CALL setPrefix(const OUString& prefix) + throw (RuntimeException, DOMException) + { + return CNode::setPrefix(prefix); + } + + + }; +} +#endif + diff --git a/unoxml/source/dom/documenttype.cxx b/unoxml/source/dom/documenttype.cxx new file mode 100644 index 000000000000..4f34c4baaa75 --- /dev/null +++ b/unoxml/source/dom/documenttype.cxx @@ -0,0 +1,130 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2008 by Sun Microsystems, Inc. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: documenttype.cxx,v $ + * $Revision: 1.6 $ + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#include "documenttype.hxx" +#include "entitiesmap.hxx" +#include "notationsmap.hxx" + +#include <string.h> + +namespace DOM +{ + + CDocumentType::CDocumentType(const xmlDtdPtr aDtdPtr) + { + m_aNodeType = NodeType_DOCUMENT_TYPE_NODE; + m_aDtdPtr = aDtdPtr; + init_node((xmlNodePtr)aDtdPtr); + } + + /** + A NamedNodeMap containing the general entities, both external and + internal, declared in the DTD. + */ + Reference< XNamedNodeMap > SAL_CALL CDocumentType::getEntities() throw (RuntimeException) + { + Reference< XNamedNodeMap > aMap; + if (m_aDtdPtr != NULL) + { + aMap = Reference< XNamedNodeMap >(new CEntitiesMap(this)); + } + return aMap; + } + + /** + The internal subset as a string, or null if there is none. + */ + OUString SAL_CALL CDocumentType::getInternalSubset() throw (RuntimeException) + { + // XXX + return OUString(); + } + + /** + The name of DTD; i.e., the name immediately following the DOCTYPE + keyword. + */ + OUString SAL_CALL CDocumentType::getName() throw (RuntimeException) + { + OUString aName; + if (m_aDtdPtr != NULL) + { + aName = OUString((sal_Char*)m_aDtdPtr->name, strlen((char*)m_aDtdPtr->name), RTL_TEXTENCODING_UTF8); + } + return aName; + } + + /** + A NamedNodeMap containing the notations declared in the DTD. + */ + Reference< XNamedNodeMap > SAL_CALL CDocumentType::getNotations() throw (RuntimeException) + { + Reference< XNamedNodeMap > aMap; + if (m_aDtdPtr != NULL) + { + aMap.set(new CNotationsMap(this)); + } + return aMap; + } + + /** + The public identifier of the external subset. + */ + OUString SAL_CALL CDocumentType::getPublicId() throw (RuntimeException) + { + OUString aId; + if (m_aDtdPtr != NULL) + { + aId = OUString((sal_Char*)m_aDtdPtr->name, strlen((char*)m_aDtdPtr->ExternalID), RTL_TEXTENCODING_UTF8); + } + return aId; + } + + /** + The system identifier of the external subset. + */ + OUString SAL_CALL CDocumentType::getSystemId() throw (RuntimeException) + { + OUString aId; + if (m_aDtdPtr != NULL) + { + aId = OUString((sal_Char*)m_aDtdPtr->name, strlen((char*)m_aDtdPtr->SystemID), RTL_TEXTENCODING_UTF8); + } + return aId; + } + OUString SAL_CALL CDocumentType::getNodeName()throw (RuntimeException) + { + return getName(); + } + OUString SAL_CALL CDocumentType::getNodeValue() throw (RuntimeException) + { + return OUString(); + } +} diff --git a/unoxml/source/dom/documenttype.hxx b/unoxml/source/dom/documenttype.hxx new file mode 100644 index 000000000000..79914d3d50b1 --- /dev/null +++ b/unoxml/source/dom/documenttype.hxx @@ -0,0 +1,220 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2008 by Sun Microsystems, Inc. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: documenttype.hxx,v $ + * $Revision: 1.8 $ + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#ifndef _DOCUMENTTYPE_HXX +#define _DOCUMENTTYPE_HXX + +#include <sal/types.h> +#include <com/sun/star/uno/Reference.h> +#include <com/sun/star/uno/Exception.hpp> +#include <com/sun/star/xml/dom/XDocumentType.hpp> +#include <com/sun/star/xml/dom/XNodeList.hpp> +#include <com/sun/star/xml/dom/XNamedNodeMap.hpp> + +#include "node.hxx" + +#include <libxml/tree.h> + +using ::rtl::OUString; +using namespace com::sun::star::uno; +using namespace com::sun::star::xml::dom; + +namespace DOM +{ + class CDocumentType : public cppu::ImplInheritanceHelper1< CNode, XDocumentType > + { + friend class CNode; + private: + xmlDtdPtr m_aDtdPtr; + + protected: + CDocumentType(const xmlDtdPtr aDtdPtr); + + public: + /** + A NamedNodeMap containing the general entities, both external and + internal, declared in the DTD. + */ + virtual Reference< XNamedNodeMap > SAL_CALL getEntities() throw (RuntimeException); + + /** + The internal subset as a string, or null if there is none. + */ + virtual OUString SAL_CALL getInternalSubset() throw (RuntimeException); + + /** + The name of DTD; i.e., the name immediately following the DOCTYPE + keyword. + */ + virtual OUString SAL_CALL getName() throw (RuntimeException); + + /** + A NamedNodeMap containing the notations declared in the DTD. + */ + virtual Reference< XNamedNodeMap > SAL_CALL getNotations() throw (RuntimeException); + + /** + The public identifier of the external subset. + */ + virtual OUString SAL_CALL getPublicId() throw (RuntimeException); + + /** + The system identifier of the external subset. + */ + virtual OUString SAL_CALL getSystemId() throw (RuntimeException); + + // ---- resolve uno inheritance problems... + // overrides for XNode base + virtual OUString SAL_CALL getNodeName() + throw (RuntimeException); + virtual OUString SAL_CALL getNodeValue() + throw (RuntimeException); + // --- delegation for XNde base. + virtual Reference< XNode > SAL_CALL appendChild(const Reference< XNode >& newChild) + throw (RuntimeException, DOMException) + { + return CNode::appendChild(newChild); + } + virtual Reference< XNode > SAL_CALL cloneNode(sal_Bool deep) + throw (RuntimeException) + { + return CNode::cloneNode(deep); + } + virtual Reference< XNamedNodeMap > SAL_CALL getAttributes() + throw (RuntimeException) + { + return CNode::getAttributes(); + } + virtual Reference< XNodeList > SAL_CALL getChildNodes() + throw (RuntimeException) + { + return CNode::getChildNodes(); + } + virtual Reference< XNode > SAL_CALL getFirstChild() + throw (RuntimeException) + { + return CNode::getFirstChild(); + } + virtual Reference< XNode > SAL_CALL getLastChild() + throw (RuntimeException) + { + return CNode::getLastChild(); + } + virtual OUString SAL_CALL getLocalName() + throw (RuntimeException) + { + return CNode::getLocalName(); + } + virtual OUString SAL_CALL getNamespaceURI() + throw (RuntimeException) + { + return CNode::getNamespaceURI(); + } + virtual Reference< XNode > SAL_CALL getNextSibling() + throw (RuntimeException) + { + return CNode::getNextSibling(); + } + virtual NodeType SAL_CALL getNodeType() + throw (RuntimeException) + { + return CNode::getNodeType(); + } + virtual Reference< XDocument > SAL_CALL getOwnerDocument() + throw (RuntimeException) + { + return CNode::getOwnerDocument(); + } + virtual Reference< XNode > SAL_CALL getParentNode() + throw (RuntimeException) + { + return CNode::getParentNode(); + } + virtual OUString SAL_CALL getPrefix() + throw (RuntimeException) + { + return CNode::getPrefix(); + } + virtual Reference< XNode > SAL_CALL getPreviousSibling() + throw (RuntimeException) + { + return CNode::getPreviousSibling(); + } + virtual sal_Bool SAL_CALL hasAttributes() + throw (RuntimeException) + { + return CNode::hasAttributes(); + } + virtual sal_Bool SAL_CALL hasChildNodes() + throw (RuntimeException) + { + return CNode::hasChildNodes(); + } + virtual Reference< XNode > SAL_CALL insertBefore( + const Reference< XNode >& newChild, const Reference< XNode >& refChild) + throw (RuntimeException, DOMException) + { + return CNode::insertBefore(newChild, refChild); + } + virtual sal_Bool SAL_CALL isSupported(const OUString& feature, const OUString& ver) + throw (RuntimeException) + { + return CNode::isSupported(feature, ver); + } + virtual void SAL_CALL normalize() + throw (RuntimeException) + { + CNode::normalize(); + } + virtual Reference< XNode > SAL_CALL removeChild(const Reference< XNode >& oldChild) + throw (RuntimeException, DOMException) + { + return CNode::removeChild(oldChild); + } + virtual Reference< XNode > SAL_CALL replaceChild( + const Reference< XNode >& newChild, const Reference< XNode >& oldChild) + throw (RuntimeException, DOMException) + { + return CNode::replaceChild(newChild, oldChild); + } + virtual void SAL_CALL setNodeValue(const OUString& nodeValue) + throw (RuntimeException, DOMException) + { + return CNode::setNodeValue(nodeValue); + } + virtual void SAL_CALL setPrefix(const OUString& prefix) + throw (RuntimeException, DOMException) + { + return CNode::setPrefix(prefix); + } + + }; +} +#endif diff --git a/unoxml/source/dom/domimplementation.cxx b/unoxml/source/dom/domimplementation.cxx new file mode 100644 index 000000000000..1cc2bd3d261c --- /dev/null +++ b/unoxml/source/dom/domimplementation.cxx @@ -0,0 +1,78 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2008 by Sun Microsystems, Inc. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: domimplementation.cxx,v $ + * $Revision: 1.5 $ + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#include "domimplementation.hxx" + +namespace DOM +{ + CDOMImplementation* CDOMImplementation::aDOMImplementation = new CDOMImplementation(); + CDOMImplementation* CDOMImplementation::get() + { + return CDOMImplementation::aDOMImplementation; + } + + /** + Creates a DOM Document object of the specified type with its document element. + */ + Reference <XDocument > SAL_CALL CDOMImplementation::createDocument( + const OUString& namespaceURI, + const OUString& qualifiedName, + const Reference< XDocumentType >& doctype) + throw (RuntimeException) + { + OUString aNamespaceURI = namespaceURI; + OUString aQName = qualifiedName; + Reference< XDocumentType > aType = doctype; + return Reference<XDocument>(); + } + + /** + Creates an empty DocumentType node. + */ + Reference< XDocumentType > SAL_CALL CDOMImplementation::createDocumentType( + const OUString& qualifiedName, const OUString& publicId, const OUString& systemId) + throw (RuntimeException) + { + OUString qName = qualifiedName; + OUString aPublicId = publicId; + OUString aSystemId = systemId; + return Reference<XDocumentType>(); + } + /** + Test if the DOM implementation implements a specific feature. + */ + sal_Bool SAL_CALL CDOMImplementation::hasFeature(const OUString& feature, const OUString& ver) + throw (RuntimeException) + { + OUString aFeature = feature; + OUString aVersion = ver; + return sal_False; + } +} diff --git a/unoxml/source/dom/domimplementation.hxx b/unoxml/source/dom/domimplementation.hxx new file mode 100644 index 000000000000..c335ab7c2ec8 --- /dev/null +++ b/unoxml/source/dom/domimplementation.hxx @@ -0,0 +1,76 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2008 by Sun Microsystems, Inc. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: domimplementation.hxx,v $ + * $Revision: 1.6 $ + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#ifndef _DOMIMPLEMENTATION_HXX +#define _DOMIMPLEMENTATION_HXX + +#include <map> +#include <sal/types.h> +#include <cppuhelper/implbase1.hxx> +#include <com/sun/star/uno/Reference.h> +#include <com/sun/star/uno/Exception.hpp> +#include <com/sun/star/xml/dom/XDocument.hpp> +#include <com/sun/star/xml/dom/XDocumentType.hpp> +#include <com/sun/star/xml/dom/XDOMImplementation.hpp> +#include <com/sun/star/xml/dom/XDOMImplementation.hpp> + +using ::rtl::OUString; +using namespace com::sun::star::uno; +using namespace com::sun::star::xml::dom; + +namespace DOM +{ + class CDOMImplementation : public cppu::WeakImplHelper1< XDOMImplementation > + { + + public: + static CDOMImplementation* aDOMImplementation; + static CDOMImplementation* get(); + + /** + Creates a DOM Document object of the specified type with its document element. + */ + virtual Reference <XDocument > SAL_CALL createDocument(const OUString& namespaceURI, const OUString& qualifiedName, const Reference< XDocumentType >& doctype) + throw (RuntimeException); + + /** + Creates an empty DocumentType node. + */ + virtual Reference< XDocumentType > SAL_CALL createDocumentType(const OUString& qualifiedName, const OUString& publicId, const OUString& systemId) + throw (RuntimeException); + + /** + Test if the DOM implementation implements a specific feature. + */ + virtual sal_Bool SAL_CALL hasFeature(const OUString& feature, const OUString& ver) + throw (RuntimeException); + }; +} +#endif diff --git a/unoxml/source/dom/element.cxx b/unoxml/source/dom/element.cxx new file mode 100644 index 000000000000..87ec048570d8 --- /dev/null +++ b/unoxml/source/dom/element.cxx @@ -0,0 +1,652 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2008 by Sun Microsystems, Inc. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: element.cxx,v $ + * $Revision: 1.14.20.1 $ + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#include "node.hxx" +#include "element.hxx" +#include "attr.hxx" +#include "elementlist.hxx" +#include "attributesmap.hxx" +#include "../events/mutationevent.hxx" + +#include "comphelper/attributelist.hxx" +#include <com/sun/star/xml/sax/FastToken.hdl> + +#include <string.h> + + +namespace DOM +{ + + CElement::CElement(const xmlNodePtr aNodePtr) + { + m_aNodeType = NodeType_ELEMENT_NODE; + init_node(aNodePtr); + } + + void SAL_CALL CElement::saxify( + const Reference< XDocumentHandler >& i_xHandler) { + if (!i_xHandler.is()) throw RuntimeException(); + comphelper::AttributeList *pAttrs = + new comphelper::AttributeList(); + OUString type = OUString::createFromAscii(""); + // add namespace definitions to attributes + for (xmlNsPtr pNs = m_aNodePtr->nsDef; pNs != 0; pNs = pNs->next) { + const xmlChar *pPrefix = pNs->prefix; + OUString prefix(reinterpret_cast<const sal_Char*>(pPrefix), + strlen(reinterpret_cast<const char*>(pPrefix)), + RTL_TEXTENCODING_UTF8); + OUString name = (prefix.equalsAscii("")) + ? OUString::createFromAscii("xmlns") + : OUString::createFromAscii("xmlns:") + prefix; + const xmlChar *pHref = pNs->href; + OUString val(reinterpret_cast<const sal_Char*>(pHref), + strlen(reinterpret_cast<const char*>(pHref)), + RTL_TEXTENCODING_UTF8); + pAttrs->AddAttribute(name, type, val); + } + // add attributes + for (xmlAttrPtr pAttr = m_aNodePtr->properties; + pAttr != 0; pAttr = pAttr->next) { + CNode * pNode = CNode::get(reinterpret_cast<xmlNodePtr>(pAttr)); + OSL_ENSURE(pNode != 0, "CNode::get returned 0"); + OUString prefix = pNode->getPrefix(); + OUString name = (prefix.getLength() == 0) + ? pNode->getLocalName() + : prefix + OUString(static_cast<sal_Unicode>(':')) + pNode->getLocalName(); + OUString val = pNode->getNodeValue(); + pAttrs->AddAttribute(name, type, val); + } + OUString prefix = getPrefix(); + OUString name = (prefix.getLength() == 0) + ? getLocalName() + : prefix + OUString(static_cast<sal_Unicode>(':')) + getLocalName(); + Reference< XAttributeList > xAttrList(pAttrs); + i_xHandler->startElement(name, xAttrList); + // recurse + for (xmlNodePtr pChild = m_aNodePtr->children; + pChild != 0; pChild = pChild->next) { + CNode * pNode = CNode::get(pChild); + OSL_ENSURE(pNode != 0, "CNode::get returned 0"); + pNode->saxify(i_xHandler); + } + i_xHandler->endElement(name); + } + + void SAL_CALL CElement::fastSaxify( Context& i_rContext ) { + if (!i_rContext.mxDocHandler.is()) throw RuntimeException(); + pushContext(i_rContext); + addNamespaces(i_rContext,m_aNodePtr); + + // add attributes + i_rContext.mxAttribList->clear(); + for (xmlAttrPtr pAttr = m_aNodePtr->properties; + pAttr != 0; pAttr = pAttr->next) { + CNode * pNode = CNode::get(reinterpret_cast<xmlNodePtr>(pAttr)); + OSL_ENSURE(pNode != 0, "CNode::get returned 0"); + + const xmlChar* xName = pAttr->name; + sal_Int32 nAttributeToken=FastToken::DONTKNOW; + + if( pAttr->ns && strlen((char*)pAttr->ns->prefix) ) + nAttributeToken = getTokenWithPrefix( i_rContext, + (sal_Char*)pAttr->ns->prefix, + (sal_Char*)xName ); + else + nAttributeToken = getToken( i_rContext, (sal_Char*)xName ); + + if( nAttributeToken != FastToken::DONTKNOW ) + i_rContext.mxAttribList->add( nAttributeToken, + OUStringToOString(pNode->getNodeValue(), + RTL_TEXTENCODING_UTF8)); + } + + const xmlChar* xPrefix = m_aNodePtr->ns ? m_aNodePtr->ns->prefix : (const xmlChar*)""; + const xmlChar* xName = m_aNodePtr->name; + sal_Int32 nElementToken=FastToken::DONTKNOW; + if( strlen((char*)xPrefix) ) + nElementToken = getTokenWithPrefix( i_rContext, (sal_Char*)xPrefix, (sal_Char*)xName ); + else + nElementToken = getToken( i_rContext, (sal_Char*)xName ); + + Reference<XFastContextHandler> xParentHandler(i_rContext.mxCurrentHandler); + try + { + Reference< XFastAttributeList > xAttr( i_rContext.mxAttribList.get() ); + if( nElementToken == FastToken::DONTKNOW ) + { + const OUString aNamespace; + const OUString aElementName( (sal_Char*)xPrefix, + strlen((char*)xPrefix), + RTL_TEXTENCODING_UTF8 ); + + if( xParentHandler.is() ) + i_rContext.mxCurrentHandler = xParentHandler->createUnknownChildContext( aNamespace, aElementName, xAttr ); + else + i_rContext.mxCurrentHandler = i_rContext.mxDocHandler->createUnknownChildContext( aNamespace, aElementName, xAttr ); + + if( i_rContext.mxCurrentHandler.is() ) + i_rContext.mxCurrentHandler->startUnknownElement( aNamespace, aElementName, xAttr ); + } + else + { + if( xParentHandler.is() ) + i_rContext.mxCurrentHandler = xParentHandler->createFastChildContext( nElementToken, xAttr ); + else + i_rContext.mxCurrentHandler = i_rContext.mxDocHandler->createFastChildContext( nElementToken, xAttr ); + + if( i_rContext.mxCurrentHandler.is() ) + i_rContext.mxCurrentHandler->startFastElement( nElementToken, xAttr ); + } + } + catch( Exception& ) + {} + + // recurse + for (xmlNodePtr pChild = m_aNodePtr->children; + pChild != 0; pChild = pChild->next) { + CNode * pNode = CNode::get(pChild); + OSL_ENSURE(pNode != 0, "CNode::get returned 0"); + pNode->fastSaxify(i_rContext); + } + + if( i_rContext.mxCurrentHandler.is() ) try + { + if( nElementToken != FastToken::DONTKNOW ) + i_rContext.mxCurrentHandler->endFastElement( nElementToken ); + else + { + const OUString aNamespace; + const OUString aElementName( (sal_Char*)xPrefix, + strlen((char*)xPrefix), + RTL_TEXTENCODING_UTF8 ); + + i_rContext.mxCurrentHandler->endUnknownElement( aNamespace, aElementName ); + } + } + catch( Exception& ) + {} + + // restore after children have been processed + i_rContext.mxCurrentHandler = xParentHandler; + popContext(i_rContext); + } + + /** + Retrieves an attribute value by name. + return empty string if attribute is not set + */ + OUString CElement::getAttribute(const OUString& name) + throw (RuntimeException) + { + OUString aValue; + // search properties + if (m_aNodePtr != NULL) + { + OString o1 = OUStringToOString(name, RTL_TEXTENCODING_UTF8); + xmlChar *xValue = xmlGetProp(m_aNodePtr, (xmlChar*)o1.getStr()); + if (xValue != NULL) { + aValue = OUString((sal_Char*)xValue, strlen((char*)xValue), RTL_TEXTENCODING_UTF8); + } + } + return aValue; + } + + /** + Retrieves an attribute node by name. + */ + Reference< XAttr > CElement::getAttributeNode(const OUString& name) + throw (RuntimeException) + { + Reference< XAttr > aAttr; + if (m_aNodePtr != NULL) + { + OString o1 = OUStringToOString(name, RTL_TEXTENCODING_UTF8); + xmlChar *xName = (xmlChar*)o1.getStr(); + xmlAttrPtr pAttr = xmlHasProp(m_aNodePtr, xName); + aAttr = Reference< XAttr >(static_cast< CAttr* >(CNode::get((xmlNodePtr)pAttr))); + } + return aAttr; + } + + /** + Retrieves an Attr node by local name and namespace URI. + */ + Reference< XAttr > CElement::getAttributeNodeNS( + const OUString& namespaceURI, const OUString& localName) + throw (RuntimeException) + { + Reference< XAttr > aAttr; + if (m_aNodePtr != NULL) + { + OString o1 = OUStringToOString(localName, RTL_TEXTENCODING_UTF8); + xmlChar *xName = (xmlChar*)o1.getStr(); + OString o2 = OUStringToOString(namespaceURI, RTL_TEXTENCODING_UTF8); + xmlChar *xNS = (xmlChar*)o2.getStr(); + xmlAttrPtr pAttr = xmlHasNsProp(m_aNodePtr, xName, xNS); + aAttr = Reference< XAttr >(static_cast< CAttr* >(CNode::get((xmlNodePtr)pAttr))); + } + return aAttr; + } + + /** + Retrieves an attribute value by local name and namespace URI. + return empty string if attribute is not set + */ + OUString CElement::getAttributeNS(const OUString& namespaceURI, const OUString& localName) + throw (RuntimeException) + { + OUString aValue; + // search properties + if (m_aNodePtr != NULL) + { + OString o1 = OUStringToOString(localName, RTL_TEXTENCODING_UTF8); + xmlChar *xName = (xmlChar*)o1.getStr(); + OString o2 = OUStringToOString(namespaceURI, RTL_TEXTENCODING_UTF8); + xmlChar *xNS = (xmlChar*)o2.getStr(); + xmlChar *xValue = (xmlChar*)xmlGetNsProp(m_aNodePtr, xName, xNS); + if (xValue != NULL) { + aValue = OUString((sal_Char*)xValue, strlen((char*)xValue), RTL_TEXTENCODING_UTF8); + xmlFree(xValue); + } + } + return aValue; + } + + /** + Returns a NodeList of all descendant Elements with a given tag name, + in the order in which they are + encountered in a preorder traversal of this Element tree. + */ + Reference< XNodeList > CElement::getElementsByTagName(const OUString& name) + throw (RuntimeException) + { + Reference< XNodeList > aList = Reference< XNodeList >(new CElementList(this, name)); + return aList; + } + + /** + Returns a NodeList of all the descendant Elements with a given local + name and namespace URI in the order in which they are encountered in + a preorder traversal of this Element tree. + */ + Reference< XNodeList > CElement::getElementsByTagNameNS(const OUString& namespaceURI, + const OUString& localName) + throw (RuntimeException) + { + Reference< XNodeList > aList = Reference< XNodeList >(new CElementList(this, localName, namespaceURI)); + return aList; + } + + /** + The name of the element. + */ + OUString CElement::getTagName() + throw (RuntimeException) + { + OUString aName; + if (m_aNodePtr != NULL) + { + aName = OUString((sal_Char*)m_aNodePtr->name, strlen((char*)m_aNodePtr->name), RTL_TEXTENCODING_UTF8); + } + return aName; + } + + + /** + Returns true when an attribute with a given name is specified on this + element or has a default value, false otherwise. + */ + sal_Bool CElement::hasAttribute(const OUString& name) + throw (RuntimeException) + { + OString o1 = OUStringToOString(name, RTL_TEXTENCODING_UTF8); + xmlChar *xName = (xmlChar*)o1.getStr(); + return (m_aNodePtr != NULL && xmlHasProp(m_aNodePtr, xName) != NULL); + } + + /** + Returns true when an attribute with a given local name and namespace + URI is specified on this element or has a default value, false otherwise. + */ + sal_Bool CElement::hasAttributeNS(const OUString& namespaceURI, const OUString& localName) + throw (RuntimeException) + { + OString o1 = OUStringToOString(localName, RTL_TEXTENCODING_UTF8); + xmlChar *xName = (xmlChar*)o1.getStr(); + OString o2 = OUStringToOString(namespaceURI, RTL_TEXTENCODING_UTF8); + xmlChar *xNs = (xmlChar*)o2.getStr(); + return (m_aNodePtr != NULL && xmlHasNsProp(m_aNodePtr, xName, xNs) != NULL); + } + + /** + Removes an attribute by name. + */ + void CElement::removeAttribute(const OUString& name) + throw (RuntimeException, DOMException) + { + xmlChar *xName = (xmlChar*)OUStringToOString(name, RTL_TEXTENCODING_UTF8).getStr(); + if (m_aNodePtr != NULL) { + xmlUnsetProp(m_aNodePtr, xName); + } + } + + /** + Removes an attribute by local name and namespace URI. + */ + void CElement::removeAttributeNS(const OUString& namespaceURI, const OUString& localName) + throw (RuntimeException, DOMException) + { + OString o1 = OUStringToOString(localName, RTL_TEXTENCODING_UTF8); + xmlChar *xName = (xmlChar*)o1.getStr(); + OString o2 = OUStringToOString(namespaceURI, RTL_TEXTENCODING_UTF8); + xmlChar *xURI = (xmlChar*)o2.getStr(); + if (m_aNodePtr != NULL) { + // XXX + xmlNsPtr pNs = xmlSearchNsByHref(m_aNodePtr->doc, m_aNodePtr, xURI); + xmlUnsetNsProp(m_aNodePtr, pNs, xName); + } + } + + /** + Removes the specified attribute node. + */ + Reference< XAttr > CElement::removeAttributeNode(const Reference< XAttr >& oldAttr) + throw (RuntimeException, DOMException) + { + Reference< XAttr > aAttr; + if(m_aNodePtr != NULL) + { + xmlAttrPtr pAttr = (xmlAttrPtr) CNode::getNodePtr(oldAttr.get()); + + if (pAttr->parent != m_aNodePtr) + { + DOMException e; + e.Code = DOMExceptionType_HIERARCHY_REQUEST_ERR; + throw e; + } + if (pAttr->doc != m_aNodePtr->doc) + { + DOMException e; + e.Code = DOMExceptionType_WRONG_DOCUMENT_ERR; + throw e; + } + + if (oldAttr->getNamespaceURI().getLength() > 0) + aAttr = oldAttr->getOwnerDocument()->createAttributeNS( + oldAttr->getNamespaceURI(), oldAttr->getName()); + else + aAttr = oldAttr->getOwnerDocument()->createAttribute(oldAttr->getName()); + aAttr->setValue(oldAttr->getValue()); + xmlRemoveProp(pAttr); + + } + return aAttr; + } + + /** + Adds a new attribute node. + */ + Reference< XAttr > CElement::_setAttributeNode(const Reference< XAttr >& newAttr, sal_Bool bNS) + throw (RuntimeException) + { + Reference< XAttr > aAttr; + if (m_aNodePtr != NULL) + { + // check whether the attrib belongs to this document + Reference< XDocument > newDoc(newAttr->getOwnerDocument(), UNO_QUERY); + Reference< XDocument > oldDoc(CNode::getOwnerDocument(), UNO_QUERY); + if (newDoc != oldDoc) { + throw RuntimeException(); + } + + // get the implementation + xmlAttrPtr pAttr = (xmlAttrPtr) CNode::getNodePtr(newAttr.get()); + + // check whether the attribute is not in use by another element + xmlNsPtr pNs = NULL; + if (pAttr->parent != NULL) + if(strcmp((char*)pAttr->parent->name, "__private") == 0 + && pNs && pAttr->ns != NULL) + { + pNs = xmlSearchNs(m_aNodePtr->doc, m_aNodePtr, pAttr->ns->prefix); + if (pNs == NULL || strcmp((char*)pNs->href, (char*)pAttr->ns->href) !=0 ) + pNs = xmlNewNs(m_aNodePtr, pAttr->ns->href, pAttr->ns->href); + else + throw RuntimeException(); + } + + xmlAttrPtr res = NULL; + + if (bNS) + res = xmlNewNsProp(m_aNodePtr, pNs, pAttr->name, pAttr->children->content); + else + res = xmlNewProp(m_aNodePtr, pAttr->name, pAttr->children->content); + + // free carrier node ... + if(pAttr->parent != NULL && strcmp((char*)pAttr->parent->name, "__private")== 0) + xmlFreeNode(pAttr->parent); + // ... remove the old attr from the node cache + CNode::remove((xmlNodePtr)pAttr); + + // get the new attr node + aAttr = Reference< XAttr >(static_cast< CAttr* >(CNode::get((xmlNodePtr)res))); + } + + if (aAttr.is()) + { + // attribute adition event + // dispatch DOMAttrModified event + Reference< XDocumentEvent > docevent(getOwnerDocument(), UNO_QUERY); + Reference< XMutationEvent > event(docevent->createEvent( + OUString::createFromAscii("DOMAttrModified")), UNO_QUERY); + event->initMutationEvent(OUString::createFromAscii("DOMAttrModified"), + sal_True, sal_False, Reference< XNode >(aAttr, UNO_QUERY), + OUString(), aAttr->getValue(), aAttr->getName(), AttrChangeType_ADDITION); + dispatchEvent(Reference< XEvent >(event, UNO_QUERY)); + dispatchSubtreeModified(); + } + return aAttr; + } + + Reference< XAttr > CElement::setAttributeNode(const Reference< XAttr >& newAttr) + throw (RuntimeException, DOMException) + { + return _setAttributeNode(newAttr, sal_False); + } + + /** + Adds a new attribute. + */ + Reference< XAttr > CElement::setAttributeNodeNS(const Reference< XAttr >& newAttr) + throw (RuntimeException, DOMException) + { + return _setAttributeNode(newAttr, sal_True); + } + + /** + Adds a new attribute. + */ + void CElement::setAttribute(const OUString& name, const OUString& value) + throw (RuntimeException, DOMException) + { + OString o1 = OUStringToOString(name, RTL_TEXTENCODING_UTF8); + xmlChar *xName = (xmlChar*)o1.getStr(); + OString o2 = OUStringToOString(value, RTL_TEXTENCODING_UTF8); + xmlChar *xValue = (xmlChar*)o2.getStr(); + if (m_aNodePtr != NULL) + { + OUString oldValue; + AttrChangeType aChangeType = AttrChangeType_MODIFICATION; + xmlChar *xOld = xmlGetProp(m_aNodePtr, xName); + if (xOld == NULL) + { + aChangeType = AttrChangeType_ADDITION; + xmlNewProp(m_aNodePtr, xName, xValue); + } + else + { + oldValue = OUString((char*)xOld, strlen((char*)xOld), RTL_TEXTENCODING_UTF8); + xmlSetProp(m_aNodePtr, xName, xValue); + } + + // dispatch DOMAttrModified event + + Reference< XDocumentEvent > docevent(getOwnerDocument(), UNO_QUERY); + Reference< XMutationEvent > event(docevent->createEvent( + OUString::createFromAscii("DOMAttrModified")), UNO_QUERY); + event->initMutationEvent(OUString::createFromAscii("DOMAttrModified"), + sal_True, sal_False, Reference< XNode >(getAttributeNode(name), UNO_QUERY), + oldValue, value, name, aChangeType); + dispatchEvent(Reference< XEvent >(event, UNO_QUERY)); + dispatchSubtreeModified(); + } + } + + /** + Adds a new attribute. + */ + void CElement::setAttributeNS( + const OUString& namespaceURI, const OUString& qualifiedName, const OUString& value) + throw (RuntimeException, DOMException) + { + if (namespaceURI.getLength() == 0) throw RuntimeException(); + + OString o1, o2, o3, o4, o5; + xmlChar *xPrefix = NULL; + xmlChar *xLName = NULL; + o1 = OUStringToOString(qualifiedName, RTL_TEXTENCODING_UTF8); + xmlChar *xQName = (xmlChar*)o1.getStr(); + sal_Int32 idx = qualifiedName.indexOf(':'); + if (idx != -1) + { + o2 = OUStringToOString( + qualifiedName.copy(0,idx), + RTL_TEXTENCODING_UTF8); + xPrefix = (xmlChar*)o2.getStr(); + o3 = OUStringToOString( + qualifiedName.copy(idx+1), + RTL_TEXTENCODING_UTF8); + xLName = (xmlChar*)o3.getStr(); + } else { + xPrefix = (xmlChar*)""; + xLName = xQName; + } + o4 = OUStringToOString(namespaceURI, RTL_TEXTENCODING_UTF8); + o5 = OUStringToOString(value, RTL_TEXTENCODING_UTF8); + xmlChar *xURI= (xmlChar*)o4.getStr(); + xmlChar *xValue = (xmlChar*)o5.getStr(); + if (m_aNodePtr != NULL) + { + //find the right namespace + xmlNsPtr pNs = xmlSearchNs(m_aNodePtr->doc, m_aNodePtr, xPrefix); + // if no namespace found, create a new one + if (pNs == NULL) + pNs = xmlNewNs(m_aNodePtr, xURI, xPrefix); + + if (strcmp((char*)pNs->href, (char*)xURI) == 0) + { + // found namespace matches + + OUString oldValue; + AttrChangeType aChangeType = AttrChangeType_MODIFICATION; + xmlChar *xOld = xmlGetNsProp(m_aNodePtr, xLName, pNs->href); + if (xOld == NULL) + { + aChangeType = AttrChangeType_ADDITION; + xmlNewNsProp(m_aNodePtr, pNs, xLName, xValue); + } + else + { + oldValue = OUString((char *)xOld, strlen((char *)xOld), RTL_TEXTENCODING_UTF8); + xmlSetNsProp(m_aNodePtr, pNs, xLName, xValue); + } + // dispatch DOMAttrModified event + Reference< XDocumentEvent > docevent(getOwnerDocument(), UNO_QUERY); + Reference< XMutationEvent > event(docevent->createEvent( + OUString::createFromAscii("DOMAttrModified")), UNO_QUERY); + event->initMutationEvent(OUString::createFromAscii("DOMAttrModified"), sal_True, sal_False, + Reference< XNode >(getAttributeNodeNS(namespaceURI, OUString((char*)xLName, strlen((char*)xLName), RTL_TEXTENCODING_UTF8)), UNO_QUERY), + oldValue, value, qualifiedName, aChangeType); + dispatchEvent(Reference< XEvent >(event, UNO_QUERY)); + dispatchSubtreeModified(); + + } else { + // ambigious ns prefix + throw RuntimeException(); + } + + } + } + + Reference< XNamedNodeMap > SAL_CALL CElement::getAttributes()throw (RuntimeException) + { + Reference< XNamedNodeMap > aMap; + if (hasAttributes()) { + aMap = Reference< XNamedNodeMap >(new CAttributesMap(this)); + } + return aMap; + } + OUString SAL_CALL CElement::getNodeName()throw (RuntimeException) + { + return getLocalName(); + } + OUString SAL_CALL CElement::getLocalName()throw (RuntimeException) + { + OUString aName; + if (m_aNodePtr != NULL) + { + const xmlChar* xName = m_aNodePtr->name; + aName = OUString((const sal_Char*)xName, strlen((const char*)xName), RTL_TEXTENCODING_UTF8); + } + return aName; + } + OUString SAL_CALL CElement::getNodeValue() throw (RuntimeException) + { + return OUString(); + } + + void SAL_CALL CElement::setElementName(const OUString& aName) throw (RuntimeException, DOMException) + { + if (aName.getLength() > 0 && aName.indexOf(OUString::createFromAscii(":")) < 0) + { + OString oName = OUStringToOString(aName, RTL_TEXTENCODING_UTF8); + xmlChar *xName = (xmlChar*)oName.getStr(); + // xmlFree((void*)m_aNodePtr->name); + m_aNodePtr->name = xmlStrdup(xName); + } + else + { + DOMException e; + e.Code = DOMExceptionType_INVALID_CHARACTER_ERR; + throw e; + } + } + +} diff --git a/unoxml/source/dom/element.hxx b/unoxml/source/dom/element.hxx new file mode 100644 index 000000000000..b56939f41ae0 --- /dev/null +++ b/unoxml/source/dom/element.hxx @@ -0,0 +1,299 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2008 by Sun Microsystems, Inc. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: element.hxx,v $ + * $Revision: 1.9 $ + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#ifndef _ELEMENT_HXX +#define _ELEMENT_HXX +#include <com/sun/star/uno/Reference.h> +#include <com/sun/star/uno/Exception.hpp> +#include <com/sun/star/xml/dom/XNode.hpp> +#include <com/sun/star/xml/dom/XNodeList.hpp> +#include <com/sun/star/xml/dom/XNamedNodeMap.hpp> +#include <com/sun/star/xml/dom/NodeType.hpp> +#include <libxml/tree.h> +#include "node.hxx" + +using ::rtl::OUString; +using namespace com::sun::star::uno; +using namespace com::sun::star::xml::dom; + +namespace DOM +{ + class CElement : public cppu::ImplInheritanceHelper1<CNode, XElement > + { + friend class CNode; + private: + Reference< XAttr > _setAttributeNode(const Reference< XAttr >& newAttr, sal_Bool bNS) + throw (RuntimeException); + + protected: + CElement(const xmlNodePtr aNodePtr); + + public: + + virtual void SAL_CALL saxify( + const Reference< XDocumentHandler >& i_xHandler); + + virtual void SAL_CALL fastSaxify( Context& i_rContext ); + + /** + Retrieves an attribute value by name. + */ + virtual OUString SAL_CALL getAttribute(const OUString& name) + throw (RuntimeException); + + /** + Retrieves an attribute node by name. + */ + virtual Reference< XAttr > SAL_CALL getAttributeNode(const OUString& name) + throw (RuntimeException); + + /** + Retrieves an Attr node by local name and namespace URI. + */ + virtual Reference< XAttr > SAL_CALL getAttributeNodeNS(const OUString& namespaceURI, const OUString& localName) + throw (RuntimeException); + + /** + Retrieves an attribute value by local name and namespace URI. + */ + virtual OUString SAL_CALL getAttributeNS(const OUString& namespaceURI, const OUString& localName) + throw (RuntimeException); + + /** + Returns a NodeList of all descendant Elements with a given tag name, + in the order in which they are + encountered in a preorder traversal of this Element tree. + */ + virtual Reference< XNodeList > SAL_CALL getElementsByTagName(const OUString& name) + throw (RuntimeException); + + /** + Returns a NodeList of all the descendant Elements with a given local + name and namespace URI in the order in which they are encountered in + a preorder traversal of this Element tree. + */ + virtual Reference< XNodeList > SAL_CALL getElementsByTagNameNS(const OUString& namespaceURI, + const OUString& localName) + throw (RuntimeException); + + /** + The name of the element. + */ + virtual OUString SAL_CALL getTagName() + throw (RuntimeException); + + /** + Returns true when an attribute with a given name is specified on this + element or has a default value, false otherwise. + */ + virtual sal_Bool SAL_CALL hasAttribute(const OUString& name) + throw (RuntimeException); + + /** + Returns true when an attribute with a given local name and namespace + URI is specified on this element or has a default value, false otherwise. + */ + virtual sal_Bool SAL_CALL hasAttributeNS(const OUString& namespaceURI, const OUString& localName) + throw (RuntimeException); + + /** + Removes an attribute by name. + */ + virtual void SAL_CALL removeAttribute(const OUString& name) + throw (RuntimeException, DOMException); + + /** + Removes the specified attribute node. + */ + virtual Reference< XAttr > SAL_CALL removeAttributeNode(const Reference< XAttr >& oldAttr) + throw (RuntimeException, DOMException); + + /** + Removes an attribute by local name and namespace URI. + */ + virtual void SAL_CALL removeAttributeNS(const OUString& namespaceURI, const OUString& localName) + throw (RuntimeException, DOMException); + + /** + Adds a new attribute. + */ + virtual void SAL_CALL setAttribute(const OUString& name, const OUString& value) + throw (RuntimeException, DOMException); + + /** + Adds a new attribute node. + */ + virtual Reference< XAttr > SAL_CALL setAttributeNode(const Reference< XAttr >& newAttr) + throw (RuntimeException, DOMException); + + /** + Adds a new attribute. + */ + virtual Reference< XAttr > SAL_CALL setAttributeNodeNS(const Reference< XAttr >& newAttr) + throw (RuntimeException, DOMException); + + /** + Adds a new attribute. + */ + virtual void SAL_CALL setAttributeNS( + const OUString& namespaceURI, const OUString& qualifiedName, const OUString& value) + throw (RuntimeException, DOMException); + + /** + sets the element name + */ + virtual void SAL_CALL setElementName(const OUString& elementName) + throw (RuntimeException, DOMException); + + // overrides for XNode base + virtual OUString SAL_CALL getNodeName() + throw (RuntimeException); + virtual OUString SAL_CALL getNodeValue() + throw (RuntimeException); + virtual Reference< XNamedNodeMap > SAL_CALL getAttributes() + throw (RuntimeException); + virtual OUString SAL_CALL getLocalName() + throw (RuntimeException); + + // resolve uno inheritance problems... + // --- delegation for XNde base. + virtual Reference< XNode > SAL_CALL appendChild(const Reference< XNode >& newChild) + throw (RuntimeException, DOMException) + { + return CNode::appendChild(newChild); + } + virtual Reference< XNode > SAL_CALL cloneNode(sal_Bool deep) + throw (RuntimeException) + { + return CNode::cloneNode(deep); + } + virtual Reference< XNodeList > SAL_CALL getChildNodes() + throw (RuntimeException) + { + return CNode::getChildNodes(); + } + virtual Reference< XNode > SAL_CALL getFirstChild() + throw (RuntimeException) + { + return CNode::getFirstChild(); + } + virtual Reference< XNode > SAL_CALL getLastChild() + throw (RuntimeException) + { + return CNode::getLastChild(); + } + virtual OUString SAL_CALL getNamespaceURI() + throw (RuntimeException) + { + return CNode::getNamespaceURI(); + } + virtual Reference< XNode > SAL_CALL getNextSibling() + throw (RuntimeException) + { + return CNode::getNextSibling(); + } + virtual NodeType SAL_CALL getNodeType() + throw (RuntimeException) + { + return CNode::getNodeType(); + } + virtual Reference< XDocument > SAL_CALL getOwnerDocument() + throw (RuntimeException) + { + return CNode::getOwnerDocument(); + } + virtual Reference< XNode > SAL_CALL getParentNode() + throw (RuntimeException) + { + return CNode::getParentNode(); + } + virtual OUString SAL_CALL getPrefix() + throw (RuntimeException) + { + return CNode::getPrefix(); + } + virtual Reference< XNode > SAL_CALL getPreviousSibling() + throw (RuntimeException) + { + return CNode::getPreviousSibling(); + } + virtual sal_Bool SAL_CALL hasAttributes() + throw (RuntimeException) + { + return CNode::hasAttributes(); + } + virtual sal_Bool SAL_CALL hasChildNodes() + throw (RuntimeException) + { + return CNode::hasChildNodes(); + } + virtual Reference< XNode > SAL_CALL insertBefore( + const Reference< XNode >& newChild, const Reference< XNode >& refChild) + throw (RuntimeException, DOMException) + { + return CNode::insertBefore(newChild, refChild); + } + virtual sal_Bool SAL_CALL isSupported(const OUString& feature, const OUString& ver) + throw (RuntimeException) + { + return CNode::isSupported(feature, ver); + } + virtual void SAL_CALL normalize() + throw (RuntimeException) + { + CNode::normalize(); + } + virtual Reference< XNode > SAL_CALL removeChild(const Reference< XNode >& oldChild) + throw (RuntimeException, DOMException) + { + return CNode::removeChild(oldChild); + } + virtual Reference< XNode > SAL_CALL replaceChild( + const Reference< XNode >& newChild, const Reference< XNode >& oldChild) + throw (RuntimeException, DOMException) + { + return CNode::replaceChild(newChild, oldChild); + } + virtual void SAL_CALL setNodeValue(const OUString& nodeValue) + throw (RuntimeException, DOMException) + { + return CNode::setNodeValue(nodeValue); + } + virtual void SAL_CALL setPrefix(const OUString& prefix) + throw (RuntimeException, DOMException) + { + return CNode::setPrefix(prefix); + } + + }; + +} + +#endif diff --git a/unoxml/source/dom/elementlist.cxx b/unoxml/source/dom/elementlist.cxx new file mode 100644 index 000000000000..53696edea1c3 --- /dev/null +++ b/unoxml/source/dom/elementlist.cxx @@ -0,0 +1,136 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2008 by Sun Microsystems, Inc. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: elementlist.cxx,v $ + * $Revision: 1.7 $ + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#include "elementlist.hxx" + +#include <string.h> + +namespace DOM +{ + + CElementList::CElementList(const CElement* aElement, const OUString& aName) + : m_pElement(aElement) + , m_aName(aName) + , xURI(0) + , m_bRebuild(sal_True) + { + OString o1 = OUStringToOString(aName, RTL_TEXTENCODING_UTF8); + xName = new xmlChar[o1.getLength()]; + strcpy((char*)xName, o1.getStr()); + registerListener(aElement); + } + + CElementList::CElementList(const CElement* aElement, const OUString& aName, const OUString& aURI) + : m_pElement(aElement) + , m_aName(aName) + , m_aURI(aURI) + , m_bRebuild(sal_True) + { + OString o1 = OUStringToOString(aName, RTL_TEXTENCODING_UTF8); + xName = new xmlChar[o1.getLength()]; + strcpy((char*)xName, o1.getStr()); + OString o2 = OUStringToOString(aURI, RTL_TEXTENCODING_UTF8); + xURI = new xmlChar[o2.getLength()]; + strcpy((char*)xURI, o2.getStr()); + registerListener(aElement); + } + + void CElementList::registerListener(const CElement* pElement) + { + try { + // get the XNode + Reference< XNode > xNode(CNode::get(static_cast<const CNode*>(pElement)->m_aNodePtr)); + Reference< XEventTarget > xTarget(xNode, UNO_QUERY_THROW); + OUString aType = OUString::createFromAscii("DOMSubtreeModified"); + sal_Bool capture = sal_False; + xTarget->addEventListener(aType, Reference< XEventListener >(this), capture); + } catch (Exception &e){ + OString aMsg("Exception caught while registering NodeList as listener:\n"); + aMsg += OUStringToOString(e.Message, RTL_TEXTENCODING_ASCII_US); + OSL_ENSURE(sal_False, aMsg.getStr()); + } + } + + void CElementList::buildlist(xmlNodePtr pNode, sal_Bool start) + { + // bail out if no rebuild is needed + if (start) { + if (!m_bRebuild) + { + return; + } else { + m_nodevector.erase(m_nodevector.begin(), m_nodevector.end()); + m_bRebuild = sal_False; // don't rebuild until tree is mutated + } + } + + while (pNode != NULL ) + { + if (pNode->type == XML_ELEMENT_NODE && strcmp((char*)pNode->name, (char*)xName)==0) + { + if (xURI == NULL) + m_nodevector.push_back(pNode); + else + if (pNode->ns != NULL && strcmp((char*)pNode->ns->href, (char*)xURI) == 0) + m_nodevector.push_back(pNode); + } + if (pNode->children != NULL) buildlist(pNode->children, sal_False); + + if (!start) pNode = pNode->next; + else break; // fold back + } + } + + /** + The number of nodes in the list. + */ + sal_Int32 SAL_CALL CElementList::getLength() throw (RuntimeException) + { + // this has to be 'live' + buildlist(static_cast<const CNode*>(m_pElement)->m_aNodePtr); + return m_nodevector.size(); + } + /** + Returns the indexth item in the collection. + */ + Reference< XNode > SAL_CALL CElementList::item(sal_Int32 index) throw (RuntimeException) + { + if (index < 0) throw RuntimeException(); + buildlist(static_cast<const CNode*>(m_pElement)->m_aNodePtr); + return Reference< XNode >(CNode::get(m_nodevector[index])); + } + + // tree mutations can change the list + void SAL_CALL CElementList::handleEvent(const Reference< XEvent >& evt) throw (RuntimeException) + { + Reference< XEvent > aEvent = evt; + m_bRebuild = sal_True; + } +} diff --git a/unoxml/source/dom/elementlist.hxx b/unoxml/source/dom/elementlist.hxx new file mode 100644 index 000000000000..2c3976063d85 --- /dev/null +++ b/unoxml/source/dom/elementlist.hxx @@ -0,0 +1,89 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2008 by Sun Microsystems, Inc. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: elementlist.hxx,v $ + * $Revision: 1.7 $ + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#ifndef _ELEMENTLIST_HXX +#define _ELEMENTLIST_HXX + +#include <vector> +#include <sal/types.h> +#include <cppuhelper/implbase1.hxx> +#include <cppuhelper/implbase2.hxx> +#include <com/sun/star/uno/Reference.h> +#include <com/sun/star/uno/Exception.hpp> +#include <com/sun/star/xml/dom/XNode.hpp> +#include <com/sun/star/xml/dom/XNodeList.hpp> +#include <com/sun/star/xml/dom/events/XEvent.hpp> +#include <com/sun/star/xml/dom/events/XEventListener.hpp> +#include "element.hxx" +#include "document.hxx" +#include "libxml/tree.h" + +using ::rtl::OUString; +using namespace com::sun::star::uno; +using namespace com::sun::star::xml::dom; +using namespace com::sun::star::xml::dom::events; + +namespace DOM +{ + typedef std::vector< xmlNodePtr > nodevector; + + class CElementList : public cppu::WeakImplHelper2< XNodeList, com::sun::star::xml::dom::events::XEventListener > + { + private: + const CElement* m_pElement; + const OUString m_aName; + const OUString m_aURI; + xmlChar *xName; + xmlChar *xURI; + sal_Bool m_bRebuild; + nodevector m_nodevector; + + + void buildlist(xmlNodePtr pNode, sal_Bool start=sal_True); + void registerListener(const CElement* pElement); + + public: + CElementList(const CElement* aDoc, const OUString& aName); + CElementList(const CElement* aDoc, const OUString& aName, const OUString& aURI); + /** + The number of nodes in the list. + */ + virtual sal_Int32 SAL_CALL getLength() throw (RuntimeException); + /** + Returns the indexth item in the collection. + */ + virtual Reference< XNode > SAL_CALL item(sal_Int32 index) throw (RuntimeException); + + // XEventListener + virtual void SAL_CALL handleEvent(const Reference< XEvent >& evt) throw (RuntimeException); + }; +} + +#endif diff --git a/unoxml/source/dom/entitiesmap.cxx b/unoxml/source/dom/entitiesmap.cxx new file mode 100644 index 000000000000..a01d651d6e2d --- /dev/null +++ b/unoxml/source/dom/entitiesmap.cxx @@ -0,0 +1,103 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2008 by Sun Microsystems, Inc. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: entitiesmap.cxx,v $ + * $Revision: 1.5 $ + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#include "entitiesmap.hxx" + +namespace DOM +{ + CEntitiesMap::CEntitiesMap(const CDocumentType* aDocType) + : m_pDocType(aDocType) + { + } + + /** + The number of nodes in this map. + */ + sal_Int32 SAL_CALL CEntitiesMap::getLength() throw (RuntimeException) + { + return 0; + } + + /** + Retrieves a node specified by local name + */ + Reference< XNode > SAL_CALL CEntitiesMap::getNamedItem(const OUString& /*name*/) throw (RuntimeException) + { + return Reference< XNode >(); + } + + /** + Retrieves a node specified by local name and namespace URI. + */ + Reference< XNode > SAL_CALL CEntitiesMap::getNamedItemNS(const OUString& /*namespaceURI*/,const OUString& /*localName*/) throw (RuntimeException) + { + return Reference< XNode >(); + } + + /** + Returns the indexth item in the map. + */ + Reference< XNode > SAL_CALL CEntitiesMap::item(sal_Int32 /*index*/) throw (RuntimeException) + { + return Reference< XNode >(); + } + + /** + Removes a node specified by name. + */ + Reference< XNode > SAL_CALL CEntitiesMap::removeNamedItem(const OUString& /*name*/) throw (RuntimeException) + { + return Reference< XNode >(); + } + + /** + // Removes a node specified by local name and namespace URI. + */ + Reference< XNode > SAL_CALL CEntitiesMap::removeNamedItemNS(const OUString& /*namespaceURI*/, const OUString& /*localName*/) throw (RuntimeException) + { + return Reference< XNode >(); + } + + /** + // Adds a node using its nodeName attribute. + */ + Reference< XNode > SAL_CALL CEntitiesMap::setNamedItem(const Reference< XNode >& /*arg*/) throw (RuntimeException) + { + return Reference< XNode >(); + } + + /** + Adds a node using its namespaceURI and localName. + */ + Reference< XNode > SAL_CALL CEntitiesMap::setNamedItemNS(const Reference< XNode >& /*arg*/) throw (RuntimeException) + { + return Reference< XNode >(); + } +} diff --git a/unoxml/source/dom/entitiesmap.hxx b/unoxml/source/dom/entitiesmap.hxx new file mode 100644 index 000000000000..002721b926a6 --- /dev/null +++ b/unoxml/source/dom/entitiesmap.hxx @@ -0,0 +1,100 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2008 by Sun Microsystems, Inc. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: entitiesmap.hxx,v $ + * $Revision: 1.4 $ + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#ifndef _ENTITIESMAP_HXX +#define _ENTITIESMAP_HXX + +#include <map> +#include <sal/types.h> +#include <cppuhelper/implbase1.hxx> +#include <com/sun/star/uno/Reference.h> +#include <com/sun/star/uno/Exception.hpp> +#include <com/sun/star/xml/dom/XNode.hpp> +#include <com/sun/star/xml/dom/XNamedNodeMap.hpp> +#include "document.hxx" +#include "documenttype.hxx" + +using ::rtl::OUString; +using namespace com::sun::star::uno; +using namespace com::sun::star::lang; +using namespace com::sun::star::xml::dom; + +namespace DOM +{ + class CEntitiesMap : public cppu::WeakImplHelper1< XNamedNodeMap > + { + private: + const CDocumentType* m_pDocType; + public: + CEntitiesMap(const CDocumentType* aDocType); + + /** + The number of nodes in this map. + */ + virtual sal_Int32 SAL_CALL getLength() throw (RuntimeException); + + /** + Retrieves a node specified by local name + */ + virtual Reference< XNode > SAL_CALL getNamedItem(const OUString& name) throw (RuntimeException); + + /** + Retrieves a node specified by local name and namespace URI. + */ + virtual Reference< XNode > SAL_CALL getNamedItemNS(const OUString& namespaceURI, const OUString& localName) throw (RuntimeException); + + /** + Returns the indexth item in the map. + */ + virtual Reference< XNode > SAL_CALL item(sal_Int32 index) throw (RuntimeException); + + /** + Removes a node specified by name. + */ + virtual Reference< XNode > SAL_CALL removeNamedItem(const OUString& name) throw (RuntimeException); + + /** + // Removes a node specified by local name and namespace URI. + */ + virtual Reference< XNode > SAL_CALL removeNamedItemNS(const OUString& namespaceURI, const OUString& localName) throw (RuntimeException); + + /** + // Adds a node using its nodeName attribute. + */ + virtual Reference< XNode > SAL_CALL setNamedItem(const Reference< XNode >& arg) throw (RuntimeException); + + /** + Adds a node using its namespaceURI and localName. + */ + virtual Reference< XNode > SAL_CALL setNamedItemNS(const Reference< XNode >& arg) throw (RuntimeException); + }; +} + +#endif diff --git a/unoxml/source/dom/entity.cxx b/unoxml/source/dom/entity.cxx new file mode 100644 index 000000000000..c56d0caba36c --- /dev/null +++ b/unoxml/source/dom/entity.cxx @@ -0,0 +1,93 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2008 by Sun Microsystems, Inc. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: entity.cxx,v $ + * $Revision: 1.6 $ + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#include "entity.hxx" + +#include <string.h> + +namespace DOM +{ + + CEntity::CEntity(const xmlEntityPtr aEntityPtr) + { + m_aNodeType = NodeType_ENTITY_NODE; + m_aEntityPtr = aEntityPtr; + init_node((xmlNodePtr)aEntityPtr); + } + + /** + For unparsed entities, the name of the notation for the entity. + */ + OUString SAL_CALL CEntity::getNotationName() throw (RuntimeException) + { + // XXX + return OUString(); + } + + /** + The public identifier associated with the entity, if specified. + */ + OUString SAL_CALL CEntity::getPublicId() throw (RuntimeException) + { + OUString aID; + if(m_aEntityPtr != NULL) + { + aID = OUString((sal_Char*)m_aEntityPtr->ExternalID, strlen((char*)m_aEntityPtr->ExternalID), RTL_TEXTENCODING_UTF8); + } + return aID; + } + + /** + The system identifier associated with the entity, if specified. + */ + OUString SAL_CALL CEntity::getSystemId() throw (RuntimeException) + { + OUString aID; + if(m_aEntityPtr != NULL) + { + aID = OUString((sal_Char*)m_aEntityPtr->SystemID, strlen((char*)m_aEntityPtr->SystemID), RTL_TEXTENCODING_UTF8); + } + return aID; + } + OUString SAL_CALL CEntity::getNodeName()throw (RuntimeException) + { + OUString aName; + if (m_aNodePtr != NULL) + { + const xmlChar* xName = m_aNodePtr->name; + aName = OUString((sal_Char*)xName, strlen((char*)xName), RTL_TEXTENCODING_UTF8); + } + return aName; + } + OUString SAL_CALL CEntity::getNodeValue() throw (RuntimeException) + { + return OUString(); + } +} diff --git a/unoxml/source/dom/entity.hxx b/unoxml/source/dom/entity.hxx new file mode 100644 index 000000000000..ffbdb770fa00 --- /dev/null +++ b/unoxml/source/dom/entity.hxx @@ -0,0 +1,203 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2008 by Sun Microsystems, Inc. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: entity.hxx,v $ + * $Revision: 1.8 $ + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#ifndef _ENTITY_HXX +#define _ENTITY_HXX + +#include <sal/types.h> +#include <com/sun/star/uno/Reference.h> +#include <com/sun/star/uno/Exception.hpp> +#include <com/sun/star/xml/dom/XEntity.hpp> +#include "node.hxx" +#include <libxml/tree.h> +#include <libxml/entities.h> + +using ::rtl::OUString; +using namespace com::sun::star::uno; +using namespace com::sun::star::xml::dom; + +namespace DOM +{ + class CEntity : public cppu::ImplInheritanceHelper1< CNode, XEntity > + { + friend class CNode; + private: + xmlEntityPtr m_aEntityPtr; + + protected: + CEntity(const xmlEntityPtr aEntityPtr); + + public: + + /** + For unparsed entities, the name of the notation for the entity. + */ + virtual OUString SAL_CALL getNotationName() throw (RuntimeException); + + /** + The public identifier associated with the entity, if specified. + */ + virtual OUString SAL_CALL getPublicId() throw (RuntimeException); + + /** + The system identifier associated with the entity, if specified. + */ + virtual OUString SAL_CALL getSystemId() throw (RuntimeException); + + // ---- resolve uno inheritance problems... + // overrides for XNode base + virtual OUString SAL_CALL getNodeName() + throw (RuntimeException); + virtual OUString SAL_CALL getNodeValue() + throw (RuntimeException); + // --- delegation for XNde base. + virtual Reference< XNode > SAL_CALL appendChild(const Reference< XNode >& newChild) + throw (RuntimeException, DOMException) + { + return CNode::appendChild(newChild); + } + virtual Reference< XNode > SAL_CALL cloneNode(sal_Bool deep) + throw (RuntimeException) + { + return CNode::cloneNode(deep); + } + virtual Reference< XNamedNodeMap > SAL_CALL getAttributes() + throw (RuntimeException) + { + return CNode::getAttributes(); + } + virtual Reference< XNodeList > SAL_CALL getChildNodes() + throw (RuntimeException) + { + return CNode::getChildNodes(); + } + virtual Reference< XNode > SAL_CALL getFirstChild() + throw (RuntimeException) + { + return CNode::getFirstChild(); + } + virtual Reference< XNode > SAL_CALL getLastChild() + throw (RuntimeException) + { + return CNode::getLastChild(); + } + virtual OUString SAL_CALL getLocalName() + throw (RuntimeException) + { + return CNode::getLocalName(); + } + virtual OUString SAL_CALL getNamespaceURI() + throw (RuntimeException) + { + return CNode::getNamespaceURI(); + } + virtual Reference< XNode > SAL_CALL getNextSibling() + throw (RuntimeException) + { + return CNode::getNextSibling(); + } + virtual NodeType SAL_CALL getNodeType() + throw (RuntimeException) + { + return CNode::getNodeType(); + } + virtual Reference< XDocument > SAL_CALL getOwnerDocument() + throw (RuntimeException) + { + return CNode::getOwnerDocument(); + } + virtual Reference< XNode > SAL_CALL getParentNode() + throw (RuntimeException) + { + return CNode::getParentNode(); + } + virtual OUString SAL_CALL getPrefix() + throw (RuntimeException) + { + return CNode::getPrefix(); + } + virtual Reference< XNode > SAL_CALL getPreviousSibling() + throw (RuntimeException) + { + return CNode::getPreviousSibling(); + } + virtual sal_Bool SAL_CALL hasAttributes() + throw (RuntimeException) + { + return CNode::hasAttributes(); + } + virtual sal_Bool SAL_CALL hasChildNodes() + throw (RuntimeException) + { + return CNode::hasChildNodes(); + } + virtual Reference< XNode > SAL_CALL insertBefore( + const Reference< XNode >& newChild, const Reference< XNode >& refChild) + throw (RuntimeException, DOMException) + { + return CNode::insertBefore(newChild, refChild); + } + virtual sal_Bool SAL_CALL isSupported(const OUString& feature, const OUString& ver) + throw (RuntimeException) + { + return CNode::isSupported(feature, ver); + } + virtual void SAL_CALL normalize() + throw (RuntimeException) + { + CNode::normalize(); + } + virtual Reference< XNode > SAL_CALL removeChild(const Reference< XNode >& oldChild) + throw (RuntimeException, DOMException) + { + return CNode::removeChild(oldChild); + } + virtual Reference< XNode > SAL_CALL replaceChild( + const Reference< XNode >& newChild, const Reference< XNode >& oldChild) + throw (RuntimeException, DOMException) + { + return CNode::replaceChild(newChild, oldChild); + } + virtual void SAL_CALL setNodeValue(const OUString& nodeValue) + throw (RuntimeException, DOMException) + { + return CNode::setNodeValue(nodeValue); + } + virtual void SAL_CALL setPrefix(const OUString& prefix) + throw (RuntimeException, DOMException) + { + return CNode::setPrefix(prefix); + } + + + }; +} + +#endif diff --git a/unoxml/source/dom/entityreference.cxx b/unoxml/source/dom/entityreference.cxx new file mode 100644 index 000000000000..56da1c2af8e1 --- /dev/null +++ b/unoxml/source/dom/entityreference.cxx @@ -0,0 +1,55 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2008 by Sun Microsystems, Inc. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: entityreference.cxx,v $ + * $Revision: 1.7 $ + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#include "entityreference.hxx" +#include <string.h> + +namespace DOM +{ + CEntityReference::CEntityReference(const xmlNodePtr aNodePtr) + { + m_aNodeType = NodeType_ENTITY_REFERENCE_NODE; + init_node(aNodePtr); + } + OUString SAL_CALL CEntityReference::getNodeName()throw (RuntimeException) + { + OUString aName; + if (m_aNodePtr != NULL) + { + const xmlChar* xName = m_aNodePtr->name; + aName = OUString((sal_Char*)xName, strlen((char*)xName), RTL_TEXTENCODING_UTF8); + } + return aName; + } + OUString SAL_CALL CEntityReference::getNodeValue() throw (RuntimeException) + { + return OUString(); + } +} diff --git a/unoxml/source/dom/entityreference.hxx b/unoxml/source/dom/entityreference.hxx new file mode 100644 index 000000000000..834476fe88c9 --- /dev/null +++ b/unoxml/source/dom/entityreference.hxx @@ -0,0 +1,179 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2008 by Sun Microsystems, Inc. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: entityreference.hxx,v $ + * $Revision: 1.7 $ + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#ifndef _ENTITYREFERENCE_HXX +#define _ENTITYREFERENCE_HXX +#include <com/sun/star/uno/Reference.h> +#include <com/sun/star/uno/Exception.hpp> +#include <com/sun/star/xml/dom/XEntityReference.hpp> +#include "node.hxx" +#include <libxml/tree.h> + +using ::rtl::OUString; +using namespace com::sun::star::uno; +using namespace com::sun::star::xml::dom; + +namespace DOM +{ + class CEntityReference : public cppu::ImplInheritanceHelper1< CNode, XEntityReference > + { + friend class CNode; + protected: + CEntityReference(const xmlNodePtr aNodePtr); + + public: + // ---- resolve uno inheritance problems... + // overrides for XNode base + virtual OUString SAL_CALL getNodeName() + throw (RuntimeException); + virtual OUString SAL_CALL getNodeValue() + throw (RuntimeException); + // --- delegation for XNde base. + virtual Reference< XNode > SAL_CALL appendChild(const Reference< XNode >& newChild) + throw (RuntimeException, DOMException) + { + return CNode::appendChild(newChild); + } + virtual Reference< XNode > SAL_CALL cloneNode(sal_Bool deep) + throw (RuntimeException) + { + return CNode::cloneNode(deep); + } + virtual Reference< XNamedNodeMap > SAL_CALL getAttributes() + throw (RuntimeException) + { + return CNode::getAttributes(); + } + virtual Reference< XNodeList > SAL_CALL getChildNodes() + throw (RuntimeException) + { + return CNode::getChildNodes(); + } + virtual Reference< XNode > SAL_CALL getFirstChild() + throw (RuntimeException) + { + return CNode::getFirstChild(); + } + virtual Reference< XNode > SAL_CALL getLastChild() + throw (RuntimeException) + { + return CNode::getLastChild(); + } + virtual OUString SAL_CALL getLocalName() + throw (RuntimeException) + { + return CNode::getLocalName(); + } + virtual OUString SAL_CALL getNamespaceURI() + throw (RuntimeException) + { + return CNode::getNamespaceURI(); + } + virtual Reference< XNode > SAL_CALL getNextSibling() + throw (RuntimeException) + { + return CNode::getNextSibling(); + } + virtual NodeType SAL_CALL getNodeType() + throw (RuntimeException) + { + return CNode::getNodeType(); + } + virtual Reference< XDocument > SAL_CALL getOwnerDocument() + throw (RuntimeException) + { + return CNode::getOwnerDocument(); + } + virtual Reference< XNode > SAL_CALL getParentNode() + throw (RuntimeException) + { + return CNode::getParentNode(); + } + virtual OUString SAL_CALL getPrefix() + throw (RuntimeException) + { + return CNode::getPrefix(); + } + virtual Reference< XNode > SAL_CALL getPreviousSibling() + throw (RuntimeException) + { + return CNode::getPreviousSibling(); + } + virtual sal_Bool SAL_CALL hasAttributes() + throw (RuntimeException) + { + return CNode::hasAttributes(); + } + virtual sal_Bool SAL_CALL hasChildNodes() + throw (RuntimeException) + { + return CNode::hasChildNodes(); + } + virtual Reference< XNode > SAL_CALL insertBefore( + const Reference< XNode >& newChild, const Reference< XNode >& refChild) + throw (RuntimeException, DOMException) + { + return CNode::insertBefore(newChild, refChild); + } + virtual sal_Bool SAL_CALL isSupported(const OUString& feature, const OUString& ver) + throw (RuntimeException) + { + return CNode::isSupported(feature, ver); + } + virtual void SAL_CALL normalize() + throw (RuntimeException) + { + CNode::normalize(); + } + virtual Reference< XNode > SAL_CALL removeChild(const Reference< XNode >& oldChild) + throw (RuntimeException, DOMException) + { + return CNode::removeChild(oldChild); + } + virtual Reference< XNode > SAL_CALL replaceChild( + const Reference< XNode >& newChild, const Reference< XNode >& oldChild) + throw (RuntimeException, DOMException) + { + return CNode::replaceChild(newChild, oldChild); + } + virtual void SAL_CALL setNodeValue(const OUString& nodeValue) + throw (RuntimeException, DOMException) + { + return CNode::setNodeValue(nodeValue); + } + virtual void SAL_CALL setPrefix(const OUString& prefix) + throw (RuntimeException, DOMException) + { + return CNode::setPrefix(prefix); + } + + }; +} +#endif diff --git a/unoxml/source/dom/makefile.mk b/unoxml/source/dom/makefile.mk new file mode 100644 index 000000000000..227259ea965c --- /dev/null +++ b/unoxml/source/dom/makefile.mk @@ -0,0 +1,78 @@ +#************************************************************************* +# +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. +# +# Copyright 2008 by Sun Microsystems, Inc. +# +# OpenOffice.org - a multi-platform office productivity suite +# +# $RCSfile: makefile.mk,v $ +# +# $Revision: 1.6 $ +# +# This file is part of OpenOffice.org. +# +# OpenOffice.org is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License version 3 +# only, as published by the Free Software Foundation. +# +# OpenOffice.org is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License version 3 for more details +# (a copy is included in the LICENSE file that accompanied this code). +# +# You should have received a copy of the GNU Lesser General Public License +# version 3 along with OpenOffice.org. If not, see +# <http://www.openoffice.org/license.html> +# for a copy of the LGPLv3 License. +# +#************************************************************************* + +PRJ=..$/.. + +PRJNAME=unoxml +TARGET=domimpl + +ENABLE_EXCEPTIONS=TRUE + +# --- Settings ----------------------------------------------------- + +.INCLUDE : settings.mk + +.IF "$(SYSTEM_LIBXML)" == "YES" +CFLAGS+=-DSYSTEM_LIBXML $(LIBXML_CFLAGS) +.ENDIF + +# --- Files -------------------------------------------------------- + +SLOFILES = \ + $(SLO)$/attr.obj \ + $(SLO)$/cdatasection.obj \ + $(SLO)$/characterdata.obj \ + $(SLO)$/comment.obj \ + $(SLO)$/document.obj \ + $(SLO)$/documentbuilder.obj \ + $(SLO)$/documentfragment.obj \ + $(SLO)$/documenttype.obj \ + $(SLO)$/element.obj \ + $(SLO)$/entity.obj \ + $(SLO)$/entityreference.obj \ + $(SLO)$/node.obj \ + $(SLO)$/notation.obj \ + $(SLO)$/processinginstruction.obj \ + $(SLO)$/text.obj \ + $(SLO)$/domimplementation.obj \ + $(SLO)$/elementlist.obj \ + $(SLO)$/childlist.obj \ + $(SLO)$/notationsmap.obj \ + $(SLO)$/entitiesmap.obj \ + $(SLO)$/attributesmap.obj \ + $(SLO)$/saxbuilder.obj + + +# --- Targets ------------------------------------------------------ + +.INCLUDE : target.mk + + diff --git a/unoxml/source/dom/node.cxx b/unoxml/source/dom/node.cxx new file mode 100644 index 000000000000..b4841508f9d9 --- /dev/null +++ b/unoxml/source/dom/node.cxx @@ -0,0 +1,1011 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2008 by Sun Microsystems, Inc. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: node.cxx,v $ + * $Revision: 1.17 $ + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#include <stdio.h> +#include <string.h> +#include "node.hxx" +#include "element.hxx" +#include "text.hxx" +#include "cdatasection.hxx" +#include "entityreference.hxx" +#include "entity.hxx" +#include "processinginstruction.hxx" +#include "comment.hxx" +#include "document.hxx" +#include "documenttype.hxx" +#include "documentfragment.hxx" +#include "notation.hxx" +#include "childlist.hxx" +#include "attr.hxx" + +#include <com/sun/star/xml/sax/FastToken.hpp> + +#include "../events/eventdispatcher.hxx" +#include "../events/mutationevent.hxx" + +#include <boost/bind.hpp> +#include <algorithm> + +namespace DOM +{ + void pushContext(Context& io_rContext) + { + io_rContext.maNamespaces.push_back( + io_rContext.maNamespaces.back()); + } + + void popContext(Context& io_rContext) + { + io_rContext.maNamespaces.pop_back(); + } + + void addNamespaces(Context& io_rContext, xmlNodePtr pNode) + { + // add node's namespaces to current context + for (xmlNsPtr pNs = pNode->nsDef; pNs != 0; pNs = pNs->next) { + const xmlChar *pPrefix = pNs->prefix; + OString prefix(reinterpret_cast<const sal_Char*>(pPrefix), + strlen(reinterpret_cast<const char*>(pPrefix))); + const xmlChar *pHref = pNs->href; + OUString val(reinterpret_cast<const sal_Char*>(pHref), + strlen(reinterpret_cast<const char*>(pHref)), + RTL_TEXTENCODING_UTF8); + + OSL_TRACE("Trying to add namespace %s (prefix %s)", + (const char*)pHref, (const char*)pPrefix); + + Context::NamespaceMapType::iterator aIter= + io_rContext.maNamespaceMap.find(val); + if( aIter != io_rContext.maNamespaceMap.end() ) + { + Context::Namespace aNS; + aNS.maPrefix = prefix; + aNS.mnToken = aIter->second; + aNS.maNamespaceURL = val; + + io_rContext.maNamespaces.back().push_back(aNS); + + OSL_TRACE("Added with token 0x%x", aIter->second); + } + } + } + + sal_Int32 getToken( const Context& rContext, const sal_Char* pToken ) + { + const Sequence<sal_Int8> aSeq( (sal_Int8*)pToken, strlen( pToken ) ); + return rContext.mxTokenHandler->getTokenFromUTF8( aSeq ); + } + + sal_Int32 getTokenWithPrefix( const Context& rContext, const sal_Char* pPrefix, const sal_Char* pName ) + { + sal_Int32 nNamespaceToken = FastToken::DONTKNOW; + OString prefix(pPrefix, + strlen(reinterpret_cast<const char*>(pPrefix))); + + OSL_TRACE("getTokenWithPrefix(): prefix %s, name %s", + (const char*)pPrefix, (const char*)pName); + + Context::NamespaceVectorType::value_type::const_iterator aIter; + if( (aIter=std::find_if(rContext.maNamespaces.back().begin(), + rContext.maNamespaces.back().end(), + boost::bind(std::equal_to<OString>(), + boost::bind(&Context::Namespace::getPrefix, + _1), + boost::cref(prefix)))) != rContext.maNamespaces.back().end() ) + { + nNamespaceToken = aIter->mnToken; + sal_Int32 nNameToken = getToken( rContext, pName ); + if( nNameToken != FastToken::DONTKNOW ) + nNamespaceToken |= nNameToken; + } + + return nNamespaceToken; + } + + + nodemap_t CNode::theNodeMap; + + void CNode::remove(const xmlNodePtr aNode) + { + nodemap_t::iterator i = CNode::theNodeMap.find(aNode); + if (i != CNode::theNodeMap.end()) + { + // CNode *pNode = i->second; + CNode::theNodeMap.erase(i); + } + } + + + CNode* CNode::get(const xmlNodePtr aNode, sal_Bool bCreate) + { + CNode* pNode = 0; + if (aNode == NULL) + return 0; + + //check whether there is already an instance for this node + nodemap_t::const_iterator i = CNode::theNodeMap.find(aNode); + if (i != CNode::theNodeMap.end()) + { + pNode = i->second; + } else + { + + // there is not yet an instance wrapping this node, + // create it and store it in the map + if (!bCreate) return NULL; + + switch (aNode->type) + { + case XML_ELEMENT_NODE: + // m_aNodeType = NodeType::ELEMENT_NODE; + pNode = static_cast< CNode* >(new CElement(aNode)); + break; + case XML_TEXT_NODE: + // m_aNodeType = NodeType::TEXT_NODE; + pNode = static_cast< CNode* >(new CText(aNode)); + break; + case XML_CDATA_SECTION_NODE: + // m_aNodeType = NodeType::CDATA_SECTION_NODE; + pNode = static_cast< CNode* >(new CCDATASection(aNode)); + break; + case XML_ENTITY_REF_NODE: + // m_aNodeType = NodeType::ENTITY_REFERENCE_NODE; + pNode = static_cast< CNode* >(new CEntityReference(aNode)); + break; + case XML_ENTITY_NODE: + // m_aNodeType = NodeType::ENTITY_NODE; + pNode = static_cast< CNode* >(new CEntity((xmlEntityPtr)aNode)); + break; + case XML_PI_NODE: + // m_aNodeType = NodeType::PROCESSING_INSTRUCTION_NODE; + pNode = static_cast< CNode* >(new CProcessingInstruction(aNode)); + break; + case XML_COMMENT_NODE: + // m_aNodeType = NodeType::COMMENT_NODE; + pNode = static_cast< CNode* >(new CComment(aNode)); + break; + case XML_DOCUMENT_NODE: + // m_aNodeType = NodeType::DOCUMENT_NODE; + pNode = static_cast< CNode* >(new CDocument((xmlDocPtr)aNode)); + break; + case XML_DOCUMENT_TYPE_NODE: + case XML_DTD_NODE: + // m_aNodeType = NodeType::DOCUMENT_TYPE_NODE; + pNode = static_cast< CNode* >(new CDocumentType((xmlDtdPtr)aNode)); + break; + case XML_DOCUMENT_FRAG_NODE: + // m_aNodeType = NodeType::DOCUMENT_FRAGMENT_NODE; + pNode = static_cast< CNode* >(new CDocumentFragment(aNode)); + break; + case XML_NOTATION_NODE: + // m_aNodeType = NodeType::NOTATION_NODE; + pNode = static_cast< CNode* >(new CNotation((xmlNotationPtr)aNode)); + break; + case XML_ATTRIBUTE_NODE: + // m_aNodeType = NodeType::NOTATION_NODE; + pNode = static_cast< CNode* >(new CAttr((xmlAttrPtr)aNode)); + break; + // unsopported node types + case XML_HTML_DOCUMENT_NODE: + case XML_ELEMENT_DECL: + case XML_ATTRIBUTE_DECL: + case XML_ENTITY_DECL: + case XML_NAMESPACE_DECL: + default: + pNode = 0; + break; + } + } + if ( pNode != 0 ) + { + if(CNode::theNodeMap.insert(nodemap_t::value_type(aNode, pNode)).second) + { + // insertion done, register node with document + xmlDocPtr doc = aNode->doc; + if( doc != NULL) + { + CDocument* pDoc = static_cast< CDocument* >(CNode::get((xmlNodePtr)doc)); + pDoc->addnode(aNode); + } else + { + // if insertion failed, delete the new instance and return null + delete pNode; + pNode = 0; + } + } + } + OSL_ENSURE(pNode, "no node produced during CNode::get!"); + return pNode; + } + + xmlNodePtr CNode::getNodePtr(const Reference< XNode >& aNode) + { + try { + CNode* pNode=dynamic_cast<CNode*>(aNode.get()); + if( pNode ) + return pNode->m_aNodePtr; + } + catch(...) {} + return 0; + } + + CNode::CNode() + : m_aNodePtr(0) + { + } + + void CNode::init_node(const xmlNodePtr aNode) + { + m_aNodePtr = aNode; + + // keep containing document alive + // (if we are not that document ourselves) + if (m_aNodePtr->type != XML_DOCUMENT_NODE) + m_rDocument = getOwnerDocument(); + } + + CNode::~CNode() + { + //remove from list if this wrapper goes away + if (m_aNodePtr != 0) + CNode::remove(m_aNodePtr); + } + + static void _nsexchange(const xmlNodePtr aNode, xmlNsPtr oldNs, xmlNsPtr newNs) + { + // recursively exchange any references to oldNs with references to newNs + xmlNodePtr cur = aNode; + while (cur != 0) + { + if (cur->ns == oldNs) + cur->ns = newNs; + if (cur->type == XML_ELEMENT_NODE) + { + xmlAttrPtr curAttr = cur->properties; + while(curAttr != 0) + { + if (curAttr->ns == oldNs) + curAttr->ns = newNs; + curAttr = curAttr->next; + } + _nsexchange(cur->children, oldNs, newNs); + } + cur = cur->next; + } + } + + /*static*/ void _nscleanup(const xmlNodePtr aNode, const xmlNodePtr aParent) + { + xmlNodePtr cur = aNode; + + //handle attributes + if (cur != NULL && cur->type == XML_ELEMENT_NODE) + { + xmlAttrPtr curAttr = cur->properties; + while(curAttr != 0) + { + if (curAttr->ns != NULL) + { + xmlNsPtr ns = xmlSearchNs(cur->doc, aParent, curAttr->ns->prefix); + if (ns != NULL) + curAttr->ns = ns; + } + curAttr = curAttr->next; + } + } + + while (cur != NULL) + { + _nscleanup(cur->children, cur); + if (cur->ns != NULL) + { + xmlNsPtr ns = xmlSearchNs(cur->doc, aParent, cur->ns->prefix); + if (ns != NULL && ns != cur->ns && strcmp((char*)ns->href, (char*)cur->ns->href)==0) + { + xmlNsPtr curDef = cur->nsDef; + xmlNsPtr *refp = &(cur->nsDef); // insert point + while (curDef != NULL) + { + ns = xmlSearchNs(cur->doc, aParent, curDef->prefix); + if (ns != NULL && ns != curDef && strcmp((char*)ns->href, (char*)curDef->href)==0) + { + // reconnect ns pointers in sub-tree to newly found ns before + // removing redundant nsdecl to prevent dangling pointers. + _nsexchange(cur, curDef, ns); + *refp = curDef->next; + xmlFreeNs(curDef); + curDef = *refp; + } else { + refp = &(curDef->next); + curDef = curDef->next; + } + } + } + } + cur = cur->next; + } + } + + void SAL_CALL CNode::saxify( + const Reference< XDocumentHandler >& i_xHandler) { + if (!i_xHandler.is()) throw RuntimeException(); + // default: do nothing + } + + void SAL_CALL CNode::fastSaxify(Context& io_rContext) { + if (!io_rContext.mxDocHandler.is()) throw RuntimeException(); + // default: do nothing + } + + /** + Adds the node newChild to the end of the list of children of this node. + */ + Reference< XNode > CNode::appendChild(const Reference< XNode >& newChild) + throw (RuntimeException, DOMException) + { + Reference< XNode> aNode; + if (m_aNodePtr != NULL) { + xmlNodePtr cur = CNode::getNodePtr(newChild.get()); + + // error checks: + // from other document + if (cur->doc != m_aNodePtr->doc) { + DOMException e; + e.Code = DOMExceptionType_WRONG_DOCUMENT_ERR; + throw e; + } + // same node + if (cur == m_aNodePtr) { + DOMException e; + e.Code = DOMExceptionType_HIERARCHY_REQUEST_ERR; + throw e; + } + // already has parant and is not attribute + if (cur->parent != NULL && cur->type != XML_ATTRIBUTE_NODE) { + DOMException e; + e.Code = DOMExceptionType_HIERARCHY_REQUEST_ERR; + throw e; + } + + // check whether this is an attribute node so we remove it's + // carrier node if it has one + xmlNodePtr res = NULL; + if (cur->type == XML_ATTRIBUTE_NODE) + { + if (cur->parent != NULL) + { + if (m_aNodePtr->type != XML_ELEMENT_NODE || + strcmp((char*)cur->parent->name, "__private") != 0) + { + DOMException e; + e.Code = DOMExceptionType_HIERARCHY_REQUEST_ERR; + throw e; + } + + xmlNsPtr pAttrNs = cur->ns; + xmlNsPtr pParentNs = xmlSearchNs(m_aNodePtr->doc, m_aNodePtr, pAttrNs->prefix); + if (pParentNs == NULL || strcmp((char*)pParentNs->href, (char*)pAttrNs->href) != 0) + pParentNs = xmlNewNs(m_aNodePtr, pAttrNs->href, pAttrNs->prefix); + + if (cur->children != NULL) + res = (xmlNodePtr)xmlNewNsProp(m_aNodePtr, pParentNs, cur->name, cur->children->content); + else + res = (xmlNodePtr)xmlNewProp(m_aNodePtr, cur->name, (xmlChar*) ""); + + xmlFreeNode(cur->parent); + cur->parent = NULL; + } + else + { + if (cur->children != NULL) + res = (xmlNodePtr)xmlNewProp(m_aNodePtr, cur->name, cur->children->content); + else + res = (xmlNodePtr)xmlNewProp(m_aNodePtr, cur->name, (xmlChar*) ""); + } + } + else + { + res = xmlAddChild(m_aNodePtr, cur); + } + + // libxml can do optimizations, when appending nodes. + // if res != cur, something was optimized and the newchild-wrapper + // should be updated + if (cur != res) + CNode::remove(cur); + + // use custom ns cleanup instaead of + // xmlReconciliateNs(m_aNodePtr->doc, m_aNodePtr); + // because that will not remove unneeded ns decls + _nscleanup(res, m_aNodePtr); + + aNode = Reference< XNode>(CNode::get(res)); + } + //XXX check for errors + + // dispatch DOMNodeInserted event, target is the new node + // this node is the related node + // does bubble + if (aNode.is()) + { + Reference< XDocumentEvent > docevent(getOwnerDocument(), UNO_QUERY); + Reference< XMutationEvent > event(docevent->createEvent( + OUString::createFromAscii("DOMNodeInserted")), UNO_QUERY); + event->initMutationEvent(OUString::createFromAscii("DOMNodeInserted") + , sal_True, sal_False, Reference< XNode >(CNode::get(m_aNodePtr)), + OUString(), OUString(), OUString(), (AttrChangeType)0 ); + dispatchEvent(Reference< XEvent >(event, UNO_QUERY)); + + // dispatch subtree modified for this node + dispatchSubtreeModified(); + } + return aNode; + } + + /** + Returns a duplicate of this node, i.e., serves as a generic copy + constructor for nodes. + */ + Reference< XNode > CNode::cloneNode(sal_Bool bDeep) + throw (RuntimeException) + { + Reference< XNode> aNode; + if (m_aNodePtr != NULL) + { + aNode = Reference< XNode>(CNode::get( + xmlCopyNode (m_aNodePtr, static_cast< int >(bDeep)) + )); + } + //XXX check for errors + return aNode; + } + + /** + A NamedNodeMap containing the attributes of this node (if it is an Element) + or null otherwise. + */ + Reference< XNamedNodeMap > CNode::getAttributes() + throw (RuntimeException) + { + // return empty reference + // only element node may override this impl + return Reference< XNamedNodeMap>(); + + // get all children that are attributes + /* --> CElement + Reference< NamedNodeMap > aNodeMap(new AttributeNamedNodeMap(m_aNodePtr), UNO_QUERY); + return aNodeMap; + */ + } + + /** + A NodeList that contains all children of this node. + */ + Reference< XNodeList > CNode::getChildNodes() + throw (RuntimeException) + { + Reference< XNodeList > aNodeList; + if (m_aNodePtr != NULL) + { + aNodeList = Reference< XNodeList >(new CChildList(CNode::get(m_aNodePtr))); + } + // XXX check for errors? + return aNodeList; + } + + /** + The first child of this node. + */ + Reference< XNode > CNode::getFirstChild() + throw (RuntimeException) + { + Reference< XNode > aNode; + if (m_aNodePtr != NULL) { + aNode = Reference< XNode >(CNode::get(m_aNodePtr->children)); + } + return aNode; + } + + /** + The last child of this node. + */ + Reference< XNode > SAL_CALL CNode::getLastChild() + throw (RuntimeException) + { + Reference< XNode > aNode; + if (m_aNodePtr != NULL) { + aNode = Reference< XNode >(CNode::get(xmlGetLastChild(m_aNodePtr))); + } + return aNode; + } + + /** + Returns the local part of the qualified name of this node. + */ + OUString SAL_CALL CNode::getLocalName() + throw (RuntimeException) + { + OUString aName; + /* + --> Element / Attribute + if(m_aNodePtr != NULL && (m_aNodeType == NodeType::ATTRIBUTE_NODE + || m_aNodeType == NodeType::ELEMENT_NODE)) + { + aName = OUString(m_aNodePtr->name, RTL_TEXTENCODING_UTF8); + } + //XXX error checking + */ + return aName; + } + + + /** + The namespace URI of this node, or null if it is unspecified. + */ + OUString SAL_CALL CNode::getNamespaceURI() + throw (RuntimeException) + { + OUString aURI; + if (m_aNodePtr != NULL && + (m_aNodePtr->type == XML_ELEMENT_NODE || m_aNodePtr->type == XML_ATTRIBUTE_NODE) && + m_aNodePtr->ns != NULL) + { + const xmlChar* xHref = m_aNodePtr->ns->href; + aURI = OUString((sal_Char*)xHref, strlen((char*)xHref), RTL_TEXTENCODING_UTF8); + } + return aURI; + } + + /** + The node immediately following this node. + */ + Reference< XNode > SAL_CALL CNode::getNextSibling() + throw (RuntimeException) + { + Reference< XNode > aNode; + if(m_aNodePtr != NULL) + { + aNode = Reference< XNode >(CNode::get(m_aNodePtr->next)); + } + return aNode; + } + + /** + The name of this node, depending on its type; see the table above. + */ + OUString SAL_CALL CNode::getNodeName() + throw (RuntimeException) + { + /* + Interface nodeName nodeValue attributes + -------------------------------------------------------------------------------------- + Attr name of attribute value of attribute null + CDATASection "#cdata-section" content of the CDATA Section null + Comment "#comment" content of the comment null + Document "#document" null null + DocumentFragment "#document-fragment" null null + DocumentType document type name null null + Element tag name null NamedNodeMap + Entity entity name null null + EntityReference name of entity null null + referenced + Notation notation name null null + Processing\ target entire content excluding null + Instruction the target + Text "#text" content of the text node null + */ + OUString aName; + return aName; + } + + /** + A code representing the type of the underlying object, as defined above. + */ + NodeType SAL_CALL CNode::getNodeType() + throw (RuntimeException) + { + return m_aNodeType; + } + + /** + The value of this node, depending on its type; see the table above. + */ + OUString SAL_CALL CNode::getNodeValue() + throw (RuntimeException) + { + OUString aValue; + return aValue; + } + + /** + The Document object associated with this node. + */ + Reference< XDocument > SAL_CALL CNode::getOwnerDocument() + throw (RuntimeException) + { + Reference<XDocument> aDoc; + if (m_aNodePtr != NULL) + { + aDoc = Reference< XDocument >(static_cast< CDocument* >( + CNode::get((xmlNodePtr)m_aNodePtr->doc))); + } + return aDoc; + + } + + /** + The parent of this node. + */ + Reference< XNode > SAL_CALL CNode::getParentNode() + throw (RuntimeException) + { + Reference<XNode> aNode; + if (m_aNodePtr != NULL) + { + aNode = Reference< XNode >(CNode::get(m_aNodePtr->parent)); + } + return aNode; + } + + /** + The namespace prefix of this node, or null if it is unspecified. + */ + OUString SAL_CALL CNode::getPrefix() + throw (RuntimeException) + { + OUString aPrefix; + if (m_aNodePtr != NULL && + (m_aNodePtr->type == XML_ELEMENT_NODE || m_aNodePtr->type == XML_ATTRIBUTE_NODE) && + m_aNodePtr->ns != NULL) + { + const xmlChar* xPrefix = m_aNodePtr->ns->prefix; + if( xPrefix != NULL ) + aPrefix = OUString((sal_Char*)xPrefix, strlen((char*)xPrefix), RTL_TEXTENCODING_UTF8); + } + return aPrefix; + + } + + /** + The node immediately preceding this node. + */ + Reference< XNode > SAL_CALL CNode::getPreviousSibling() + throw (RuntimeException) + { + Reference< XNode > aNode; + if (m_aNodePtr != NULL) + { + aNode = Reference< XNode >(CNode::get(m_aNodePtr->prev)); + } + return aNode; + } + + /** + Returns whether this node (if it is an element) has any attributes. + */ + sal_Bool SAL_CALL CNode::hasAttributes() + throw (RuntimeException) + { + return (m_aNodePtr != NULL && m_aNodePtr->properties != NULL); + } + + /** + Returns whether this node has any children. + */ + sal_Bool SAL_CALL CNode::hasChildNodes() + throw (RuntimeException) + { + return (m_aNodePtr != NULL && m_aNodePtr->children != NULL); + } + + /** + Inserts the node newChild before the existing child node refChild. + */ + Reference< XNode > SAL_CALL CNode::insertBefore( + const Reference< XNode >& newChild, const Reference< XNode >& refChild) + throw (RuntimeException, DOMException) + { + + if (newChild->getOwnerDocument() != getOwnerDocument()) { + DOMException e; + e.Code = DOMExceptionType_WRONG_DOCUMENT_ERR; + throw e; + } + if (refChild->getParentNode() != Reference< XNode >(this)) { + DOMException e; + e.Code = DOMExceptionType_HIERARCHY_REQUEST_ERR; + throw e; + } + + xmlNodePtr pRefChild = CNode::getNodePtr(refChild.get()); + xmlNodePtr pNewChild = CNode::getNodePtr(newChild.get()); + xmlNodePtr cur = m_aNodePtr->children; + + //search cild before which to insert + while (cur != NULL) + { + if (cur == pRefChild) { + // insert before + pNewChild->next = cur; + pNewChild->prev = cur->prev; + cur->prev = pNewChild; + if( pNewChild->prev != NULL) + pNewChild->prev->next = pNewChild; + } + cur = cur->next; + } + return refChild; + } + + /** + Tests whether the DOM implementation implements a specific feature and + that feature is supported by this node. + */ + sal_Bool SAL_CALL CNode::isSupported(const OUString& /*feature*/, const OUString& /*ver*/) + throw (RuntimeException) + { + // XXX + return sal_False; + } + + /** + Puts all Text nodes in the full depth of the sub-tree underneath this + Node, including attribute nodes, into a "normal" form where only structure + (e.g., elements, comments, processing instructions, CDATA sections, and + entity references) separates Text nodes, i.e., there are neither adjacent + Text nodes nor empty Text nodes. + */ + void SAL_CALL CNode::normalize() + throw (RuntimeException) + { + //XXX combine adjacent text nodes and remove empty ones + } + + /** + Removes the child node indicated by oldChild from the list of children, + and returns it. + */ + Reference< XNode > SAL_CALL CNode::removeChild(const Reference< XNode >& oldChild) + throw (RuntimeException, DOMException) + { + + if (oldChild->getParentNode() != Reference< XNode >(this)) { + DOMException e; + e.Code = DOMExceptionType_HIERARCHY_REQUEST_ERR; + throw e; + } + + Reference<XNode> xReturn( oldChild ); + + xmlNodePtr old = CNode::getNodePtr(oldChild); + + if( old->type == XML_ATTRIBUTE_NODE ) + { + xmlAttrPtr pAttr = (xmlAttrPtr) old; + xmlRemoveProp( pAttr ); + xReturn.clear(); + } + else + { + + // update .last + if (m_aNodePtr->last == old) + m_aNodePtr->last = old->prev; + + xmlNodePtr cur = m_aNodePtr->children; + //find old node in child list + while (cur != NULL) + { + if(cur == old) + { + // unlink node from list + if (cur->prev != NULL) + cur->prev->next = cur->next; + if (cur->next != NULL) + cur->next->prev = cur->prev; + if (cur->parent != NULL && cur->parent->children == cur) + cur->parent->children = cur->next; + cur->prev = NULL; + cur->next = NULL; + cur->parent = NULL; + } + cur = cur->next; + } + } + + /*DOMNodeRemoved + * Fired when a node is being removed from its parent node. + * This event is dispatched before the node is removed from the tree. + * The target of this event is the node being removed. + * Bubbles: Yes + * Cancelable: No + * Context Info: relatedNode holds the parent node + */ + if (oldChild.is()) + { + Reference< XDocumentEvent > docevent(getOwnerDocument(), UNO_QUERY); + Reference< XMutationEvent > event(docevent->createEvent( + OUString::createFromAscii("DOMNodeRemoved")), UNO_QUERY); + event->initMutationEvent(OUString::createFromAscii("DOMNodeRemoved"), sal_True, + sal_False, Reference< XNode >(CNode::get(m_aNodePtr)), + OUString(), OUString(), OUString(), (AttrChangeType)0 ); + dispatchEvent(Reference< XEvent >(event, UNO_QUERY)); + + // subtree modofied for this node + dispatchSubtreeModified(); + } + return xReturn; + } + + /** + Replaces the child node oldChild with newChild in the list of children, + and returns the oldChild node. + */ + Reference< XNode > SAL_CALL CNode::replaceChild( + const Reference< XNode >& newChild, const Reference< XNode >& oldChild) + throw (RuntimeException, DOMException) + { + // XXX check node types + + if (oldChild->getParentNode() != Reference< XNode >(this)) { + DOMException e; + e.Code = DOMExceptionType_HIERARCHY_REQUEST_ERR; + throw e; + } + +/* + Reference< XNode > aNode = removeChild(oldChild); + appendChild(newChild); +*/ + xmlNodePtr pOld = CNode::getNodePtr(oldChild); + xmlNodePtr pNew = CNode::getNodePtr(newChild); + + if( pOld->type == XML_ATTRIBUTE_NODE ) + { + // can only replace attribute with attribute + if ( pOld->type != pNew->type ) + { + DOMException e; + e.Code = DOMExceptionType_HIERARCHY_REQUEST_ERR; + throw e; + } + + xmlAttrPtr pAttr = (xmlAttrPtr)pOld; + xmlRemoveProp( pAttr ); + appendChild( newChild ); + } + else + { + + xmlNodePtr cur = m_aNodePtr->children; + //find old node in child list + while (cur != NULL) + { + if(cur == pOld) + { + // exchange nodes + pNew->prev = pOld->prev; + if (pNew->prev != NULL) + pNew->prev->next = pNew; + pNew->next = pOld->next; + if (pNew->next != NULL) + pNew->next->prev = pNew; + pNew->parent = pOld->parent; + if(pNew->parent->children == pOld) + pNew->parent->children = pNew; + if(pNew->parent->last == pOld) + pNew->parent->last = pNew; + pOld->next = NULL; + pOld->prev = NULL; + pOld->parent = NULL; + } + cur = cur->next; + } + } + + dispatchSubtreeModified(); + + return oldChild; + } + + void CNode::dispatchSubtreeModified() + { + // dispatch DOMSubtreeModified + // target is _this_ node + Reference< XDocumentEvent > docevent(getOwnerDocument(), UNO_QUERY); + Reference< XMutationEvent > event(docevent->createEvent( + OUString::createFromAscii("DOMSubtreeModified")), UNO_QUERY); + event->initMutationEvent(OUString::createFromAscii("DOMSubtreeModified"), sal_True, + sal_False, Reference< XNode >(), + OUString(), OUString(), OUString(), (AttrChangeType)0 ); + dispatchEvent(Reference< XEvent >(event, UNO_QUERY)); + } + + /** + The value of this node, depending on its type; see the table above. + */ + void SAL_CALL CNode::setNodeValue(const OUString& /*nodeValue*/) + throw (RuntimeException, DOMException) + { + // use specific node implememntation + // if we end up down here, something went wrong + DOMException e; + e.Code = DOMExceptionType_NO_MODIFICATION_ALLOWED_ERR; + throw e; + } + + /** + The namespace prefix of this node, or null if it is unspecified. + */ + void SAL_CALL CNode::setPrefix(const OUString& prefix) + throw (RuntimeException, DOMException) + { + OString o1 = OUStringToOString(prefix, RTL_TEXTENCODING_UTF8); + xmlChar *pBuf = (xmlChar*)o1.getStr(); + // XXX copy buf? + // XXX free old string? (leak?) + if (m_aNodePtr != NULL && m_aNodePtr->ns != NULL) + { + m_aNodePtr->ns->prefix = pBuf; + } + + } + + // --- XEventTarget + void SAL_CALL CNode::addEventListener(const OUString& eventType, + const Reference< com::sun::star::xml::dom::events::XEventListener >& listener, + sal_Bool useCapture) + throw (RuntimeException) + { + events::CEventDispatcher::addListener(m_aNodePtr, eventType, listener, useCapture); + } + + void SAL_CALL CNode::removeEventListener(const OUString& eventType, + const Reference< com::sun::star::xml::dom::events::XEventListener >& listener, + sal_Bool useCapture) + throw (RuntimeException) + { + events::CEventDispatcher::removeListener(m_aNodePtr, eventType, listener, useCapture); + } + + sal_Bool SAL_CALL CNode::dispatchEvent(const Reference< XEvent >& evt) + throw(RuntimeException, EventException) + { + events::CEventDispatcher::dispatchEvent(m_aNodePtr, evt); + return sal_True; + } + + ::sal_Int64 SAL_CALL CNode::getSomething(const Sequence< ::sal_Int8 >& /*aIdentifier*/) + throw (RuntimeException) + { + return sal::static_int_cast<sal_Int64>(reinterpret_cast<sal_IntPtr>(m_aNodePtr)); + } +} + diff --git a/unoxml/source/dom/node.hxx b/unoxml/source/dom/node.hxx new file mode 100644 index 000000000000..f70f2c064104 --- /dev/null +++ b/unoxml/source/dom/node.hxx @@ -0,0 +1,357 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2008 by Sun Microsystems, Inc. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: node.hxx,v $ + * $Revision: 1.13 $ + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#ifndef _NODE_HXX +#define _NODE_HXX + +#include <rtl/ref.hxx> +#include <rtl/string.hxx> +#include <rtl/ustring.hxx> +#include <sal/types.h> +#include <sax/fastattribs.hxx> +#include <cppuhelper/implbase1.hxx> +#include <cppuhelper/implbase3.hxx> +#include <com/sun/star/uno/Reference.h> +#include <com/sun/star/uno/Exception.hpp> +#include <com/sun/star/lang/XUnoTunnel.hpp> +#include <com/sun/star/xml/dom/XNode.hpp> +#include <com/sun/star/xml/dom/XNodeList.hpp> +#include <com/sun/star/xml/dom/XNamedNodeMap.hpp> +#include <com/sun/star/xml/dom/NodeType.hpp> +#include <com/sun/star/uno/Sequence.h> +#include <com/sun/star/xml/dom/events/XEventTarget.hpp> +#include <com/sun/star/xml/dom/events/XDocumentEvent.hpp> +#include <com/sun/star/xml/dom/events/XEvent.hpp> +#include <com/sun/star/xml/dom/events/XMutationEvent.hpp> +#include <com/sun/star/xml/dom/events/XUIEvent.hpp> +#include <com/sun/star/xml/dom/events/XMouseEvent.hpp> +#include <com/sun/star/xml/dom/DOMException.hpp> +#include <com/sun/star/xml/sax/XDocumentHandler.hpp> +#include <com/sun/star/xml/sax/XFastDocumentHandler.hpp> +#include <libxml/tree.h> + +#include <map> +#include <hash_map> + +using ::rtl::OUString; +using ::rtl::OString; +using namespace sax_fastparser; +using namespace com::sun::star::uno; +using namespace com::sun::star::xml::sax; +using namespace com::sun::star::xml::dom; +using namespace com::sun::star::xml::dom::events; + +using com::sun::star::lang::XUnoTunnel; + +namespace DOM +{ + struct Context + { + Context( const Reference< XFastDocumentHandler >& i_xHandler, + const Reference< XFastTokenHandler >& i_xTokenHandler ) : + maNamespaces( 1, std::vector<Namespace>() ), + maNamespaceMap(101), + mxAttribList(new FastAttributeList(i_xTokenHandler)), + mxCurrentHandler(i_xHandler, UNO_QUERY_THROW), + mxDocHandler(i_xHandler), + mxTokenHandler(i_xTokenHandler) + {} + + struct Namespace + { + OString maPrefix; + sal_Int32 mnToken; + OUString maNamespaceURL; + + const OString& getPrefix() const { return maPrefix; } + }; + + typedef std::vector< std::vector<Namespace> > NamespaceVectorType; + typedef std::hash_map< OUString, + sal_Int32, + rtl::OUStringHash > NamespaceMapType; + + /// outer vector: xml context; inner vector: current NS + NamespaceVectorType maNamespaces; + NamespaceMapType maNamespaceMap; + ::rtl::Reference<FastAttributeList> mxAttribList; + Reference<XFastContextHandler> mxCurrentHandler; + Reference<XFastDocumentHandler> mxDocHandler; + Reference<XFastTokenHandler> mxTokenHandler; + }; + + void pushContext(Context& io_rContext); + void popContext(Context& io_rContext); + + sal_Int32 getTokenWithPrefix( const Context& rContext, const sal_Char* xPrefix, const sal_Char* xName ); + sal_Int32 getToken( const Context& rContext, const sal_Char* xName ); + + /// add namespaces on this node to context + void addNamespaces(Context& io_rContext, xmlNodePtr pNode); + + class CNode; + typedef std::map< const xmlNodePtr, CNode* > nodemap_t; + + + class CNode : public cppu::WeakImplHelper3< XNode, XUnoTunnel, XEventTarget > + { + friend class CDocument; + friend class CElement; + friend class CAttributesMap; + friend class CChildList; + friend class CElementList; + friend class CEntitiesMap; + friend class CNotationsMap; + private: + static nodemap_t theNodeMap; + + protected: + NodeType m_aNodeType; + xmlNodePtr m_aNodePtr; + + Reference< XDocument > m_rDocument; + + // for initialization by classes derived through ImplInheritanceHelper + CNode(); + void init_node(const xmlNodePtr aNode); + + void dispatchSubtreeModified(); + + public: + + virtual ~CNode(); + + // get a representaion for a libxml node + static CNode* get(const xmlNodePtr aNode, sal_Bool bCreate = sal_True); + // remove a wrapper instance + static void remove(const xmlNodePtr aNode); + + // get the libxml node implementation + static xmlNodePtr getNodePtr(const Reference< XNode >& aNode); + + //static Sequence< sal_Int8 > + + // recursively create SAX events + virtual void SAL_CALL saxify( + const Reference< XDocumentHandler >& i_xHandler); + + // recursively create SAX events + virtual void SAL_CALL fastSaxify( Context& io_rContext ); + + // ---- DOM interfaces + + /** + Adds the node newChild to the end of the list of children of this node. + */ + virtual Reference< XNode > SAL_CALL appendChild(const Reference< XNode >& newChild) + throw (RuntimeException, DOMException); + + /** + Returns a duplicate of this node, i.e., serves as a generic copy + constructor for nodes. + */ + virtual Reference< XNode > SAL_CALL cloneNode(sal_Bool deep) + throw (RuntimeException); + + /** + A NamedNodeMap containing the attributes of this node (if it is an Element) + or null otherwise. + */ + virtual Reference< XNamedNodeMap > SAL_CALL getAttributes() + throw (RuntimeException); + + /** + A NodeList that contains all children of this node. + */ + virtual Reference< XNodeList > SAL_CALL getChildNodes() + throw (RuntimeException); + + /** + The first child of this node. + */ + virtual Reference< XNode > SAL_CALL getFirstChild() + throw (RuntimeException); + + /** + The last child of this node. + */ + virtual Reference< XNode > SAL_CALL getLastChild() + throw (RuntimeException); + + /** + Returns the local part of the qualified name of this node. + */ + virtual OUString SAL_CALL getLocalName() + throw (RuntimeException); + + /** + The namespace URI of this node, or null if it is unspecified. + */ + virtual OUString SAL_CALL getNamespaceURI() + throw (RuntimeException); + + /** + The node immediately following this node. + */ + virtual Reference< XNode > SAL_CALL getNextSibling() + throw (RuntimeException); + + /** + The name of this node, depending on its type; see the table above. + -- virtual implemented by actual node types + */ + virtual OUString SAL_CALL getNodeName() + throw (RuntimeException); + + /** + A code representing the type of the underlying object, as defined above. + */ + virtual NodeType SAL_CALL getNodeType() + throw (RuntimeException); + + /** + The value of this node, depending on its type; see the table above. + -- virtual implemented by actual node types + */ + virtual OUString SAL_CALL getNodeValue() + throw (RuntimeException); + + /** + The Document object associated with this node. + */ + virtual Reference< XDocument > SAL_CALL getOwnerDocument() + throw (RuntimeException); + + /** + The parent of this node. + */ + virtual Reference< XNode > SAL_CALL getParentNode() + throw (RuntimeException); + + /** + The namespace prefix of this node, or null if it is unspecified. + */ + virtual OUString SAL_CALL getPrefix() + throw (RuntimeException); + + /** + The node immediately preceding this node. + */ + virtual Reference< XNode > SAL_CALL getPreviousSibling() + throw (RuntimeException); + + /** + Returns whether this node (if it is an element) has any attributes. + */ + virtual sal_Bool SAL_CALL hasAttributes() + throw (RuntimeException); + + /** + Returns whether this node has any children. + */ + virtual sal_Bool SAL_CALL hasChildNodes() + throw (RuntimeException); + + /** + Inserts the node newChild before the existing child node refChild. + */ + virtual Reference< XNode > SAL_CALL insertBefore( + const Reference< XNode >& newChild, const Reference< XNode >& refChild) + throw (RuntimeException, DOMException); + + /** + Tests whether the DOM implementation implements a specific feature and + that feature is supported by this node. + */ + virtual sal_Bool SAL_CALL isSupported(const OUString& feature, const OUString& ver) + throw (RuntimeException); + + /** + Puts all Text nodes in the full depth of the sub-tree underneath this + Node, including attribute nodes, into a "normal" form where only structure + (e.g., elements, comments, processing instructions, CDATA sections, and + entity references) separates Text nodes, i.e., there are neither adjacent + Text nodes nor empty Text nodes. + */ + virtual void SAL_CALL normalize() + throw (RuntimeException); + + /** + Removes the child node indicated by oldChild from the list of children, + and returns it. + */ + virtual Reference< XNode > SAL_CALL removeChild(const Reference< XNode >& oldChild) + throw (RuntimeException, DOMException); + + /** + Replaces the child node oldChild with newChild in the list of children, + and returns the oldChild node. + */ + virtual Reference< XNode > SAL_CALL replaceChild( + const Reference< XNode >& newChild, const Reference< XNode >& oldChild) + throw (RuntimeException, DOMException); + + /** + The value of this node, depending on its type; see the table above. + */ + virtual void SAL_CALL setNodeValue(const OUString& nodeValue) + throw (RuntimeException, DOMException); + + /** + The namespace prefix of this node, or null if it is unspecified. + */ + virtual void SAL_CALL setPrefix(const OUString& prefix) + throw (RuntimeException, DOMException); + + + // --- XEventTarget + virtual void SAL_CALL addEventListener(const OUString& eventType, + const Reference< XEventListener >& listener, + sal_Bool useCapture) + throw (RuntimeException); + + virtual void SAL_CALL removeEventListener(const OUString& eventType, + const Reference< XEventListener >& listener, + sal_Bool useCapture) + throw (RuntimeException); + + virtual sal_Bool SAL_CALL dispatchEvent(const Reference< XEvent >& evt) + throw(RuntimeException, EventException); + + // --- XUnoTunnel + virtual ::sal_Int64 SAL_CALL getSomething(const Sequence< ::sal_Int8 >& aIdentifier) + throw (RuntimeException); + }; + + /// eliminate redundant namespace declarations + void _nscleanup(const xmlNodePtr aNode, const xmlNodePtr aParent); +} + +#endif diff --git a/unoxml/source/dom/notation.cxx b/unoxml/source/dom/notation.cxx new file mode 100644 index 000000000000..7f4acfad1de9 --- /dev/null +++ b/unoxml/source/dom/notation.cxx @@ -0,0 +1,73 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2008 by Sun Microsystems, Inc. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: notation.cxx,v $ + * $Revision: 1.5 $ + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#include "notation.hxx" +#include <string.h> + +namespace DOM +{ + CNotation::CNotation(const xmlNotationPtr aNotationPtr) + { + m_aNodeType = NodeType_NOTATION_NODE; + m_aNotationPtr = aNotationPtr; + init_node((xmlNodePtr)aNotationPtr); + } + + OUString SAL_CALL CNotation::getPublicId() throw (RuntimeException) + { + // XXX + return OUString(); + } + + /** + The system identifier of this notation. + */ + OUString SAL_CALL CNotation::getSystemId() throw (RuntimeException) + { + // XXX + return OUString(); + } + + + OUString SAL_CALL CNotation::getNodeName()throw (RuntimeException) + { + OUString aName; + if (m_aNodePtr != NULL) + { + const xmlChar* xName = m_aNodePtr->name; + aName = OUString((sal_Char*)xName, strlen((char*)xName), RTL_TEXTENCODING_UTF8); + } + return aName; + } + OUString SAL_CALL CNotation::getNodeValue() throw (RuntimeException) + { + return OUString(); + } +} diff --git a/unoxml/source/dom/notation.hxx b/unoxml/source/dom/notation.hxx new file mode 100644 index 000000000000..1dfa6bf22c72 --- /dev/null +++ b/unoxml/source/dom/notation.hxx @@ -0,0 +1,194 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2008 by Sun Microsystems, Inc. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: notation.hxx,v $ + * $Revision: 1.8 $ + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#ifndef _NOTATION_HXX +#define _NOTATION_HXX + +#include <com/sun/star/uno/Reference.h> +#include <com/sun/star/uno/Exception.hpp> +#include <com/sun/star/xml/dom/XNotation.hpp> +#include "node.hxx" +#include <libxml/tree.h> + +using ::rtl::OUString; +using namespace com::sun::star::uno; +using namespace com::sun::star::xml::dom; + +namespace DOM +{ + class CNotation : public cppu::ImplInheritanceHelper1< CNode, XNotation > + { + friend class CNode; + private: + xmlNotationPtr m_aNotationPtr; + + protected: + CNotation(const xmlNotationPtr); + + /** + The public identifier of this notation. + */ + virtual OUString SAL_CALL getPublicId() throw (RuntimeException); + + /** + The system identifier of this notation. + */ + virtual OUString SAL_CALL getSystemId() throw (RuntimeException); + + // ---- resolve uno inheritance problems... + // overrides for XNode base + virtual OUString SAL_CALL getNodeName() + throw (RuntimeException); + virtual OUString SAL_CALL getNodeValue() + throw (RuntimeException); + // --- delegation for XNde base. + virtual Reference< XNode > SAL_CALL appendChild(const Reference< XNode >& newChild) + throw (RuntimeException, DOMException) + { + return CNode::appendChild(newChild); + } + virtual Reference< XNode > SAL_CALL cloneNode(sal_Bool deep) + throw (RuntimeException) + { + return CNode::cloneNode(deep); + } + virtual Reference< XNamedNodeMap > SAL_CALL getAttributes() + throw (RuntimeException) + { + return CNode::getAttributes(); + } + virtual Reference< XNodeList > SAL_CALL getChildNodes() + throw (RuntimeException) + { + return CNode::getChildNodes(); + } + virtual Reference< XNode > SAL_CALL getFirstChild() + throw (RuntimeException) + { + return CNode::getFirstChild(); + } + virtual Reference< XNode > SAL_CALL getLastChild() + throw (RuntimeException) + { + return CNode::getLastChild(); + } + virtual OUString SAL_CALL getLocalName() + throw (RuntimeException) + { + return CNode::getLocalName(); + } + virtual OUString SAL_CALL getNamespaceURI() + throw (RuntimeException) + { + return CNode::getNamespaceURI(); + } + virtual Reference< XNode > SAL_CALL getNextSibling() + throw (RuntimeException) + { + return CNode::getNextSibling(); + } + virtual NodeType SAL_CALL getNodeType() + throw (RuntimeException) + { + return CNode::getNodeType(); + } + virtual Reference< XDocument > SAL_CALL getOwnerDocument() + throw (RuntimeException) + { + return CNode::getOwnerDocument(); + } + virtual Reference< XNode > SAL_CALL getParentNode() + throw (RuntimeException) + { + return CNode::getParentNode(); + } + virtual OUString SAL_CALL getPrefix() + throw (RuntimeException) + { + return CNode::getPrefix(); + } + virtual Reference< XNode > SAL_CALL getPreviousSibling() + throw (RuntimeException) + { + return CNode::getPreviousSibling(); + } + virtual sal_Bool SAL_CALL hasAttributes() + throw (RuntimeException) + { + return CNode::hasAttributes(); + } + virtual sal_Bool SAL_CALL hasChildNodes() + throw (RuntimeException) + { + return CNode::hasChildNodes(); + } + virtual Reference< XNode > SAL_CALL insertBefore( + const Reference< XNode >& newChild, const Reference< XNode >& refChild) + throw (RuntimeException, DOMException) + { + return CNode::insertBefore(newChild, refChild); + } + virtual sal_Bool SAL_CALL isSupported(const OUString& feature, const OUString& ver) + throw (RuntimeException) + { + return CNode::isSupported(feature, ver); + } + virtual void SAL_CALL normalize() + throw (RuntimeException) + { + CNode::normalize(); + } + virtual Reference< XNode > SAL_CALL removeChild(const Reference< XNode >& oldChild) + throw (RuntimeException, DOMException) + { + return CNode::removeChild(oldChild); + } + virtual Reference< XNode > SAL_CALL replaceChild( + const Reference< XNode >& newChild, const Reference< XNode >& oldChild) + throw (RuntimeException, DOMException) + { + return CNode::replaceChild(newChild, oldChild); + } + virtual void SAL_CALL setNodeValue(const OUString& nodeValue) + throw (RuntimeException, DOMException) + { + return CNode::setNodeValue(nodeValue); + } + virtual void SAL_CALL setPrefix(const OUString& prefix) + throw (RuntimeException, DOMException) + { + return CNode::setPrefix(prefix); + } + + + }; +} + +#endif diff --git a/unoxml/source/dom/notationsmap.cxx b/unoxml/source/dom/notationsmap.cxx new file mode 100644 index 000000000000..b4c2ae0fc702 --- /dev/null +++ b/unoxml/source/dom/notationsmap.cxx @@ -0,0 +1,104 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2008 by Sun Microsystems, Inc. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: notationsmap.cxx,v $ + * $Revision: 1.5 $ + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#include "notationsmap.hxx" + +namespace DOM +{ + CNotationsMap::CNotationsMap(const CDocumentType* aDocType) + : m_pDocType(aDocType) + { + } + + /** + The number of nodes in this map. + */ + sal_Int32 SAL_CALL CNotationsMap::getLength() throw (RuntimeException) + { + return 0; + } + + /** + Retrieves a node specified by local name + */ + Reference< XNode > SAL_CALL CNotationsMap::getNamedItem(const OUString& /* name */) throw (RuntimeException) + { + return Reference< XNode >(); + } + + /** + Retrieves a node specified by local name and namespace URI. + */ + Reference< XNode > SAL_CALL CNotationsMap::getNamedItemNS(const OUString& /*namespaceURI*/, const OUString& /*localName*/) +throw (RuntimeException) + { + return Reference< XNode >(); + } + + /** + Returns the indexth item in the map. + */ + Reference< XNode > SAL_CALL CNotationsMap::item(sal_Int32 /*index*/) throw (RuntimeException) + { + return Reference< XNode >(); + } + + /** + Removes a node specified by name. + */ + Reference< XNode > SAL_CALL CNotationsMap::removeNamedItem(const OUString& /*name*/) throw (RuntimeException) + { + return Reference< XNode >(); + } + + /** + // Removes a node specified by local name and namespace URI. + */ + Reference< XNode > SAL_CALL CNotationsMap::removeNamedItemNS(const OUString& /*namespaceURI*/, const OUString& /*localName*/) throw (RuntimeException) + { + return Reference< XNode >(); + } + + /** + // Adds a node using its nodeName attribute. + */ + Reference< XNode > SAL_CALL CNotationsMap::setNamedItem(const Reference< XNode >& /*arg*/) throw (RuntimeException) + { + return Reference< XNode >(); + } + + /** + Adds a node using its namespaceURI and localName. + */ + Reference< XNode > SAL_CALL CNotationsMap::setNamedItemNS(const Reference< XNode >& /*arg*/) throw (RuntimeException) + { + return Reference< XNode >(); + } +} diff --git a/unoxml/source/dom/notationsmap.hxx b/unoxml/source/dom/notationsmap.hxx new file mode 100644 index 000000000000..86f838fff713 --- /dev/null +++ b/unoxml/source/dom/notationsmap.hxx @@ -0,0 +1,100 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2008 by Sun Microsystems, Inc. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: notationsmap.hxx,v $ + * $Revision: 1.5 $ + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#ifndef _NOTATIONSMAP_HXX +#define _NOTATIONSMAP_HXX + +#include <map> +#include <sal/types.h> +#include <cppuhelper/implbase1.hxx> +#include <com/sun/star/uno/Reference.h> +#include <com/sun/star/uno/Exception.hpp> +#include <com/sun/star/xml/dom/XNode.hpp> +#include <com/sun/star/xml/dom/XNamedNodeMap.hpp> +#include "document.hxx" +#include "documenttype.hxx" + +using ::rtl::OUString; +using namespace com::sun::star::uno; +using namespace com::sun::star::xml::dom; + +namespace DOM +{ + class CNotationsMap : public cppu::WeakImplHelper1< XNamedNodeMap > + { + private: + const CDocumentType* m_pDocType; + + public: + CNotationsMap(const CDocumentType* aDocType); + + /** + The number of nodes in this map. + */ + virtual sal_Int32 SAL_CALL getLength() throw (RuntimeException); + + /** + Retrieves a node specified by local name + */ + virtual Reference< XNode > SAL_CALL getNamedItem(const OUString& name) throw (RuntimeException); + + /** + Retrieves a node specified by local name and namespace URI. + */ + virtual Reference< XNode > SAL_CALL getNamedItemNS(const OUString& namespaceURI,const OUString& localName) throw (RuntimeException); + + /** + Returns the indexth item in the map. + */ + virtual Reference< XNode > SAL_CALL item(sal_Int32 index) throw (RuntimeException); + + /** + Removes a node specified by name. + */ + virtual Reference< XNode > SAL_CALL removeNamedItem(const OUString& name) throw (RuntimeException); + + /** + // Removes a node specified by local name and namespace URI. + */ + virtual Reference< XNode > SAL_CALL removeNamedItemNS(const OUString& namespaceURI, const OUString& localName) throw (RuntimeException); + + /** + // Adds a node using its nodeName attribute. + */ + virtual Reference< XNode > SAL_CALL setNamedItem(const Reference< XNode >& arg) throw (RuntimeException); + + /** + Adds a node using its namespaceURI and localName. + */ + virtual Reference< XNode > SAL_CALL setNamedItemNS(const Reference< XNode >& arg) throw (RuntimeException); + }; +} + +#endif diff --git a/unoxml/source/dom/processinginstruction.cxx b/unoxml/source/dom/processinginstruction.cxx new file mode 100644 index 000000000000..089cccb80131 --- /dev/null +++ b/unoxml/source/dom/processinginstruction.cxx @@ -0,0 +1,95 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2008 by Sun Microsystems, Inc. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: processinginstruction.cxx,v $ + * $Revision: 1.10 $ + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#include "processinginstruction.hxx" +#include <com/sun/star/xml/sax/XExtendedDocumentHandler.hpp> +#include <string.h> + +namespace DOM +{ + CProcessingInstruction::CProcessingInstruction(const xmlNodePtr aNodePtr) + { + m_aNodeType = NodeType_PROCESSING_INSTRUCTION_NODE; + init_node(aNodePtr); + } + + void SAL_CALL CProcessingInstruction::saxify( + const Reference< XDocumentHandler >& i_xHandler) { + if (!i_xHandler.is()) throw RuntimeException(); + Reference< XExtendedDocumentHandler > xExtended(i_xHandler, UNO_QUERY); + if (xExtended.is()) { + xExtended->processingInstruction(getTarget(), getData()); + } + } + + /** + The content of this processing instruction. + */ + OUString SAL_CALL CProcessingInstruction::getData() throw (RuntimeException) + { + // XXX + return OUString(); + } + + /** + The target of this processing instruction. + */ + OUString SAL_CALL CProcessingInstruction::getTarget() throw (RuntimeException) + { + // XXX + return OUString(); + } + + + /** + The content of this processing instruction. + */ + void SAL_CALL CProcessingInstruction::setData(const OUString& /*data*/) throw (RuntimeException, DOMException) + { + // XXX + } + + + OUString SAL_CALL CProcessingInstruction::getNodeName()throw (RuntimeException) + { + OUString aName; + if (m_aNodePtr != NULL) + { + const xmlChar* xName = m_aNodePtr->name; + aName = OUString((sal_Char*)xName, strlen((char*)xName), RTL_TEXTENCODING_UTF8); + } + return aName; + } + + OUString SAL_CALL CProcessingInstruction::getNodeValue() throw (RuntimeException) + { + return getData(); + } +} diff --git a/unoxml/source/dom/processinginstruction.hxx b/unoxml/source/dom/processinginstruction.hxx new file mode 100644 index 000000000000..95dcf647ba0f --- /dev/null +++ b/unoxml/source/dom/processinginstruction.hxx @@ -0,0 +1,201 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2008 by Sun Microsystems, Inc. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: processinginstruction.hxx,v $ + * $Revision: 1.8 $ + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#ifndef _PROCESSINGINSTRUCTION_HXX +#define _PROCESSINGINSTRUCTION_HXX + +#include <com/sun/star/uno/Reference.h> +#include <com/sun/star/uno/Exception.hpp> +#include <com/sun/star/xml/dom/XProcessingInstruction.hpp> +#include "node.hxx" +#include <libxml/tree.h> + +using ::rtl::OUString; +using namespace com::sun::star::uno; +using namespace com::sun::star::xml::dom; + +namespace DOM +{ + class CProcessingInstruction : public cppu::ImplInheritanceHelper1< CNode, XProcessingInstruction > + { + friend class CNode; + + protected: + CProcessingInstruction(const xmlNodePtr aNodePtr); + + public: + + virtual void SAL_CALL saxify( + const Reference< XDocumentHandler >& i_xHandler); + + /** + The content of this processing instruction. + */ + virtual OUString SAL_CALL getData() throw (RuntimeException); + + /** + The target of this processing instruction. + */ + virtual OUString SAL_CALL getTarget() throw (RuntimeException); + + /** + The content of this processing instruction. + */ + virtual void SAL_CALL setData(const OUString& data) throw (RuntimeException, DOMException); + + // ---- resolve uno inheritance problems... + // overrides for XNode base + virtual OUString SAL_CALL getNodeName() + throw (RuntimeException); + virtual OUString SAL_CALL getNodeValue() + throw (RuntimeException); + // --- delegation for XNde base. + virtual Reference< XNode > SAL_CALL appendChild(const Reference< XNode >& newChild) + throw (RuntimeException, DOMException) + { + return CNode::appendChild(newChild); + } + virtual Reference< XNode > SAL_CALL cloneNode(sal_Bool deep) + throw (RuntimeException) + { + return CNode::cloneNode(deep); + } + virtual Reference< XNamedNodeMap > SAL_CALL getAttributes() + throw (RuntimeException) + { + return CNode::getAttributes(); + } + virtual Reference< XNodeList > SAL_CALL getChildNodes() + throw (RuntimeException) + { + return CNode::getChildNodes(); + } + virtual Reference< XNode > SAL_CALL getFirstChild() + throw (RuntimeException) + { + return CNode::getFirstChild(); + } + virtual Reference< XNode > SAL_CALL getLastChild() + throw (RuntimeException) + { + return CNode::getLastChild(); + } + virtual OUString SAL_CALL getLocalName() + throw (RuntimeException) + { + return CNode::getLocalName(); + } + virtual OUString SAL_CALL getNamespaceURI() + throw (RuntimeException) + { + return CNode::getNamespaceURI(); + } + virtual Reference< XNode > SAL_CALL getNextSibling() + throw (RuntimeException) + { + return CNode::getNextSibling(); + } + virtual NodeType SAL_CALL getNodeType() + throw (RuntimeException) + { + return CNode::getNodeType(); + } + virtual Reference< XDocument > SAL_CALL getOwnerDocument() + throw (RuntimeException) + { + return CNode::getOwnerDocument(); + } + virtual Reference< XNode > SAL_CALL getParentNode() + throw (RuntimeException) + { + return CNode::getParentNode(); + } + virtual OUString SAL_CALL getPrefix() + throw (RuntimeException) + { + return CNode::getPrefix(); + } + virtual Reference< XNode > SAL_CALL getPreviousSibling() + throw (RuntimeException) + { + return CNode::getPreviousSibling(); + } + virtual sal_Bool SAL_CALL hasAttributes() + throw (RuntimeException) + { + return CNode::hasAttributes(); + } + virtual sal_Bool SAL_CALL hasChildNodes() + throw (RuntimeException) + { + return CNode::hasChildNodes(); + } + virtual Reference< XNode > SAL_CALL insertBefore( + const Reference< XNode >& newChild, const Reference< XNode >& refChild) + throw (RuntimeException, DOMException) + { + return CNode::insertBefore(newChild, refChild); + } + virtual sal_Bool SAL_CALL isSupported(const OUString& feature, const OUString& ver) + throw (RuntimeException) + { + return CNode::isSupported(feature, ver); + } + virtual void SAL_CALL normalize() + throw (RuntimeException) + { + CNode::normalize(); + } + virtual Reference< XNode > SAL_CALL removeChild(const Reference< XNode >& oldChild) + throw (RuntimeException, DOMException) + { + return CNode::removeChild(oldChild); + } + virtual Reference< XNode > SAL_CALL replaceChild( + const Reference< XNode >& newChild, const Reference< XNode >& oldChild) + throw (RuntimeException, DOMException) + { + return CNode::replaceChild(newChild, oldChild); + } + virtual void SAL_CALL setNodeValue(const OUString& nodeValue) + throw (RuntimeException, DOMException) + { + return CNode::setNodeValue(nodeValue); + } + virtual void SAL_CALL setPrefix(const OUString& prefix) + throw (RuntimeException, DOMException) + { + return CNode::setPrefix(prefix); + } + + }; +} + +#endif diff --git a/unoxml/source/dom/saxbuilder.cxx b/unoxml/source/dom/saxbuilder.cxx new file mode 100644 index 000000000000..e06a3915b851 --- /dev/null +++ b/unoxml/source/dom/saxbuilder.cxx @@ -0,0 +1,356 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2008 by Sun Microsystems, Inc. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: saxbuilder.cxx,v $ + * $Revision: 1.8 $ + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ +#if defined(_MSC_VER) && (_MSC_VER > 1310) +#pragma warning(disable : 4701) +#endif + +#include "node.hxx" +#include "saxbuilder.hxx" +#include <com/sun/star/xml/dom/XDocumentBuilder.hpp> +#include <libxml/tree.h> +#include <com/sun/star/uno/Sequence.h> + +namespace DOM +{ + Reference< XInterface > CSAXDocumentBuilder::_getInstance(const Reference< XMultiServiceFactory >& rSMgr) + { + return static_cast< XSAXDocumentBuilder* >(new CSAXDocumentBuilder(rSMgr)); + } + + const char* CSAXDocumentBuilder::aImplementationName = "com.sun.star.comp.xml.dom.SAXDocumentBuilder"; + const char* CSAXDocumentBuilder::aSupportedServiceNames[] = { + "com.sun.star.xml.dom.SAXDocumentBuilder", + NULL + }; + + CSAXDocumentBuilder::CSAXDocumentBuilder(const Reference< XMultiServiceFactory >& mgr) + : m_aServiceManager(mgr) + , m_aState( SAXDocumentBuilderState_READY) + {} + + OUString CSAXDocumentBuilder::_getImplementationName() + { + return OUString::createFromAscii(aImplementationName); + } + Sequence<OUString> CSAXDocumentBuilder::_getSupportedServiceNames() + { + Sequence<OUString> aSequence; + for (int i=0; aSupportedServiceNames[i]!=NULL; i++) { + aSequence.realloc(i+1); + aSequence[i]=(OUString::createFromAscii(aSupportedServiceNames[i])); + } + return aSequence; + } + + Sequence< OUString > SAL_CALL CSAXDocumentBuilder::getSupportedServiceNames() + throw (RuntimeException) + { + return CSAXDocumentBuilder::_getSupportedServiceNames(); + } + + OUString SAL_CALL CSAXDocumentBuilder::getImplementationName() + throw (RuntimeException) + { + return CSAXDocumentBuilder::_getImplementationName(); + } + + sal_Bool SAL_CALL CSAXDocumentBuilder::supportsService(const OUString& aServiceName) + throw (RuntimeException) + { + Sequence< OUString > supported = CSAXDocumentBuilder::_getSupportedServiceNames(); + for (sal_Int32 i=0; i<supported.getLength(); i++) + { + if (supported[i] == aServiceName) return sal_True; + } + return sal_False; + } + + + SAXDocumentBuilderState SAL_CALL CSAXDocumentBuilder::getState() + throw (RuntimeException) + { + return m_aState; + } + + void SAL_CALL CSAXDocumentBuilder::reset() + throw (RuntimeException) + { + m_aDocument = Reference< XDocument >(); + m_aFragment = Reference< XDocumentFragment >(); + while (!m_aNodeStack.empty()) m_aNodeStack.pop(); + while (!m_aNSStack.empty()) m_aNSStack.pop(); + m_aState = SAXDocumentBuilderState_READY; + } + + Reference< XDocument > SAL_CALL CSAXDocumentBuilder::getDocument() + throw (RuntimeException) + { + if (m_aState != SAXDocumentBuilderState_DOCUMENT_FINISHED) + throw RuntimeException(); + + return m_aDocument; + } + + Reference< XDocumentFragment > SAL_CALL CSAXDocumentBuilder::getDocumentFragment() + throw (RuntimeException) + { + if (m_aState != SAXDocumentBuilderState_FRAGMENT_FINISHED) + throw RuntimeException(); + return m_aFragment; + } + + void SAL_CALL CSAXDocumentBuilder::startDocumentFragment(const Reference< XDocument >& ownerDoc) + throw (RuntimeException) + { + // start a new document fragment and push it onto the stack + // we have to be in a clean state to do this + if (!m_aState == SAXDocumentBuilderState_READY) + throw RuntimeException(); + + m_aDocument = ownerDoc; + Reference< XDocumentFragment > aFragment = m_aDocument->createDocumentFragment(); + m_aNodeStack.push(Reference< XNode >(aFragment, UNO_QUERY)); + m_aFragment = aFragment; + m_aState = SAXDocumentBuilderState_BUILDING_FRAGMENT; + } + + void SAL_CALL CSAXDocumentBuilder::endDocumentFragment() + throw (RuntimeException) + { + // there should only be the document left on the node stack + if (m_aState != SAXDocumentBuilderState_BUILDING_FRAGMENT) + throw RuntimeException(); + + Reference< XNode > aNode = m_aNodeStack.top(); + if ( aNode->getNodeType() != NodeType_DOCUMENT_FRAGMENT_NODE) + throw RuntimeException(); + m_aNodeStack.pop(); + m_aState = SAXDocumentBuilderState_FRAGMENT_FINISHED; + } + + // document handler + + void SAL_CALL CSAXDocumentBuilder::startDocument() throw (RuntimeException, SAXException) + { + + // start a new document and push it onto the stack + // we have to be in a clean state to do this + if (!m_aState == SAXDocumentBuilderState_READY) + throw SAXException(); + + Reference< XDocumentBuilder > aBuilder(m_aServiceManager->createInstance( + OUString::createFromAscii("com.sun.star.xml.dom.DocumentBuilder")), UNO_QUERY_THROW); + Reference< XDocument > aDocument = aBuilder->newDocument(); + m_aNodeStack.push(Reference< XNode >(aDocument, UNO_QUERY)); + m_aDocument = aDocument; + m_aState = SAXDocumentBuilderState_BUILDING_DOCUMENT; + } + + void SAL_CALL CSAXDocumentBuilder::endDocument() throw (RuntimeException, SAXException) + { + // there should only be the document left on the node stack + if (!m_aState == SAXDocumentBuilderState_BUILDING_DOCUMENT) + throw SAXException(); + + Reference< XNode > aNode = m_aNodeStack.top(); + if ( aNode->getNodeType() != NodeType_DOCUMENT_NODE) + throw SAXException(); + m_aNodeStack.pop(); + m_aState = SAXDocumentBuilderState_DOCUMENT_FINISHED; + } + + void SAL_CALL CSAXDocumentBuilder::startElement(const OUString& aName, const Reference< XAttributeList>& attribs) + throw (RuntimeException, SAXException) + { + if ( m_aState != SAXDocumentBuilderState_BUILDING_DOCUMENT && + m_aState != SAXDocumentBuilderState_BUILDING_FRAGMENT) + { + throw SAXException(); + } + + // start with mappings in effect for last level + NSMap aNSMap; + if (!m_aNSStack.empty()) + aNSMap = NSMap(m_aNSStack.top()); + + // handle xmlns: attributes and add to mappings + OUString attr_qname; + OUString attr_value; + OUString newprefix; + AttrMap aAttrMap; + sal_Int32 idx=-1; + sal_Int16 nAttributes = attribs->getLength(); + for (sal_Int16 i=0; i<nAttributes; i++) + { + attr_qname = attribs->getNameByIndex(i); + attr_value = attribs->getValueByIndex(i); + // new prefix mapping + if (attr_qname.indexOf(OUString::createFromAscii("xmlns:")) == 0) + { + newprefix = attr_qname.copy(attr_qname.indexOf(':')+1); + aNSMap.insert(NSMap::value_type(newprefix, attr_value)); + } + else if (attr_qname == OUString::createFromAscii("xmlns")) + { + // new default prefix + aNSMap.insert(NSMap::value_type(OUString(), attr_value)); + } + else + { + aAttrMap.insert(AttrMap::value_type(attr_qname, attr_value)); + } + } + + // does the element have a prefix? + OUString aPrefix; + OUString aURI; + Reference< XElement > aElement; + idx = aName.indexOf(':'); + if (idx != -1) + { + aPrefix = aName.copy(0, idx); + } + else + aPrefix = OUString(); + + NSMap::const_iterator result = aNSMap.find(aPrefix); + if ( result != aNSMap.end()) + { + // found a URI for prefix + aElement = m_aDocument->createElementNS( result->second, aName); // qualified name + } + else + { + // no URI for prefix + aElement = m_aDocument->createElement(aName); + } + aElement = Reference< XElement > ( + m_aNodeStack.top()->appendChild(Reference< XNode >(aElement, UNO_QUERY)), + UNO_QUERY); + m_aNodeStack.push(Reference< XNode >(aElement, UNO_QUERY)); + + // set non xmlns attributes + aPrefix = OUString(); + aURI = OUString(); + AttrMap::const_iterator a = aAttrMap.begin(); + while (a != aAttrMap.end()) + { + attr_qname = a->first; + attr_value = a->second; + idx = attr_qname.indexOf(':'); + if(idx != -1) + { + aPrefix = attr_qname.copy(0, idx); + } + else + aPrefix = OUString(); + + result = aNSMap.find(aPrefix); + if (result != aNSMap.end()) + { + // set attribute with namespace + aElement->setAttributeNS(result->second, attr_qname, attr_value); + } else { + // set attribute without namespace + aElement->setAttribute(attr_qname, attr_value); + } + a++; + } + m_aNSStack.push(aNSMap); + } + + void SAL_CALL CSAXDocumentBuilder::endElement(const OUString& aName) + throw (RuntimeException, SAXException) + { + // pop the current element from the stack + if ( m_aState != SAXDocumentBuilderState_BUILDING_DOCUMENT && + m_aState != SAXDocumentBuilderState_BUILDING_FRAGMENT) + throw SAXException(); + + Reference< XNode > aNode(m_aNodeStack.top()); + if (aNode->getNodeType() != NodeType_ELEMENT_NODE) + throw SAXException(); + + Reference< XElement > aElement(aNode, UNO_QUERY); + OUString aRefName; + OUString aPrefix = aElement->getPrefix(); + if (aPrefix.getLength() > 0) + aRefName = aPrefix + OUString::createFromAscii(":") + aElement->getTagName(); + else + aRefName = aElement->getTagName(); + if (aRefName != aName) // consistency check + throw SAXException(); + + // pop it + m_aNodeStack.pop(); + m_aNSStack.pop(); + } + + void SAL_CALL CSAXDocumentBuilder::characters(const OUString& aChars) + throw (RuntimeException, SAXException) + { + // append text node to the current top element + if (m_aState != SAXDocumentBuilderState_BUILDING_DOCUMENT && + m_aState != SAXDocumentBuilderState_BUILDING_FRAGMENT) + throw SAXException(); + + Reference< XText > aText = m_aDocument->createTextNode(aChars); + m_aNodeStack.top()->appendChild(Reference< XNode >(aText, UNO_QUERY)); + } + + void SAL_CALL CSAXDocumentBuilder::ignorableWhitespace(const OUString& ) + throw (RuntimeException, SAXException) + { + // ignore ignorable whitespace + if ( m_aState != SAXDocumentBuilderState_BUILDING_DOCUMENT && + m_aState != SAXDocumentBuilderState_BUILDING_FRAGMENT) + throw SAXException(); + } + + void SAL_CALL CSAXDocumentBuilder::processingInstruction(const OUString& aTarget, const OUString& aData) + throw (RuntimeException, SAXException) + { + // append PI node to the current top + if ( m_aState != SAXDocumentBuilderState_BUILDING_DOCUMENT && + m_aState != SAXDocumentBuilderState_BUILDING_FRAGMENT) + throw SAXException(); + + Reference< XProcessingInstruction > aInstruction = m_aDocument->createProcessingInstruction( + aTarget, aData); + m_aNodeStack.top()->appendChild(Reference< XNode >(aInstruction, UNO_QUERY)); + } + + void SAL_CALL CSAXDocumentBuilder::setDocumentLocator(const Reference< XLocator >& aLocator) + throw (RuntimeException, SAXException) + { + // set the document locator... + m_aLocator = aLocator; + } +} diff --git a/unoxml/source/dom/saxbuilder.hxx b/unoxml/source/dom/saxbuilder.hxx new file mode 100644 index 000000000000..454b22f2d457 --- /dev/null +++ b/unoxml/source/dom/saxbuilder.hxx @@ -0,0 +1,153 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2008 by Sun Microsystems, Inc. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: saxbuilder.hxx,v $ + * $Revision: 1.7 $ + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#ifndef _SAXBUILDER_HXX +#define _SAXBUILDER_HXX + +#include <stack> +#include <map> + +#include <sal/types.h> +#include <cppuhelper/implbase3.hxx> +#include <com/sun/star/uno/Reference.h> +#include <com/sun/star/uno/Sequence.h> + +#include <com/sun/star/uno/XInterface.hpp> +#include <com/sun/star/uno/Exception.hpp> +#include <com/sun/star/xml/dom/XSAXDocumentBuilder.hpp> +#include <com/sun/star/xml/dom/SAXDocumentBuilderState.hpp> +#include <com/sun/star/xml/dom/XDocument.hpp> +#include <com/sun/star/xml/dom/XDocumentFragment.hpp> +#include <com/sun/star/xml/sax/XDocumentHandler.hpp> +#include <com/sun/star/xml/sax/XLocator.hpp> +#include <com/sun/star/xml/sax/XAttributeList.hpp> +#include <com/sun/star/lang/XServiceInfo.hpp> +#include <com/sun/star/lang/XSingleServiceFactory.hpp> +#include <com/sun/star/lang/XMultiServiceFactory.hpp> + +#include "libxml/tree.h" + +using ::rtl::OUString; +using namespace com::sun::star::uno; +using namespace com::sun::star::xml::dom; +using namespace com::sun::star::xml::sax; + +using com::sun::star::lang::XServiceInfo; +using com::sun::star::lang::XSingleServiceFactory; +using com::sun::star::lang::XMultiServiceFactory; + +namespace DOM +{ + + typedef std::stack< Reference< XNode > > NodeStack; + typedef std::map< OUString, OUString > NSMap; + typedef std::map< OUString, OUString > AttrMap; + typedef std::stack< NSMap > NSStack; + + class CSAXDocumentBuilder + : public ::cppu::WeakImplHelper3< XDocumentHandler, XSAXDocumentBuilder, XServiceInfo > + { + + private: + const Reference< XMultiServiceFactory > m_aServiceManager; + + SAXDocumentBuilderState m_aState; + NodeStack m_aNodeStack; + NSStack m_aNSStack; + + OUString resolvePrefix(const OUString& aPrefix); + + Reference< XDocument > m_aDocument; + Reference< XDocumentFragment > m_aFragment; + Reference< XLocator > m_aLocator; + + + public: + + // call for factory + static Reference< XInterface > getInstance(const Reference < XMultiServiceFactory >& xFactory); + + // static helpers for service info and component management + static const char* aImplementationName; + static const char* aSupportedServiceNames[]; + static OUString _getImplementationName(); + static Sequence< OUString > _getSupportedServiceNames(); + static Reference< XInterface > _getInstance(const Reference< XMultiServiceFactory >& rSMgr); + + CSAXDocumentBuilder(const Reference< XMultiServiceFactory >& mgr); + + // XServiceInfo + virtual OUString SAL_CALL getImplementationName() + throw (RuntimeException); + virtual sal_Bool SAL_CALL supportsService(const OUString& ServiceName) + throw (RuntimeException); + virtual Sequence< OUString > SAL_CALL getSupportedServiceNames () + throw (RuntimeException); + + // XDocumentHandler + virtual void SAL_CALL startDocument() + throw( RuntimeException, com::sun::star::xml::sax::SAXException ); + virtual void SAL_CALL endDocument() + throw( RuntimeException, com::sun::star::xml::sax::SAXException ); + virtual void SAL_CALL startElement( const OUString& aName, + const Reference< XAttributeList >& xAttribs ) + throw( RuntimeException, com::sun::star::xml::sax::SAXException ); + virtual void SAL_CALL endElement( const OUString& aName ) + throw( RuntimeException, com::sun::star::xml::sax::SAXException ); + virtual void SAL_CALL characters( const OUString& aChars ) + throw( RuntimeException, com::sun::star::xml::sax::SAXException ); + virtual void SAL_CALL ignorableWhitespace( const OUString& aWhitespaces ) + throw( RuntimeException, com::sun::star::xml::sax::SAXException ); + virtual void SAL_CALL processingInstruction( const OUString& aTarget, + const OUString& aData ) + throw( RuntimeException, com::sun::star::xml::sax::SAXException ); + virtual void SAL_CALL setDocumentLocator( const Reference< XLocator >& xLocator ) + throw( RuntimeException, com::sun::star::xml::sax::SAXException ); + + + // XSAXDocumentBuilder + virtual SAXDocumentBuilderState SAL_CALL getState() + throw (RuntimeException); + virtual void SAL_CALL reset() + throw (RuntimeException); + virtual Reference< XDocument > SAL_CALL getDocument() + throw (RuntimeException); + virtual Reference< XDocumentFragment > SAL_CALL getDocumentFragment() + throw (RuntimeException); + virtual void SAL_CALL startDocumentFragment(const Reference< XDocument >& ownerDoc) + throw (RuntimeException); + virtual void SAL_CALL endDocumentFragment() + throw (RuntimeException); + + + }; +} + +#endif diff --git a/unoxml/source/dom/text.cxx b/unoxml/source/dom/text.cxx new file mode 100644 index 000000000000..68eb775981e4 --- /dev/null +++ b/unoxml/source/dom/text.cxx @@ -0,0 +1,75 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2008 by Sun Microsystems, Inc. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: text.cxx,v $ + * $Revision: 1.7 $ + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#include "text.hxx" +namespace DOM +{ + CText::CText(const xmlNodePtr aNodePtr) + { + m_aNodeType = NodeType_TEXT_NODE; + init_characterdata(aNodePtr); + } + + void SAL_CALL CText::saxify( + const Reference< XDocumentHandler >& i_xHandler) { + if (!i_xHandler.is()) throw RuntimeException(); + i_xHandler->characters(getData()); + } + + void CText::init_text(const xmlNodePtr aNodePtr) + { + init_characterdata(aNodePtr); + } + + Reference< XText > SAL_CALL CText::splitText(sal_Int32 /*offset*/) + throw (RuntimeException) + { + return Reference< XText >(this); + } + + OUString SAL_CALL CText::getNodeName()throw (RuntimeException) + { + return OUString::createFromAscii("#text"); + } + + void SAL_CALL CText::fastSaxify( Context& io_rContext ) + { + if( io_rContext.mxCurrentHandler.is() ) + { + try + { + io_rContext.mxCurrentHandler->characters( getData() ); + } + catch( Exception& ) + {} + } + } + +} diff --git a/unoxml/source/dom/text.hxx b/unoxml/source/dom/text.hxx new file mode 100644 index 000000000000..8d97365ba744 --- /dev/null +++ b/unoxml/source/dom/text.hxx @@ -0,0 +1,244 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2008 by Sun Microsystems, Inc. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: text.hxx,v $ + * $Revision: 1.8 $ + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#ifndef _TEXT_HXX +#define _TEXT_HXX + +#include <sal/types.h> +#include <cppuhelper/implbase1.hxx> +#include <com/sun/star/uno/Reference.h> +#include <com/sun/star/uno/Exception.hpp> +#include <com/sun/star/xml/dom/XNode.hpp> +#include <com/sun/star/xml/dom/XText.hpp> +#include <com/sun/star/xml/dom/XCharacterData.hpp> +#include <libxml/tree.h> +#include "characterdata.hxx" + +using ::rtl::OUString; +using namespace com::sun::star::uno; +using namespace com::sun::star::lang; +using namespace com::sun::star::xml::dom; + +namespace DOM +{ + class CText : public cppu::ImplInheritanceHelper1< CCharacterData, XText > + { + friend class CNode; + + protected: + CText(){} + CText(const xmlNodePtr aNodePtr); + void init_text(const xmlNodePtr aNodePtr); + + public: + + virtual void SAL_CALL saxify( + const Reference< XDocumentHandler >& i_xHandler); + + virtual void SAL_CALL fastSaxify( Context& io_rContext ); + + // Breaks this node into two nodes at the specified offset, keeping + // both in the tree as siblings. + virtual Reference< XText > SAL_CALL splitText(sal_Int32 offset) + throw (RuntimeException); + + + // --- delegations for XCharacterData + virtual void SAL_CALL appendData(const OUString& arg) + throw (RuntimeException, DOMException) + { + CCharacterData::appendData(arg); + } + virtual void SAL_CALL deleteData(sal_Int32 offset, sal_Int32 count) + throw (RuntimeException, DOMException) + { + CCharacterData::deleteData(offset, count); + } + virtual OUString SAL_CALL getData() throw (RuntimeException) + { + return CCharacterData::getData(); + } + virtual sal_Int32 SAL_CALL getLength() throw (RuntimeException) + { + return CCharacterData::getLength(); + } + virtual void SAL_CALL insertData(sal_Int32 offset, const OUString& arg) + throw (RuntimeException, DOMException) + { + CCharacterData::insertData(offset, arg); + } + virtual void SAL_CALL replaceData(sal_Int32 offset, sal_Int32 count, const OUString& arg) + throw (RuntimeException, DOMException) + { + CCharacterData::replaceData(offset, count, arg); + } + virtual void SAL_CALL setData(const OUString& data) + throw (RuntimeException, DOMException) + { + CCharacterData::setData(data); + } + virtual OUString SAL_CALL subStringData(sal_Int32 offset, sal_Int32 count) + throw (RuntimeException, DOMException) + { + return CCharacterData::subStringData(offset, count); + } + + + // --- overrides for XNode base + virtual OUString SAL_CALL getNodeName() + throw (RuntimeException); + + // --- resolve uno inheritance problems... + // --- delegation for XNde base. + virtual Reference< XNode > SAL_CALL appendChild(const Reference< XNode >& newChild) + throw (RuntimeException, DOMException) + { + return CCharacterData::appendChild(newChild); + } + virtual Reference< XNode > SAL_CALL cloneNode(sal_Bool deep) + throw (RuntimeException) + { + return CCharacterData::cloneNode(deep); + } + virtual Reference< XNamedNodeMap > SAL_CALL getAttributes() + throw (RuntimeException) + { + return CCharacterData::getAttributes(); + } + virtual Reference< XNodeList > SAL_CALL getChildNodes() + throw (RuntimeException) + { + return CCharacterData::getChildNodes(); + } + virtual Reference< XNode > SAL_CALL getFirstChild() + throw (RuntimeException) + { + return CCharacterData::getFirstChild(); + } + virtual Reference< XNode > SAL_CALL getLastChild() + throw (RuntimeException) + { + return CCharacterData::getLastChild(); + } + virtual OUString SAL_CALL getLocalName() + throw (RuntimeException) + { + return CCharacterData::getLocalName(); + } + virtual OUString SAL_CALL getNamespaceURI() + throw (RuntimeException) + { + return CCharacterData::getNamespaceURI(); + } + virtual Reference< XNode > SAL_CALL getNextSibling() + throw (RuntimeException) + { + return CCharacterData::getNextSibling(); + } + virtual NodeType SAL_CALL getNodeType() + throw (RuntimeException) + { + return CCharacterData::getNodeType(); + } + virtual OUString SAL_CALL getNodeValue() throw (RuntimeException) + { + return CCharacterData::getNodeValue(); + } + virtual Reference< XDocument > SAL_CALL getOwnerDocument() + throw (RuntimeException) + { + return CCharacterData::getOwnerDocument(); + } + virtual Reference< XNode > SAL_CALL getParentNode() + throw (RuntimeException) + { + return CCharacterData::getParentNode(); + } + virtual OUString SAL_CALL getPrefix() + throw (RuntimeException) + { + return CCharacterData::getPrefix(); + } + virtual Reference< XNode > SAL_CALL getPreviousSibling() + throw (RuntimeException) + { + return CCharacterData::getPreviousSibling(); + } + virtual sal_Bool SAL_CALL hasAttributes() + throw (RuntimeException) + { + return CCharacterData::hasAttributes(); + } + virtual sal_Bool SAL_CALL hasChildNodes() + throw (RuntimeException) + { + return CCharacterData::hasChildNodes(); + } + virtual Reference< XNode > SAL_CALL insertBefore( + const Reference< XNode >& newChild, const Reference< XNode >& refChild) + throw (RuntimeException, DOMException) + { + return CCharacterData::insertBefore(newChild, refChild); + } + virtual sal_Bool SAL_CALL isSupported(const OUString& feature, const OUString& ver) + throw (RuntimeException) + { + return CCharacterData::isSupported(feature, ver); + } + virtual void SAL_CALL normalize() + throw (RuntimeException) + { + CCharacterData::normalize(); + } + virtual Reference< XNode > SAL_CALL removeChild(const Reference< XNode >& oldChild) + throw (RuntimeException, DOMException) + { + return CCharacterData::removeChild(oldChild); + } + virtual Reference< XNode > SAL_CALL replaceChild( + const Reference< XNode >& newChild, const Reference< XNode >& oldChild) + throw (RuntimeException, DOMException) + { + return CCharacterData::replaceChild(newChild, oldChild); + } + virtual void SAL_CALL setNodeValue(const OUString& nodeValue) + throw (RuntimeException, DOMException) + { + return CCharacterData::setNodeValue(nodeValue); + } + virtual void SAL_CALL setPrefix(const OUString& prefix) + throw (RuntimeException, DOMException) + { + return CCharacterData::setPrefix(prefix); + } + + }; +} +#endif diff --git a/unoxml/source/events/event.cxx b/unoxml/source/events/event.cxx new file mode 100644 index 000000000000..1d41bbba9911 --- /dev/null +++ b/unoxml/source/events/event.cxx @@ -0,0 +1,62 @@ +#include "event.hxx" + +namespace DOM { namespace events +{ + + CEvent::~CEvent() + { + } + + OUString SAL_CALL CEvent::getType() throw (RuntimeException) + { + return m_eventType; + } + + Reference< XEventTarget > SAL_CALL CEvent::getTarget() throw (RuntimeException) + { + return m_target; + } + + Reference< XEventTarget > SAL_CALL CEvent::getCurrentTarget() throw (RuntimeException) + { + return m_currentTarget; + } + + PhaseType SAL_CALL CEvent::getEventPhase() throw (RuntimeException) + { + return m_phase; + } + + sal_Bool SAL_CALL CEvent::getBubbles() throw (RuntimeException) + { + return m_bubbles; + } + + sal_Bool SAL_CALL CEvent::getCancelable() throw (RuntimeException) + { + return m_cancelable; + } + + com::sun::star::util::Time SAL_CALL CEvent::getTimeStamp() throw (RuntimeException) + { + return m_time; + } + + void SAL_CALL CEvent::stopPropagation() throw (RuntimeException) + { + if (m_cancelable) m_canceled = sal_True; + } + + void SAL_CALL CEvent::preventDefault() throw (RuntimeException) + { + } + + void SAL_CALL CEvent::initEvent(const OUString& eventTypeArg, sal_Bool canBubbleArg, + sal_Bool cancelableArg) throw (RuntimeException) + { + m_eventType = eventTypeArg; + m_bubbles = canBubbleArg; + m_cancelable = cancelableArg; + } + +}} diff --git a/unoxml/source/events/event.hxx b/unoxml/source/events/event.hxx new file mode 100644 index 000000000000..3620d84374dc --- /dev/null +++ b/unoxml/source/events/event.hxx @@ -0,0 +1,70 @@ +#ifndef __EVENT_HXX +#define __EVENT_HXX + +#include <sal/types.h> + +#include <cppuhelper/implbase1.hxx> +#include <cppuhelper/implbase2.hxx> +#include <cppuhelper/implbase3.hxx> +#include <com/sun/star/uno/Reference.h> +#include <com/sun/star/uno/Exception.hpp> +#include <com/sun/star/xml/dom/events/XEventTarget.hpp> +#include <com/sun/star/util/Time.hpp> + +#include "../dom/node.hxx" + +#include <libxml/tree.h> + +using namespace com::sun::star::uno; +using namespace com::sun::star::xml::dom; +using namespace com::sun::star::xml::dom::events; + + +namespace DOM {namespace events +{ +class CEvent : public cppu::WeakImplHelper1< XEvent > +{ +friend class CEventDispatcher; +friend class CNode; +friend class CDocument; +friend class CElement; +friend class CText; +friend class CCharacterData; +friend class CAttr; + + +private: + sal_Bool m_canceled; + +protected: + OUString m_eventType; + Reference< XEventTarget > m_target; + Reference< XEventTarget > m_currentTarget; + //xmlNodePtr m_target; + //xmlNodePtr m_currentTarget; + PhaseType m_phase; + sal_Bool m_bubbles; + sal_Bool m_cancelable; + com::sun::star::util::Time m_time; + +public: + + CEvent() : m_canceled(sal_False){} + + virtual ~CEvent(); + virtual OUString SAL_CALL getType() throw (RuntimeException); + virtual Reference< XEventTarget > SAL_CALL getTarget() throw (RuntimeException); + virtual Reference< XEventTarget > SAL_CALL getCurrentTarget() throw (RuntimeException); + virtual PhaseType SAL_CALL getEventPhase() throw (RuntimeException); + virtual sal_Bool SAL_CALL getBubbles() throw (RuntimeException); + virtual sal_Bool SAL_CALL getCancelable() throw (RuntimeException); + virtual com::sun::star::util::Time SAL_CALL getTimeStamp() throw (RuntimeException); + virtual void SAL_CALL stopPropagation() throw (RuntimeException); + virtual void SAL_CALL preventDefault() throw (RuntimeException); + virtual void SAL_CALL initEvent( + const OUString& eventTypeArg, + sal_Bool canBubbleArg, + sal_Bool cancelableArg) throw (RuntimeException); +}; +}} +#endif diff --git a/unoxml/source/events/eventdispatcher.cxx b/unoxml/source/events/eventdispatcher.cxx new file mode 100644 index 000000000000..4b1c1548bf38 --- /dev/null +++ b/unoxml/source/events/eventdispatcher.cxx @@ -0,0 +1,198 @@ +#include "eventdispatcher.hxx" +#include "event.hxx" +#include "mutationevent.hxx" +#include "uievent.hxx" +#include "mouseevent.hxx" +#include "../dom/node.hxx" + +namespace DOM { namespace events { + + TypeListenerMap CEventDispatcher::captureListeners; + TypeListenerMap CEventDispatcher::targetListeners; + + void CEventDispatcher::addListener(xmlNodePtr pNode, OUString aType, const Reference<XEventListener>& aListener, sal_Bool bCapture) + { + TypeListenerMap* pTMap = &targetListeners; + if (bCapture) pTMap = &captureListeners; + + // get the multimap for the specified type + ListenerMap *pMap = 0; + TypeListenerMap::const_iterator tIter = pTMap->find(aType); + if (tIter == pTMap->end()) { + // the map has to be created + pMap = new ListenerMap(); + pTMap->insert(TypeListenerMap::value_type(aType, pMap)); + } else { + pMap = tIter->second; + } + if (pMap !=0) + pMap->insert(ListenerMap::value_type(pNode, aListener)); + } + + void CEventDispatcher::removeListener(xmlNodePtr pNode, OUString aType, const Reference<XEventListener>& aListener, sal_Bool bCapture) + { + TypeListenerMap *pTMap = &targetListeners; + if (bCapture) pTMap = &captureListeners; + + // get the multimap for the specified type + TypeListenerMap::const_iterator tIter = pTMap->find(aType); + if (tIter != pTMap->end()) { + ListenerMap *pMap = tIter->second; + // find listeners of specied type for specified node + ListenerMap::iterator iter = pMap->find(pNode); + while (iter != pMap->end() && iter->first == pNode) + { + // erase all references to specified listener + if ((iter->second).is() && iter->second == aListener) + { + ListenerMap::iterator tmp_iter = iter; + iter++; + pMap->erase(tmp_iter); + } + else + iter++; + } + } + } + + void CEventDispatcher::callListeners(xmlNodePtr pNode, OUString aType, const Reference< XEvent >& xEvent, sal_Bool bCapture) + { + TypeListenerMap *pTMap = &targetListeners; + if (bCapture) pTMap = &captureListeners; + + // get the multimap for the specified type + TypeListenerMap::const_iterator tIter = pTMap->find(aType); + if (tIter != pTMap->end()) { + ListenerMap *pMap = tIter->second; + ListenerMap::const_iterator iter = pMap->lower_bound(pNode); + ListenerMap::const_iterator ibound = pMap->upper_bound(pNode); + for( ; iter != ibound; iter++ ) + { + if((iter->second).is()) + (iter->second)->handleEvent(xEvent); + } + } + } + + sal_Bool CEventDispatcher::dispatchEvent(xmlNodePtr aNodePtr, const Reference< XEvent >& aEvent) + { + CEvent *pEvent = 0; // pointer to internal event representation + Reference< XEvent > xEvent; // reference to the event being dispatched; + + OUString aType = aEvent->getType(); + if (aType.compareToAscii("DOMSubtreeModified") == 0|| + aType.compareToAscii("DOMNodeInserted") == 0|| + aType.compareToAscii("DOMNodeRemoved") == 0|| + aType.compareToAscii("DOMNodeRemovedFromDocument") == 0|| + aType.compareToAscii("DOMNodeInsertedIntoDocument") == 0|| + aType.compareToAscii("DOMAttrModified") == 0|| + aType.compareToAscii("DOMCharacterDataModified") == 0) + { + Reference< XMutationEvent > aMEvent(aEvent, UNO_QUERY); + // dispatch a mutation event + // we need to clone the event in order to have complete control + // over the implementation + CMutationEvent* pMEvent = new CMutationEvent; + pMEvent->initMutationEvent( + aType, aMEvent->getBubbles(), aMEvent->getCancelable(), + aMEvent->getRelatedNode(), aMEvent->getPrevValue(), + aMEvent->getNewValue(), aMEvent->getAttrName(), + aMEvent->getAttrChange()); + pEvent = pMEvent; + } else if ( // UIEvent + aType.compareToAscii("DOMFocusIn") == 0|| + aType.compareToAscii("DOMFocusOut") == 0|| + aType.compareToAscii("DOMActivate") == 0) + { + Reference< XUIEvent > aUIEvent(aEvent, UNO_QUERY); + CUIEvent* pUIEvent = new CUIEvent; + pUIEvent->initUIEvent(aType, + aUIEvent->getBubbles(), aUIEvent->getCancelable(), + aUIEvent->getView(), aUIEvent->getDetail()); + pEvent = pUIEvent; + } else if ( // MouseEvent + aType.compareToAscii("click") == 0|| + aType.compareToAscii("mousedown") == 0|| + aType.compareToAscii("mouseup") == 0|| + aType.compareToAscii("mouseover") == 0|| + aType.compareToAscii("mousemove") == 0|| + aType.compareToAscii("mouseout") == 0) + { + Reference< XMouseEvent > aMouseEvent(aEvent, UNO_QUERY); + CMouseEvent *pMouseEvent = new CMouseEvent; + pMouseEvent->initMouseEvent(aType, + aMouseEvent->getBubbles(), aMouseEvent->getCancelable(), + aMouseEvent->getView(), aMouseEvent->getDetail(), + aMouseEvent->getScreenX(), aMouseEvent->getScreenY(), + aMouseEvent->getClientX(), aMouseEvent->getClientY(), + aMouseEvent->getCtrlKey(), aMouseEvent->getAltKey(), + aMouseEvent->getShiftKey(), aMouseEvent->getMetaKey(), + aMouseEvent->getButton(), aMouseEvent->getRelatedTarget()); + pEvent = pMouseEvent; + } + else // generic event + { + pEvent = new CEvent; + pEvent->initEvent( + aType, aEvent->getBubbles(), aEvent->getCancelable()); + } + pEvent->m_target = Reference< XEventTarget >(DOM::CNode::get(aNodePtr)); + pEvent->m_currentTarget = aEvent->getCurrentTarget(); + pEvent->m_time = aEvent->getTimeStamp(); + + // create the reference to the provate event implementation + // that will be dispatched to the listeners + xEvent = Reference< XEvent >(pEvent); + + // build the path from target node to the root + NodeVector captureVector; + xmlNodePtr cur = DOM::CNode::getNodePtr(Reference< XNode >(xEvent->getTarget(), UNO_QUERY_THROW)); + while (cur != NULL) + { + captureVector.push_back(cur); + cur = cur->parent; + } + + // the caputre vector now holds the node path from target to root + // first we must search for capture listernes in order root to + // to target. after that, any target listeners have to be called + // then bubbeling phase listeners are called in target to root + // order + NodeVector::const_iterator inode; + + // start at the root + inode = captureVector.end(); + inode--; + if (inode != captureVector.end()) + { + // capturing phase: + pEvent->m_phase = PhaseType_CAPTURING_PHASE; + while (inode != captureVector.begin()) + { + //pEvent->m_currentTarget = *inode; + pEvent->m_currentTarget = Reference< XEventTarget >(DOM::CNode::get(*inode)); + callListeners(*inode, aType, xEvent, sal_True); + if (pEvent->m_canceled) return sal_True; + inode--; + } + + // target phase + pEvent->m_phase = PhaseType_AT_TARGET; + callListeners(*inode, aType, xEvent, sal_False); + if (pEvent->m_canceled) return sal_True; + // bubbeling phase + inode++; + if (aEvent->getBubbles()) { + pEvent->m_phase = PhaseType_BUBBLING_PHASE; + while (inode != captureVector.end()) + { + pEvent->m_currentTarget = Reference< XEventTarget >(DOM::CNode::get(*inode)); + callListeners(*inode, aType, xEvent, sal_False); + if (pEvent->m_canceled) return sal_True; + inode++; + } + } + } + return sal_True; + } +}} diff --git a/unoxml/source/events/eventdispatcher.hxx b/unoxml/source/events/eventdispatcher.hxx new file mode 100644 index 000000000000..2af5884842f6 --- /dev/null +++ b/unoxml/source/events/eventdispatcher.hxx @@ -0,0 +1,51 @@ + +//#include <multimap> +#include <map> +#include <vector> + +#include <com/sun/star/uno/Reference.h> +#include <com/sun/star/xml/dom/events/EventType.hpp> +#include <com/sun/star/xml/dom/events/PhaseType.hpp> +#include <com/sun/star/xml/dom/events/XEvent.hpp> +#include "event.hxx" + +using namespace com::sun::star::uno; +using namespace com::sun::star::xml::dom; +using namespace com::sun::star::xml::dom::events; + +namespace DOM { namespace events +{ + +typedef std::vector< xmlNodePtr > NodeVector; +typedef std::multimap< xmlNodePtr, Reference< com::sun::star::xml::dom::events::XEventListener> > ListenerMap; +typedef std::map<OUString, ListenerMap*> TypeListenerMap; +typedef std::vector<ListenerMap::value_type> ListenerPairVector; + +class CEventDispatcher +{ +private: + static TypeListenerMap captureListeners; + static TypeListenerMap targetListeners; + +public: + static sal_Bool dispatchEvent(xmlNodePtr aNode, const Reference< XEvent >& aEvent); + + static void addListener( + xmlNodePtr pNode, + OUString aType, + const Reference<com::sun::star::xml::dom::events::XEventListener>& aListener, + sal_Bool bCapture); + + static void removeListener( + xmlNodePtr pNode, + OUString aType, + const Reference<com::sun::star::xml::dom::events::XEventListener>& aListener, + sal_Bool bCapture); + + static void callListeners( + xmlNodePtr pNode, + OUString aType, + const Reference< XEvent >& xEvent, + sal_Bool bCapture); +}; +}} diff --git a/unoxml/source/events/makefile.mk b/unoxml/source/events/makefile.mk new file mode 100644 index 000000000000..2f407031ec9b --- /dev/null +++ b/unoxml/source/events/makefile.mk @@ -0,0 +1,60 @@ +#************************************************************************* +# +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. +# +# Copyright 2008 by Sun Microsystems, Inc. +# +# OpenOffice.org - a multi-platform office productivity suite +# +# $RCSfile: makefile.mk,v $ +# +# $Revision: 1.6 $ +# +# This file is part of OpenOffice.org. +# +# OpenOffice.org is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License version 3 +# only, as published by the Free Software Foundation. +# +# OpenOffice.org is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License version 3 for more details +# (a copy is included in the LICENSE file that accompanied this code). +# +# You should have received a copy of the GNU Lesser General Public License +# version 3 along with OpenOffice.org. If not, see +# <http://www.openoffice.org/license.html> +# for a copy of the LGPLv3 License. +# +#************************************************************************* + +PRJ=../.. + +PRJNAME=unoxml +TARGET=eventsimpl +ENABLE_EXCEPTIONS=TRUE + +# --- Settings ----------------------------------------------------- + +.INCLUDE : settings.mk + +.IF "$(SYSTEM_LIBXML)" == "YES" +CFLAGS+=-DSYSTEM_LIBXML $(LIBXML_CFLAGS) +.ENDIF + +# --- Files -------------------------------------------------------- + +SLOFILES =\ + $(SLO)$/event.obj \ + $(SLO)$/eventdispatcher.obj \ + $(SLO)$/mutationevent.obj \ + $(SLO)$/uievent.obj \ + $(SLO)$/mouseevent.obj \ + $(SLO)$/testlistener.obj + +# --- Targets ------------------------------------------------------ + +.INCLUDE : target.mk + + diff --git a/unoxml/source/events/mouseevent.cxx b/unoxml/source/events/mouseevent.cxx new file mode 100644 index 000000000000..362a1149ba72 --- /dev/null +++ b/unoxml/source/events/mouseevent.cxx @@ -0,0 +1,149 @@ +#include "mouseevent.hxx" + +namespace DOM { namespace events +{ + + sal_Int32 SAL_CALL CMouseEvent::getScreenX() throw (RuntimeException) + { + return m_screenX; + } + sal_Int32 SAL_CALL CMouseEvent::getScreenY() throw (RuntimeException) + { + return m_screenY; + } + sal_Int32 SAL_CALL CMouseEvent::getClientX() throw (RuntimeException) + { + return m_clientX; + } + sal_Int32 SAL_CALL CMouseEvent::getClientY() throw (RuntimeException) + { + return m_clientY; + } + sal_Bool SAL_CALL CMouseEvent::getCtrlKey() throw (RuntimeException) + { + return m_ctrlKey; + } + sal_Bool SAL_CALL CMouseEvent::getShiftKey() throw (RuntimeException) + { + return m_shiftKey; + } + sal_Bool SAL_CALL CMouseEvent::getAltKey() throw (RuntimeException) + { + return m_altKey; + } + sal_Bool SAL_CALL CMouseEvent::getMetaKey() throw (RuntimeException) + { + return m_metaKey; + } + sal_Int16 SAL_CALL CMouseEvent::getButton() throw (RuntimeException) + { + return m_button; + } + Reference< XEventTarget > SAL_CALL CMouseEvent::getRelatedTarget() throw(RuntimeException) + { + return m_relatedTarget; + } + + void SAL_CALL CMouseEvent::initMouseEvent( + const OUString& typeArg, + sal_Bool canBubbleArg, + sal_Bool cancelableArg, + const Reference< XAbstractView >& viewArg, + sal_Int32 detailArg, + sal_Int32 screenXArg, + sal_Int32 screenYArg, + sal_Int32 clientXArg, + sal_Int32 clientYArg, + sal_Bool ctrlKeyArg, + sal_Bool altKeyArg, + sal_Bool shiftKeyArg, + sal_Bool metaKeyArg, + sal_Int16 buttonArg, + const Reference< XEventTarget >& /*relatedTargetArg*/) + throw(RuntimeException) + { + CUIEvent::initUIEvent(typeArg, canBubbleArg, cancelableArg, viewArg, detailArg); + m_screenX = screenXArg; + m_screenY = screenYArg; + m_clientX = clientXArg; + m_clientY = clientYArg; + m_ctrlKey = ctrlKeyArg; + m_altKey = altKeyArg; + m_shiftKey = shiftKeyArg; + m_metaKey = metaKeyArg; + m_button = buttonArg; + } + + // delegate to CUIEvent, since we are inheriting from CUIEvent and XUIEvent + Reference< XAbstractView > SAL_CALL CMouseEvent::getView() throw(RuntimeException) + { + return CUIEvent::getView(); + } + + sal_Int32 SAL_CALL CMouseEvent::getDetail() throw(RuntimeException) + { + return CUIEvent::getDetail(); + } + + void SAL_CALL CMouseEvent::initUIEvent(const OUString& typeArg, + sal_Bool canBubbleArg, + sal_Bool cancelableArg, + const Reference< XAbstractView >& viewArg, + sal_Int32 detailArg) throw(RuntimeException) + { + CUIEvent::initUIEvent(typeArg, canBubbleArg, cancelableArg, viewArg, detailArg); + } + + OUString SAL_CALL CMouseEvent::getType() throw (RuntimeException) + { + return CUIEvent::getType(); + } + + Reference< XEventTarget > SAL_CALL CMouseEvent::getTarget() throw (RuntimeException) + { + return CUIEvent::getTarget(); + } + + Reference< XEventTarget > SAL_CALL CMouseEvent::getCurrentTarget() throw (RuntimeException) + { + return CUIEvent::getCurrentTarget(); + } + + PhaseType SAL_CALL CMouseEvent::getEventPhase() throw (RuntimeException) + { + return CUIEvent::getEventPhase(); + } + + sal_Bool SAL_CALL CMouseEvent::getBubbles() throw (RuntimeException) + { + return CEvent::getBubbles(); + } + + sal_Bool SAL_CALL CMouseEvent::getCancelable() throw (RuntimeException) + { + return CUIEvent::getCancelable(); + } + + com::sun::star::util::Time SAL_CALL CMouseEvent::getTimeStamp() throw (RuntimeException) + { + return CUIEvent::getTimeStamp(); + } + + void SAL_CALL CMouseEvent::stopPropagation() throw (RuntimeException) + { + CUIEvent::stopPropagation(); + } + + void SAL_CALL CMouseEvent::preventDefault() throw (RuntimeException) + { + CUIEvent::preventDefault(); + } + + void SAL_CALL CMouseEvent::initEvent(const OUString& eventTypeArg, sal_Bool canBubbleArg, + sal_Bool cancelableArg) throw (RuntimeException) + { + // base initializer + CUIEvent::initEvent(eventTypeArg, canBubbleArg, cancelableArg); + } +}} + diff --git a/unoxml/source/events/mouseevent.hxx b/unoxml/source/events/mouseevent.hxx new file mode 100644 index 000000000000..d70f1c1ac605 --- /dev/null +++ b/unoxml/source/events/mouseevent.hxx @@ -0,0 +1,92 @@ +#ifndef __MOUSEEVENT_HXX +#define __MOUSEEVENT_HXX + +#include <sal/types.h> +#include <cppuhelper/implbase1.hxx> +#include <cppuhelper/implbase2.hxx> +#include <cppuhelper/implbase3.hxx> +#include <com/sun/star/uno/Reference.h> +#include <com/sun/star/xml/dom/events/EventType.hpp> +#include <com/sun/star/xml/dom/events/PhaseType.hpp> +#include <com/sun/star/xml/dom/events/AttrChangeType.hpp> +#include <com/sun/star/xml/dom/events/XEvent.hpp> +#include <com/sun/star/xml/dom/events/XUIEvent.hpp> +#include <com/sun/star/xml/dom/events/XMouseEvent.hpp> +#include "event.hxx" +#include "uievent.hxx" + +using ::rtl::OUString; + +namespace DOM { namespace events { + +class CMouseEvent : public cppu::ImplInheritanceHelper1< CUIEvent, XMouseEvent > +{ + friend class CEventDispatcher; +protected: + sal_Int32 m_screenX; + sal_Int32 m_screenY; + sal_Int32 m_clientX; + sal_Int32 m_clientY; + sal_Bool m_ctrlKey; + sal_Bool m_shiftKey; + sal_Bool m_altKey; + sal_Bool m_metaKey; + sal_Int16 m_button; + Reference< XEventTarget > m_relatedTarget; + +public: + + virtual sal_Int32 SAL_CALL getScreenX() throw (RuntimeException); + virtual sal_Int32 SAL_CALL getScreenY() throw (RuntimeException); + virtual sal_Int32 SAL_CALL getClientX() throw (RuntimeException); + virtual sal_Int32 SAL_CALL getClientY() throw (RuntimeException); + virtual sal_Bool SAL_CALL getCtrlKey() throw (RuntimeException); + virtual sal_Bool SAL_CALL getShiftKey() throw (RuntimeException); + virtual sal_Bool SAL_CALL getAltKey() throw (RuntimeException); + virtual sal_Bool SAL_CALL getMetaKey() throw (RuntimeException); + virtual sal_Int16 SAL_CALL getButton() throw (RuntimeException); + virtual Reference< XEventTarget > SAL_CALL getRelatedTarget() throw(RuntimeException); + + virtual void SAL_CALL initMouseEvent( + const OUString& typeArg, + sal_Bool canBubbleArg, + sal_Bool cancelableArg, + const Reference< XAbstractView >& viewArg, + sal_Int32 detailArg, + sal_Int32 screenXArg, + sal_Int32 screenYArg, + sal_Int32 clientXArg, + sal_Int32 clientYArg, + sal_Bool ctrlKeyArg, + sal_Bool altKeyArg, + sal_Bool shiftKeyArg, + sal_Bool metaKeyArg, + sal_Int16 buttonArg, + const Reference< XEventTarget >& relatedTargetArg) + throw(RuntimeException); + + // delegate to CUIevent + virtual Reference< XAbstractView > SAL_CALL getView() throw (RuntimeException); + virtual sal_Int32 SAL_CALL getDetail() throw (RuntimeException); + virtual void SAL_CALL initUIEvent(const OUString& typeArg, + sal_Bool canBubbleArg, + sal_Bool cancelableArg, + const Reference< XAbstractView >& viewArg, + sal_Int32 detailArg) throw (RuntimeException); + virtual OUString SAL_CALL getType() throw (RuntimeException); + virtual Reference< XEventTarget > SAL_CALL getTarget() throw (RuntimeException); + virtual Reference< XEventTarget > SAL_CALL getCurrentTarget() throw (RuntimeException); + virtual PhaseType SAL_CALL getEventPhase() throw (RuntimeException); + virtual sal_Bool SAL_CALL getBubbles() throw (RuntimeException); + virtual sal_Bool SAL_CALL getCancelable() throw (RuntimeException); + virtual com::sun::star::util::Time SAL_CALL getTimeStamp() throw (RuntimeException); + virtual void SAL_CALL stopPropagation() throw (RuntimeException); + virtual void SAL_CALL preventDefault() throw (RuntimeException); + virtual void SAL_CALL initEvent( + const OUString& eventTypeArg, + sal_Bool canBubbleArg, + sal_Bool cancelableArg) + throw (RuntimeException); +}; +}} +#endif diff --git a/unoxml/source/events/mutationevent.cxx b/unoxml/source/events/mutationevent.cxx new file mode 100644 index 000000000000..bc06cb2af61a --- /dev/null +++ b/unoxml/source/events/mutationevent.cxx @@ -0,0 +1,100 @@ +#include "mutationevent.hxx" + +namespace DOM { namespace events +{ + CMutationEvent::~CMutationEvent() + { + } + + Reference< XNode > SAL_CALL CMutationEvent::getRelatedNode() throw (RuntimeException) + { + return m_relatedNode; + } + + OUString SAL_CALL CMutationEvent::getPrevValue() throw (RuntimeException) + { + return m_prevValue; + } + + OUString SAL_CALL CMutationEvent::getNewValue() throw (RuntimeException) + { + return m_newValue; + } + + OUString SAL_CALL CMutationEvent::getAttrName() throw (RuntimeException) + { + return m_attrName; + } + + AttrChangeType SAL_CALL CMutationEvent::getAttrChange() throw (RuntimeException) + { + return m_attrChangeType; + } + + void SAL_CALL CMutationEvent::initMutationEvent(const OUString& typeArg, + sal_Bool canBubbleArg, sal_Bool cancelableArg, + const Reference< XNode >& relatedNodeArg, const OUString& prevValueArg, + const OUString& newValueArg, const OUString& attrNameArg, + AttrChangeType attrChangeArg) throw (RuntimeException) + { + initEvent(typeArg, canBubbleArg, cancelableArg); + m_relatedNode = relatedNodeArg; + m_prevValue = prevValueArg; + m_newValue = newValueArg; + m_attrName = attrNameArg; + m_attrChangeType = attrChangeArg; + } + + // delegate to CEvent, since we are inheriting from CEvent and XEvent + OUString SAL_CALL CMutationEvent::getType() throw (RuntimeException) + { + return CEvent::getType(); + } + + Reference< XEventTarget > SAL_CALL CMutationEvent::getTarget() throw (RuntimeException) + { + return CEvent::getTarget(); + } + + Reference< XEventTarget > SAL_CALL CMutationEvent::getCurrentTarget() throw (RuntimeException) + { + return CEvent::getCurrentTarget(); + } + + PhaseType SAL_CALL CMutationEvent::getEventPhase() throw (RuntimeException) + { + return CEvent::getEventPhase(); + } + + sal_Bool SAL_CALL CMutationEvent::getBubbles() throw (RuntimeException) + { + return CEvent::getBubbles(); + } + + sal_Bool SAL_CALL CMutationEvent::getCancelable() throw (RuntimeException) + { + return CEvent::getCancelable(); + } + + com::sun::star::util::Time SAL_CALL CMutationEvent::getTimeStamp() throw (RuntimeException) + { + return CEvent::getTimeStamp(); + } + + void SAL_CALL CMutationEvent::stopPropagation() throw (RuntimeException) + { + CEvent::stopPropagation(); + } + void SAL_CALL CMutationEvent::preventDefault() throw (RuntimeException) + { + CEvent::preventDefault(); + } + + void SAL_CALL CMutationEvent::initEvent(const OUString& eventTypeArg, sal_Bool canBubbleArg, + sal_Bool cancelableArg) throw (RuntimeException) + { + // base initializer + CEvent::initEvent(eventTypeArg, canBubbleArg, cancelableArg); + } +}} + diff --git a/unoxml/source/events/mutationevent.hxx b/unoxml/source/events/mutationevent.hxx new file mode 100644 index 000000000000..e28613ad5df9 --- /dev/null +++ b/unoxml/source/events/mutationevent.hxx @@ -0,0 +1,66 @@ +#ifndef __MUTATIONEVENT_HXX +#define __MUTATIONEVENT_HXX + +#include <sal/types.h> +#include <cppuhelper/implbase1.hxx> +#include <cppuhelper/implbase2.hxx> +#include <cppuhelper/implbase3.hxx> +#include <com/sun/star/uno/Reference.h> +#include <com/sun/star/xml/dom/events/EventType.hpp> +#include <com/sun/star/xml/dom/events/PhaseType.hpp> +#include <com/sun/star/xml/dom/events/AttrChangeType.hpp> +#include <com/sun/star/xml/dom/events/XEvent.hpp> +#include <com/sun/star/xml/dom/events/XMutationEvent.hpp> +#include "event.hxx" + +using ::rtl::OUString; + +namespace DOM { namespace events { + +class CMutationEvent : public cppu::ImplInheritanceHelper1< CEvent, XMutationEvent > +{ + friend class CEventDispatcher; +protected: + Reference< XNode > m_relatedNode; + OUString m_prevValue; + OUString m_newValue; + OUString m_attrName; + AttrChangeType m_attrChangeType; + +public: + + virtual ~CMutationEvent(); + + virtual Reference< XNode > SAL_CALL getRelatedNode() throw (RuntimeException); + virtual OUString SAL_CALL getPrevValue() throw (RuntimeException); + virtual OUString SAL_CALL getNewValue() throw (RuntimeException); + virtual OUString SAL_CALL getAttrName() throw (RuntimeException); + virtual AttrChangeType SAL_CALL getAttrChange() throw (RuntimeException); + virtual void SAL_CALL initMutationEvent( + const OUString& typeArg, + sal_Bool canBubbleArg, + sal_Bool cancelableArg, + const Reference< XNode >& relatedNodeArg, + const OUString& prevValueArg, + const OUString& newValueArg, + const OUString& attrNameArg, + AttrChangeType attrChangeArg) throw (RuntimeException); + + // delegate to CEvent, since we are inheriting from CEvent and XEvent + virtual OUString SAL_CALL getType() throw (RuntimeException); + virtual Reference< XEventTarget > SAL_CALL getTarget() throw (RuntimeException); + virtual Reference< XEventTarget > SAL_CALL getCurrentTarget() throw (RuntimeException); + virtual PhaseType SAL_CALL getEventPhase() throw (RuntimeException); + virtual sal_Bool SAL_CALL getBubbles() throw (RuntimeException); + virtual sal_Bool SAL_CALL getCancelable() throw (RuntimeException); + virtual com::sun::star::util::Time SAL_CALL getTimeStamp() throw (RuntimeException); + virtual void SAL_CALL stopPropagation() throw (RuntimeException); + virtual void SAL_CALL preventDefault() throw (RuntimeException); + virtual void SAL_CALL initEvent( + const OUString& eventTypeArg, + sal_Bool canBubbleArg, + sal_Bool cancelableArg) + throw (RuntimeException); +}; +}} +#endif diff --git a/unoxml/source/events/testlistener.cxx b/unoxml/source/events/testlistener.cxx new file mode 100644 index 000000000000..ccd930de5824 --- /dev/null +++ b/unoxml/source/events/testlistener.cxx @@ -0,0 +1,140 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2008 by Sun Microsystems, Inc. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: testlistener.cxx,v $ + * $Revision: 1.5 $ + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ +#include <stdio.h> + +#include <com/sun/star/lang/IllegalArgumentException.hpp> + +#include "testlistener.hxx" + +#define U2S(s) OUStringToOString(s, RTL_TEXTENCODING_UTF8).getStr() + + +namespace DOM { namespace events +{ + + Reference< XInterface > CTestListener::_getInstance(const Reference< XMultiServiceFactory >& rSMgr) + { + // XXX + // return static_cast< XXPathAPI* >(new CTestListener()); + return Reference< XInterface >(static_cast<XEventListener*>(new CTestListener(rSMgr))); + } + + const char* CTestListener::aImplementationName = "com.sun.star.comp.xml.dom.events.TestListener"; + const char* CTestListener::aSupportedServiceNames[] = { + "com.sun.star.comp.xml.dom.events.TestListener", + NULL + }; + + OUString CTestListener::_getImplementationName() + { + return OUString::createFromAscii(aImplementationName); + } + Sequence<OUString> CTestListener::_getSupportedServiceNames() + { + Sequence<OUString> aSequence; + for (int i=0; aSupportedServiceNames[i]!=NULL; i++) { + aSequence.realloc(i+1); + aSequence[i]=(OUString::createFromAscii(aSupportedServiceNames[i])); + } + return aSequence; + } + + Sequence< OUString > SAL_CALL CTestListener::getSupportedServiceNames() + throw (RuntimeException) + { + return CTestListener::_getSupportedServiceNames(); + } + + OUString SAL_CALL CTestListener::getImplementationName() + throw (RuntimeException) + { + return CTestListener::_getImplementationName(); + } + + sal_Bool SAL_CALL CTestListener::supportsService(const OUString& aServiceName) + throw (RuntimeException) + { + Sequence< OUString > supported = CTestListener::_getSupportedServiceNames(); + for (sal_Int32 i=0; i<supported.getLength(); i++) + { + if (supported[i] == aServiceName) return sal_True; + } + return sal_False; + } + + // --- XInitialize + + void SAL_CALL CTestListener::initialize(const Sequence< Any >& args) throw(RuntimeException) + { + if (args.getLength() < 3) throw IllegalArgumentException( + OUString::createFromAscii("Wrong number of arguments"), Reference< XInterface >(), 0); + + Reference <XEventTarget> aTarget; + if(! (args[0] >>= aTarget)) throw IllegalArgumentException( + OUString::createFromAscii("Illegal argument 1"), Reference< XInterface >(), 1); + + OUString aType; + if (! (args[1] >>= aType)) + throw IllegalArgumentException(OUString::createFromAscii("Illegal argument 2"), Reference< XInterface >(), 2); + + sal_Bool bCapture = sal_False; + if(! (args[2] >>= bCapture)) throw IllegalArgumentException( + OUString::createFromAscii("Illegal argument 3"), Reference< XInterface >(), 3); + + if(! (args[3] >>= m_name)) m_name = OUString::createFromAscii("<unnamed listener>"); + + m_target = aTarget; + m_type = aType; + m_capture = bCapture; + + m_target->addEventListener(m_type, Reference< XEventListener >(this), m_capture); + + + } + + CTestListener::~CTestListener() + { + fprintf(stderr, "CTestListener::~CTestListener()\n"); + if( m_target.is()) + m_target->removeEventListener(m_type, Reference< XEventListener >(this), m_capture); + } + + // --- XEventListener + + void SAL_CALL CTestListener::handleEvent(const Reference< XEvent >& evt) throw (RuntimeException) + { + FILE* f = fopen("C:\\listener.out", "a"); + fprintf(f, "CTestListener::handleEvent in %s\n", U2S(m_name)); + fprintf(f, " type: %s\n\n", OUStringToOString(evt->getType(), RTL_TEXTENCODING_ASCII_US).getStr()); + fclose(f); + + } + +}} diff --git a/unoxml/source/events/testlistener.hxx b/unoxml/source/events/testlistener.hxx new file mode 100644 index 000000000000..7a9808e25235 --- /dev/null +++ b/unoxml/source/events/testlistener.hxx @@ -0,0 +1,109 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2008 by Sun Microsystems, Inc. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: testlistener.hxx,v $ + * $Revision: 1.5 $ + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#ifndef _TESTLISTENER_HXX +#define _TESTLISTENER_HXX + +#include <map> + +#include <sal/types.h> +#include <cppuhelper/implbase3.hxx> +#include <com/sun/star/uno/Reference.h> +#include <com/sun/star/uno/Sequence.h> + +#include <com/sun/star/uno/XInterface.hpp> +#include <com/sun/star/uno/Exception.hpp> +#include <com/sun/star/xml/dom/XNode.hpp> +#include <com/sun/star/lang/XServiceInfo.hpp> +#include <com/sun/star/lang/XInitialization.hpp> +#include <com/sun/star/lang/XSingleServiceFactory.hpp> +#include <com/sun/star/lang/XMultiServiceFactory.hpp> +#include <com/sun/star/lang/XUnoTunnel.hpp> +#include <com/sun/star/xml/dom/events/XEventTarget.hpp> +#include <com/sun/star/xml/dom/events/XEventListener.hpp> +#include <com/sun/star/xml/dom/events/XEvent.hpp> +#include <com/sun/star/xml/dom/events/EventType.hpp> +#include <com/sun/star/xml/dom/events/XMutationEvent.hpp> + +#include "libxml/tree.h" + +using ::rtl::OUString; +using namespace com::sun::star::uno; +using namespace com::sun::star::lang; +using namespace com::sun::star::xml::dom; +using namespace com::sun::star::xml::dom::events; + +namespace DOM { namespace events +{ + + class CTestListener + : public ::cppu::WeakImplHelper3< com::sun::star::xml::dom::events::XEventListener, XInitialization, XServiceInfo > + { + + private: + Reference< XMultiServiceFactory > m_factory; + Reference <XEventTarget> m_target; + OUString m_type; + sal_Bool m_capture; + OUString m_name; + + public: + + // static helpers for service info and component management + static const char* aImplementationName; + static const char* aSupportedServiceNames[]; + static OUString _getImplementationName(); + static Sequence< OUString > _getSupportedServiceNames(); + static Reference< XInterface > _getInstance(const Reference< XMultiServiceFactory >& rSMgr); + + CTestListener(const Reference< XMultiServiceFactory >& rSMgr) + : m_factory(rSMgr){}; + + virtual ~CTestListener(); + + // XServiceInfo + virtual OUString SAL_CALL getImplementationName() + throw (RuntimeException); + virtual sal_Bool SAL_CALL supportsService(const OUString& ServiceName) + throw (RuntimeException); + virtual Sequence< OUString > SAL_CALL getSupportedServiceNames () + throw (RuntimeException); + + + // XEventListener + virtual void SAL_CALL initialize(const Sequence< Any >& args) throw (RuntimeException); + + virtual void SAL_CALL handleEvent(const Reference< XEvent >& evt) throw (RuntimeException); + + + }; +}} + +#endif diff --git a/unoxml/source/events/uievent.cxx b/unoxml/source/events/uievent.cxx new file mode 100644 index 000000000000..29d2e10e57dc --- /dev/null +++ b/unoxml/source/events/uievent.cxx @@ -0,0 +1,81 @@ +#include "event.hxx" +#include "uievent.hxx" + +namespace DOM { namespace events +{ + + Reference< XAbstractView > SAL_CALL CUIEvent::getView() throw(RuntimeException) + { + return m_view; + } + + sal_Int32 SAL_CALL CUIEvent::getDetail() throw(RuntimeException) + { + return m_detail; + } + + void SAL_CALL CUIEvent::initUIEvent(const OUString& typeArg, + sal_Bool canBubbleArg, + sal_Bool cancelableArg, + const Reference< XAbstractView >& viewArg, + sal_Int32 detailArg) throw(RuntimeException) + { + initEvent(typeArg, canBubbleArg, cancelableArg); + m_view = viewArg; + m_detail = detailArg; + } + + + // delegate to CEvent, since we are inheriting from CEvent and XEvent + OUString SAL_CALL CUIEvent::getType() throw (RuntimeException) + { + return CEvent::getType(); + } + + Reference< XEventTarget > SAL_CALL CUIEvent::getTarget() throw (RuntimeException) + { + return CEvent::getTarget(); + } + + Reference< XEventTarget > SAL_CALL CUIEvent::getCurrentTarget() throw (RuntimeException) + { + return CEvent::getCurrentTarget(); + } + + PhaseType SAL_CALL CUIEvent::getEventPhase() throw (RuntimeException) + { + return CEvent::getEventPhase(); + } + + sal_Bool SAL_CALL CUIEvent::getBubbles() throw (RuntimeException) + { + return CEvent::getBubbles(); + } + + sal_Bool SAL_CALL CUIEvent::getCancelable() throw (RuntimeException) + { + // mutation events cannot be canceled + return sal_False; + } + + com::sun::star::util::Time SAL_CALL CUIEvent::getTimeStamp() throw (RuntimeException) + { + return CEvent::getTimeStamp(); + } + + void SAL_CALL CUIEvent::stopPropagation() throw (RuntimeException) + { + CEvent::stopPropagation(); + } + void SAL_CALL CUIEvent::preventDefault() throw (RuntimeException) + { + CEvent::preventDefault(); + } + + void SAL_CALL CUIEvent::initEvent(const OUString& eventTypeArg, sal_Bool canBubbleArg, + sal_Bool cancelableArg) throw (RuntimeException) + { + // base initializer + CEvent::initEvent(eventTypeArg, canBubbleArg, cancelableArg); + } +}} diff --git a/unoxml/source/events/uievent.hxx b/unoxml/source/events/uievent.hxx new file mode 100644 index 000000000000..f7c5806e48e0 --- /dev/null +++ b/unoxml/source/events/uievent.hxx @@ -0,0 +1,55 @@ +#ifndef __UIEVENT_HXX +#define __UIEVENT_HXX + +#include <sal/types.h> +#include <cppuhelper/implbase1.hxx> +#include <cppuhelper/implbase2.hxx> +#include <cppuhelper/implbase3.hxx> +#include <com/sun/star/uno/Reference.h> +#include <com/sun/star/xml/dom/events/EventType.hpp> +#include <com/sun/star/xml/dom/events/PhaseType.hpp> +#include <com/sun/star/xml/dom/events/AttrChangeType.hpp> +#include <com/sun/star/xml/dom/events/XEvent.hpp> +#include <com/sun/star/xml/dom/events/XUIEvent.hpp> +#include <com/sun/star/xml/dom/views/XAbstractView.hpp> +#include "event.hxx" + +using ::rtl::OUString; +using namespace com::sun::star::xml::dom::views; + +namespace DOM { namespace events { + +class CUIEvent : public cppu::ImplInheritanceHelper1< CEvent, XUIEvent > +{ + friend class CEventDispatcher; +protected: + sal_Int32 m_detail; + Reference< XAbstractView > m_view; + +public: + virtual Reference< XAbstractView > SAL_CALL getView() throw(RuntimeException); + virtual sal_Int32 SAL_CALL getDetail() throw(RuntimeException); + virtual void SAL_CALL initUIEvent(const OUString& typeArg, + sal_Bool canBubbleArg, + sal_Bool cancelableArg, + const Reference< XAbstractView >& viewArg, + sal_Int32 detailArg) throw(RuntimeException); + + // delegate to CEvent, since we are inheriting from CEvent and XEvent + virtual OUString SAL_CALL getType() throw (RuntimeException); + virtual Reference< XEventTarget > SAL_CALL getTarget() throw (RuntimeException); + virtual Reference< XEventTarget > SAL_CALL getCurrentTarget() throw (RuntimeException); + virtual PhaseType SAL_CALL getEventPhase() throw (RuntimeException); + virtual sal_Bool SAL_CALL getBubbles() throw (RuntimeException); + virtual sal_Bool SAL_CALL getCancelable() throw (RuntimeException); + virtual com::sun::star::util::Time SAL_CALL getTimeStamp() throw (RuntimeException); + virtual void SAL_CALL stopPropagation() throw (RuntimeException); + virtual void SAL_CALL preventDefault() throw (RuntimeException); + virtual void SAL_CALL initEvent( + const OUString& eventTypeArg, + sal_Bool canBubbleArg, + sal_Bool cancelableArg) + throw (RuntimeException); +}; +}} +#endif diff --git a/unoxml/source/rdf/CBlankNode.cxx b/unoxml/source/rdf/CBlankNode.cxx new file mode 100644 index 000000000000..213a8099da35 --- /dev/null +++ b/unoxml/source/rdf/CBlankNode.cxx @@ -0,0 +1,161 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2008 by Sun Microsystems, Inc. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: CBlankNode.cxx,v $ + * $Revision: 1.2 $ + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#include "CNodes.hxx" + +#include <cppuhelper/implbase3.hxx> +#include <com/sun/star/lang/XServiceInfo.hpp> +#include <com/sun/star/lang/XInitialization.hpp> +#include <com/sun/star/rdf/XBlankNode.hpp> + +#include <com/sun/star/lang/IllegalArgumentException.hpp> + + +/// anonymous implementation namespace +namespace { + +namespace css = ::com::sun::star; + +class CBlankNode: + public ::cppu::WeakImplHelper3< + css::lang::XServiceInfo, + css::lang::XInitialization, + css::rdf::XBlankNode> +{ +public: + explicit CBlankNode(css::uno::Reference< css::uno::XComponentContext > const & context); + virtual ~CBlankNode() {} + + // ::com::sun::star::lang::XServiceInfo: + virtual ::rtl::OUString SAL_CALL getImplementationName() throw (css::uno::RuntimeException); + virtual ::sal_Bool SAL_CALL supportsService(const ::rtl::OUString & ServiceName) throw (css::uno::RuntimeException); + virtual css::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames() throw (css::uno::RuntimeException); + + // ::com::sun::star::lang::XInitialization: + virtual void SAL_CALL initialize(const css::uno::Sequence< ::com::sun::star::uno::Any > & aArguments) throw (css::uno::RuntimeException, css::uno::Exception); + + // ::com::sun::star::rdf::XNode: + virtual ::rtl::OUString SAL_CALL getStringValue() throw (css::uno::RuntimeException); + +private: + CBlankNode(const CBlankNode &); // not defined + CBlankNode& operator=(const CBlankNode &); // not defined + + css::uno::Reference< css::uno::XComponentContext > m_xContext; + + ::rtl::OUString m_NodeID; +}; + +CBlankNode::CBlankNode(css::uno::Reference< css::uno::XComponentContext > const & context) : + m_xContext(context), m_NodeID() +{} + +// com.sun.star.uno.XServiceInfo: +::rtl::OUString SAL_CALL CBlankNode::getImplementationName() throw (css::uno::RuntimeException) +{ + return comp_CBlankNode::_getImplementationName(); +} + +::sal_Bool SAL_CALL CBlankNode::supportsService(::rtl::OUString const & serviceName) throw (css::uno::RuntimeException) +{ + css::uno::Sequence< ::rtl::OUString > serviceNames = comp_CBlankNode::_getSupportedServiceNames(); + for (::sal_Int32 i = 0; i < serviceNames.getLength(); ++i) { + if (serviceNames[i] == serviceName) + return sal_True; + } + return sal_False; +} + +css::uno::Sequence< ::rtl::OUString > SAL_CALL CBlankNode::getSupportedServiceNames() throw (css::uno::RuntimeException) +{ + return comp_CBlankNode::_getSupportedServiceNames(); +} + +// ::com::sun::star::lang::XInitialization: +void SAL_CALL CBlankNode::initialize(const css::uno::Sequence< ::com::sun::star::uno::Any > & aArguments) throw (css::uno::RuntimeException, css::uno::Exception) +{ + if (aArguments.getLength() != 1) { + throw css::lang::IllegalArgumentException( + ::rtl::OUString::createFromAscii("CBlankNode::initialize: " + "must give exactly 1 argument"), *this, 1); + } + + ::rtl::OUString arg; + if (!(aArguments[0] >>= arg)) { + throw css::lang::IllegalArgumentException( + ::rtl::OUString::createFromAscii("CBlankNode::initialize: " + "argument must be string"), *this, 0); + } + + //FIXME: what is legal? + if (arg.getLength() > 0) { + m_NodeID = arg; + } else { + throw css::lang::IllegalArgumentException( + ::rtl::OUString::createFromAscii("CBlankNode::initialize: " + "argument is not valid blank node ID"), *this, 0); + } +} + +// ::com::sun::star::rdf::XNode: +::rtl::OUString SAL_CALL CBlankNode::getStringValue() throw (css::uno::RuntimeException) +{ + return m_NodeID; +} + +} // closing anonymous implementation namespace + + + +// component helper namespace +namespace comp_CBlankNode { + +::rtl::OUString SAL_CALL _getImplementationName() { + return ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( + "CBlankNode")); +} + +css::uno::Sequence< ::rtl::OUString > SAL_CALL _getSupportedServiceNames() +{ + css::uno::Sequence< ::rtl::OUString > s(1); + s[0] = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( + "com.sun.star.rdf.BlankNode")); + return s; +} + +css::uno::Reference< css::uno::XInterface > SAL_CALL _create( + const css::uno::Reference< css::uno::XComponentContext > & context) + SAL_THROW((css::uno::Exception)) +{ + return static_cast< ::cppu::OWeakObject * >(new CBlankNode(context)); +} + +} // closing component helper namespace + diff --git a/unoxml/source/rdf/CLiteral.cxx b/unoxml/source/rdf/CLiteral.cxx new file mode 100644 index 000000000000..25a8267f5cc5 --- /dev/null +++ b/unoxml/source/rdf/CLiteral.cxx @@ -0,0 +1,224 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2008 by Sun Microsystems, Inc. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: CLiteral.cxx,v $ + * $Revision: 1.2 $ + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#include "CNodes.hxx" + +#include <cppuhelper/implbase3.hxx> +#include <com/sun/star/lang/XServiceInfo.hpp> +#include <com/sun/star/lang/XInitialization.hpp> +#include <com/sun/star/rdf/XLiteral.hpp> + +#include <com/sun/star/lang/IllegalArgumentException.hpp> + +#include <rtl/ustrbuf.hxx> + + +/// anonymous implementation namespace +namespace { + +namespace css = ::com::sun::star; + +class CLiteral: + public ::cppu::WeakImplHelper3< + css::lang::XServiceInfo, + css::lang::XInitialization, + css::rdf::XLiteral> +{ +public: + explicit CLiteral(css::uno::Reference< css::uno::XComponentContext > const & context); + virtual ~CLiteral() {} + + // ::com::sun::star::lang::XServiceInfo: + virtual ::rtl::OUString SAL_CALL getImplementationName() throw (css::uno::RuntimeException); + virtual ::sal_Bool SAL_CALL supportsService(const ::rtl::OUString & ServiceName) throw (css::uno::RuntimeException); + virtual css::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames() throw (css::uno::RuntimeException); + + // ::com::sun::star::lang::XInitialization: + virtual void SAL_CALL initialize(const css::uno::Sequence< ::com::sun::star::uno::Any > & aArguments) throw (css::uno::RuntimeException, css::uno::Exception); + + // ::com::sun::star::rdf::XNode: + virtual ::rtl::OUString SAL_CALL getStringValue() throw (css::uno::RuntimeException); + + // ::com::sun::star::rdf::XLiteral: + virtual ::rtl::OUString SAL_CALL getValue() throw (css::uno::RuntimeException); + virtual ::rtl::OUString SAL_CALL getLanguage() throw (css::uno::RuntimeException); + virtual css::uno::Reference< css::rdf::XURI > SAL_CALL getDatatype() throw (css::uno::RuntimeException); + +private: + CLiteral(const CLiteral &); // not defined + CLiteral& operator=(const CLiteral &); // not defined + + css::uno::Reference< css::uno::XComponentContext > m_xContext; + + ::rtl::OUString m_Value; + ::rtl::OUString m_Language; + css::uno::Reference< css::rdf::XURI > m_xDatatype; +}; + +CLiteral::CLiteral(css::uno::Reference< css::uno::XComponentContext > const & context) : + m_xContext(context), m_Value(), m_Language(), m_xDatatype() +{} + +// com.sun.star.uno.XServiceInfo: +::rtl::OUString SAL_CALL CLiteral::getImplementationName() throw (css::uno::RuntimeException) +{ + return comp_CLiteral::_getImplementationName(); +} + +::sal_Bool SAL_CALL CLiteral::supportsService(::rtl::OUString const & serviceName) throw (css::uno::RuntimeException) +{ + css::uno::Sequence< ::rtl::OUString > serviceNames = comp_CLiteral::_getSupportedServiceNames(); + for (::sal_Int32 i = 0; i < serviceNames.getLength(); ++i) { + if (serviceNames[i] == serviceName) + return sal_True; + } + return sal_False; +} + +css::uno::Sequence< ::rtl::OUString > SAL_CALL CLiteral::getSupportedServiceNames() throw (css::uno::RuntimeException) +{ + return comp_CLiteral::_getSupportedServiceNames(); +} + +// ::com::sun::star::lang::XInitialization: +void SAL_CALL CLiteral::initialize(const css::uno::Sequence< ::com::sun::star::uno::Any > & aArguments) throw (css::uno::RuntimeException, css::uno::Exception) +{ + const sal_Int32 len( aArguments.getLength() ); + if (len < 1 || len > 2) { + throw css::lang::IllegalArgumentException( + ::rtl::OUString::createFromAscii("CLiteral::initialize: " + "must give 1 or 2 argument(s)"), *this, 2); + } + + ::rtl::OUString arg0; + if (!(aArguments[0] >>= arg0)) { + throw css::lang::IllegalArgumentException( + ::rtl::OUString::createFromAscii("CLiteral::initialize: " + "argument must be string"), *this, 0); + } + //FIXME: what is legal? + if (true) { + m_Value = arg0; + } else { + throw css::lang::IllegalArgumentException( + ::rtl::OUString::createFromAscii("CLiteral::initialize: " + "argument is not valid literal value"), *this, 0); + } + + if (len > 1) { + ::rtl::OUString arg1; + css::uno::Reference< css::rdf::XURI > xURI; + if ((aArguments[1] >>= arg1)) { + if (arg1.getLength() > 0) { + m_Language = arg1; + } else { + throw css::lang::IllegalArgumentException( + ::rtl::OUString::createFromAscii("CLiteral::initialize: " + "argument is not valid language"), *this, 1); + } + } else if ((aArguments[1] >>= xURI)) { + if (xURI.is()) { + m_xDatatype = xURI; + } else { + throw css::lang::IllegalArgumentException( + ::rtl::OUString::createFromAscii("CLiteral::initialize: " + "argument is null"), *this, 1); + } + } else { + throw css::lang::IllegalArgumentException( + ::rtl::OUString::createFromAscii("CLiteral::initialize: " + "argument must be string or URI"), *this, 1); + } + } +} + +// ::com::sun::star::rdf::XNode: +::rtl::OUString SAL_CALL CLiteral::getStringValue() throw (css::uno::RuntimeException) +{ + if (!m_Language.equalsAscii("")) { + ::rtl::OUStringBuffer buf(m_Value); + buf.appendAscii("@"); + buf.append(m_Language); + return buf.makeStringAndClear(); + } else if (m_xDatatype.is()) { + ::rtl::OUStringBuffer buf(m_Value); + buf.appendAscii("^^"); + buf.append(m_xDatatype->getStringValue()); + return buf.makeStringAndClear(); + } else { + return m_Value; + } +} + +// ::com::sun::star::rdf::XLiteral: +::rtl::OUString SAL_CALL CLiteral::getValue() throw (css::uno::RuntimeException) +{ + return m_Value; +} + +::rtl::OUString SAL_CALL CLiteral::getLanguage() throw (css::uno::RuntimeException) +{ + return m_Language; +} + +css::uno::Reference< css::rdf::XURI > SAL_CALL CLiteral::getDatatype() throw (css::uno::RuntimeException) +{ + return m_xDatatype; +} + +} // closing anonymous implementation namespace + + + +// component helper namespace +namespace comp_CLiteral { + +::rtl::OUString SAL_CALL _getImplementationName() { + return ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( + "CLiteral")); +} + +css::uno::Sequence< ::rtl::OUString > SAL_CALL _getSupportedServiceNames() +{ + css::uno::Sequence< ::rtl::OUString > s(1); + s[0] = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( + "com.sun.star.rdf.Literal")); + return s; +} + +css::uno::Reference< css::uno::XInterface > SAL_CALL _create( + const css::uno::Reference< css::uno::XComponentContext > & context) + SAL_THROW((css::uno::Exception)) +{ + return static_cast< ::cppu::OWeakObject * >(new CLiteral(context)); +} + +} // closing component helper namespace + diff --git a/unoxml/source/rdf/CNodes.hxx b/unoxml/source/rdf/CNodes.hxx new file mode 100644 index 000000000000..bd9f210ff37f --- /dev/null +++ b/unoxml/source/rdf/CNodes.hxx @@ -0,0 +1,77 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2008 by Sun Microsystems, Inc. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: CNodes.hxx,v $ + * $Revision: 1.2 $ + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#ifndef _CNODES_HXX +#define _CNODES_HXX + +#include <sal/config.h> +#include <cppuhelper/factory.hxx> + + +// component helper namespace +namespace comp_CBlankNode { + +namespace css = ::com::sun::star; + +// component and service helper functions: +::rtl::OUString SAL_CALL _getImplementationName(); +css::uno::Sequence< ::rtl::OUString > SAL_CALL _getSupportedServiceNames(); +css::uno::Reference< css::uno::XInterface > SAL_CALL _create( css::uno::Reference< css::uno::XComponentContext > const & context ); + +} // closing component helper namespace + + +// component helper namespace +namespace comp_CURI { + +namespace css = ::com::sun::star; + +// component and service helper functions: +::rtl::OUString SAL_CALL _getImplementationName(); +css::uno::Sequence< ::rtl::OUString > SAL_CALL _getSupportedServiceNames(); +css::uno::Reference< css::uno::XInterface > SAL_CALL _create( css::uno::Reference< css::uno::XComponentContext > const & context ); + +} // closing component helper namespace + + +// component helper namespace +namespace comp_CLiteral { + +namespace css = ::com::sun::star; + +// component and service helper functions: +::rtl::OUString SAL_CALL _getImplementationName(); +css::uno::Sequence< ::rtl::OUString > SAL_CALL _getSupportedServiceNames(); +css::uno::Reference< css::uno::XInterface > SAL_CALL _create( css::uno::Reference< css::uno::XComponentContext > const & context ); + +} // closing component helper namespace + +#endif + diff --git a/unoxml/source/rdf/CURI.cxx b/unoxml/source/rdf/CURI.cxx new file mode 100644 index 000000000000..e02fe5e03461 --- /dev/null +++ b/unoxml/source/rdf/CURI.cxx @@ -0,0 +1,870 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2008 by Sun Microsystems, Inc. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: CURI.cxx,v $ + * $Revision: 1.2 $ + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#include "CNodes.hxx" + +#include <cppuhelper/implbase3.hxx> +#include <com/sun/star/lang/XServiceInfo.hpp> +#include <com/sun/star/lang/XInitialization.hpp> +#include <com/sun/star/rdf/XURI.hpp> +#include <com/sun/star/rdf/URIs.hpp> + +#include <com/sun/star/lang/IllegalArgumentException.hpp> + + +/// anonymous implementation namespace +namespace { + +namespace css = ::com::sun::star; + +class CURI: + public ::cppu::WeakImplHelper3< + css::lang::XServiceInfo, + css::lang::XInitialization, + css::rdf::XURI> +{ +public: + explicit CURI(css::uno::Reference< css::uno::XComponentContext > const & context); + virtual ~CURI() {} + + // ::com::sun::star::lang::XServiceInfo: + virtual ::rtl::OUString SAL_CALL getImplementationName() throw (css::uno::RuntimeException); + virtual ::sal_Bool SAL_CALL supportsService(const ::rtl::OUString & ServiceName) throw (css::uno::RuntimeException); + virtual css::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames() throw (css::uno::RuntimeException); + + // ::com::sun::star::lang::XInitialization: + virtual void SAL_CALL initialize(const css::uno::Sequence< ::com::sun::star::uno::Any > & aArguments) throw (css::uno::RuntimeException, css::uno::Exception); + + // ::com::sun::star::rdf::XNode: + virtual ::rtl::OUString SAL_CALL getStringValue() throw (css::uno::RuntimeException); + + // ::com::sun::star::rdf::XURI: + virtual ::rtl::OUString SAL_CALL getLocalName() throw (css::uno::RuntimeException); + virtual ::rtl::OUString SAL_CALL getNamespace() throw (css::uno::RuntimeException); + +private: + CURI(const CURI &); // not defined + CURI& operator=(const CURI &); // not defined + + /// handle css.rdf.URIs + void SAL_CALL initFromConstant(const sal_Int16 i_Constant); + + css::uno::Reference< css::uno::XComponentContext > m_xContext; + + ::rtl::OUString m_Namespace; + ::rtl::OUString m_LocalName; +}; + +CURI::CURI(css::uno::Reference< css::uno::XComponentContext > const & context) : + m_xContext(context), m_Namespace(), m_LocalName() +{} + +// com.sun.star.uno.XServiceInfo: +::rtl::OUString SAL_CALL CURI::getImplementationName() throw (css::uno::RuntimeException) +{ + return comp_CURI::_getImplementationName(); +} + +::sal_Bool SAL_CALL CURI::supportsService(::rtl::OUString const & serviceName) throw (css::uno::RuntimeException) +{ + css::uno::Sequence< ::rtl::OUString > serviceNames = comp_CURI::_getSupportedServiceNames(); + for (::sal_Int32 i = 0; i < serviceNames.getLength(); ++i) { + if (serviceNames[i] == serviceName) + return sal_True; + } + return sal_False; +} + +css::uno::Sequence< ::rtl::OUString > SAL_CALL CURI::getSupportedServiceNames() throw (css::uno::RuntimeException) +{ + return comp_CURI::_getSupportedServiceNames(); +} + +const char s_nsXS [] = "http://www.w3.org/2001/XMLSchema#"; +const char s_nsXSD [] = "http://www.w3.org/2001/XMLSchema-datatypes#"; +const char s_nsRDF [] = "http://www.w3.org/1999/02/22-rdf-syntax-ns#"; +const char s_nsRDFs [] = "http://www.w3.org/2000/01/rdf-schema#"; +const char s_nsOWL [] = "http://www.w3.org/2002/07/owl#"; +const char s_nsPkg [] = + "http://docs.oasis-open.org/ns/office/1.2/meta/pkg#"; +const char s_nsODF [] = + "http://docs.oasis-open.org/ns/office/1.2/meta/odf#"; + +void SAL_CALL CURI::initFromConstant(const sal_Int16 i_Constant) +{ + const char *ns(0); + const char *ln(0); + switch (i_Constant) + { + case css::rdf::URIs::XSD_NCNAME: + ns = s_nsXSD; + ln = "NCName"; + break; + + case css::rdf::URIs::XSD_STRING: + ns = s_nsXSD; + ln = "string"; + break; + + case css::rdf::URIs::XSD_NORMALIZEDSTRING: + ns = s_nsXSD; + ln = "normalizedString"; + break; + + case css::rdf::URIs::XSD_BOOLEAN: + ns = s_nsXSD; + ln = "boolean"; + break; + + case css::rdf::URIs::XSD_DECIMAL: + ns = s_nsXSD; + ln = "decimal"; + break; + + case css::rdf::URIs::XSD_FLOAT: + ns = s_nsXSD; + ln = "float"; + break; + + case css::rdf::URIs::XSD_DOUBLE: + ns = s_nsXSD; + ln = "double"; + break; + + case css::rdf::URIs::XSD_INTEGER: + ns = s_nsXSD; + ln = "integer"; + break; + + case css::rdf::URIs::XSD_NONNEGATIVEINTEGER: + ns = s_nsXSD; + ln = "nonNegativeInteger"; + break; + + case css::rdf::URIs::XSD_POSITIVEINTEGER: + ns = s_nsXSD; + ln = "positiveInteger"; + break; + + case css::rdf::URIs::XSD_NONPOSITIVEINTEGER: + ns = s_nsXSD; + ln = "nonPositiveInteger"; + break; + + case css::rdf::URIs::XSD_NEGATIVEINTEGER: + ns = s_nsXSD; + ln = "negativeInteger"; + break; + + case css::rdf::URIs::XSD_LONG: + ns = s_nsXSD; + ln = "long"; + break; + + case css::rdf::URIs::XSD_INT: + ns = s_nsXSD; + ln = "int"; + break; + + case css::rdf::URIs::XSD_SHORT: + ns = s_nsXSD; + ln = "short"; + break; + + case css::rdf::URIs::XSD_BYTE: + ns = s_nsXSD; + ln = "byte"; + break; + + case css::rdf::URIs::XSD_UNSIGNEDLONG: + ns = s_nsXSD; + ln = "unsignedLong"; + break; + + case css::rdf::URIs::XSD_UNSIGNEDINT: + ns = s_nsXSD; + ln = "unsignedInt"; + break; + + case css::rdf::URIs::XSD_UNSIGNEDSHORT: + ns = s_nsXSD; + ln = "unsignedShort"; + break; + + case css::rdf::URIs::XSD_UNSIGNEDBYTE: + ns = s_nsXSD; + ln = "unsignedByte"; + break; + + case css::rdf::URIs::XSD_HEXBINARY: + ns = s_nsXSD; + ln = "hexBinary"; + break; + + case css::rdf::URIs::XSD_BASE64BINARY: + ns = s_nsXSD; + ln = "base64Binary"; + break; + + case css::rdf::URIs::XSD_DATETIME: + ns = s_nsXSD; + ln = "dateTime"; + break; + + case css::rdf::URIs::XSD_TIME: + ns = s_nsXSD; + ln = "time"; + break; + + case css::rdf::URIs::XSD_DATE: + ns = s_nsXSD; + ln = "date"; + break; + + case css::rdf::URIs::XSD_GYEARMONTH: + ns = s_nsXSD; + ln = "gYearMonth"; + break; + + case css::rdf::URIs::XSD_GYEAR: + ns = s_nsXSD; + ln = "gYear"; + break; + + case css::rdf::URIs::XSD_GMONTHDAY: + ns = s_nsXSD; + ln = "gMonthDay"; + break; + + case css::rdf::URIs::XSD_GDAY: + ns = s_nsXSD; + ln = "gDay"; + break; + + case css::rdf::URIs::XSD_GMONTH: + ns = s_nsXSD; + ln = "gMonth"; + break; + + case css::rdf::URIs::XSD_ANYURI: + ns = s_nsXSD; + ln = "anyURI"; + break; + + case css::rdf::URIs::XSD_TOKEN: + ns = s_nsXSD; + ln = "token"; + break; + + case css::rdf::URIs::XSD_LANGUAGE: + ns = s_nsXSD; + ln = "language"; + break; + + case css::rdf::URIs::XSD_NMTOKEN: + ns = s_nsXSD; + ln = "NMTOKEN"; + break; + + case css::rdf::URIs::XSD_NAME: + ns = s_nsXSD; + ln = "Name"; + break; + + case css::rdf::URIs::XSD_DURATION: + ns = s_nsXSD; + ln = "duration"; + break; + + case css::rdf::URIs::XSD_QNAME: + ns = s_nsXSD; + ln = "QName"; + break; + + case css::rdf::URIs::XSD_NOTATION: + ns = s_nsXSD; + ln = "NOTATION"; + break; + + case css::rdf::URIs::XSD_NMTOKENS: + ns = s_nsXSD; + ln = "NMTOKENS"; + break; + + case css::rdf::URIs::XSD_ID: + ns = s_nsXSD; + ln = "ID"; + break; + + case css::rdf::URIs::XSD_IDREF: + ns = s_nsXSD; + ln = "IDREF"; + break; + + case css::rdf::URIs::XSD_IDREFS: + ns = s_nsXSD; + ln = "IDREFS"; + break; + + case css::rdf::URIs::XSD_ENTITY: + ns = s_nsXSD; + ln = "ENTITY"; + break; + + case css::rdf::URIs::XSD_ENTITIES: + ns = s_nsXSD; + ln = "ENTITIES"; + break; + + case css::rdf::URIs::RDF_TYPE: + ns = s_nsRDF; + ln = "type"; + break; + + case css::rdf::URIs::RDF_SUBJECT: + ns = s_nsRDF; + ln = "subject"; + break; + + case css::rdf::URIs::RDF_PREDICATE: + ns = s_nsRDF; + ln = "predicate"; + break; + + case css::rdf::URIs::RDF_OBJECT: + ns = s_nsRDF; + ln = "object"; + break; + + case css::rdf::URIs::RDF_PROPERTY: + ns = s_nsRDF; + ln = "Property"; + break; + + case css::rdf::URIs::RDF_STATEMENT: + ns = s_nsRDF; + ln = "Statement"; + break; + + case css::rdf::URIs::RDF_VALUE: + ns = s_nsRDF; + ln = "value"; + break; + + case css::rdf::URIs::RDF_FIRST: + ns = s_nsRDF; + ln = "first"; + break; + + case css::rdf::URIs::RDF_REST: + ns = s_nsRDF; + ln = "rest"; + break; + + case css::rdf::URIs::RDF_NIL: + ns = s_nsRDF; + ln = "nil"; + break; + + case css::rdf::URIs::RDF_XMLLITERAL: + ns = s_nsRDF; + ln = "XMLLiteral"; + break; + + case css::rdf::URIs::RDF_ALT: + ns = s_nsRDF; + ln = "Alt"; + break; + + case css::rdf::URIs::RDF_BAG: + ns = s_nsRDF; + ln = "Bag"; + break; + + case css::rdf::URIs::RDF_LIST: + ns = s_nsRDF; + ln = "List"; + break; + + case css::rdf::URIs::RDF_SEQ: + ns = s_nsRDF; + ln = "Seq"; + break; + + case css::rdf::URIs::RDF_1: + ns = s_nsRDF; + ln = "_1"; + break; + + case css::rdf::URIs::RDFS_COMMENT: + ns = s_nsRDFs; + ln = "comment"; + break; + + case css::rdf::URIs::RDFS_LABEL: + ns = s_nsRDFs; + ln = "label"; + break; + + case css::rdf::URIs::RDFS_DOMAIN: + ns = s_nsRDFs; + ln = "domain"; + break; + + case css::rdf::URIs::RDFS_RANGE: + ns = s_nsRDFs; + ln = "range"; + break; + + case css::rdf::URIs::RDFS_SUBCLASSOF: + ns = s_nsRDFs; + ln = "subClassOf"; + break; + + case css::rdf::URIs::RDFS_LITERAL: + ns = s_nsRDFs; + ln = "Literal"; + break; + + case css::rdf::URIs::OWL_CLASS: + ns = s_nsOWL; + ln = "Class"; + break; + + case css::rdf::URIs::OWL_OBJECTPROPERTY: + ns = s_nsOWL; + ln = "ObjectProperty"; + break; + + case css::rdf::URIs::OWL_DATATYPEPROPERTY: + ns = s_nsOWL; + ln = "DatatypeProperty"; + break; + + case css::rdf::URIs::OWL_FUNCTIONALPROPERTY: + ns = s_nsOWL; + ln = "FunctionalProperty"; + break; + + case css::rdf::URIs::OWL_THING: + ns = s_nsOWL; + ln = "Thing"; + break; + + case css::rdf::URIs::OWL_NOTHING: + ns = s_nsOWL; + ln = "Nothing"; + break; + + case css::rdf::URIs::OWL_INDIVIDUAL: + ns = s_nsOWL; + ln = "Individual"; + break; + + case css::rdf::URIs::OWL_EQUIVALENTCLASS: + ns = s_nsOWL; + ln = "equivalentClass"; + break; + + case css::rdf::URIs::OWL_EQUIVALENTPROPERTY: + ns = s_nsOWL; + ln = "equivalentProperty"; + break; + + case css::rdf::URIs::OWL_SAMEAS: + ns = s_nsOWL; + ln = "sameAs"; + break; + + case css::rdf::URIs::OWL_DIFFERENTFROM: + ns = s_nsOWL; + ln = "differentFrom"; + break; + + case css::rdf::URIs::OWL_ALLDIFFERENT: + ns = s_nsOWL; + ln = "AllDifferent"; + break; + + case css::rdf::URIs::OWL_DISTINCTMEMBERS: + ns = s_nsOWL; + ln = "distinctMembers"; + break; + + case css::rdf::URIs::OWL_INVERSEOF: + ns = s_nsOWL; + ln = "inverseOf"; + break; + + case css::rdf::URIs::OWL_TRANSITIVEPROPERTY: + ns = s_nsOWL; + ln = "TransitiveProperty"; + break; + + case css::rdf::URIs::OWL_SYMMETRICPROPERTY: + ns = s_nsOWL; + ln = "SymmetricProperty"; + break; + + case css::rdf::URIs::OWL_INVERSEFUNCTIONALPROPERTY: + ns = s_nsOWL; + ln = "InverseFunctionalProperty"; + break; + + case css::rdf::URIs::OWL_RESTRICTION: + ns = s_nsOWL; + ln = "Restriction"; + break; + + case css::rdf::URIs::OWL_ONPROPERTY: + ns = s_nsOWL; + ln = "onProperty"; + break; + + case css::rdf::URIs::OWL_ALLVALUESFROM: + ns = s_nsOWL; + ln = "allValuesFrom"; + break; + + case css::rdf::URIs::OWL_SOMEVALUESFROM: + ns = s_nsOWL; + ln = "someValuesFrom"; + break; + + case css::rdf::URIs::OWL_MINCARDINALITY: + ns = s_nsOWL; + ln = "minCardinality"; + break; + + case css::rdf::URIs::OWL_MAXCARDINALITY: + ns = s_nsOWL; + ln = "maxCardinality"; + break; + + case css::rdf::URIs::OWL_CARDINALITY: + ns = s_nsOWL; + ln = "cardinality"; + break; + + case css::rdf::URIs::OWL_ONTOLOGY: + ns = s_nsOWL; + ln = "Ontology"; + break; + + case css::rdf::URIs::OWL_IMPORTS: + ns = s_nsOWL; + ln = "imports"; + break; + + case css::rdf::URIs::OWL_VERSIONINFO: + ns = s_nsOWL; + ln = "versionInfo"; + break; + + case css::rdf::URIs::OWL_PRIORVERSION: + ns = s_nsOWL; + ln = "priorVersion"; + break; + + case css::rdf::URIs::OWL_BACKWARDCOMPATIBLEWITH: + ns = s_nsOWL; + ln = "backwardCompatibleWith"; + break; + + case css::rdf::URIs::OWL_INCOMPATIBLEWITH: + ns = s_nsOWL; + ln = "incompatibleWith"; + break; + + case css::rdf::URIs::OWL_DEPRECATEDCLASS: + ns = s_nsOWL; + ln = "DeprecatedClass"; + break; + + case css::rdf::URIs::OWL_DEPRECATEDPROPERTY: + ns = s_nsOWL; + ln = "DeprecatedProperty"; + break; + + case css::rdf::URIs::OWL_ANNOTATIONPROPERTY: + ns = s_nsOWL; + ln = "AnnotationProperty"; + break; + + case css::rdf::URIs::OWL_ONTOLOGYPROPERTY: + ns = s_nsOWL; + ln = "OntologyProperty"; + break; + + case css::rdf::URIs::OWL_ONEOF: + ns = s_nsOWL; + ln = "oneOf"; + break; + + case css::rdf::URIs::OWL_DATARANGE: + ns = s_nsOWL; + ln = "dataRange"; + break; + + case css::rdf::URIs::OWL_DISJOINTWITH: + ns = s_nsOWL; + ln = "disjointWith"; + break; + + case css::rdf::URIs::OWL_UNIONOF: + ns = s_nsOWL; + ln = "unionOf"; + break; + + case css::rdf::URIs::OWL_COMPLEMENTOF: + ns = s_nsOWL; + ln = "complementOf"; + break; + + case css::rdf::URIs::OWL_INTERSECTIONOF: + ns = s_nsOWL; + ln = "intersectionOf"; + break; + + case css::rdf::URIs::OWL_HASVALUE: + ns = s_nsOWL; + ln = "hasValue"; + break; + + + case css::rdf::URIs::PKG_HASPART: + ns = s_nsPkg; + ln = "hasPart"; + break; + +/* REMOVED + case css::rdf::URIs::PKG_IDREF: + ns = s_nsPkg; + ln = "idref"; + break; + + case css::rdf::URIs::PKG_PATH: + ns = s_nsPkg; + ln = "path"; + break; +*/ + + case css::rdf::URIs::PKG_MIMETYPE: + ns = s_nsPkg; + ln = "mimeType"; + break; + + case css::rdf::URIs::PKG_PACKAGE: + ns = s_nsPkg; + ln = "Package"; + break; + + case css::rdf::URIs::PKG_ELEMENT: + ns = s_nsPkg; + ln = "Element"; + break; + + case css::rdf::URIs::PKG_FILE: + ns = s_nsPkg; + ln = "File"; + break; + + case css::rdf::URIs::PKG_METADATAFILE: + ns = s_nsPkg; + ln = "MetadataFile"; + break; + + case css::rdf::URIs::PKG_DOCUMENT: + ns = s_nsPkg; + ln = "Document"; + break; + + case css::rdf::URIs::ODF_PREFIX: + ns = s_nsODF; + ln = "prefix"; + break; + + case css::rdf::URIs::ODF_SUFFIX: + ns = s_nsODF; + ln = "suffix"; + break; + + case css::rdf::URIs::ODF_ELEMENT: + ns = s_nsODF; + ln = "Element"; + break; + + case css::rdf::URIs::ODF_CONTENTFILE: + ns = s_nsODF; + ln = "ContentFile"; + break; + + case css::rdf::URIs::ODF_STYLESFILE: + ns = s_nsODF; + ln = "StylesFile"; + break; + +/* REMOVED + case css::rdf::URIs::ODF_METADATAFILE: + ns = s_nsODF; + ln = "MetadataFile"; + break; +*/ + + default: + throw css::lang::IllegalArgumentException( + ::rtl::OUString::createFromAscii("CURI::initialize: " + "invalid URIs constant argument"), *this, 0); + } + m_Namespace = ::rtl::OUString::createFromAscii(ns).intern(); + m_LocalName = ::rtl::OUString::createFromAscii(ln).intern(); + return; +} + +// ::com::sun::star::lang::XInitialization: +void SAL_CALL CURI::initialize(const css::uno::Sequence< ::com::sun::star::uno::Any > & aArguments) throw (css::uno::RuntimeException, css::uno::Exception) +{ + sal_Int32 len = aArguments.getLength(); + if ((len < 1) || (len > 2)) { + throw css::lang::IllegalArgumentException( + ::rtl::OUString::createFromAscii("CURI::initialize: " + "must give 1 or 2 argument(s)"), *this, 2); + } + + sal_Int16 arg(0); + ::rtl::OUString arg0; + ::rtl::OUString arg1; + if ((aArguments[0] >>= arg)) { + // integer argument: constant from rdf::URIs + if (len != 1) { + throw css::lang::IllegalArgumentException( + ::rtl::OUString::createFromAscii("CURI::initialize: " + "must give 1 int argument"), *this, 1); + } + initFromConstant(arg); + return; + } + if (!(aArguments[0] >>= arg0)) { + throw css::lang::IllegalArgumentException( + ::rtl::OUString::createFromAscii("CURI::initialize: " + "argument must be string or short"), *this, 0); + } + if (len > 1) { + if (!(aArguments[1] >>= arg1)) { + throw css::lang::IllegalArgumentException( + ::rtl::OUString::createFromAscii("CURI::initialize: " + "argument must be string"), *this, 1); + } + // just append the parameters and then split them again; seems simplest + arg0 = arg0 + arg1; + arg1 = ::rtl::OUString(); + } + + // split parameter + sal_Int32 idx; + if ( ((idx = arg0.indexOf ('#')) >= 0) + || ((idx = arg0.lastIndexOf('/')) >= 0) + || ((idx = arg0.lastIndexOf(':')) >= 0)) + { + + if (idx < arg0.getLength() - 1) { + arg1 = arg0.copy(idx+1); + arg0 = arg0.copy(0, idx+1); + } + } else { + throw css::lang::IllegalArgumentException( + ::rtl::OUString::createFromAscii("CURI::initialize: " + "argument not splittable: no separator [#/:]"), *this, 0); + } + + //FIXME: what is legal? + if (arg0.getLength() > 0) { + m_Namespace = arg0; + } else { + throw css::lang::IllegalArgumentException( + ::rtl::OUString::createFromAscii("CURI::initialize: " + "argument is not valid namespace"), *this, 0); + } + //FIXME: what is legal? + if (true) { + m_LocalName = arg1; + } else { + throw css::lang::IllegalArgumentException( + ::rtl::OUString::createFromAscii("CURI::initialize: " + "argument is not valid local name"), *this, 1); + } +} + +// ::com::sun::star::rdf::XNode: +::rtl::OUString SAL_CALL CURI::getStringValue() throw (css::uno::RuntimeException) +{ + return m_Namespace + m_LocalName; +} + +// ::com::sun::star::rdf::XURI: +::rtl::OUString SAL_CALL CURI::getNamespace() throw (css::uno::RuntimeException) +{ + return m_Namespace; +} + +::rtl::OUString SAL_CALL CURI::getLocalName() throw (css::uno::RuntimeException) +{ + return m_LocalName; +} + +} // closing anonymous implementation namespace + + + +// component helper namespace +namespace comp_CURI { + +::rtl::OUString SAL_CALL _getImplementationName() { + return ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( + "CURI")); +} + +css::uno::Sequence< ::rtl::OUString > SAL_CALL _getSupportedServiceNames() +{ + css::uno::Sequence< ::rtl::OUString > s(1); + s[0] = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( + "com.sun.star.rdf.URI")); + return s; +} + +css::uno::Reference< css::uno::XInterface > SAL_CALL _create( + const css::uno::Reference< css::uno::XComponentContext > & context) + SAL_THROW((css::uno::Exception)) +{ + return static_cast< ::cppu::OWeakObject * >(new CURI(context)); +} + +} // closing component helper namespace + diff --git a/unoxml/source/rdf/librdf_repository.cxx b/unoxml/source/rdf/librdf_repository.cxx new file mode 100644 index 000000000000..c42887a9cf9f --- /dev/null +++ b/unoxml/source/rdf/librdf_repository.cxx @@ -0,0 +1,2264 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2008 by Sun Microsystems, Inc. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: librdf_repository.cxx,v $ + * $Revision: 1.2 $ + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#include "librdf_repository.hxx" + +#include <comphelper/stlunosequence.hxx> +#include <comphelper/sequenceasvector.hxx> +#include <comphelper/makesequence.hxx> + +#include <com/sun/star/lang/XServiceInfo.hpp> +#include <com/sun/star/lang/XInitialization.hpp> +#include <com/sun/star/lang/WrappedTargetRuntimeException.hpp> +#include <com/sun/star/lang/IllegalArgumentException.hpp> +#include <com/sun/star/io/XSeekableInputStream.hpp> +#include <com/sun/star/text/XTextRange.hpp> +#include "com/sun/star/rdf/XDocumentRepository.hpp" +#include <com/sun/star/rdf/XLiteral.hpp> +#include <com/sun/star/rdf/FileFormat.hpp> +#include <com/sun/star/rdf/URIs.hpp> +#include <com/sun/star/rdf/BlankNode.hpp> +#include <com/sun/star/rdf/URI.hpp> +#include <com/sun/star/rdf/Literal.hpp> + +#include <cppuhelper/implbase1.hxx> +#include <cppuhelper/implbase3.hxx> +#include <cppuhelper/basemutex.hxx> +#include <rtl/ref.hxx> +#include <rtl/ustring.hxx> + +#include <librdf.h> + +#include <boost/utility.hpp> +#include <boost/shared_ptr.hpp> +#include <boost/shared_array.hpp> +#include <boost/bind.hpp> + +#include <map> +#include <functional> +#include <algorithm> + +#include <string.h> + + +/** + Implementation of the service com.sun.star.rdf.Repository. + + This implementation uses the Redland RDF library (librdf). + + There are several classes involved: + librdf_TypeConverter: helper class to convert data types redland <-> uno + librdf_Repository: the main repository, does almost all the work + librdf_NamedGraph: the XNamedGraph, forwards everything to repository + librdf_GraphResult: an XEnumeration<Statement> + librdf_QuerySelectResult: an XEnumeration<sequence<XNode>> + + @author mst + */ + +/// anonymous implementation namespace +namespace { + +class librdf_NamedGraph; +class librdf_Repository; + +using namespace ::com::sun::star; + +typedef std::map< ::rtl::OUString, ::rtl::Reference<librdf_NamedGraph> > + NamedGraphMap_t; + +const char s_sparql [] = "sparql"; +const char s_nsRDFs [] = "http://www.w3.org/2000/01/rdf-schema#"; +const char s_label [] = "label"; +const char s_nsOOo [] = "http://openoffice.org/2004/office/rdfa/"; + +//////////////////////////////////////////////////////////////////////////// + +//FIXME: this approach is not ideal. can we use blind nodes instead? +bool isInternalContext(librdf_node *i_pNode) throw () +{ + OSL_ENSURE(i_pNode, "isInternalContext: context null"); + OSL_ENSURE(librdf_node_is_resource(i_pNode), + "isInternalContext: context not resource"); + if (i_pNode) { + librdf_uri *pURI(librdf_node_get_uri(i_pNode)); + OSL_ENSURE(pURI, "isInternalContext: URI null"); + if (pURI) { + unsigned char *pContextURI(librdf_uri_as_string(pURI)); + OSL_ENSURE(pContextURI, + "isInternalContext: URI string null"); + // if prefix matches reserved uri, it is RDFa context + if (!strncmp(reinterpret_cast<char *>(pContextURI), + s_nsOOo, sizeof(s_nsOOo)-1)) { + return true; + } + } + return false; + } + return true; +} + + +//////////////////////////////////////////////////////////////////////////// + +#if 0 +class librdf_Statement: + public ::cppu::WeakImplHelper1< + rdf::XStatement> +{ +public: + + librdf_Statement( + uno::Reference< rdf::XResource > const & i_xSubject, + uno::Reference< rdf::XResource > const & i_xPredicate, + uno::Reference< rdf::XNode > const & i_xObject, + uno::Reference< rdf::XURI > const & i_xGraph) + : m_xSubject(i_xSubject), m_xPredicate(i_xPredicate), + m_xObject(i_xObject), m_xGraph(i_xGraph) + { } + virtual ~librdf_Statement() {} + + // ::com::sun::star::rdf::XStatement: + virtual uno::Reference< rdf::XResource > SAL_CALL getSubject() + throw (uno::RuntimeException); + virtual uno::Reference< rdf::XResource > SAL_CALL getPredicate() + throw (uno::RuntimeException); + virtual uno::Reference< rdf::XNode > SAL_CALL getObject() + throw (uno::RuntimeException); + virtual uno::Reference< rdf::XURI > SAL_CALL getGraph() + throw (uno::RuntimeException); + +private: + + uno::Reference< rdf::XResource > m_xSubject; + uno::Reference< rdf::XResource > m_xPredicate; + uno::Reference< rdf::XNode > m_xObject; + uno::Reference< rdf::XURI > m_xGraph; +}; + +// ::com::sun::star::rdf::XStatement: +uno::Reference< rdf::XResource > SAL_CALL +librdf_Statement::getSubject() throw (uno::RuntimeException) +{ + return m_xSubject; +} + +uno::Reference< rdf::XResource > SAL_CALL +librdf_Statement::getPredicate() throw (uno::RuntimeException) +{ + return m_xPredicate; +} + +uno::Reference< rdf::XNode > SAL_CALL +librdf_Statement::getObject() throw (uno::RuntimeException) +{ + return m_xObject; +} + +uno::Reference< rdf::XURI > SAL_CALL +librdf_Statement::getGraph() throw (uno::RuntimeException) +{ + return m_xGraph; +} +#endif + + +//////////////////////////////////////////////////////////////////////////// + +/** converts between librdf types and UNO API types. + */ +class librdf_TypeConverter +{ +public: + librdf_TypeConverter( + uno::Reference< uno::XComponentContext > const & i_xContext, + librdf_Repository &i_rRep) + : m_xContext(i_xContext) + , m_rRep(i_rRep) + { }; + + librdf_world *createWorld() const; + librdf_storage *createStorage(librdf_world *i_pWorld) const; + librdf_model *createModel(librdf_world *i_pWorld, + librdf_storage * i_pStorage) const; + librdf_uri* mkURI( librdf_world* i_pWorld, + const uno::Reference< rdf::XURI > & i_xURI) const; + librdf_node* mkResource( librdf_world* i_pWorld, + const uno::Reference< rdf::XResource > & i_xResource) const; + librdf_node* mkNode( librdf_world* i_pWorld, + const uno::Reference< rdf::XNode > & i_xNode) const; + librdf_statement* mkStatement( librdf_world* i_pWorld, + const uno::Reference< rdf::XResource > & i_xSubject, + const uno::Reference< rdf::XURI > & i_xPredicate, + const uno::Reference< rdf::XNode > & i_xObject) const; + uno::Reference<rdf::XURI> convertToXURI(librdf_uri* i_pURI) const; + uno::Reference<rdf::XURI> convertToXURI(librdf_node* i_pURI) const; + uno::Reference<rdf::XResource> + convertToXResource(librdf_node* i_pNode) const; + uno::Reference<rdf::XNode> convertToXNode(librdf_node* i_pNode) const; +// uno::Reference<rdf::XStatement> +// convertToXStatement(librdf_statement* i_pStmt, librdf_node* i_pContext) +// const; + rdf::Statement + convertToStatement(librdf_statement* i_pStmt, librdf_node* i_pContext) + const; + uno::Reference<rdf::XURI> getRDFsLabel() const; + +private: + uno::Reference< uno::XComponentContext > m_xContext; + librdf_Repository & m_rRep; +}; + + +//////////////////////////////////////////////////////////////////////////// + +/** implements the repository service. + */ +class librdf_Repository: + private boost::noncopyable, +// private ::cppu::BaseMutex, + public ::cppu::WeakImplHelper3< + lang::XServiceInfo, + rdf::XDocumentRepository, + lang::XInitialization> +{ +public: + + explicit librdf_Repository( + uno::Reference< uno::XComponentContext > const & i_xContext); + virtual ~librdf_Repository(); + + // ::com::sun::star::lang::XServiceInfo: + virtual ::rtl::OUString SAL_CALL getImplementationName() + throw (uno::RuntimeException); + virtual ::sal_Bool SAL_CALL supportsService( + const ::rtl::OUString & ServiceName) throw (uno::RuntimeException); + virtual uno::Sequence< ::rtl::OUString > SAL_CALL + getSupportedServiceNames() throw (uno::RuntimeException); + + // ::com::sun::star::rdf::XRepository: + virtual uno::Reference< rdf::XBlankNode > SAL_CALL createBlankNode() + throw (uno::RuntimeException); + virtual uno::Reference<rdf::XNamedGraph> SAL_CALL importGraph( + ::sal_Int16 i_Format, + const uno::Reference< io::XInputStream > & i_xInStream, + const uno::Reference< rdf::XURI > & i_xGraphName, + const uno::Reference< rdf::XURI > & i_xBaseURI) + throw (uno::RuntimeException, lang::IllegalArgumentException, + datatransfer::UnsupportedFlavorException, + container::ElementExistException, rdf::ParseException, + rdf::RepositoryException, io::IOException); + virtual void SAL_CALL exportGraph(::sal_Int16 i_Format, + const uno::Reference< io::XOutputStream > & i_xOutStream, + const uno::Reference< rdf::XURI > & i_xGraphName, + const uno::Reference< rdf::XURI > & i_xBaseURI) + throw (uno::RuntimeException, lang::IllegalArgumentException, + datatransfer::UnsupportedFlavorException, + container::NoSuchElementException, rdf::RepositoryException, + io::IOException); + virtual uno::Sequence< uno::Reference< rdf::XURI > > SAL_CALL + getGraphNames() throw (uno::RuntimeException, rdf::RepositoryException); + virtual uno::Reference< rdf::XNamedGraph > SAL_CALL getGraph( + const uno::Reference< rdf::XURI > & i_xGraphName) + throw (uno::RuntimeException, lang::IllegalArgumentException, + rdf::RepositoryException); + virtual uno::Reference< rdf::XNamedGraph > SAL_CALL createGraph( + const uno::Reference< rdf::XURI > & i_xGraphName) + throw (uno::RuntimeException, lang::IllegalArgumentException, + container::ElementExistException, rdf::RepositoryException); + virtual void SAL_CALL destroyGraph( + const uno::Reference< rdf::XURI > & i_xGraphName) + throw (uno::RuntimeException, lang::IllegalArgumentException, + container::NoSuchElementException, rdf::RepositoryException); + virtual uno::Reference< container::XEnumeration > SAL_CALL getStatements( + const uno::Reference< rdf::XResource > & i_xSubject, + const uno::Reference< rdf::XURI > & i_xPredicate, + const uno::Reference< rdf::XNode > & i_xObject) + throw (uno::RuntimeException, + rdf::RepositoryException); + virtual uno::Reference< rdf::XQuerySelectResult > SAL_CALL + querySelect(const ::rtl::OUString & i_rQuery) + throw (uno::RuntimeException, rdf::QueryException, + rdf::RepositoryException); + virtual uno::Reference< container::XEnumeration > SAL_CALL + queryConstruct(const ::rtl::OUString & i_rQuery) + throw (uno::RuntimeException, rdf::QueryException, + rdf::RepositoryException); + virtual ::sal_Bool SAL_CALL queryAsk(const ::rtl::OUString & i_rQuery) + throw (uno::RuntimeException, rdf::QueryException, + rdf::RepositoryException); + + // ::com::sun::star::rdf::XDocumentRepository: + virtual void SAL_CALL setStatementRDFa( + const uno::Reference< rdf::XResource > & i_xSubject, + const uno::Sequence< uno::Reference< rdf::XURI > > & i_rPredicates, + const uno::Reference< rdf::XMetadatable > & i_xObject, + const ::rtl::OUString & i_rRDFaContent, + const uno::Reference< rdf::XURI > & i_xRDFaDatatype) + throw (uno::RuntimeException, lang::IllegalArgumentException, + rdf::RepositoryException); + virtual void SAL_CALL removeStatementRDFa( + const uno::Reference< rdf::XMetadatable > & i_xElement) + throw (uno::RuntimeException, lang::IllegalArgumentException, + rdf::RepositoryException); + virtual uno::Sequence<rdf::Statement> SAL_CALL getStatementRDFa( + const uno::Reference< rdf::XMetadatable > & i_xElement) + throw (uno::RuntimeException, lang::IllegalArgumentException, + rdf::RepositoryException); + virtual uno::Reference< container::XEnumeration > SAL_CALL + getStatementsRDFa( + const uno::Reference< rdf::XResource > & i_xSubject, + const uno::Reference< rdf::XURI > & i_xPredicate, + const uno::Reference< rdf::XNode > & i_xObject) + throw (uno::RuntimeException, + rdf::RepositoryException); + + // ::com::sun::star::lang::XInitialization: + virtual void SAL_CALL initialize( + const uno::Sequence< ::com::sun::star::uno::Any > & i_rArguments) + throw (uno::RuntimeException, uno::Exception); + + // XNamedGraph forwards --------------------------------------------- + const NamedGraphMap_t::iterator SAL_CALL clearGraph( + const uno::Reference< rdf::XURI > & i_xName, + bool i_Internal = false ); + void SAL_CALL addStatementGraph( + const uno::Reference< rdf::XResource > & i_xSubject, + const uno::Reference< rdf::XURI > & i_xPredicate, + const uno::Reference< rdf::XNode > & i_xObject, + const uno::Reference< rdf::XURI > & i_xName, + bool i_Internal = false ); +// throw (uno::RuntimeException, lang::IllegalArgumentException, +// container::NoSuchElementException, rdf::RepositoryException); + void SAL_CALL removeStatementsGraph( + const uno::Reference< rdf::XResource > & i_xSubject, + const uno::Reference< rdf::XURI > & i_xPredicate, + const uno::Reference< rdf::XNode > & i_xObject, + const uno::Reference< rdf::XURI > & i_xName ); +// throw (uno::RuntimeException, lang::IllegalArgumentException, +// container::NoSuchElementException, rdf::RepositoryException); + uno::Reference< container::XEnumeration > SAL_CALL getStatementsGraph( + const uno::Reference< rdf::XResource > & i_xSubject, + const uno::Reference< rdf::XURI > & i_xPredicate, + const uno::Reference< rdf::XNode > & i_xObject, + const uno::Reference< rdf::XURI > & i_xName, + bool i_Internal = false ); +// throw (uno::RuntimeException, lang::IllegalArgumentException, +// container::NoSuchElementException, rdf::RepositoryException); + + const librdf_TypeConverter& getTypeConverter() { return m_TypeConverter; }; + +private: + + uno::Reference< uno::XComponentContext > m_xContext; + + /// librdf global data + /** N.B.: The redland documentation gives the impression that you can have + as many librdf_worlds as you like. This is true in the same sense + that you can physically be in as many places as you like. + Well, you can, just not at the same time. + The ugly truth is that destroying a librdf_world kills a bunch + of static variables; other librdf_worlds become very unhappy + when they access these. + And of course this is not documented anywhere that I could find. + So we allocate a single world, and refcount that. + */ + static boost::shared_ptr<librdf_world> m_pWorld; + /// refcount + static sal_uInt32 m_NumInstances; + /// mutex for m_pWorld - redland is not as threadsafe as is often claimed + static osl::Mutex m_aMutex; + + // NB: sequence of the shared pointers is important! + /// librdf repository storage + boost::shared_ptr<librdf_storage> m_pStorage; + /// librdf repository model + boost::shared_ptr<librdf_model> m_pModel; + + /// all named graphs + NamedGraphMap_t m_NamedGraphs; + + /// type conversion helper + librdf_TypeConverter m_TypeConverter; +}; + + +//////////////////////////////////////////////////////////////////////////// + +/** result of operations that return a graph, i.e., + an XEnumeration of statements. + */ +class librdf_GraphResult: + private boost::noncopyable, + public ::cppu::WeakImplHelper1< + container::XEnumeration> +{ +public: + + librdf_GraphResult(librdf_Repository *i_pRepository, + ::osl::Mutex & i_rMutex, + boost::shared_ptr<librdf_stream> const& i_pStream, + boost::shared_ptr<librdf_query> const& i_pQuery = + boost::shared_ptr<librdf_query>() ) + : m_xRep(i_pRepository) + , m_rMutex(i_rMutex) + , m_pQuery(i_pQuery) + , m_pStream(i_pStream) + { }; + + virtual ~librdf_GraphResult() {} + + // ::com::sun::star::container::XEnumeration: + virtual ::sal_Bool SAL_CALL hasMoreElements() + throw (uno::RuntimeException); + virtual uno::Any SAL_CALL nextElement() + throw (uno::RuntimeException, container::NoSuchElementException, + lang::WrappedTargetException); + +private: + // NB: this is not a weak pointer: streams _must_ be deleted before the + // storage they point into, so we keep the repository alive here + // also, sequence is important: the stream must be destroyed first. + ::rtl::Reference< librdf_Repository > m_xRep; + // needed for synchronizing access to librdf (it doesnt do win32 threading) + ::osl::Mutex & m_rMutex; + // the query (in case this is a result of a graph query) + // not that the redland documentation spells this out explicity, but + // queries must be freed only after all the results are completely read + boost::shared_ptr<librdf_query> m_pQuery; + boost::shared_ptr<librdf_stream> m_pStream; +}; + + +// ::com::sun::star::container::XEnumeration: +::sal_Bool SAL_CALL +librdf_GraphResult::hasMoreElements() throw (uno::RuntimeException) +{ + ::osl::MutexGuard g(m_rMutex); + return m_pStream.get() && !librdf_stream_end(m_pStream.get()); +} + +::com::sun::star::uno::Any SAL_CALL +librdf_GraphResult::nextElement() +throw (uno::RuntimeException, container::NoSuchElementException, + lang::WrappedTargetException) +{ + ::osl::MutexGuard g(m_rMutex); + if (!m_pStream.get() || !librdf_stream_end(m_pStream.get())) { + librdf_node *pCtxt( static_cast<librdf_node *> + (librdf_stream_get_context(m_pStream.get())) ); + librdf_statement *pStmt( librdf_stream_get_object(m_pStream.get()) ); + if (!pStmt) { + rdf::QueryException e(::rtl::OUString::createFromAscii( + "librdf_GraphResult::nextElement: " + "librdf_stream_get_object failed"), *this); + throw lang::WrappedTargetException(::rtl::OUString::createFromAscii( + "librdf_GraphResult::nextElement: " + "librdf_stream_get_object failed"), *this, + uno::makeAny(e)); + } + // NB: pCtxt may be null here if this is result of a graph query + if (pCtxt && isInternalContext(pCtxt)) { + pCtxt = 0; // XML ID context is implementation detail! + } + rdf::Statement Stmt( + m_xRep->getTypeConverter().convertToStatement(pStmt, pCtxt) ); + // NB: this will invalidate current item. + librdf_stream_next(m_pStream.get()); + return uno::makeAny(Stmt); + } else { + throw container::NoSuchElementException(); + } +} + + +//////////////////////////////////////////////////////////////////////////// + +/** result of tuple queries ("SELECT"). + */ +class librdf_QuerySelectResult: + private boost::noncopyable, + public ::cppu::WeakImplHelper1< + rdf::XQuerySelectResult> +{ +public: + + librdf_QuerySelectResult(librdf_Repository *i_pRepository, + ::osl::Mutex & i_rMutex, + boost::shared_ptr<librdf_query> const& i_pQuery, + boost::shared_ptr<librdf_query_results> const& i_pQueryResult, + uno::Sequence< ::rtl::OUString > const& i_rBindingNames ) + : m_xRep(i_pRepository) + , m_rMutex(i_rMutex) + , m_pQuery(i_pQuery) + , m_pQueryResult(i_pQueryResult) + , m_BindingNames(i_rBindingNames) + { }; + + virtual ~librdf_QuerySelectResult() {} + + // ::com::sun::star::container::XEnumeration: + virtual ::sal_Bool SAL_CALL hasMoreElements() + throw (uno::RuntimeException); + virtual uno::Any SAL_CALL nextElement() + throw (uno::RuntimeException, container::NoSuchElementException, + lang::WrappedTargetException); + + // ::com::sun::star::rdf::XQuerySelectResult: + virtual uno::Sequence< ::rtl::OUString > SAL_CALL getBindingNames() + throw (uno::RuntimeException); + +private: + + // NB: this is not a weak pointer: streams _must_ be deleted before the + // storage they point into, so we keep the repository alive here + // also, sequence is important: the stream must be destroyed first. + ::rtl::Reference< librdf_Repository > m_xRep; + // needed for synchronizing access to librdf (it doesnt do win32 threading) + ::osl::Mutex & m_rMutex; + // not that the redland documentation spells this out explicity, but + // queries must be freed only after all the results are completely read + boost::shared_ptr<librdf_query> m_pQuery; + boost::shared_ptr<librdf_query_results> m_pQueryResult; + uno::Sequence< ::rtl::OUString > m_BindingNames; +}; + + +// ::com::sun::star::container::XEnumeration: +::sal_Bool SAL_CALL +librdf_QuerySelectResult::hasMoreElements() throw (uno::RuntimeException) +{ + ::osl::MutexGuard g(m_rMutex); + return !librdf_query_results_finished(m_pQueryResult.get()); +} + +class NodeArrayDeleter : public std::unary_function<librdf_node**, void> +{ + const int m_Count; + +public: + NodeArrayDeleter(int i_Count) : m_Count(i_Count) { } + + void operator() (librdf_node** io_pArray) const throw () + { + std::for_each(io_pArray, io_pArray + m_Count, librdf_free_node); + delete[] io_pArray; + } +}; + +::com::sun::star::uno::Any SAL_CALL +librdf_QuerySelectResult::nextElement() +throw (uno::RuntimeException, container::NoSuchElementException, + lang::WrappedTargetException) +{ + ::osl::MutexGuard g(m_rMutex); + if (!librdf_query_results_finished(m_pQueryResult.get())) { + sal_Int32 count(m_BindingNames.getLength()); + OSL_ENSURE(count >= 0, "negative length?"); + boost::shared_array<librdf_node*> pNodes( new librdf_node*[count], + NodeArrayDeleter(count)); + for (int i = 0; i < count; ++i) { + pNodes[i] = 0; + } + if (librdf_query_results_get_bindings(m_pQueryResult.get(), NULL, + pNodes.get())) + { + rdf::QueryException e(::rtl::OUString::createFromAscii( + "librdf_QuerySelectResult::nextElement: " + "librdf_query_results_get_bindings failed"), *this); + throw lang::WrappedTargetException(::rtl::OUString::createFromAscii( + "librdf_QuerySelectResult::nextElement: " + "librdf_query_results_get_bindings failed"), *this, + uno::makeAny(e)); + } + uno::Sequence< uno::Reference< rdf::XNode > > ret(count); + for (int i = 0; i < count; ++i) { + ret[i] = m_xRep->getTypeConverter().convertToXNode(pNodes[i]); + } + // NB: this will invalidate current item. + librdf_query_results_next(m_pQueryResult.get()); + return uno::makeAny(ret); + } else { + throw container::NoSuchElementException(); + } +} + +// ::com::sun::star::rdf::XQuerySelectResult: +uno::Sequence< ::rtl::OUString > SAL_CALL +librdf_QuerySelectResult::getBindingNames() throw (uno::RuntimeException) +{ + return m_BindingNames; +} + + +//////////////////////////////////////////////////////////////////////////// + +/** represents a named graph, and forwards all the work to repository. + */ +class librdf_NamedGraph: + private boost::noncopyable, + public ::cppu::WeakImplHelper1< + rdf::XNamedGraph> +{ +public: + librdf_NamedGraph(librdf_Repository * i_pRep, + uno::Reference<rdf::XURI> const & i_xName) + : m_wRep(i_pRep) + , m_pRep(i_pRep) + , m_xName(i_xName) + { }; + + virtual ~librdf_NamedGraph() {} + + // ::com::sun::star::rdf::XNode: + virtual ::rtl::OUString SAL_CALL getStringValue() + throw (uno::RuntimeException); + + // ::com::sun::star::rdf::XURI: + virtual ::rtl::OUString SAL_CALL getNamespace() + throw (uno::RuntimeException); + virtual ::rtl::OUString SAL_CALL getLocalName() + throw (uno::RuntimeException); + + // ::com::sun::star::rdf::XNamedGraph: + virtual uno::Reference<rdf::XURI> SAL_CALL getName() + throw (uno::RuntimeException); + virtual void SAL_CALL clear() + throw (uno::RuntimeException, + container::NoSuchElementException, rdf::RepositoryException); + virtual void SAL_CALL addStatement( + const uno::Reference< rdf::XResource > & i_xSubject, + const uno::Reference< rdf::XURI > & i_xPredicate, + const uno::Reference< rdf::XNode > & i_xObject) + throw (uno::RuntimeException, lang::IllegalArgumentException, + container::NoSuchElementException, rdf::RepositoryException); + virtual void SAL_CALL removeStatements( + const uno::Reference< rdf::XResource > & i_xSubject, + const uno::Reference< rdf::XURI > & i_xPredicate, + const uno::Reference< rdf::XNode > & i_xObject) + throw (uno::RuntimeException, + container::NoSuchElementException, rdf::RepositoryException); + virtual uno::Reference< container::XEnumeration > SAL_CALL getStatements( + const uno::Reference< rdf::XResource > & i_xSubject, + const uno::Reference< rdf::XURI > & i_xPredicate, + const uno::Reference< rdf::XNode > & i_xObject) + throw (uno::RuntimeException, + container::NoSuchElementException, rdf::RepositoryException); + +private: + + /// weak reference: this is needed to check if m_pRep is valid + uno::WeakReference< rdf::XRepository > m_wRep; + librdf_Repository *m_pRep; + uno::Reference< rdf::XURI > m_xName; +}; + + +// ::com::sun::star::rdf::XNode: +::rtl::OUString SAL_CALL librdf_NamedGraph::getStringValue() +throw (uno::RuntimeException) +{ + return m_xName->getStringValue(); +} + +// ::com::sun::star::rdf::XURI: +::rtl::OUString SAL_CALL librdf_NamedGraph::getNamespace() +throw (uno::RuntimeException) +{ + return m_xName->getNamespace(); +} + +::rtl::OUString SAL_CALL librdf_NamedGraph::getLocalName() +throw (uno::RuntimeException) +{ + return m_xName->getLocalName(); +} + +// ::com::sun::star::rdf::XNamedGraph: +uno::Reference< rdf::XURI > SAL_CALL librdf_NamedGraph::getName() +throw (uno::RuntimeException) +{ + return m_xName; +} + +void SAL_CALL librdf_NamedGraph::clear() +throw (uno::RuntimeException, + container::NoSuchElementException, rdf::RepositoryException) +{ + uno::Reference< rdf::XRepository > xRep( m_wRep ); + if (!xRep.is()) { + throw rdf::RepositoryException(::rtl::OUString::createFromAscii( + "librdf_NamedGraph::clear: repository is gone"), *this); + } + try { + m_pRep->clearGraph(m_xName); + } catch (lang::IllegalArgumentException &) { + throw uno::RuntimeException(); + } +} + +void SAL_CALL librdf_NamedGraph::addStatement( + const uno::Reference< rdf::XResource > & i_xSubject, + const uno::Reference< rdf::XURI > & i_xPredicate, + const uno::Reference< rdf::XNode > & i_xObject) +throw (uno::RuntimeException, lang::IllegalArgumentException, + container::NoSuchElementException, rdf::RepositoryException) +{ + uno::Reference< rdf::XRepository > xRep( m_wRep ); + if (!xRep.is()) { + throw rdf::RepositoryException(::rtl::OUString::createFromAscii( + "librdf_NamedGraph::addStatement: repository is gone"), *this); + } + m_pRep->addStatementGraph(i_xSubject, i_xPredicate, i_xObject, m_xName); +} + +void SAL_CALL librdf_NamedGraph::removeStatements( + const uno::Reference< rdf::XResource > & i_xSubject, + const uno::Reference< rdf::XURI > & i_xPredicate, + const uno::Reference< rdf::XNode > & i_xObject) +throw (uno::RuntimeException, + container::NoSuchElementException, rdf::RepositoryException) +{ + uno::Reference< rdf::XRepository > xRep( m_wRep ); + if (!xRep.is()) { + throw rdf::RepositoryException(::rtl::OUString::createFromAscii( + "librdf_NamedGraph::removeStatements: repository is gone"), *this); + } + m_pRep->removeStatementsGraph(i_xSubject, i_xPredicate, i_xObject, m_xName); +} + +uno::Reference< container::XEnumeration > SAL_CALL +librdf_NamedGraph::getStatements( + const uno::Reference< rdf::XResource > & i_xSubject, + const uno::Reference< rdf::XURI > & i_xPredicate, + const uno::Reference< rdf::XNode > & i_xObject) +throw (uno::RuntimeException, + container::NoSuchElementException, rdf::RepositoryException) +{ + uno::Reference< rdf::XRepository > xRep( m_wRep ); + if (!xRep.is()) { + throw rdf::RepositoryException(::rtl::OUString::createFromAscii( + "librdf_NamedGraph::getStatements: repository is gone"), *this); + } + return m_pRep->getStatementsGraph( + i_xSubject, i_xPredicate, i_xObject, m_xName); +} + + +//////////////////////////////////////////////////////////////////////////// + +boost::shared_ptr<librdf_world> librdf_Repository::m_pWorld; +sal_uInt32 librdf_Repository::m_NumInstances = 0; +osl::Mutex librdf_Repository::m_aMutex; + +librdf_Repository::librdf_Repository( + uno::Reference< uno::XComponentContext > const & i_xContext) + : /*BaseMutex(),*/ m_xContext(i_xContext) +// m_pWorld (static_cast<librdf_world *>(0), librdf_free_world ), + , m_pStorage(static_cast<librdf_storage*>(0), librdf_free_storage) + , m_pModel (static_cast<librdf_model *>(0), librdf_free_model ) + , m_NamedGraphs() + , m_TypeConverter(i_xContext, *this) +{ + OSL_ENSURE(i_xContext.is(), "librdf_Repository: null context"); + + ::osl::MutexGuard g(m_aMutex); + if (!m_NumInstances++) { + m_pWorld.reset(m_TypeConverter.createWorld(), librdf_free_world); + } +} + +librdf_Repository::~librdf_Repository() +{ + // must destroy these before world! + m_pModel.reset(); + m_pStorage.reset(); + + // FIXME: so it turns out that calling librdf_free_world will + // (via raptor_sax2_finish) call xmlCleanupParser, which will + // free libxml2's globals! ARRRGH!!! => never call librdf_free_world +#if 0 + ::osl::MutexGuard g(m_aMutex); + if (!--m_NumInstances) { + m_pWorld.reset(); + } +#endif +} + +// com.sun.star.uno.XServiceInfo: +::rtl::OUString SAL_CALL librdf_Repository::getImplementationName() +throw (uno::RuntimeException) +{ + return comp_librdf_Repository::_getImplementationName(); +} + +::sal_Bool SAL_CALL librdf_Repository::supportsService( + ::rtl::OUString const & serviceName) throw (uno::RuntimeException) +{ + uno::Sequence< ::rtl::OUString > serviceNames + = comp_librdf_Repository::_getSupportedServiceNames(); + for (::sal_Int32 i = 0; i < serviceNames.getLength(); ++i) { + if (serviceNames[i] == serviceName) + return sal_True; + } + return sal_False; +} + +uno::Sequence< ::rtl::OUString > SAL_CALL +librdf_Repository::getSupportedServiceNames() throw (uno::RuntimeException) +{ + return comp_librdf_Repository::_getSupportedServiceNames(); +} + +// ::com::sun::star::rdf::XRepository: +uno::Reference< rdf::XBlankNode > SAL_CALL librdf_Repository::createBlankNode() +throw (uno::RuntimeException) +{ + ::osl::MutexGuard g(m_aMutex); + const boost::shared_ptr<librdf_node> pNode( + librdf_new_node_from_blank_identifier(m_pWorld.get(), NULL), + librdf_free_node); + if (!pNode) { + throw uno::RuntimeException(::rtl::OUString::createFromAscii( + "librdf_Repository::createBlankNode: " + "librdf_new_node_from_blank_identifier failed"), *this); + } + const unsigned char * id (librdf_node_get_blank_identifier(pNode.get())); + if (!id) { + throw uno::RuntimeException(::rtl::OUString::createFromAscii( + "librdf_Repository::createBlankNode: " + "librdf_node_get_blank_identifier failed"), *this); + } + const ::rtl::OUString nodeID(::rtl::OUString::createFromAscii( + reinterpret_cast<const char *>(id))); + try { + return rdf::BlankNode::create(m_xContext, nodeID); + } catch (lang::IllegalArgumentException & iae) { + throw lang::WrappedTargetRuntimeException( + ::rtl::OUString::createFromAscii( + "librdf_Repository::createBlankNode: " + "illegal blank node label"), *this, uno::makeAny(iae)); + } +} + +bool formatNeedsBaseURI(::sal_Int16 i_Format) +{ + (void) i_Format; //FIXME any which dont? + return true; +} + +//void SAL_CALL +uno::Reference<rdf::XNamedGraph> SAL_CALL +librdf_Repository::importGraph(::sal_Int16 i_Format, + const uno::Reference< io::XInputStream > & i_xInStream, + const uno::Reference< rdf::XURI > & i_xGraphName, + const uno::Reference< rdf::XURI > & i_xBaseURI) +throw (uno::RuntimeException, lang::IllegalArgumentException, + datatransfer::UnsupportedFlavorException, + container::ElementExistException, rdf::ParseException, + rdf::RepositoryException, io::IOException) +{ + ::osl::MutexGuard g(m_aMutex); + if (!i_xInStream.is()) { + throw lang::IllegalArgumentException( + ::rtl::OUString::createFromAscii("librdf_Repository::importGraph: " + "stream is null"), *this, 1); + } + //FIXME: other formats + if (i_Format != rdf::FileFormat::RDF_XML) { + throw datatransfer::UnsupportedFlavorException( + ::rtl::OUString::createFromAscii("librdf_Repository::importGraph: " + "file format not supported"), *this); + } + if (!i_xGraphName.is()) { + throw lang::IllegalArgumentException( + ::rtl::OUString::createFromAscii("librdf_Repository::importGraph: " + "graph name is null"), *this, 2); + } + if (i_xGraphName->getStringValue().matchAsciiL(s_nsOOo, sizeof(s_nsOOo)-1)) + { + throw lang::IllegalArgumentException( + ::rtl::OUString::createFromAscii("librdf_Repository::importGraph: " + "URI is reserved"), *this, 0); + } + if (formatNeedsBaseURI(i_Format) && !i_xBaseURI.is()) { + throw lang::IllegalArgumentException( + ::rtl::OUString::createFromAscii("librdf_Repository::importGraph: " + "base URI is null"), *this, 3); + } + OSL_ENSURE(i_xBaseURI.is(), "no base uri"); + const ::rtl::OUString baseURIU( i_xBaseURI->getStringValue() ); + if (baseURIU.indexOf('#') >= 0) { + throw lang::IllegalArgumentException( + ::rtl::OUString::createFromAscii("librdf_Repository::importGraph: " + "base URI is not absolute"), *this, 3); + } + + const ::rtl::OUString contextU( i_xGraphName->getStringValue() ); + if (m_NamedGraphs.find(contextU) != m_NamedGraphs.end()) { + throw container::ElementExistException( + ::rtl::OUString::createFromAscii("librdf_Repository::importGraph: " + "graph with given URI exists"), *this); + } + const ::rtl::OString context( + ::rtl::OUStringToOString(contextU, RTL_TEXTENCODING_UTF8) ); + + const boost::shared_ptr<librdf_node> pContext( + librdf_new_node_from_uri_string(m_pWorld.get(), + reinterpret_cast<const unsigned char*> (context.getStr())), + librdf_free_node); + if (!pContext) { + throw uno::RuntimeException(::rtl::OUString::createFromAscii( + "librdf_Repository::importGraph: " + "librdf_new_node_from_uri_string failed"), *this); + } + + const ::rtl::OString baseURI( + ::rtl::OUStringToOString(baseURIU, RTL_TEXTENCODING_UTF8) ); + const boost::shared_ptr<librdf_uri> pBaseURI( + librdf_new_uri(m_pWorld.get(), + reinterpret_cast<const unsigned char*> (baseURI.getStr())), + librdf_free_uri); + if (!pBaseURI) { + throw uno::RuntimeException(::rtl::OUString::createFromAscii( + "librdf_Repository::importGraph: " + "librdf_new_uri failed"), *this); + } + + const boost::shared_ptr<librdf_parser> pParser( + librdf_new_parser(m_pWorld.get(), "rdfxml", NULL, NULL), + librdf_free_parser); + if (!pParser) { + throw uno::RuntimeException(::rtl::OUString::createFromAscii( + "librdf_Repository::importGraph: " + "librdf_new_parser failed"), *this); + } + + uno::Sequence<sal_Int8> buf; + uno::Reference<io::XSeekable> xSeekable(i_xInStream, uno::UNO_QUERY); + // UGLY: if only that redland junk could read streams... + const sal_Int64 sz( xSeekable.is() ? xSeekable->getLength() : 1 << 20 ); + // exceptions are propagated + i_xInStream->readBytes( buf, static_cast<sal_Int32>( sz ) ); + const boost::shared_ptr<librdf_stream> pStream( + librdf_parser_parse_counted_string_as_stream(pParser.get(), + reinterpret_cast<const unsigned char*>(buf.getConstArray()), + buf.getLength(), pBaseURI.get()), + librdf_free_stream); + if (!pStream) { + throw rdf::ParseException(::rtl::OUString::createFromAscii( + "librdf_Repository::importGraph: " + "librdf_parser_parse_counted_string_as_stream failed"), *this); + } + m_NamedGraphs.insert(std::make_pair(contextU, + new librdf_NamedGraph(this, i_xGraphName))); + if (librdf_model_context_add_statements(m_pModel.get(), + pContext.get(), pStream.get())) { + throw rdf::RepositoryException(::rtl::OUString::createFromAscii( + "librdf_Repository::importGraph: " + "librdf_model_context_add_statements failed"), *this); + } + return getGraph(i_xGraphName); +} + +void SAL_CALL +librdf_Repository::exportGraph(::sal_Int16 i_Format, + const uno::Reference< io::XOutputStream > & i_xOutStream, + const uno::Reference< rdf::XURI > & i_xGraphName, + const uno::Reference< rdf::XURI > & i_xBaseURI) +throw (uno::RuntimeException, lang::IllegalArgumentException, + datatransfer::UnsupportedFlavorException, + container::NoSuchElementException, rdf::RepositoryException, + io::IOException) +{ + ::osl::MutexGuard g(m_aMutex); + if (!i_xOutStream.is()) { + throw lang::IllegalArgumentException( + ::rtl::OUString::createFromAscii("librdf_Repository::exportGraph: " + "stream is null"), *this, 1); + } + // FIXME: other formats + if (i_Format != rdf::FileFormat::RDF_XML) { + throw datatransfer::UnsupportedFlavorException( + ::rtl::OUString::createFromAscii("librdf_Repository::exportGraph: " + "file format not supported"), *this); + } + if (!i_xGraphName.is()) { + throw lang::IllegalArgumentException( + ::rtl::OUString::createFromAscii("librdf_Repository::exportGraph: " + "graph name is null"), *this, 2); + } + if (formatNeedsBaseURI(i_Format) && !i_xBaseURI.is()) { + throw lang::IllegalArgumentException( + ::rtl::OUString::createFromAscii("librdf_Repository::exportGraph: " + "base URI is null"), *this, 3); + } + OSL_ENSURE(i_xBaseURI.is(), "no base uri"); + const ::rtl::OUString baseURIU( i_xBaseURI->getStringValue() ); + if (baseURIU.indexOf('#') >= 0) { + throw lang::IllegalArgumentException( + ::rtl::OUString::createFromAscii("librdf_Repository::exportGraph: " + "base URI is not absolute"), *this, 3); + } + + const ::rtl::OUString contextU( i_xGraphName->getStringValue() ); + if (m_NamedGraphs.find(contextU) == m_NamedGraphs.end()) { + throw container::NoSuchElementException( + ::rtl::OUString::createFromAscii("librdf_Repository::exportGraph: " + "no graph with given URI exists"), *this); + } + const ::rtl::OString context( + ::rtl::OUStringToOString(contextU, RTL_TEXTENCODING_UTF8) ); + + const boost::shared_ptr<librdf_node> pContext( + librdf_new_node_from_uri_string(m_pWorld.get(), + reinterpret_cast<const unsigned char*> (context.getStr())), + librdf_free_node); + if (!pContext) { + throw uno::RuntimeException(::rtl::OUString::createFromAscii( + "librdf_Repository::exportGraph: " + "librdf_new_node_from_uri_string failed"), *this); + } + const ::rtl::OString baseURI( + ::rtl::OUStringToOString(baseURIU, RTL_TEXTENCODING_UTF8) ); + const boost::shared_ptr<librdf_uri> pBaseURI( + librdf_new_uri(m_pWorld.get(), + reinterpret_cast<const unsigned char*> (baseURI.getStr())), + librdf_free_uri); + if (!pBaseURI) { + throw uno::RuntimeException(::rtl::OUString::createFromAscii( + "librdf_Repository::exportGraph: " + "librdf_new_uri failed"), *this); + } + + const boost::shared_ptr<librdf_stream> pStream( + librdf_model_context_as_stream(m_pModel.get(), pContext.get()), + librdf_free_stream); + if (!pStream) { + throw rdf::RepositoryException(::rtl::OUString::createFromAscii( + "librdf_Repository::exportGraph: " + "librdf_model_context_as_stream failed"), *this); + } +// const char *format("rdfxml"); + const char *format("rdfxml-abbrev"); + const boost::shared_ptr<librdf_serializer> pSerializer( + librdf_new_serializer(m_pWorld.get(), format, NULL, NULL), + librdf_free_serializer); + if (!pSerializer) { + throw uno::RuntimeException(::rtl::OUString::createFromAscii( + "librdf_Repository::exportGraph: " + "librdf_new_serializer failed"), *this); + } + + const boost::shared_ptr<librdf_uri> pRelativeURI( + librdf_new_uri(m_pWorld.get(), reinterpret_cast<const unsigned char*> + ("http://feature.librdf.org/raptor-relativeURIs")), + librdf_free_uri); + const boost::shared_ptr<librdf_uri> pWriteBaseURI( + librdf_new_uri(m_pWorld.get(), reinterpret_cast<const unsigned char*> + ("http://feature.librdf.org/raptor-writeBaseURI")), + librdf_free_uri); + const boost::shared_ptr<librdf_node> p0( + librdf_new_node_from_literal(m_pWorld.get(), + reinterpret_cast<const unsigned char*> ("0"), NULL, 0), + librdf_free_node); + const boost::shared_ptr<librdf_node> p1( + librdf_new_node_from_literal(m_pWorld.get(), + reinterpret_cast<const unsigned char*> ("1"), NULL, 0), + librdf_free_node); + if (!pWriteBaseURI || !pRelativeURI || !p0 || !p1) { + throw uno::RuntimeException(::rtl::OUString::createFromAscii( + "librdf_Repository::exportGraph: " + "librdf_new_uri or librdf_new_node_from_literal failed"), *this); + } + + // make URIs relative to base URI + if (librdf_serializer_set_feature(pSerializer.get(), + pRelativeURI.get(), p1.get())) + { + throw uno::RuntimeException(::rtl::OUString::createFromAscii( + "librdf_Repository::exportGraph: " + "librdf_serializer_set_feature relativeURIs failed"), *this); + } + // but do not write the base URI to the file! + if (librdf_serializer_set_feature(pSerializer.get(), + pWriteBaseURI.get(), p0.get())) + { + throw uno::RuntimeException(::rtl::OUString::createFromAscii( + "librdf_Repository::exportGraph: " + "librdf_serializer_set_feature writeBaseURI failed"), *this); + } + + size_t length; + const boost::shared_ptr<unsigned char> pBuf( + librdf_serializer_serialize_stream_to_counted_string( + pSerializer.get(), pBaseURI.get(), pStream.get(), &length), free); + if (!pBuf) { + throw rdf::RepositoryException(::rtl::OUString::createFromAscii( + "librdf_Repository::exportGraph: " + "librdf_serializer_serialize_stream_to_counted_string failed"), + *this); + } + const uno::Sequence<sal_Int8> buf( + reinterpret_cast<sal_Int8*>(pBuf.get()), length); + // exceptions are propagated + i_xOutStream->writeBytes(buf); +} + +uno::Sequence< uno::Reference< rdf::XURI > > SAL_CALL +librdf_Repository::getGraphNames() +throw (uno::RuntimeException, rdf::RepositoryException) +{ + ::osl::MutexGuard g(m_aMutex); + ::comphelper::SequenceAsVector< uno::Reference<rdf::XURI> > ret; + std::transform(m_NamedGraphs.begin(), m_NamedGraphs.end(), + std::back_inserter(ret), + boost::bind(&rdf::XNamedGraph::getName, + boost::bind(&NamedGraphMap_t::value_type::second, _1))); + return ret.getAsConstList(); +} + +uno::Reference< rdf::XNamedGraph > SAL_CALL +librdf_Repository::getGraph(const uno::Reference< rdf::XURI > & i_xGraphName) +throw (uno::RuntimeException, lang::IllegalArgumentException, + rdf::RepositoryException) +{ + ::osl::MutexGuard g(m_aMutex); + if (!i_xGraphName.is()) { + throw lang::IllegalArgumentException( + ::rtl::OUString::createFromAscii("librdf_Repository::getGraph: " + "URI is null"), *this, 0); + } + const NamedGraphMap_t::iterator iter( + m_NamedGraphs.find(i_xGraphName->getStringValue()) ); + if (iter != m_NamedGraphs.end()) { + return uno::Reference<rdf::XNamedGraph>(iter->second.get()); + } else { + return 0; + } +} + +uno::Reference< rdf::XNamedGraph > SAL_CALL +librdf_Repository::createGraph(const uno::Reference< rdf::XURI > & i_xGraphName) +throw (uno::RuntimeException, lang::IllegalArgumentException, + container::ElementExistException, rdf::RepositoryException) +{ + ::osl::MutexGuard g(m_aMutex); + if (!i_xGraphName.is()) { + throw lang::IllegalArgumentException( + ::rtl::OUString::createFromAscii("librdf_Repository::createGraph: " + "URI is null"), *this, 0); + } + if (i_xGraphName->getStringValue().matchAsciiL(s_nsOOo, sizeof(s_nsOOo)-1)) + { + throw lang::IllegalArgumentException( + ::rtl::OUString::createFromAscii("librdf_Repository::createGraph: " + "URI is reserved"), *this, 0); + } + + // NB: librdf does not have a concept of graphs as such; + // a librdf named graph exists iff the model contains a statement with + // the graph name as context + const ::rtl::OUString contextU( i_xGraphName->getStringValue() ); + if (m_NamedGraphs.find(contextU) != m_NamedGraphs.end()) { + throw container::ElementExistException( + ::rtl::OUString::createFromAscii("librdf_Repository::createGraph: " + "graph with given URI exists"), *this); + } + m_NamedGraphs.insert(std::make_pair(contextU, + new librdf_NamedGraph(this, i_xGraphName))); + return uno::Reference<rdf::XNamedGraph>( + m_NamedGraphs.find(contextU)->second.get()); +} + +void SAL_CALL +librdf_Repository::destroyGraph( + const uno::Reference< rdf::XURI > & i_xGraphName) +throw (uno::RuntimeException, lang::IllegalArgumentException, + container::NoSuchElementException, rdf::RepositoryException) +{ + ::osl::MutexGuard g(m_aMutex); + const NamedGraphMap_t::iterator iter( clearGraph(i_xGraphName) ); + m_NamedGraphs.erase(iter); +} + +static bool isMetadatableWithoutMetadata( + uno::Reference<uno::XInterface> const & i_xNode) +{ + const uno::Reference<rdf::XMetadatable> xMeta( i_xNode, uno::UNO_QUERY ); + return (xMeta.is() && !xMeta->getMetadataReference().Second.getLength()); +} + +uno::Reference< container::XEnumeration > SAL_CALL +librdf_Repository::getStatements( + const uno::Reference< rdf::XResource > & i_xSubject, + const uno::Reference< rdf::XURI > & i_xPredicate, + const uno::Reference< rdf::XNode > & i_xObject) +throw (uno::RuntimeException, rdf::RepositoryException) +{ + if (isMetadatableWithoutMetadata(i_xSubject) || + isMetadatableWithoutMetadata(i_xPredicate) || + isMetadatableWithoutMetadata(i_xObject)) + { + return new librdf_GraphResult(this, m_aMutex, + ::boost::shared_ptr<librdf_stream>()); + } + + ::osl::MutexGuard g(m_aMutex); + const boost::shared_ptr<librdf_statement> pStatement( + m_TypeConverter.mkStatement(m_pWorld.get(), + i_xSubject, i_xPredicate, i_xObject), + librdf_free_statement); + OSL_ENSURE(pStatement, "mkStatement failed"); + + const boost::shared_ptr<librdf_stream> pStream( + librdf_model_find_statements(m_pModel.get(), pStatement.get()), + librdf_free_stream); + if (!pStream) { + throw rdf::RepositoryException(::rtl::OUString::createFromAscii( + "librdf_Repository::getStatements: " + "librdf_model_find_statements failed"), *this); + } + + return new librdf_GraphResult(this, m_aMutex, pStream); +} + + +uno::Reference< rdf::XQuerySelectResult > SAL_CALL +librdf_Repository::querySelect(const ::rtl::OUString & i_rQuery) +throw (uno::RuntimeException, rdf::QueryException, rdf::RepositoryException) +{ + ::osl::MutexGuard g(m_aMutex); + const ::rtl::OString query( + ::rtl::OUStringToOString(i_rQuery, RTL_TEXTENCODING_UTF8) ); + const boost::shared_ptr<librdf_query> pQuery( + librdf_new_query(m_pWorld.get(), s_sparql, NULL, + reinterpret_cast<const unsigned char*> (query.getStr()), NULL), + librdf_free_query); + if (!pQuery) { + throw rdf::QueryException(::rtl::OUString::createFromAscii( + "librdf_Repository::querySelect: " + "librdf_new_query failed"), *this); + } + const boost::shared_ptr<librdf_query_results> pResults( + librdf_model_query_execute(m_pModel.get(), pQuery.get()), + librdf_free_query_results); + if (!pResults || !librdf_query_results_is_bindings(pResults.get())) { + throw rdf::QueryException(::rtl::OUString::createFromAscii( + "librdf_Repository::querySelect: " + "query result is null or not bindings"), *this); + } + + const int count( librdf_query_results_get_bindings_count(pResults.get()) ); + if (count >= 0) { + uno::Sequence< ::rtl::OUString > names(count); + for (int i = 0; i < count; ++i) { + const char* name( librdf_query_results_get_binding_name( + pResults.get(), i) ); + if (!name) { + throw rdf::QueryException(::rtl::OUString::createFromAscii( + "librdf_Repository::querySelect: " + "binding is null"), *this); + } + + names[i] = ::rtl::OUString::createFromAscii(name); + } + + return new librdf_QuerySelectResult(this, m_aMutex, + pQuery, pResults, names); + + } else { + throw rdf::QueryException(::rtl::OUString::createFromAscii( + "librdf_Repository::querySelect: " + "librdf_query_results_get_bindings_count failed"), *this); + } +} + +uno::Reference< container::XEnumeration > SAL_CALL +librdf_Repository::queryConstruct(const ::rtl::OUString & i_rQuery) +throw (uno::RuntimeException, rdf::QueryException, rdf::RepositoryException) +{ + ::osl::MutexGuard g(m_aMutex); + const ::rtl::OString query( + ::rtl::OUStringToOString(i_rQuery, RTL_TEXTENCODING_UTF8) ); + const boost::shared_ptr<librdf_query> pQuery( + librdf_new_query(m_pWorld.get(), s_sparql, NULL, + reinterpret_cast<const unsigned char*> (query.getStr()), NULL), + librdf_free_query); + if (!pQuery) { + throw rdf::QueryException(::rtl::OUString::createFromAscii( + "librdf_Repository::queryConstruct: " + "librdf_new_query failed"), *this); + } + const boost::shared_ptr<librdf_query_results> pResults( + librdf_model_query_execute(m_pModel.get(), pQuery.get()), + librdf_free_query_results); + if (!pResults || !librdf_query_results_is_graph(pResults.get())) { + throw rdf::QueryException(::rtl::OUString::createFromAscii( + "librdf_Repository::queryConstruct: " + "query result is null or not graph"), *this); + } + const boost::shared_ptr<librdf_stream> pStream( + librdf_query_results_as_stream(pResults.get()), + librdf_free_stream); + if (!pStream) { + throw rdf::QueryException(::rtl::OUString::createFromAscii( + "librdf_Repository::queryConstruct: " + "librdf_query_results_as_stream failed"), *this); + } + + return new librdf_GraphResult(this, m_aMutex, pStream, pQuery); +} + +::sal_Bool SAL_CALL +librdf_Repository::queryAsk(const ::rtl::OUString & i_rQuery) +throw (uno::RuntimeException, rdf::QueryException, rdf::RepositoryException) +{ + ::osl::MutexGuard g(m_aMutex); + + const ::rtl::OString query( + ::rtl::OUStringToOString(i_rQuery, RTL_TEXTENCODING_UTF8) ); + const boost::shared_ptr<librdf_query> pQuery( + librdf_new_query(m_pWorld.get(), s_sparql, NULL, + reinterpret_cast<const unsigned char*> (query.getStr()), NULL), + librdf_free_query); + if (!pQuery) { + throw rdf::QueryException(::rtl::OUString::createFromAscii( + "librdf_Repository::queryAsk: " + "librdf_new_query failed"), *this); + } + const boost::shared_ptr<librdf_query_results> pResults( + librdf_model_query_execute(m_pModel.get(), pQuery.get()), + librdf_free_query_results); + if (!pResults || !librdf_query_results_is_boolean(pResults.get())) { + throw rdf::QueryException(::rtl::OUString::createFromAscii( + "librdf_Repository::queryAsk: " + "query result is null or not boolean"), *this); + } + return librdf_query_results_get_boolean(pResults.get()) + ? sal_True : sal_False; +} + +// ::com::sun::star::rdf::XDocumentRepository: +void SAL_CALL librdf_Repository::setStatementRDFa( + const uno::Reference< rdf::XResource > & i_xSubject, + const uno::Sequence< uno::Reference< rdf::XURI > > & i_rPredicates, + const uno::Reference< rdf::XMetadatable > & i_xObject, + const ::rtl::OUString & i_rRDFaContent, + const uno::Reference< rdf::XURI > & i_xRDFaDatatype) +throw (uno::RuntimeException, lang::IllegalArgumentException, + rdf::RepositoryException) +{ + static const ::rtl::OUString s_cell( + ::rtl::OUString::createFromAscii("com.sun.star.table.Cell")); + static const ::rtl::OUString s_cellprops( // for writer + ::rtl::OUString::createFromAscii("com.sun.star.text.CellProperties")); + static const ::rtl::OUString s_paragraph( + ::rtl::OUString::createFromAscii("com.sun.star.text.Paragraph")); + static const ::rtl::OUString s_bookmark( + ::rtl::OUString::createFromAscii("com.sun.star.text.Bookmark")); + static const ::rtl::OUString s_meta( ::rtl::OUString::createFromAscii( + "com.sun.star.text.InContentMetadata")); + + if (!i_xSubject.is()) { + throw lang::IllegalArgumentException(::rtl::OUString::createFromAscii( + "librdf_Repository::setStatementRDFa: Subject is null"), *this, 0); + } + if (!i_rPredicates.getLength()) { + throw lang::IllegalArgumentException(::rtl::OUString::createFromAscii( + "librdf_Repository::setStatementRDFa: no Predicates"), + *this, 1); + } + for (sal_Int32 i = 0; i < i_rPredicates.getLength(); ++i) { + if (!i_rPredicates[i].is()) { + throw lang::IllegalArgumentException( + ::rtl::OUString::createFromAscii( + "librdf_Repository::setStatementRDFa: Predicate is null"), + *this, 1); + } + } + if (!i_xObject.is()) { + throw lang::IllegalArgumentException(::rtl::OUString::createFromAscii( + "librdf_Repository::setStatementRDFa: Object is null"), *this, 2); + } + const uno::Reference<lang::XServiceInfo> xService(i_xObject, + uno::UNO_QUERY_THROW); + uno::Reference<text::XTextRange> xTextRange; + if (xService->supportsService(s_cell) || + xService->supportsService(s_cellprops) || + xService->supportsService(s_paragraph)) + { + xTextRange.set(i_xObject, uno::UNO_QUERY_THROW); + } + else if (xService->supportsService(s_bookmark) || + xService->supportsService(s_meta)) + { + const uno::Reference<text::XTextContent> xTextContent(i_xObject, + uno::UNO_QUERY_THROW); + xTextRange = xTextContent->getAnchor(); + } + if (!xTextRange.is()) { + throw lang::IllegalArgumentException(::rtl::OUString::createFromAscii( + "librdf_Repository::setStatementRDFa: " + "Object does not support RDFa"), *this, 2); + } + // ensure that the metadatable has an XML ID + i_xObject->ensureMetadataReference(); + const beans::StringPair mdref( i_xObject->getMetadataReference() ); + if (mdref.First.equalsAscii("") || mdref.Second.equalsAscii("")) { + throw uno::RuntimeException( ::rtl::OUString::createFromAscii( + "librdf_Repository::setStatementRDFa: " + "ensureMetadataReference did not"), *this); + } + uno::Reference<rdf::XURI> xXmlId; + try { + xXmlId.set( rdf::URI::create(m_xContext, + ::rtl::OUString::createFromAscii(s_nsOOo) + + mdref.First + ::rtl::OUString::createFromAscii("#") + + mdref.Second), + uno::UNO_QUERY_THROW); + } catch (lang::IllegalArgumentException & iae) { + throw lang::WrappedTargetRuntimeException( + ::rtl::OUString::createFromAscii( + "librdf_Repository::setStatementRDFa: " + "cannot create URI for XML ID"), *this, uno::makeAny(iae)); + } + + ::osl::MutexGuard g(m_aMutex); + uno::Reference<rdf::XNode> xText; + try { + if (i_xRDFaDatatype.is() && (i_rRDFaContent.equalsAscii(""))) { + xText.set( rdf::Literal::createWithType(m_xContext, + xTextRange->getString(), i_xRDFaDatatype), + uno::UNO_QUERY_THROW); + } else { + xText.set( rdf::Literal::create(m_xContext, + xTextRange->getString()), uno::UNO_QUERY_THROW); + } + } catch (lang::IllegalArgumentException & iae) { + throw lang::WrappedTargetRuntimeException( + ::rtl::OUString::createFromAscii( + "librdf_Repository::setStatementRDFa: " + "cannot create literal"), *this, uno::makeAny(iae)); + } + if (i_rRDFaContent.equalsAscii("")) { + removeStatementRDFa(i_xObject); + ::std::for_each(::comphelper::stl_begin(i_rPredicates), + ::comphelper::stl_end(i_rPredicates), + ::boost::bind( &librdf_Repository::addStatementGraph, + this, i_xSubject, _1, xText, xXmlId, true)); + } else { + uno::Reference<rdf::XURI> xLabel( m_TypeConverter.getRDFsLabel() ); + uno::Reference<rdf::XNode> xContent; + try { + if (!i_xRDFaDatatype.is()) { + xContent.set(rdf::Literal::create(m_xContext, i_rRDFaContent), + uno::UNO_QUERY_THROW); + } else { + xContent.set(rdf::Literal::createWithType(m_xContext, + i_rRDFaContent, i_xRDFaDatatype), uno::UNO_QUERY_THROW); + } + } catch (lang::IllegalArgumentException & iae) { + throw lang::WrappedTargetRuntimeException( + ::rtl::OUString::createFromAscii( + "librdf_Repository::setStatementRDFa: " + "cannot create literal"), *this, uno::makeAny(iae)); + } + removeStatementRDFa(i_xObject); + ::std::for_each(::comphelper::stl_begin(i_rPredicates), + ::comphelper::stl_end(i_rPredicates), + ::boost::bind( &librdf_Repository::addStatementGraph, + this, i_xSubject, _1, xContent, xXmlId, true)); + addStatementGraph(i_xSubject, xLabel, xText, xXmlId, true); + } +} + +void SAL_CALL librdf_Repository::removeStatementRDFa( + const uno::Reference< rdf::XMetadatable > & i_xElement) +throw (uno::RuntimeException, lang::IllegalArgumentException, + rdf::RepositoryException) +{ + if (!i_xElement.is()) { + throw lang::IllegalArgumentException(::rtl::OUString::createFromAscii( + "librdf_Repository::removeStatementRDFa: Element is null"), + *this, 0); + } + + const beans::StringPair mdref( i_xElement->getMetadataReference() ); + if (mdref.First.equalsAscii("") || mdref.Second.equalsAscii("")) { + return; // nothing to do... + } + uno::Reference<rdf::XURI> xXmlId; + try { + xXmlId.set( rdf::URI::create(m_xContext, + ::rtl::OUString::createFromAscii(s_nsOOo) + + mdref.First + ::rtl::OUString::createFromAscii("#") + + mdref.Second), + uno::UNO_QUERY_THROW); + } catch (lang::IllegalArgumentException & iae) { + throw lang::WrappedTargetRuntimeException( + ::rtl::OUString::createFromAscii( + "librdf_Repository::removeStatementRDFa: " + "cannot create URI for XML ID"), *this, uno::makeAny(iae)); + } + // clearGraph does locking, not needed here + clearGraph(xXmlId, true); +} + +uno::Sequence<rdf::Statement> SAL_CALL +librdf_Repository::getStatementRDFa( + const uno::Reference< rdf::XMetadatable > & i_xElement) +throw (uno::RuntimeException, lang::IllegalArgumentException, + rdf::RepositoryException) +{ + if (!i_xElement.is()) { + throw lang::IllegalArgumentException(::rtl::OUString::createFromAscii( + "librdf_Repository::getStatementRDFa: Element is null"), *this, 0); + } + const beans::StringPair mdref( i_xElement->getMetadataReference() ); + if (mdref.First.equalsAscii("") || mdref.Second.equalsAscii("")) { + return uno::Sequence<rdf::Statement>(); + } + uno::Reference<rdf::XURI> xXmlId; + try { + xXmlId.set( rdf::URI::create(m_xContext, + ::rtl::OUString::createFromAscii(s_nsOOo) + + mdref.First + ::rtl::OUString::createFromAscii("#") + + mdref.Second), + uno::UNO_QUERY_THROW); + } catch (lang::IllegalArgumentException & iae) { + throw lang::WrappedTargetRuntimeException( + ::rtl::OUString::createFromAscii( + "librdf_Repository::getStatementRDFa: " + "cannot create URI for XML ID"), *this, uno::makeAny(iae)); + } + + ::osl::MutexGuard g(m_aMutex); + ::comphelper::SequenceAsVector< rdf::Statement > ret; + const uno::Reference<container::XEnumeration> xIter( + getStatementsGraph(0, 0, 0, xXmlId, true) ); + OSL_ENSURE(xIter.is(), "getStatementRDFa: no result?"); + if (!xIter.is()) throw uno::RuntimeException(); + const uno::Reference<rdf::XURI> xLabel( m_TypeConverter.getRDFsLabel() ); + while (xIter->hasMoreElements()) { + rdf::Statement stmt; + if (!(xIter->nextElement() >>= stmt)) { + OSL_ENSURE(false, "getStatementRDFa: result of wrong type?"); + } else { + OSL_ENSURE(stmt.Predicate.is(), "getStatementRDFa: no predicate?"); + if (stmt.Predicate->getStringValue() != xLabel->getStringValue()) { + ret.push_back(stmt); + } else { // the RDFs:label comes first + ret.insert(ret.begin(), stmt); + } + } + } + return ret.getAsConstList(); +} + +extern "C" +librdf_statement *rdfa_context_stream_map_handler( + librdf_stream *i_pStream, void *, librdf_statement *i_pStatement) +{ + OSL_ENSURE(i_pStream, "rdfa_context_stream_map_handler: stream null"); + if (i_pStream) { + librdf_node *pCtxt( static_cast<librdf_node *> + (librdf_stream_get_context(i_pStream)) ); + OSL_ENSURE(pCtxt, "rdfa_context_stream_map_handler: context null"); + if (pCtxt && isInternalContext(pCtxt)) { + return i_pStatement; + } + } + return 0; +}; + +uno::Reference< container::XEnumeration > SAL_CALL +librdf_Repository::getStatementsRDFa( + const uno::Reference< rdf::XResource > & i_xSubject, + const uno::Reference< rdf::XURI > & i_xPredicate, + const uno::Reference< rdf::XNode > & i_xObject) +throw (uno::RuntimeException, rdf::RepositoryException) +{ + if (isMetadatableWithoutMetadata(i_xSubject) || + isMetadatableWithoutMetadata(i_xPredicate) || + isMetadatableWithoutMetadata(i_xObject)) + { + return new librdf_GraphResult(this, m_aMutex, + ::boost::shared_ptr<librdf_stream>()); + } + + ::osl::MutexGuard g(m_aMutex); + const boost::shared_ptr<librdf_statement> pStatement( + m_TypeConverter.mkStatement(m_pWorld.get(), + i_xSubject, i_xPredicate, i_xObject), + librdf_free_statement); + OSL_ENSURE(pStatement, "mkStatement failed"); + + const boost::shared_ptr<librdf_stream> pStream( + librdf_model_find_statements(m_pModel.get(), pStatement.get()), + librdf_free_stream); + if (!pStream) { + throw rdf::RepositoryException(::rtl::OUString::createFromAscii( + "librdf_Repository::getStatementsRDFa: " + "librdf_model_find_statements failed"), *this); + } + + if (librdf_stream_add_map(pStream.get(), rdfa_context_stream_map_handler, + 0, 0)) { + throw rdf::RepositoryException(::rtl::OUString::createFromAscii( + "librdf_Repository::getStatementsRDFa: " + "librdf_stream_add_map failed"), *this); + } + + return new librdf_GraphResult(this, m_aMutex, pStream); +} + +// ::com::sun::star::lang::XInitialization: +void SAL_CALL librdf_Repository::initialize( + const uno::Sequence< ::com::sun::star::uno::Any > & i_rArguments) +throw (uno::RuntimeException, uno::Exception) +{ + (void) i_rArguments; + + ::osl::MutexGuard g(m_aMutex); + +// m_pWorld.reset(m_TypeConverter.createWorld(), librdf_free_world); + m_pStorage.reset(m_TypeConverter.createStorage(m_pWorld.get()), + librdf_free_storage); + m_pModel.reset(m_TypeConverter.createModel( + m_pWorld.get(), m_pStorage.get()), librdf_free_model); +} + +const NamedGraphMap_t::iterator SAL_CALL librdf_Repository::clearGraph( + const uno::Reference< rdf::XURI > & i_xGraphName, bool i_Internal) +// throw (uno::RuntimeException, container::NoSuchElementException, +// rdf::RepositoryException) +{ + if (!i_xGraphName.is()) { + throw lang::IllegalArgumentException( + ::rtl::OUString::createFromAscii("librdf_Repository::clearGraph: " + "URI is null"), *this, 0); + } + ::osl::MutexGuard g(m_aMutex); + const ::rtl::OUString contextU( i_xGraphName->getStringValue() ); + const NamedGraphMap_t::iterator iter( m_NamedGraphs.find(contextU) ); + if (!i_Internal && iter == m_NamedGraphs.end()) { + throw container::NoSuchElementException( + ::rtl::OUString::createFromAscii("librdf_Repository::clearGraph: " + "no graph with given URI exists"), *this); + } + const ::rtl::OString context( + ::rtl::OUStringToOString(contextU, RTL_TEXTENCODING_UTF8) ); + + const boost::shared_ptr<librdf_node> pContext( + librdf_new_node_from_uri_string(m_pWorld.get(), + reinterpret_cast<const unsigned char*> (context.getStr())), + librdf_free_node); + if (!pContext) { + throw uno::RuntimeException(::rtl::OUString::createFromAscii( + "librdf_Repository::clearGraph: " + "librdf_new_node_from_uri_string failed"), *this); + } + if (librdf_model_context_remove_statements(m_pModel.get(), pContext.get())) + { + throw rdf::RepositoryException(::rtl::OUString::createFromAscii( + "librdf_Repository::clearGraph: " + "librdf_model_context_remove_statements failed"), *this); + } + return iter; +} + +void SAL_CALL librdf_Repository::addStatementGraph( + const uno::Reference< rdf::XResource > & i_xSubject, + const uno::Reference< rdf::XURI > & i_xPredicate, + const uno::Reference< rdf::XNode > & i_xObject, + const uno::Reference< rdf::XURI > & i_xGraphName, + bool i_Internal) +//throw (uno::RuntimeException, lang::IllegalArgumentException, +// container::NoSuchElementException, rdf::RepositoryException) +{ + if (!i_xSubject.is()) { + throw lang::IllegalArgumentException(::rtl::OUString::createFromAscii( + "librdf_Repository::addStatement: Subject is null"), *this, 0); + } + if (!i_xPredicate.is()) { + throw lang::IllegalArgumentException(::rtl::OUString::createFromAscii( + "librdf_Repository::addStatement: Predicate is null"), + *this, 1); + } + if (!i_xObject.is()) { + throw lang::IllegalArgumentException(::rtl::OUString::createFromAscii( + "librdf_Repository::addStatement: Object is null"), *this, 2); + } + + ::osl::MutexGuard g(m_aMutex); + const ::rtl::OUString contextU( i_xGraphName->getStringValue() ); + if (!i_Internal && (m_NamedGraphs.find(contextU) == m_NamedGraphs.end())) { + throw container::NoSuchElementException( + ::rtl::OUString::createFromAscii("librdf_Repository::addStatement: " + "no graph with given URI exists"), *this); + } + const ::rtl::OString context( + ::rtl::OUStringToOString(contextU, RTL_TEXTENCODING_UTF8) ); + + const boost::shared_ptr<librdf_node> pContext( + librdf_new_node_from_uri_string(m_pWorld.get(), + reinterpret_cast<const unsigned char*> (context.getStr())), + librdf_free_node); + if (!pContext) { + throw uno::RuntimeException(::rtl::OUString::createFromAscii( + "librdf_Repository::addStatement: " + "librdf_new_node_from_uri_string failed"), *this); + } + const boost::shared_ptr<librdf_statement> pStatement( + m_TypeConverter.mkStatement(m_pWorld.get(), + i_xSubject, i_xPredicate, i_xObject), + librdf_free_statement); + OSL_ENSURE(pStatement, "mkStatement failed"); + if (librdf_model_context_add_statement(m_pModel.get(), + pContext.get(), pStatement.get())) { + throw rdf::RepositoryException(::rtl::OUString::createFromAscii( + "librdf_Repository::addStatement: " + "librdf_model_context_add_statement failed"), *this); + } +} + +void SAL_CALL librdf_Repository::removeStatementsGraph( + const uno::Reference< rdf::XResource > & i_xSubject, + const uno::Reference< rdf::XURI > & i_xPredicate, + const uno::Reference< rdf::XNode > & i_xObject, + const uno::Reference< rdf::XURI > & i_xGraphName) +//throw (uno::RuntimeException, lang::IllegalArgumentException, +// container::NoSuchElementException, rdf::RepositoryException) +{ + if (isMetadatableWithoutMetadata(i_xSubject) || + isMetadatableWithoutMetadata(i_xPredicate) || + isMetadatableWithoutMetadata(i_xObject)) + { + return; + } + + ::osl::MutexGuard g(m_aMutex); + const ::rtl::OUString contextU( i_xGraphName->getStringValue() ); + if (m_NamedGraphs.find(contextU) == m_NamedGraphs.end()) { + throw container::NoSuchElementException( + ::rtl::OUString::createFromAscii( + "librdf_Repository::removeStatements: " + "no graph with given URI exists"), *this); + } + const ::rtl::OString context( + ::rtl::OUStringToOString(contextU, RTL_TEXTENCODING_UTF8) ); + + const boost::shared_ptr<librdf_node> pContext( + librdf_new_node_from_uri_string(m_pWorld.get(), + reinterpret_cast<const unsigned char*> (context.getStr())), + librdf_free_node); + if (!pContext) { + throw uno::RuntimeException(::rtl::OUString::createFromAscii( + "librdf_Repository::removeStatements: " + "librdf_new_node_from_uri_string failed"), *this); + } + const boost::shared_ptr<librdf_statement> pStatement( + m_TypeConverter.mkStatement(m_pWorld.get(), + i_xSubject, i_xPredicate, i_xObject), + librdf_free_statement); + OSL_ENSURE(pStatement, "mkStatement failed"); + + const boost::shared_ptr<librdf_stream> pStream( + librdf_model_find_statements_in_context(m_pModel.get(), + pStatement.get(), pContext.get()), + librdf_free_stream); + if (!pStream) { + throw rdf::RepositoryException(::rtl::OUString::createFromAscii( + "librdf_Repository::removeStatements: " + "librdf_model_find_statements_in_context failed"), *this); + } + + if (!librdf_stream_end(pStream.get())) { + do { + librdf_statement *pStmt( librdf_stream_get_object(pStream.get()) ); + if (!pStmt) { + throw rdf::RepositoryException(::rtl::OUString::createFromAscii( + "librdf_Repository::removeStatements: " + "librdf_stream_get_object failed"), *this); + } + if (librdf_model_context_remove_statement(m_pModel.get(), + pContext.get(), pStmt)) { + throw rdf::RepositoryException(::rtl::OUString::createFromAscii( + "librdf_Repository::removeStatements: " + "librdf_model_context_remove_statement failed"), *this); + } + } while (!librdf_stream_next(pStream.get())); + } +} + +uno::Reference< container::XEnumeration > SAL_CALL +librdf_Repository::getStatementsGraph( + const uno::Reference< rdf::XResource > & i_xSubject, + const uno::Reference< rdf::XURI > & i_xPredicate, + const uno::Reference< rdf::XNode > & i_xObject, + const uno::Reference< rdf::XURI > & i_xGraphName, + bool i_Internal) +//throw (uno::RuntimeException, lang::IllegalArgumentException, +// container::NoSuchElementException, rdf::RepositoryException) +{ + // N.B.: if any of subject, predicate, object is an XMetadatable, and + // has no metadata reference, then there cannot be any node in the graph + // representing it; in order to prevent side effect + // (ensureMetadataReference), check for this condition and return + if (isMetadatableWithoutMetadata(i_xSubject) || + isMetadatableWithoutMetadata(i_xPredicate) || + isMetadatableWithoutMetadata(i_xObject)) + { + return new librdf_GraphResult(this, m_aMutex, + ::boost::shared_ptr<librdf_stream>()); + } + + ::osl::MutexGuard g(m_aMutex); + const ::rtl::OUString contextU( i_xGraphName->getStringValue() ); + if (!i_Internal && (m_NamedGraphs.find(contextU) == m_NamedGraphs.end())) { + throw container::NoSuchElementException( + ::rtl::OUString::createFromAscii( + "librdf_Repository::getStatements: " + "no graph with given URI exists"), *this); + } + const ::rtl::OString context( + ::rtl::OUStringToOString(contextU, RTL_TEXTENCODING_UTF8) ); + + const boost::shared_ptr<librdf_node> pContext( + librdf_new_node_from_uri_string(m_pWorld.get(), + reinterpret_cast<const unsigned char*> (context.getStr())), + librdf_free_node); + if (!pContext) { + throw uno::RuntimeException(::rtl::OUString::createFromAscii( + "librdf_Repository::getStatements: " + "librdf_new_node_from_uri_string failed"), *this); + } + const boost::shared_ptr<librdf_statement> pStatement( + m_TypeConverter.mkStatement(m_pWorld.get(), + i_xSubject, i_xPredicate, i_xObject), + librdf_free_statement); + OSL_ENSURE(pStatement, "mkStatement failed"); + + const boost::shared_ptr<librdf_stream> pStream( + librdf_model_find_statements_in_context(m_pModel.get(), + pStatement.get(), pContext.get()), + librdf_free_stream); + if (!pStream) { + throw rdf::RepositoryException(::rtl::OUString::createFromAscii( + "librdf_Repository::getStatements: " + "librdf_model_find_statements_in_context failed"), *this); + } + + return new librdf_GraphResult(this, m_aMutex, pStream); +} + +librdf_world *librdf_TypeConverter::createWorld() const +{ + // create and initialize world + librdf_world *pWorld( librdf_new_world() ); + if (!pWorld) { + throw uno::RuntimeException(::rtl::OUString::createFromAscii( + "librdf_TypeConverter::createWorld: librdf_new_world failed"), + m_rRep); + } + //FIXME logger, digest, features? + librdf_world_open(pWorld); + return pWorld; +} + +librdf_storage * +librdf_TypeConverter::createStorage(librdf_world *i_pWorld) const +{ + librdf_storage *pStorage( +// librdf_new_storage(i_pWorld, "memory", NULL, "contexts='yes'") ); + librdf_new_storage(i_pWorld, "hashes", NULL, + "contexts='yes',hash-type='memory'") ); + if (!pStorage) { + throw uno::RuntimeException(::rtl::OUString::createFromAscii( + "librdf_TypeConverter::createStorage: librdf_new_storage failed"), + m_rRep); + } + return pStorage; +} + +librdf_model *librdf_TypeConverter::createModel( + librdf_world *i_pWorld, librdf_storage * i_pStorage) const +{ + librdf_model *pRepository( librdf_new_model(i_pWorld, i_pStorage, NULL) ); + if (!pRepository) { + throw uno::RuntimeException(::rtl::OUString::createFromAscii( + "librdf_TypeConverter::createModel: librdf_new_model failed"), + m_rRep); + } + //FIXME +#if 0 + { + librdf_uri * ctxt = librdf_new_uri(i_pWorld, reinterpret_cast<const unsigned char *>(LIBRDF_MODEL_FEATURE_CONTEXTS)); + librdf_node * contexts = librdf_model_get_feature(repository, ctxt); + if (!contexts) + throw; + std::cout << "value of contexts feature: "; + prtNode(contexts); + std::cout << std::endl; + // librdf_model_set_feature(repository, LIBRDF_FEATURE_CONTEXTS, ...); + librdf_free_node(contexts); + librdf_free_uri(ctxt); + } +#endif + return pRepository; +} + +// this does NOT create a node, only URI +librdf_uri* librdf_TypeConverter::mkURI( librdf_world* i_pWorld, + const uno::Reference< rdf::XURI > & i_xURI) const +{ + const ::rtl::OString uri( + ::rtl::OUStringToOString(i_xURI->getStringValue(), + RTL_TEXTENCODING_UTF8) ); + librdf_uri *pURI( librdf_new_uri(i_pWorld, + reinterpret_cast<const unsigned char *>(uri.getStr()))); + if (!pURI) { + throw uno::RuntimeException(::rtl::OUString::createFromAscii( + "librdf_TypeConverter::mkURI: librdf_new_uri failed"), 0); + } + return pURI; +} + +// create blank or URI node +librdf_node* librdf_TypeConverter::mkResource( librdf_world* i_pWorld, + const uno::Reference< rdf::XResource > & i_xResource) const +{ + if (!i_xResource.is()) return 0; + uno::Reference< rdf::XBlankNode > xBlankNode(i_xResource, uno::UNO_QUERY); + if (xBlankNode.is()) { + const ::rtl::OString label( + ::rtl::OUStringToOString(xBlankNode->getStringValue(), + RTL_TEXTENCODING_UTF8) ); + librdf_node *pNode( + librdf_new_node_from_blank_identifier(i_pWorld, + reinterpret_cast<const unsigned char*> (label.getStr()))); + if (!pNode) { + throw uno::RuntimeException(::rtl::OUString::createFromAscii( + "librdf_TypeConverter::mkResource: " + "librdf_new_node_from_blank_identifier failed"), 0); + } + return pNode; + } else { // assumption: everything else is URI + const ::rtl::OString uri( + ::rtl::OUStringToOString(i_xResource->getStringValue(), + RTL_TEXTENCODING_UTF8) ); + librdf_node *pNode( + librdf_new_node_from_uri_string(i_pWorld, + reinterpret_cast<const unsigned char*> (uri.getStr()))); + if (!pNode) { + throw uno::RuntimeException(::rtl::OUString::createFromAscii( + "librdf_TypeConverter::mkResource: " + "librdf_new_node_from_uri_string failed"), 0); + } + return pNode; + } +} + +// create blank or URI or literal node +librdf_node* librdf_TypeConverter::mkNode( librdf_world* i_pWorld, + const uno::Reference< rdf::XNode > & i_xNode) const +{ + if (!i_xNode.is()) return 0; + uno::Reference< rdf::XResource > xResource(i_xNode, uno::UNO_QUERY); + if (xResource.is()) { + return mkResource(i_pWorld, xResource); + } + uno::Reference< rdf::XLiteral> xLiteral(i_xNode, uno::UNO_QUERY); + OSL_ENSURE(xLiteral.is(), + "mkNode: someone invented a new rdf.XNode and did not tell me"); + if (!xLiteral.is()) return 0; + const ::rtl::OString val( + ::rtl::OUStringToOString(xLiteral->getValue(), + RTL_TEXTENCODING_UTF8) ); + const ::rtl::OString lang( + ::rtl::OUStringToOString(xLiteral->getLanguage(), + RTL_TEXTENCODING_UTF8) ); + const uno::Reference< rdf::XURI > xType(xLiteral->getDatatype()); + librdf_node * ret(0); + if (lang.getLength() == 0) { + if (!xType.is()) { + ret = librdf_new_node_from_literal(i_pWorld, + reinterpret_cast<const unsigned char*> (val.getStr()), + NULL, 0); + } else { + const boost::shared_ptr<librdf_uri> pDatatype( + mkURI(i_pWorld, xType), librdf_free_uri); + ret = librdf_new_node_from_typed_literal(i_pWorld, + reinterpret_cast<const unsigned char*> (val.getStr()), + NULL, pDatatype.get()); + } + } else { + if (!xType.is()) { + ret = librdf_new_node_from_literal(i_pWorld, + reinterpret_cast<const unsigned char*> (val.getStr()), + (lang.getStr()), 0); + + } else { + OSL_ENSURE(false, "mkNode: invalid literal"); + return 0; + } + } + if (!ret) { + throw uno::RuntimeException(::rtl::OUString::createFromAscii( + "librdf_TypeConverter::mkNode: " + "librdf_new_node_from_literal failed"), 0); + } + return ret; +} + +librdf_statement* librdf_TypeConverter::mkStatement( librdf_world* i_pWorld, + const uno::Reference< rdf::XResource > & i_xSubject, + const uno::Reference< rdf::XURI > & i_xPredicate, + const uno::Reference< rdf::XNode > & i_xObject) const +{ + librdf_node* pSubject( mkResource(i_pWorld, i_xSubject) ); + librdf_node* pPredicate(0); + librdf_node* pObject(0); + try { + const uno::Reference<rdf::XResource> xPredicate(i_xPredicate, + uno::UNO_QUERY); + pPredicate = mkResource(i_pWorld, xPredicate); + try { + pObject = mkNode(i_pWorld, i_xObject); + } catch (...) { + librdf_free_node(pPredicate); + throw; + } + } catch (...) { + librdf_free_node(pSubject); + throw; + } + // NB: this takes ownership of the nodes! (which is really ugly) + librdf_statement* pStatement( librdf_new_statement_from_nodes(i_pWorld, + pSubject, pPredicate, pObject) ); + if (!pStatement) { + throw uno::RuntimeException(::rtl::OUString::createFromAscii( + "librdf_TypeConverter::mkStatement: " + "librdf_new_statement_from_nodes failed"), 0); + } + return pStatement; +} + +uno::Reference<rdf::XURI> +librdf_TypeConverter::convertToXURI(librdf_uri* i_pURI) const +{ + if (!i_pURI) return 0; + const unsigned char* uri( librdf_uri_as_string(i_pURI) ); + if (!uri) { + throw uno::RuntimeException(::rtl::OUString::createFromAscii( + "librdf_TypeConverter::convertToXURI: " + "librdf_uri_as_string failed"), m_rRep); + } + ::rtl::OUString uriU( ::rtl::OStringToOUString( + ::rtl::OString(reinterpret_cast<const sal_Char*>(uri)), + RTL_TEXTENCODING_UTF8) ); + try { + return rdf::URI::create(m_xContext, uriU); + } catch (lang::IllegalArgumentException & iae) { + throw lang::WrappedTargetRuntimeException( + ::rtl::OUString::createFromAscii( + "librdf_TypeConverter::convertToXURI: " + "illegal uri"), m_rRep, uno::makeAny(iae)); + } +} + +uno::Reference<rdf::XURI> +librdf_TypeConverter::convertToXURI(librdf_node* i_pNode) const +{ + if (!i_pNode) return 0; + if (librdf_node_is_resource(i_pNode)) { + librdf_uri* pURI( librdf_node_get_uri(i_pNode) ); + if (!pURI) { + throw uno::RuntimeException(::rtl::OUString::createFromAscii( + "librdf_TypeConverter::convertToXURI: " + "resource has no uri"), m_rRep); + } + return convertToXURI(pURI); + } else { + OSL_ENSURE(false, "convertToXURI: unknown librdf_node"); + return 0; + } +} + +uno::Reference<rdf::XResource> +librdf_TypeConverter::convertToXResource(librdf_node* i_pNode) const +{ + if (!i_pNode) return 0; + if (librdf_node_is_blank(i_pNode)) { + const unsigned char* label( librdf_node_get_blank_identifier(i_pNode) ); + if (!label) { + throw uno::RuntimeException(::rtl::OUString::createFromAscii( + "librdf_TypeConverter::convertToXResource: " + "blank node has no label"), m_rRep); + } + ::rtl::OUString labelU( ::rtl::OStringToOUString( + ::rtl::OString(reinterpret_cast<const sal_Char*>(label)), + RTL_TEXTENCODING_UTF8) ); + try { + return uno::Reference<rdf::XResource>( + rdf::BlankNode::create(m_xContext, labelU), uno::UNO_QUERY); + } catch (lang::IllegalArgumentException & iae) { + throw lang::WrappedTargetRuntimeException( + ::rtl::OUString::createFromAscii( + "librdf_TypeConverter::convertToXResource: " + "illegal blank node label"), m_rRep, uno::makeAny(iae)); + } + } else { + return uno::Reference<rdf::XResource>(convertToXURI(i_pNode), + uno::UNO_QUERY); + } +} + +uno::Reference<rdf::XNode> +librdf_TypeConverter::convertToXNode(librdf_node* i_pNode) const +{ + if (!i_pNode) return 0; + if (!librdf_node_is_literal(i_pNode)) { + return uno::Reference<rdf::XNode>(convertToXResource(i_pNode), + uno::UNO_QUERY); + } + const unsigned char* value( librdf_node_get_literal_value(i_pNode) ); + if (!value) { + throw uno::RuntimeException(::rtl::OUString::createFromAscii( + "librdf_TypeConverter::convertToXNode: " + "literal has no value"), m_rRep); + } + const char * lang( librdf_node_get_literal_value_language(i_pNode) ); + librdf_uri* pType( + librdf_node_get_literal_value_datatype_uri(i_pNode) ); + OSL_ENSURE(!lang || !pType, "convertToXNode: invalid literal"); + const ::rtl::OUString valueU( ::rtl::OStringToOUString( + ::rtl::OString(reinterpret_cast<const sal_Char*>(value)), + RTL_TEXTENCODING_UTF8) ); + if (lang) { + const ::rtl::OUString langU( ::rtl::OStringToOUString( + ::rtl::OString(reinterpret_cast<const sal_Char*>(lang)), + RTL_TEXTENCODING_UTF8) ); + return uno::Reference<rdf::XNode>( + rdf::Literal::createWithLanguage(m_xContext, valueU, langU), + uno::UNO_QUERY); + } else if (pType) { + uno::Reference<rdf::XURI> xType(convertToXURI(pType)); + OSL_ENSURE(xType.is(), "convertToXNode: null uri"); + return uno::Reference<rdf::XNode>( + rdf::Literal::createWithType(m_xContext, valueU, xType), + uno::UNO_QUERY); + } else { + return uno::Reference<rdf::XNode>( + rdf::Literal::create(m_xContext, valueU), + uno::UNO_QUERY); + } +} + +#if 0 +uno::Reference<rdf::XStatement> +librdf_TypeConverter::convertToXStatement(librdf_statement* i_pStmt, + librdf_node* i_pContext) const +{ + if (!i_pStmt) { + throw uno::RuntimeException(); + } + return new librdf_Statement( + convertToXResource(librdf_statement_get_subject(i_pStmt)), + convertToXResource(librdf_statement_get_predicate(i_pStmt)), + convertToXNode(librdf_statement_get_object(i_pStmt)), + convertToXURI(i_pContext)); +} +#endif + +rdf::Statement +librdf_TypeConverter::convertToStatement(librdf_statement* i_pStmt, + librdf_node* i_pContext) const +{ + if (!i_pStmt) { + throw uno::RuntimeException(); + } + return rdf::Statement( + convertToXResource(librdf_statement_get_subject(i_pStmt)), + convertToXURI(librdf_statement_get_predicate(i_pStmt)), + convertToXNode(librdf_statement_get_object(i_pStmt)), + convertToXURI(i_pContext)); +} + +uno::Reference<rdf::XURI> librdf_TypeConverter::getRDFsLabel() const +{ + static uno::Reference< rdf::XURI> xLabel; + + if (!xLabel.is()) { + try { + // rdfs:label + xLabel.set(rdf::URI::createKnown(m_xContext, + rdf::URIs::RDFS_LABEL), + uno::UNO_QUERY_THROW); + } catch (lang::IllegalArgumentException & iae) { + throw lang::WrappedTargetRuntimeException( + ::rtl::OUString::createFromAscii( + "librdf_TypeConverter::getRDFsLabel: " + "cannot create rdfs:label"), m_rRep, uno::makeAny(iae)); + } + } + return xLabel; +} + +} // closing anonymous implementation namespace + + + +// component helper namespace +namespace comp_librdf_Repository { + +::rtl::OUString SAL_CALL _getImplementationName() { + return ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( + "librdf_Repository")); +} + +uno::Sequence< ::rtl::OUString > SAL_CALL _getSupportedServiceNames() +{ + uno::Sequence< ::rtl::OUString > s(1); + s[0] = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( + "com.sun.star.rdf.Repository")); + return s; +} + +uno::Reference< uno::XInterface > SAL_CALL _create( + const uno::Reference< uno::XComponentContext > & context) + SAL_THROW((uno::Exception)) +{ + return static_cast< ::cppu::OWeakObject * >(new librdf_Repository(context)); +} + +} // closing component helper namespace + diff --git a/unoxml/source/rdf/librdf_repository.hxx b/unoxml/source/rdf/librdf_repository.hxx new file mode 100644 index 000000000000..b89f47ac24ad --- /dev/null +++ b/unoxml/source/rdf/librdf_repository.hxx @@ -0,0 +1,51 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2008 by Sun Microsystems, Inc. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: librdf_repository.hxx,v $ + * $Revision: 1.2 $ + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#ifndef _LIBRDF_REPOSITORY_HXX +#define _LIBRDF_REPOSITORY_HXX + +#include <sal/config.h> +#include <cppuhelper/factory.hxx> + + +// component helper namespace +namespace comp_librdf_Repository { + +namespace css = ::com::sun::star; + +// component and service helper functions: +::rtl::OUString SAL_CALL _getImplementationName(); +css::uno::Sequence< ::rtl::OUString > SAL_CALL _getSupportedServiceNames(); +css::uno::Reference< css::uno::XInterface > SAL_CALL _create( css::uno::Reference< css::uno::XComponentContext > const & context ); + +} // closing component helper namespace + +#endif + diff --git a/unoxml/source/rdf/librdf_services.cxx b/unoxml/source/rdf/librdf_services.cxx new file mode 100644 index 000000000000..8a0a32c8e0f3 --- /dev/null +++ b/unoxml/source/rdf/librdf_services.cxx @@ -0,0 +1,86 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2008 by Sun Microsystems, Inc. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: librdf_services.cxx,v $ + * $Revision: 1.2 $ + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#include <cppuhelper/factory.hxx> +#include <cppuhelper/implementationentry.hxx> + +#include "CNodes.hxx" +#include "librdf_repository.hxx" + + +using namespace ::com::sun::star; + + +extern "C" +{ + +void SAL_CALL +component_getImplementationEnvironment(const sal_Char **o_ppEnvironmentTypeName, + uno_Environment ** /* ppEnvironment */) +{ + *o_ppEnvironmentTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME; +} + +static ::cppu::ImplementationEntry const entries[] = { + { &comp_CBlankNode::_create, + &comp_CBlankNode::_getImplementationName, + &comp_CBlankNode::_getSupportedServiceNames, + &::cppu::createSingleComponentFactory, 0, 0 }, + { &comp_CURI::_create, + &comp_CURI::_getImplementationName, + &comp_CURI::_getSupportedServiceNames, + &::cppu::createSingleComponentFactory, 0, 0 }, + { &comp_CLiteral::_create, + &comp_CLiteral::_getImplementationName, + &comp_CLiteral::_getSupportedServiceNames, + &::cppu::createSingleComponentFactory, 0, 0 }, + { &comp_librdf_Repository::_create, + &comp_librdf_Repository::_getImplementationName, + &comp_librdf_Repository::_getSupportedServiceNames, + &::cppu::createSingleComponentFactory, 0, 0 }, + { 0, 0, 0, 0, 0, 0 } +}; + +extern "C" void * SAL_CALL component_getFactory( + const char * implName, void * serviceManager, void * registryKey) +{ + return ::cppu::component_getFactoryHelper( + implName, serviceManager, registryKey, entries); +} + +extern "C" sal_Bool SAL_CALL component_writeInfo( + void * serviceManager, void * registryKey) +{ + return ::cppu::component_writeInfoHelper(serviceManager, registryKey, + entries); +} + +} // extern "C" + diff --git a/unoxml/source/rdf/makefile.mk b/unoxml/source/rdf/makefile.mk new file mode 100644 index 000000000000..298c1dcdae25 --- /dev/null +++ b/unoxml/source/rdf/makefile.mk @@ -0,0 +1,80 @@ +#************************************************************************* +# +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. +# +# Copyright 2008 by Sun Microsystems, Inc. +# +# OpenOffice.org - a multi-platform office productivity suite +# +# $RCSfile: makefile.mk,v $ +# +# $Revision: 1.2 $ +# +# This file is part of OpenOffice.org. +# +# OpenOffice.org is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License version 3 +# only, as published by the Free Software Foundation. +# +# OpenOffice.org is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License version 3 for more details +# (a copy is included in the LICENSE file that accompanied this code). +# +# You should have received a copy of the GNU Lesser General Public License +# version 3 along with OpenOffice.org. If not, see +# <http://www.openoffice.org/license.html> +# for a copy of the LGPLv3 License. +# +#************************************************************************* + +PRJ=..$/.. + +PRJNAME=unoxml +TARGET=unordf +LIBTARGET=NO + +ENABLE_EXCEPTIONS=TRUE + +# --- Settings ----------------------------------------------------- + +.INCLUDE : settings.mk + +.IF "$(SYSTEM_REDLAND)" == "YES" +CFLAGS+=-DSYSTEM_REDLAND $(REDLAND_CFLAGS) +.ENDIF + +# --- Files -------------------------------------------------------- +.IF "$(L10N_framework)"=="" + +SLOFILES = \ + $(SLO)$/CBlankNode.obj \ + $(SLO)$/CURI.obj \ + $(SLO)$/CLiteral.obj \ + $(SLO)$/librdf_repository.obj \ + $(SLO)$/librdf_services.obj + + +SHL1DEPN= makefile.mk +SHL1OBJS= $(SLOFILES) + +SHL1TARGET= $(TARGET)$(DLLPOSTFIX) +SHL1IMPLIB= i$(TARGET) + +SHL1VERSIONMAP=../service/exports.map +SHL1DEF=$(MISC)$/$(SHL1TARGET).def +DEF1NAME=$(SHL1TARGET) + +SHL1STDLIBS= \ + $(REDLANDLIB) \ + $(CPPUHELPERLIB) \ + $(CPPULIB) \ + $(SALLIB) \ + +.ENDIF + +# --- Targets ------------------------------------------------------ + +.INCLUDE : target.mk + diff --git a/unoxml/source/service/exports.map b/unoxml/source/service/exports.map new file mode 100644 index 000000000000..ba501f9ae076 --- /dev/null +++ b/unoxml/source/service/exports.map @@ -0,0 +1,10 @@ +UDK_3_0_0 { + global: + GetVersionInfo; + component_getImplementationEnvironment; + component_getFactory; + component_writeInfo; + + local: + *; +}; diff --git a/unoxml/source/service/makefile.mk b/unoxml/source/service/makefile.mk new file mode 100644 index 000000000000..e86e07f1c89e --- /dev/null +++ b/unoxml/source/service/makefile.mk @@ -0,0 +1,82 @@ +#************************************************************************* +# +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. +# +# Copyright 2008 by Sun Microsystems, Inc. +# +# OpenOffice.org - a multi-platform office productivity suite +# +# $RCSfile: makefile.mk,v $ +# +# $Revision: 1.12 $ +# +# This file is part of OpenOffice.org. +# +# OpenOffice.org is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License version 3 +# only, as published by the Free Software Foundation. +# +# OpenOffice.org is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License version 3 for more details +# (a copy is included in the LICENSE file that accompanied this code). +# +# You should have received a copy of the GNU Lesser General Public License +# version 3 along with OpenOffice.org. If not, see +# <http://www.openoffice.org/license.html> +# for a copy of the LGPLv3 License. +# +#************************************************************************* + +PRJ=..$/.. + +PRJNAME=unoxml +TARGET=unoxml +LIBTARGET=NO + +ENABLE_EXCEPTIONS=TRUE + +# --- Settings ----------------------------------------------------- + +.INCLUDE : settings.mk + +.IF "$(SYSTEM_LIBXML)" == "YES" +CFLAGS+=-DSYSTEM_LIBXML $(LIBXML_CFLAGS) +.ENDIF + +# --- Files -------------------------------------------------------- + +SLOFILES = \ + $(SLO)$/services.obj + + +SHL1DEPN= makefile.mk +SHL1OBJS= $(SLOFILES) + +SHL1TARGET= $(TARGET)$(DLLPOSTFIX) +SHL1IMPLIB= i$(TARGET) + +SHL1VERSIONMAP=exports.map +SHL1DEF=$(MISC)$/$(SHL1TARGET).def +DEF1NAME=$(SHL1TARGET) + +SHL1LIBS= \ + $(SLB)$/domimpl.lib \ + $(SLB)$/xpathimpl.lib \ + $(SLB)$/eventsimpl.lib + +SHL1STDLIBS= \ + $(UCBHELPERLIB) \ + $(LIBXML2LIB) \ + $(COMPHELPERLIB) \ + $(CPPUHELPERLIB) \ + $(CPPULIB) \ + $(SAXLIB) \ + $(SALLIB)\ + $(EXPATASCII3RDLIB) + +# --- Targets ------------------------------------------------------ + +.INCLUDE : target.mk + diff --git a/unoxml/source/service/services.cxx b/unoxml/source/service/services.cxx new file mode 100644 index 000000000000..aa4f1b18ca60 --- /dev/null +++ b/unoxml/source/service/services.cxx @@ -0,0 +1,156 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2008 by Sun Microsystems, Inc. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: services.cxx,v $ + * $Revision: 1.9 $ + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#include <osl/mutex.hxx> +#include <rtl/ustring.hxx> +#include <com/sun/star/uno/Reference.h> +#include <com/sun/star/uno/Reference.hxx> +#include <cppuhelper/interfacecontainer.h> +#include <cppuhelper/factory.hxx> +#include <com/sun/star/lang/XServiceInfo.hpp> +#include <com/sun/star/uno/Exception.hpp> +#include <com/sun/star/lang/XComponent.hpp> +#include <com/sun/star/lang/XSingleServiceFactory.hpp> +#include <com/sun/star/registry/XRegistryKey.hpp> +#include <cppuhelper/factory.hxx> + +#include "../dom/documentbuilder.hxx" +#include "../dom/saxbuilder.hxx" +#include "../xpath/xpathapi.hxx" +#include "../events/testlistener.hxx" + +using namespace ::DOM; +using namespace ::DOM::events; +using namespace ::XPath; +using ::rtl::OUString; +using namespace ::com::sun::star::uno; +using namespace ::com::sun::star::lang; +using namespace ::com::sun::star::registry; + +extern "C" +{ + +void SAL_CALL +component_getImplementationEnvironment(const sal_Char **ppEnvironmentTypeName, uno_Environment ** /*ppEnvironment */) +{ + *ppEnvironmentTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME ; +} + +sal_Bool SAL_CALL +component_writeInfo(void * /*pServiceManager*/, void* pRegistryKey ) +{ + Reference< XRegistryKey > xKey(reinterpret_cast< XRegistryKey* >(pRegistryKey)); + Reference< XRegistryKey > xNewKey; + OUString aImpl; + + // register DOM service + aImpl = OUString(RTL_CONSTASCII_USTRINGPARAM("/")); + aImpl += CDocumentBuilder::_getImplementationName(); + aImpl += ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("/UNO/SERVICES")); + xNewKey = xKey->createKey(aImpl); + xNewKey->createKey(CDocumentBuilder::_getSupportedServiceNames()[0]); + + // register DOM service + aImpl = OUString(RTL_CONSTASCII_USTRINGPARAM("/")); + aImpl += CSAXDocumentBuilder::_getImplementationName(); + aImpl += ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("/UNO/SERVICES")); + xNewKey = xKey->createKey(aImpl); + xNewKey->createKey(CSAXDocumentBuilder::_getSupportedServiceNames()[0]); + + // register XPath service + aImpl = OUString(RTL_CONSTASCII_USTRINGPARAM("/")); + aImpl += CXPathAPI::_getImplementationName(); + aImpl += ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("/UNO/SERVICES")); + xNewKey = xKey->createKey(aImpl); + xNewKey->createKey(CXPathAPI::_getSupportedServiceNames()[0]); + + // register EventTest service + aImpl = OUString(RTL_CONSTASCII_USTRINGPARAM("/")); + aImpl += CTestListener::_getImplementationName(); + aImpl += ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("/UNO/SERVICES")); + xNewKey = xKey->createKey(aImpl); + xNewKey->createKey(CTestListener::_getSupportedServiceNames()[0]); + + return sal_True; +} + +void* SAL_CALL +component_getFactory(const sal_Char *pImplementationName, void *pServiceManager, void * /*pRegistryKey*/) +{ + void* pReturn = NULL ; + if ( pImplementationName && pServiceManager ) + { + // Define variables which are used in following macros. + Reference< XSingleServiceFactory > xFactory; + Reference< XMultiServiceFactory > xServiceManager( + reinterpret_cast< XMultiServiceFactory* >(pServiceManager)); + + if (CDocumentBuilder::_getImplementationName().compareToAscii( pImplementationName ) == 0 ) + { + xFactory = Reference< XSingleServiceFactory >( + cppu::createOneInstanceFactory( + xServiceManager, CDocumentBuilder::_getImplementationName(), + CDocumentBuilder::_getInstance, CDocumentBuilder::_getSupportedServiceNames())); + } + else if (CSAXDocumentBuilder::_getImplementationName().compareToAscii( pImplementationName ) == 0 ) + { + xFactory = Reference< XSingleServiceFactory >( + cppu::createSingleFactory( + xServiceManager, CSAXDocumentBuilder::_getImplementationName(), + CSAXDocumentBuilder::_getInstance, CSAXDocumentBuilder::_getSupportedServiceNames())); + } + else if (CXPathAPI::_getImplementationName().compareToAscii( pImplementationName ) == 0 ) + { + xFactory = Reference< XSingleServiceFactory >( + cppu::createSingleFactory( + xServiceManager, CXPathAPI::_getImplementationName(), + CXPathAPI::_getInstance, CXPathAPI::_getSupportedServiceNames())); + } + else if (CTestListener::_getImplementationName().compareToAscii( pImplementationName ) == 0 ) + { + xFactory = Reference< XSingleServiceFactory >( + cppu::createSingleFactory( + xServiceManager, CTestListener::_getImplementationName(), + CTestListener::_getInstance, CTestListener::_getSupportedServiceNames())); + } + + // Factory is valid - service was found. + if ( xFactory.is() ) + { + xFactory->acquire(); + pReturn = xFactory.get(); + } + } + + // Return with result of this operation. + return pReturn ; +} + +} // extern "C" diff --git a/unoxml/source/xpath/makefile.mk b/unoxml/source/xpath/makefile.mk new file mode 100644 index 000000000000..30d157105ca6 --- /dev/null +++ b/unoxml/source/xpath/makefile.mk @@ -0,0 +1,56 @@ +#************************************************************************* +# +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. +# +# Copyright 2008 by Sun Microsystems, Inc. +# +# OpenOffice.org - a multi-platform office productivity suite +# +# $RCSfile: makefile.mk,v $ +# +# $Revision: 1.7 $ +# +# This file is part of OpenOffice.org. +# +# OpenOffice.org is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License version 3 +# only, as published by the Free Software Foundation. +# +# OpenOffice.org is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License version 3 for more details +# (a copy is included in the LICENSE file that accompanied this code). +# +# You should have received a copy of the GNU Lesser General Public License +# version 3 along with OpenOffice.org. If not, see +# <http://www.openoffice.org/license.html> +# for a copy of the LGPLv3 License. +# +#************************************************************************* + +PRJ=../.. + +PRJNAME=unoxml +TARGET=xpathimpl +ENABLE_EXCEPTIONS=TRUE + +# --- Settings ----------------------------------------------------- + +.INCLUDE : settings.mk + +.IF "$(SYSTEM_LIBXML)" == "YES" +CFLAGS+=-DSYSTEM_LIBXML $(LIBXML_CFLAGS) +.ENDIF + +# --- Files -------------------------------------------------------- + +SLOFILES = \ + $(SLO)$/xpathobject.obj \ + $(SLO)$/nodelist.obj \ + $(SLO)$/xpathapi.obj + +# --- Targets ------------------------------------------------------ + +.INCLUDE : target.mk + diff --git a/unoxml/source/xpath/nodelist.cxx b/unoxml/source/xpath/nodelist.cxx new file mode 100644 index 000000000000..1bc8e9df9bd5 --- /dev/null +++ b/unoxml/source/xpath/nodelist.cxx @@ -0,0 +1,68 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2008 by Sun Microsystems, Inc. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: nodelist.cxx,v $ + * $Revision: 1.4.20.3 $ + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#include "nodelist.hxx" +#include "../dom/node.hxx" + +namespace XPath +{ + CNodeList::CNodeList(boost::shared_ptr<xmlXPathObject>& rxpathObj) + : m_pNodeSet(0) + { + if (rxpathObj != NULL && rxpathObj->type == XPATH_NODESET) + { + m_pNodeSet = rxpathObj->nodesetval; + m_pXPathObj = rxpathObj; + } + } + + /** + The number of nodes in the list. + */ + sal_Int32 SAL_CALL CNodeList::getLength() throw (RuntimeException) + { + sal_Int32 value = 0; + if (m_pNodeSet != NULL) + value = xmlXPathNodeSetGetLength(m_pNodeSet); + return value; + } + + /** + Returns the indexth item in the collection. + */ + Reference< XNode > SAL_CALL CNodeList::item(sal_Int32 index) throw (RuntimeException) + { + Reference< XNode > aNode; + if (m_pNodeSet != NULL) + aNode = Reference< XNode >(DOM::CNode::get(xmlXPathNodeSetItem(m_pNodeSet, index))); + return aNode; + } +} + diff --git a/unoxml/source/xpath/nodelist.hxx b/unoxml/source/xpath/nodelist.hxx new file mode 100644 index 000000000000..b81e2726c341 --- /dev/null +++ b/unoxml/source/xpath/nodelist.hxx @@ -0,0 +1,74 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2008 by Sun Microsystems, Inc. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: nodelist.hxx,v $ + * $Revision: 1.4.20.3 $ + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#ifndef _NODELIST_HXX +#define _NODELIST_HXX + +#include <vector> +#include <sal/types.h> +#include <cppuhelper/implbase1.hxx> +#include <com/sun/star/uno/Reference.h> +#include <com/sun/star/uno/Exception.hpp> +#include <com/sun/star/xml/dom/XNode.hpp> +#include <com/sun/star/xml/dom/XNodeList.hpp> +#include <com/sun/star/xml/xpath/XXPathObject.hpp> +#include "libxml/tree.h" +#include "libxml/xpath.h" +#include <boost/shared_ptr.hpp> + +using ::rtl::OUString; +using namespace com::sun::star::uno; +using namespace com::sun::star::lang; +using namespace com::sun::star::xml::dom; +using namespace com::sun::star::xml::xpath; + +namespace XPath +{ + + class CNodeList : public cppu::WeakImplHelper1< XNodeList > + { + private: + boost::shared_ptr<xmlXPathObject> m_pXPathObj; + xmlNodeSetPtr m_pNodeSet; + + public: + CNodeList(boost::shared_ptr<xmlXPathObject> &rxpathObj); + /** + The number of nodes in the list. + */ + virtual sal_Int32 SAL_CALL getLength() throw (RuntimeException); + /** + Returns the indexth item in the collection. + */ + virtual Reference< XNode > SAL_CALL item(sal_Int32 index) throw (RuntimeException); + }; +} + +#endif diff --git a/unoxml/source/xpath/xpathapi.cxx b/unoxml/source/xpath/xpathapi.cxx new file mode 100644 index 000000000000..4b8fe4224092 --- /dev/null +++ b/unoxml/source/xpath/xpathapi.cxx @@ -0,0 +1,403 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2008 by Sun Microsystems, Inc. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: xpathapi.cxx,v $ + * $Revision: 1.8 $ + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#include "xpathapi.hxx" +#include "nodelist.hxx" +#include "xpathobject.hxx" +#include "../dom/node.hxx" + +#include <rtl/ustrbuf.hxx> + +#include <libxml/xmlerror.h> +#include <libxml/xpath.h> +#include <libxml/xpathInternals.h> + +#include <stdarg.h> +#include <string.h> + + +namespace XPath +{ + // factory + Reference< XInterface > CXPathAPI::_getInstance(const Reference< XMultiServiceFactory >& rSMgr) + { + return Reference< XInterface >(static_cast<XXPathAPI*>(new CXPathAPI(rSMgr))); + } + + // ctor + CXPathAPI::CXPathAPI(const Reference< XMultiServiceFactory >& rSMgr) + : m_aFactory(rSMgr) + { + } + + const char* CXPathAPI::aImplementationName = "com.sun.star.comp.xml.xpath.XPathAPI"; + const char* CXPathAPI::aSupportedServiceNames[] = { + "com.sun.star.xml.xpath.XPathAPI", + NULL + }; + + OUString CXPathAPI::_getImplementationName() + { + return OUString::createFromAscii(aImplementationName); + } + + Sequence<OUString> CXPathAPI::_getSupportedServiceNames() + { + Sequence<OUString> aSequence; + for (int i=0; aSupportedServiceNames[i]!=NULL; i++) { + aSequence.realloc(i+1); + aSequence[i]=(OUString::createFromAscii(aSupportedServiceNames[i])); + } + return aSequence; + } + + Sequence< OUString > SAL_CALL CXPathAPI::getSupportedServiceNames() + throw (RuntimeException) + { + return CXPathAPI::_getSupportedServiceNames(); + } + + OUString SAL_CALL CXPathAPI::getImplementationName() + throw (RuntimeException) + { + return CXPathAPI::_getImplementationName(); + } + + sal_Bool SAL_CALL CXPathAPI::supportsService(const OUString& aServiceName) + throw (RuntimeException) + { + Sequence< OUString > supported = CXPathAPI::_getSupportedServiceNames(); + for (sal_Int32 i=0; i<supported.getLength(); i++) + { + if (supported[i] == aServiceName) return sal_True; + } + return sal_False; + } + + // ------------------------------------------------------------------- + + void SAL_CALL CXPathAPI::registerNS( + const OUString& aPrefix, + const OUString& aURI) + throw (RuntimeException) + { + m_nsmap.insert(nsmap_t::value_type(aPrefix, aURI)); + } + + void SAL_CALL CXPathAPI::unregisterNS( + const OUString& aPrefix, + const OUString& aURI) + throw (RuntimeException) + { + if ((m_nsmap.find(aPrefix))->second.equals(aURI)) + m_nsmap.erase(aPrefix); + } + + // register all namespaces stored in the namespace list for this object + // with the current xpath evaluation context + static void _registerNamespaces( + xmlXPathContextPtr ctx, + const nsmap_t& nsmap) + { + nsmap_t::const_iterator i = nsmap.begin(); + OString oprefix, ouri; + xmlChar *p, *u; + while (i != nsmap.end()) + { + oprefix = OUStringToOString(i->first, RTL_TEXTENCODING_UTF8); + ouri = OUStringToOString(i->second, RTL_TEXTENCODING_UTF8); + p = (xmlChar*)oprefix.getStr(); + u = (xmlChar*)ouri.getStr(); + xmlXPathRegisterNs(ctx, p, u); + i++; + } + } + + // get all ns decls on a node (and parent nodes, if any) and register them + static void _collectNamespaces( + CXPathAPI* pAPI, + const Reference< XNode >& namespaceNode) + { + // get namespace decls from node... + xmlNodePtr pNode = DOM::CNode::getNodePtr(namespaceNode); + while (pNode != 0) { + xmlNsPtr curDef = pNode->nsDef; + while (curDef != 0) { + const xmlChar* xHref = curDef->href; + OUString aURI((sal_Char*)xHref, strlen((char*)xHref), RTL_TEXTENCODING_UTF8); + const xmlChar* xPre = curDef->prefix; + OUString aPrefix((sal_Char*)xPre, strlen((char*)xPre), RTL_TEXTENCODING_UTF8); + pAPI->registerNS(aPrefix, aURI); + curDef = curDef->next; + } + pNode = pNode->parent; + } + } + + // register function and variable lookup functions with the current + // xpath evaluation context + static void _registerExtensions( + xmlXPathContextPtr ctx, + const extensions_t& extensions) + { + extensions_t::const_iterator i = extensions.begin(); + while (i != extensions.end()) + { + Libxml2ExtensionHandle aHandle = (*i)->getLibxml2ExtensionHandle(); + if ( aHandle.functionLookupFunction != 0 ) + { + xmlXPathRegisterFuncLookup(ctx, + reinterpret_cast<xmlXPathFuncLookupFunc>( + sal::static_int_cast<sal_IntPtr>(aHandle.functionLookupFunction)), + reinterpret_cast<void*>( + sal::static_int_cast<sal_IntPtr>(aHandle.functionData))); + } + if ( aHandle.variableLookupFunction != 0 ) + { + xmlXPathRegisterVariableLookup(ctx, + reinterpret_cast<xmlXPathVariableLookupFunc>( + sal::static_int_cast<sal_IntPtr>(aHandle.variableLookupFunction)), + reinterpret_cast<void*>( + sal::static_int_cast<sal_IntPtr>(aHandle.variableData))); + } + i++; + } + } + + /** + * Use an XPath string to select a nodelist. + */ + Reference< XNodeList > SAL_CALL CXPathAPI::selectNodeList( + const Reference< XNode >& contextNode, + const OUString& expr) + throw (RuntimeException, XPathException) + { + Reference< XXPathObject > xobj = eval(contextNode, expr); + return xobj->getNodeList(); + } + + /** + * same as selectNodeList but registers all name space decalratiosn found on namespaceNode + */ + Reference< XNodeList > SAL_CALL CXPathAPI::selectNodeListNS( + const Reference< XNode >& contextNode, + const OUString& expr, + const Reference< XNode >& namespaceNode) + throw (RuntimeException, XPathException) + { + _collectNamespaces(this, namespaceNode); + return selectNodeList(contextNode, expr); + } + + /** + * Same as selectNodeList but returns the first node (if any) + */ + Reference< XNode > SAL_CALL CXPathAPI::selectSingleNode( + const Reference< XNode >& contextNode, + const OUString& expr) + throw (RuntimeException, XPathException) + { + Reference< XNodeList > aList = selectNodeList(contextNode, expr); + Reference< XNode > aNode = aList->item(0); + return aNode; + } + + /** + * Same as selectSingleNode but registers all namespaces declared on + * namespaceNode + */ + Reference< XNode > SAL_CALL CXPathAPI::selectSingleNodeNS( + const Reference< XNode >& contextNode, + const OUString& expr, + const Reference< XNode >& namespaceNode ) + throw (RuntimeException, XPathException) + { + _collectNamespaces(this, namespaceNode); + return selectSingleNode(contextNode, expr); + } + + static OUString make_error_message(xmlErrorPtr pError) + { + ::rtl::OUStringBuffer buf; + if (pError->message) { + buf.appendAscii(pError->message); + } + int line = pError->line; + if (line) { + buf.appendAscii("Line: "); + buf.append(static_cast<sal_Int32>(line)); + buf.appendAscii("\n"); + } + int column = pError->int2; + if (column) { + buf.appendAscii("Column: "); + buf.append(static_cast<sal_Int32>(column)); + buf.appendAscii("\n"); + } + OUString msg = buf.makeStringAndClear(); + return msg; + } + + extern "C" { + + static void generic_error_func(void *userData, const char *format, ...) + { + (void) userData; + char str[1000]; + va_list args; + + va_start(args, format); +#ifdef _WIN32 +#define vsnprintf _vsnprintf +#endif + vsnprintf(str, sizeof(str), format, args); + va_end(args); + + ::rtl::OUStringBuffer buf( + OUString::createFromAscii("libxml2 error:\n")); + buf.appendAscii(str); + OString msg = OUStringToOString(buf.makeStringAndClear(), + RTL_TEXTENCODING_ASCII_US); + OSL_ENSURE(sal_False, msg.getStr()); + } + + static void structured_error_func(void * userData, xmlErrorPtr error) + { + (void) userData; + ::rtl::OUStringBuffer buf( + OUString::createFromAscii("libxml2 error:\n")); + if (error) { + buf.append(make_error_message(error)); + } else { + buf.append(OUString::createFromAscii("no error argument!")); + } + OString msg = OUStringToOString(buf.makeStringAndClear(), + RTL_TEXTENCODING_ASCII_US); + OSL_ENSURE(sal_False, msg.getStr()); + } + + } // extern "C" + + /** + * evaluates an XPath string. relative XPath expressions are evaluated relative to + * the context Node + */ + Reference< XXPathObject > SAL_CALL CXPathAPI::eval( + const Reference< XNode >& contextNode, + const OUString& expr) + throw (RuntimeException, XPathException) + { + xmlXPathContextPtr xpathCtx; + xmlXPathObjectPtr xpathObj; + + // get the node and document + xmlNodePtr pNode = DOM::CNode::getNodePtr(contextNode); + xmlDocPtr pDoc = pNode->doc; + + /* NB: workaround for #i87252#: + libxml < 2.6.17 considers it an error if the context + node is the empty document (i.e. its xpathCtx->doc has no + children). libxml 2.6.17 does not consider it an error. + Unfortunately, old libxml prints an error message to stderr, + which (afaik) cannot be turned off in this case, so we handle it. + */ + if (!pDoc->children) { + throw XPathException(); + } + + /* Create xpath evaluation context */ + xpathCtx = xmlXPathNewContext(pDoc); + if (xpathCtx == NULL) throw XPathException(); + + // set context node + xpathCtx->node = pNode; + // error handling + xpathCtx->error = structured_error_func; + xmlSetGenericErrorFunc(NULL, generic_error_func); + + // register namespaces and extension + _registerNamespaces(xpathCtx, m_nsmap); + _registerExtensions(xpathCtx, m_extensions); + + /* run the query */ + OString o1 = OUStringToOString(expr, RTL_TEXTENCODING_UTF8); + xmlChar *xStr = (xmlChar*)o1.getStr(); + if ((xpathObj = xmlXPathEval(xStr, xpathCtx)) == NULL) { + // OSL_ENSURE(xpathCtx->lastError == NULL, xpathCtx->lastError->message); + xmlXPathFreeContext(xpathCtx); + throw XPathException(); + } + xmlXPathFreeContext(xpathCtx); + Reference< XXPathObject > aObj(new CXPathObject(xpathObj)); + return aObj; + } + + /** + * same as eval but registers all namespace declarations found on namespaceNode + */ + Reference< XXPathObject > SAL_CALL CXPathAPI::evalNS( + const Reference< XNode >& contextNode, + const OUString& expr, + const Reference< XNode >& namespaceNode) + throw (RuntimeException, XPathException) + { + _collectNamespaces(this, namespaceNode); + return eval(contextNode, expr); + } + + /** + * uses the service manager to create an instance of the service denoted by aName. + * If the returned object implements the XXPathExtension interface, it is added to the list + * of extensions that are used when evaluating XPath strings with this XPathAPI instance + */ + void SAL_CALL CXPathAPI::registerExtension( + const OUString& aName) + throw (RuntimeException) + { + // get extension from service manager + Reference< XXPathExtension > aExtension(m_aFactory->createInstance(aName), UNO_QUERY_THROW); + m_extensions.push_back( aExtension ); + } + + /** + * registers the given extension instance to be used by XPath evaluations performed through this + * XPathAPI instance + */ + void SAL_CALL CXPathAPI::registerExtensionInstance( + const Reference< XXPathExtension>& aExtension) + throw (RuntimeException) + { + if (aExtension.is()) { + m_extensions.push_back( aExtension ); + } else { + throw RuntimeException(); + } + } +} diff --git a/unoxml/source/xpath/xpathapi.hxx b/unoxml/source/xpath/xpathapi.hxx new file mode 100644 index 000000000000..127f765f30f3 --- /dev/null +++ b/unoxml/source/xpath/xpathapi.hxx @@ -0,0 +1,145 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2008 by Sun Microsystems, Inc. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: xpathapi.hxx,v $ + * $Revision: 1.6 $ + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#ifndef _XPATHAPI_HXX +#define _XPATHAPI_HXX + +#include <map> +#include <vector> + +#include <sal/types.h> +#include <cppuhelper/implbase2.hxx> +#include <com/sun/star/uno/Reference.h> +#include <com/sun/star/uno/Sequence.h> + +#include <com/sun/star/uno/XInterface.hpp> +#include <com/sun/star/uno/Exception.hpp> +#include <com/sun/star/xml/xpath/XXPathAPI.hpp> +#include <com/sun/star/xml/dom/XNode.hpp> +#include <com/sun/star/xml/dom/XNodeList.hpp> +#include <com/sun/star/xml/xpath/XXPathAPI.hpp> +#include <com/sun/star/xml/xpath/XXPathObject.hpp> +#include <com/sun/star/xml/xpath/XXPathExtension.hpp> +#include <com/sun/star/xml/xpath/Libxml2ExtensionHandle.hpp> +#include <com/sun/star/xml/xpath/XPathException.hpp> +#include <com/sun/star/lang/XServiceInfo.hpp> +#include <com/sun/star/lang/XSingleServiceFactory.hpp> +#include <com/sun/star/lang/XMultiServiceFactory.hpp> + +#include "libxml/tree.h" + +using ::rtl::OUString; +using namespace com::sun::star::uno; +using namespace com::sun::star::lang; +using namespace com::sun::star::xml::dom; +using namespace com::sun::star::xml::xpath; + +namespace XPath +{ + typedef std::map<OUString, OUString> nsmap_t; + typedef std::vector< Reference<XXPathExtension> > extensions_t; + + class CXPathAPI + : public ::cppu::WeakImplHelper2< XXPathAPI, XServiceInfo > + { + + private: + nsmap_t m_nsmap; + const Reference < XMultiServiceFactory >& m_aFactory; + extensions_t m_extensions; + + public: + // ctor + CXPathAPI(const Reference< XMultiServiceFactory >& rSMgr); + + // call for factory + static Reference< XInterface > getInstance(const Reference < XMultiServiceFactory >& xFactory); + + // static helpers for service info and component management + static const char* aImplementationName; + static const char* aSupportedServiceNames[]; + static OUString _getImplementationName(); + static Sequence< OUString > _getSupportedServiceNames(); + static Reference< XInterface > _getInstance(const Reference< XMultiServiceFactory >& rSMgr); + + // XServiceInfo + virtual OUString SAL_CALL getImplementationName() + throw (RuntimeException); + virtual sal_Bool SAL_CALL supportsService(const OUString& ServiceName) + throw (RuntimeException); + virtual Sequence< OUString > SAL_CALL getSupportedServiceNames () + throw (RuntimeException); + + + // --- XXPathAPI --- + + virtual void SAL_CALL registerNS(const OUString& aPrefix, const OUString& aURI) + throw (RuntimeException); + + virtual void SAL_CALL unregisterNS(const OUString& aPrefix, const OUString& aURI) + throw (RuntimeException); + + /** + Use an XPath string to select a nodelist. + */ + virtual Reference< XNodeList > SAL_CALL selectNodeList(const Reference< XNode >& contextNode, const OUString& str) + throw (RuntimeException, XPathException); + + /** + Use an XPath string to select a nodelist. + */ + virtual Reference< XNodeList > SAL_CALL selectNodeListNS(const Reference< XNode >& contextNode, const OUString& str, const Reference< XNode >& namespaceNode) + throw (RuntimeException, XPathException); + + /** + Use an XPath string to select a single node. + */ + virtual Reference< XNode > SAL_CALL selectSingleNode(const Reference< XNode >& contextNode, const OUString& str) + throw (RuntimeException, XPathException); + + /** + Use an XPath string to select a single node. + */ + virtual Reference< XNode > SAL_CALL selectSingleNodeNS(const Reference< XNode >& contextNode, const OUString& str, const Reference< XNode >& namespaceNode) + throw (RuntimeException, XPathException); + + virtual Reference< XXPathObject > SAL_CALL eval(const Reference< XNode >& contextNode, const OUString& str) + throw (RuntimeException, XPathException); + + virtual Reference< XXPathObject > SAL_CALL evalNS(const Reference< XNode >& contextNode, const OUString& str, const Reference< XNode >& namespaceNode) + throw (RuntimeException, XPathException); + + virtual void SAL_CALL registerExtension(const OUString& aName) throw (RuntimeException); + virtual void SAL_CALL registerExtensionInstance(const Reference< XXPathExtension>& aExtension) throw (RuntimeException); + + }; +} + +#endif diff --git a/unoxml/source/xpath/xpathobject.cxx b/unoxml/source/xpath/xpathobject.cxx new file mode 100644 index 000000000000..5a4b7b93b81e --- /dev/null +++ b/unoxml/source/xpath/xpathobject.cxx @@ -0,0 +1,160 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2008 by Sun Microsystems, Inc. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: xpathobject.cxx,v $ + * $Revision: 1.4.20.3 $ + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#include <string.h> +#include "xpathobject.hxx" +#include "nodelist.hxx" + +namespace XPath +{ + CXPathObject::CXPathObject(xmlXPathObjectPtr xpathObj) + : m_pXPathObj(xpathObj, xmlXPathFreeObject) + { + switch (m_pXPathObj->type) + { + case XPATH_UNDEFINED: + m_xPathObjectType = XPathObjectType_XPATH_UNDEFINED; + break; + case XPATH_NODESET: + m_xPathObjectType = XPathObjectType_XPATH_NODESET; + break; + case XPATH_BOOLEAN: + m_xPathObjectType = XPathObjectType_XPATH_BOOLEAN; + break; + case XPATH_NUMBER: + m_xPathObjectType = XPathObjectType_XPATH_NUMBER; + break; + case XPATH_STRING: + m_xPathObjectType = XPathObjectType_XPATH_STRING; + break; + case XPATH_POINT: + m_xPathObjectType = XPathObjectType_XPATH_POINT; + break; + case XPATH_RANGE: + m_xPathObjectType = XPathObjectType_XPATH_RANGE; + break; + case XPATH_LOCATIONSET: + m_xPathObjectType = XPathObjectType_XPATH_LOCATIONSET; + break; + case XPATH_USERS: + m_xPathObjectType = XPathObjectType_XPATH_USERS; + break; + case XPATH_XSLT_TREE: + m_xPathObjectType = XPathObjectType_XPATH_XSLT_TREE; + break; + default: + m_xPathObjectType = XPathObjectType_XPATH_UNDEFINED; + break; + } + } + + /** + get object type + */ + XPathObjectType CXPathObject::getObjectType() throw (RuntimeException) + { + return m_xPathObjectType; + } + + /** + get the nodes from a nodelist type object + */ + Reference< XNodeList > SAL_CALL CXPathObject::getNodeList() throw (RuntimeException) + { + return Reference< XNodeList >(new CNodeList(m_pXPathObj)); + } + + /** + get value of a boolean object + */ + sal_Bool SAL_CALL CXPathObject::getBoolean() throw (RuntimeException) + { + return (sal_Bool) xmlXPathCastToBoolean(m_pXPathObj.get()); + } + + /** + get number as byte + */ + sal_Int8 SAL_CALL CXPathObject::getByte() throw (RuntimeException) + { + return (sal_Int8) xmlXPathCastToNumber(m_pXPathObj.get()); + } + + /** + get number as short + */ + sal_Int16 SAL_CALL CXPathObject::getShort() throw (RuntimeException) + { + return (sal_Int16) xmlXPathCastToNumber(m_pXPathObj.get()); + } + + /** + get number as long + */ + sal_Int32 SAL_CALL CXPathObject::getLong() throw (RuntimeException) + { + return (sal_Int32) xmlXPathCastToNumber(m_pXPathObj.get()); + } + + /** + get number as hyper + */ + sal_Int64 SAL_CALL CXPathObject::getHyper() throw (RuntimeException) + { + return (sal_Int64) xmlXPathCastToNumber(m_pXPathObj.get()); + } + + /** + get number as float + */ + float SAL_CALL CXPathObject::getFloat() throw (RuntimeException) + { + return (float) xmlXPathCastToNumber(m_pXPathObj.get()); + } + + /** + get number as double + */ + double SAL_CALL CXPathObject::getDouble() throw (RuntimeException) + { + return xmlXPathCastToNumber(m_pXPathObj.get()); + } + + /** + get string value + */ + OUString SAL_CALL CXPathObject::getString() throw (RuntimeException) + { + const sal_Char* x1 = (sal_Char*) xmlXPathCastToString(m_pXPathObj.get()); + return OUString(x1, strlen(x1), RTL_TEXTENCODING_UTF8); + } + +} + diff --git a/unoxml/source/xpath/xpathobject.hxx b/unoxml/source/xpath/xpathobject.hxx new file mode 100644 index 000000000000..5fc7bf8a8661 --- /dev/null +++ b/unoxml/source/xpath/xpathobject.hxx @@ -0,0 +1,116 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2008 by Sun Microsystems, Inc. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: xpathobject.hxx,v $ + * $Revision: 1.4.20.3 $ + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#ifndef _XPATHOBJECT_HXX +#define _XPATHOBJECT_HXX + +#include <map> +#include <sal/types.h> +#include <cppuhelper/implbase1.hxx> +#include <com/sun/star/uno/Reference.h> +#include <com/sun/star/uno/Exception.hpp> +#include <com/sun/star/xml/dom/XNode.hpp> +#include <com/sun/star/xml/dom/XNodeList.hpp> +#include <com/sun/star/xml/xpath/XXPathObject.hpp> +#include <libxml/tree.h> +#include <libxml/xpath.h> +#include <boost/shared_ptr.hpp> + +using ::rtl::OUString; +using namespace com::sun::star::uno; +using namespace com::sun::star::lang; +using namespace com::sun::star::xml::dom; +using namespace com::sun::star::xml::xpath; + +namespace XPath +{ + class CXPathObject : public cppu::WeakImplHelper1< XXPathObject > + { + private: + boost::shared_ptr<xmlXPathObject> m_pXPathObj; + XPathObjectType m_xPathObjectType; + + public: + CXPathObject(xmlXPathObjectPtr xpathObj); + + /** + get object type + */ + virtual XPathObjectType SAL_CALL getObjectType() throw (RuntimeException); + + /** + get the nodes from a nodelist type object + */ + virtual Reference< XNodeList > SAL_CALL getNodeList() throw (RuntimeException); + + /** + get value of a boolean object + */ + virtual sal_Bool SAL_CALL getBoolean() throw (RuntimeException); + + /** + get number as byte + */ + virtual sal_Int8 SAL_CALL getByte() throw (RuntimeException); + + /** + get number as short + */ + virtual sal_Int16 SAL_CALL getShort() throw (RuntimeException); + + /** + get number as long + */ + virtual sal_Int32 SAL_CALL getLong() throw (RuntimeException); + + /** + get number as hyper + */ + virtual sal_Int64 SAL_CALL getHyper() throw (RuntimeException); + + /** + get number as float + */ + virtual float SAL_CALL getFloat() throw (RuntimeException); + + /** + get number as double + */ + virtual double SAL_CALL getDouble() throw (RuntimeException); + + /** + get string value + */ + virtual OUString SAL_CALL getString() throw (RuntimeException); + + }; +} + +#endif diff --git a/unoxml/test/domtest.cxx b/unoxml/test/domtest.cxx new file mode 100644 index 000000000000..6687669fd875 --- /dev/null +++ b/unoxml/test/domtest.cxx @@ -0,0 +1,402 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2008 by Sun Microsystems, Inc. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: basictest.cxx,v $ + * $Revision: 1.12 $ + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +// autogenerated file with codegen.pl + +#include <cppunit/simpleheader.hxx> + +#include <rtl/ref.hxx> +#include <osl/file.hxx> +#include <osl/process.h> +#include <comphelper/seqstream.hxx> +#include <comphelper/sequence.hxx> +#include <cppuhelper/compbase1.hxx> +#include <cppuhelper/bootstrap.hxx> +#include <cppuhelper/basemutex.hxx> + +#include <com/sun/star/xml/sax/FastToken.hpp> +#include <com/sun/star/xml/sax/XSAXSerializable.hpp> +#include <com/sun/star/xml/sax/XFastSAXSerializable.hpp> + +#include "../source/dom/documentbuilder.hxx" + + +using namespace ::DOM; +using namespace ::comphelper; +using namespace ::com::sun::star; + +namespace +{ + +// valid xml +static const char validTestFile[] = +"<?xml version=\"1.0\" encoding=\"UTF-8\"?> \ + <office:document-content \ + xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\" \ + xmlns:xlink=\"http://www.w3.org/1999/xlink\" \ + office:version=\"1.0\"> \ + <office:scripts/> \ + <xlink:test/> \ + <office:automatic-styles teststyle=\"test\"/> \ + <moretest/> \ + some text öäü \ + </office:document-content> \ +"; + +// generates a warning: unsupported xml version, unknown xml:space +// value +static const char warningTestFile[] = +"<?xml version=\"47-11.0\" encoding=\"UTF-8\"?> \ + <office:document-content \ + xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\" \ + xml:space=\"blafasl\" \ + office:version=\"1.0\"> \ + <office:scripts/> \ + <office:automatic-styles/> \ + </office:document-content> \ +"; + +// <?xml not at start of file +static const char errorTestFile[] = +" <?xml version=\"1.0\" encoding=\"UTF-8\"?> \ + <office:document-content \ + xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\" \ + office:version=\"1.0\"> \ + <office:scripts/> \ + <office:automatic-styles/> \ + </office:document-content> \ +"; + +// plain empty +static const char fatalTestFile[] = ""; + +struct ErrorHandler + : public ::cppu::WeakImplHelper1< xml::sax::XErrorHandler > +{ + sal_uInt32 mnErrCount; + sal_uInt32 mnFatalCount; + sal_uInt32 mnWarnCount; + + bool noErrors() const { return !mnErrCount && !mnFatalCount && !mnWarnCount; } + + ErrorHandler() : mnErrCount(0), mnFatalCount(0), mnWarnCount(0) + {} + + virtual void SAL_CALL error( const uno::Any& ) throw (xml::sax::SAXException, uno::RuntimeException) + { + ++mnErrCount; + } + + virtual void SAL_CALL fatalError( const uno::Any& ) throw (xml::sax::SAXException, uno::RuntimeException) + { + ++mnFatalCount; + } + + virtual void SAL_CALL warning( const uno::Any& ) throw (xml::sax::SAXException, uno::RuntimeException) + { + ++mnWarnCount; + } +}; + +struct DocumentHandler + : public ::cppu::WeakImplHelper1< xml::sax::XFastDocumentHandler > +{ + // XFastContextHandler + virtual void SAL_CALL startFastElement( ::sal_Int32 Element, const uno::Reference< xml::sax::XFastAttributeList >& Attribs ) throw (xml::sax::SAXException, uno::RuntimeException) + { + OSL_TRACE("Seen element: %c with namespace 0x%x", + Element & 0xFFFF, Element & 0xFFFF0000); + } + + virtual void SAL_CALL startUnknownElement( const ::rtl::OUString& Namespace, const ::rtl::OUString& Name, const uno::Reference< xml::sax::XFastAttributeList >& Attribs ) throw (xml::sax::SAXException, uno::RuntimeException) + { + } + + virtual void SAL_CALL endFastElement( ::sal_Int32 Element ) throw (xml::sax::SAXException, uno::RuntimeException) + { + } + + virtual void SAL_CALL endUnknownElement( const ::rtl::OUString& Namespace, const ::rtl::OUString& Name ) throw (xml::sax::SAXException, uno::RuntimeException) + { + } + + virtual uno::Reference< xml::sax::XFastContextHandler > SAL_CALL createFastChildContext( ::sal_Int32 Element, const uno::Reference< xml::sax::XFastAttributeList >& Attribs ) throw (xml::sax::SAXException, uno::RuntimeException) + { + return this; + } + + virtual uno::Reference< xml::sax::XFastContextHandler > SAL_CALL createUnknownChildContext( const ::rtl::OUString& Namespace, const ::rtl::OUString& Name, const uno::Reference< xml::sax::XFastAttributeList >& Attribs ) throw (xml::sax::SAXException, uno::RuntimeException) + { + return this; + } + + virtual void SAL_CALL characters( const ::rtl::OUString& aChars ) throw (xml::sax::SAXException, uno::RuntimeException) + { + } + + // XFastDocumentHandler + virtual void SAL_CALL startDocument( ) throw (xml::sax::SAXException, uno::RuntimeException) + { + } + + virtual void SAL_CALL endDocument( ) throw (xml::sax::SAXException, uno::RuntimeException) + { + } + + virtual void SAL_CALL setDocumentLocator( const uno::Reference< xml::sax::XLocator >& xLocator ) throw (xml::sax::SAXException, uno::RuntimeException) + { + } +}; + +struct TokenHandler + : public ::cppu::WeakImplHelper1< xml::sax::XFastTokenHandler > +{ + virtual ::sal_Int32 SAL_CALL getToken( const ::rtl::OUString& Identifier ) throw (uno::RuntimeException) + { + CPPUNIT_ASSERT_MESSAGE( "TokenHandler::getToken() unexpected call", + false ); + return -1; + } + + virtual ::rtl::OUString SAL_CALL getIdentifier( ::sal_Int32 Token ) throw (uno::RuntimeException) + { + CPPUNIT_ASSERT_MESSAGE( "TokenHandler::getIdentifier() unexpected call", + false ); + return rtl::OUString(); + } + + virtual ::sal_Int32 SAL_CALL getTokenFromUTF8( const uno::Sequence< ::sal_Int8 >& Identifier ) throw (uno::RuntimeException) + { + OSL_TRACE("getTokenFromUTF8() %s", (const char*)Identifier.getConstArray()); + return Identifier.getLength() ? Identifier[0] : 0; + } + + virtual uno::Sequence< ::sal_Int8 > SAL_CALL getUTF8Identifier( ::sal_Int32 Token ) throw (uno::RuntimeException) + { + CPPUNIT_ASSERT_MESSAGE( "TokenHandler::getUTF8Identifier() unexpected call", + false ); + return uno::Sequence<sal_Int8>(); + } +}; + +struct BasicTest : public CppUnit::TestFixture +{ + rtl::Reference<CDocumentBuilder> mxDomBuilder; + rtl::Reference<ErrorHandler> mxErrHandler; + rtl::Reference<SequenceInputStream> mxValidInStream; + rtl::Reference<SequenceInputStream> mxWarningInStream; + rtl::Reference<SequenceInputStream> mxErrorInStream; + rtl::Reference<SequenceInputStream> mxFatalInStream; + + void setUp() + { + // luckily, DOM builder doesn't use service fac, so we need + // not bootstrap uno here + mxErrHandler.set( new ErrorHandler() ); + mxDomBuilder.set( new CDocumentBuilder(Reference< XMultiServiceFactory >() )); + mxValidInStream.set( new SequenceInputStream(ByteSequence((sal_Int8*)validTestFile, + sizeof(validTestFile)/sizeof(*validTestFile))) ); + mxWarningInStream.set( new SequenceInputStream(ByteSequence((sal_Int8*)warningTestFile, + sizeof(warningTestFile)/sizeof(*warningTestFile))) ); + mxErrorInStream.set( new SequenceInputStream(ByteSequence((sal_Int8*)errorTestFile, + sizeof(errorTestFile)/sizeof(*errorTestFile))) ); + mxFatalInStream.set( new SequenceInputStream(ByteSequence((sal_Int8*)fatalTestFile, + sizeof(fatalTestFile)/sizeof(*fatalTestFile))) ); + mxDomBuilder->setErrorHandler(mxErrHandler.get()); + } + + void validInputTest() + { + CPPUNIT_ASSERT_MESSAGE( "Valid input file did not result in XDocument #1", + mxDomBuilder->parse( + uno::Reference<io::XInputStream>( + mxValidInStream.get())).is() ); + CPPUNIT_ASSERT_MESSAGE( "Valid input file resulted in parse errors", + mxErrHandler->noErrors() ); + } + + void warningInputTest() + { + CPPUNIT_ASSERT_MESSAGE( "Valid input file did not result in XDocument #2", + mxDomBuilder->parse( + uno::Reference<io::XInputStream>( + mxWarningInStream.get())).is() ); + CPPUNIT_ASSERT_MESSAGE( "No parse warnings in unclean input file", + mxErrHandler->mnWarnCount && !mxErrHandler->mnErrCount && !mxErrHandler->mnFatalCount ); + } + + void errorInputTest() + { + CPPUNIT_ASSERT_MESSAGE( "Valid input file did not result in XDocument #3", + mxDomBuilder->parse( + uno::Reference<io::XInputStream>( + mxErrorInStream.get())).is() ); + CPPUNIT_ASSERT_MESSAGE( "No parse errors in unclean input file", + !mxErrHandler->mnWarnCount && mxErrHandler->mnErrCount && !mxErrHandler->mnFatalCount ); + } + + void fatalInputTest() + { + CPPUNIT_ASSERT_MESSAGE( "Broken input file resulted in XDocument", + !mxDomBuilder->parse( + uno::Reference<io::XInputStream>( + mxFatalInStream.get())).is() ); + CPPUNIT_ASSERT_MESSAGE( "No fatal parse errors in unclean input file", + !mxErrHandler->mnWarnCount && !mxErrHandler->mnErrCount && mxErrHandler->mnFatalCount ); + }; + + // Change the following lines only, if you add, remove or rename + // member functions of the current class, + // because these macros are need by auto register mechanism. + CPPUNIT_TEST_SUITE(BasicTest); + CPPUNIT_TEST(validInputTest); + CPPUNIT_TEST(warningInputTest); + CPPUNIT_TEST(errorInputTest); + CPPUNIT_TEST(fatalInputTest); + CPPUNIT_TEST_SUITE_END(); +}; + +struct SerializerTest : public CppUnit::TestFixture +{ + SerializerTest() : mbUnoInitialized(false) {} + + uno::Reference<uno::XComponentContext> mxCtx; + rtl::Reference<CDocumentBuilder> mxDomBuilder; + rtl::Reference<ErrorHandler> mxErrHandler; + rtl::Reference<SequenceInputStream> mxInStream; + rtl::Reference<DocumentHandler> mxHandler; + rtl::Reference<TokenHandler> mxTokHandler; + uno::Sequence< beans::Pair< rtl::OUString, sal_Int32 > > maRegisteredNamespaces; + bool mbUnoInitialized; + + void setUp() + { + // need working typelib, bootstrap UNO now + if( !mbUnoInitialized ) + { + const char* pArgs( getForwardString() ); + CPPUNIT_ASSERT_MESSAGE("Test file parameter", pArgs); + + const rtl::OUString sBaseDir=rtl::OUString::createFromAscii(pArgs); + + // bootstrap UNO + try + { + ::rtl::OUString aIniUrl; + CPPUNIT_ASSERT_MESSAGE( + "Converting ini file to URL", + osl_getFileURLFromSystemPath( + (sBaseDir+rtl::OUString::createFromAscii("unoxml_unittest_test.ini")).pData, + &aIniUrl.pData ) == osl_File_E_None ); + + mxCtx = ::cppu::defaultBootstrap_InitialComponentContext(aIniUrl); + CPPUNIT_ASSERT_MESSAGE("Getting component context", mxCtx.is()); + } + catch( uno::Exception& ) + { + CPPUNIT_ASSERT_MESSAGE("Bootstrapping UNO", false); + } + + mbUnoInitialized = true; + } + + mxErrHandler.set( new ErrorHandler() ); + mxDomBuilder.set( new CDocumentBuilder( + uno::Reference< lang::XMultiServiceFactory >( + mxCtx->getServiceManager(), + uno::UNO_QUERY ))); + mxInStream.set( new SequenceInputStream(ByteSequence((sal_Int8*)validTestFile, + sizeof(validTestFile)/sizeof(*validTestFile))) ); + mxDomBuilder->setErrorHandler(mxErrHandler.get()); + + mxHandler.set( new DocumentHandler() ); + mxTokHandler.set( new TokenHandler() ); + + maRegisteredNamespaces.realloc(2); + maRegisteredNamespaces[0] = beans::make_Pair( + rtl::OUString( + RTL_CONSTASCII_USTRINGPARAM( + "urn:oasis:names:tc:opendocument:xmlns:office:1.0") ), + xml::sax::FastToken::NAMESPACE); + maRegisteredNamespaces[1] = beans::make_Pair( + rtl::OUString( + RTL_CONSTASCII_USTRINGPARAM( + "http://www.w3.org/1999/xlink") ), + 2*xml::sax::FastToken::NAMESPACE); + } + + void serializerTest () + { + uno::Reference< xml::dom::XDocument > xDoc= + mxDomBuilder->parse( + uno::Reference<io::XInputStream>( + mxInStream.get())); + CPPUNIT_ASSERT_MESSAGE( "Valid input file did not result in XDocument", + xDoc.is() ); + CPPUNIT_ASSERT_MESSAGE( "Valid input file resulted in parse errors", + mxErrHandler->noErrors() ); + + uno::Reference< xml::sax::XSAXSerializable > xSaxSerializer( + xDoc, uno::UNO_QUERY); + CPPUNIT_ASSERT_MESSAGE( "XSAXSerializable not supported", + xSaxSerializer.is() ); + + uno::Reference< xml::sax::XFastSAXSerializable > xFastSaxSerializer( + xDoc, uno::UNO_QUERY); + CPPUNIT_ASSERT_MESSAGE( "XFastSAXSerializable not supported", + xSaxSerializer.is() ); + + xFastSaxSerializer->fastSerialize( mxHandler.get(), + mxTokHandler.get(), + uno::Sequence< beans::StringPair >(), + maRegisteredNamespaces ); + } + + // Change the following lines only, if you add, remove or rename + // member functions of the current class, + // because these macros are need by auto register mechanism. + + CPPUNIT_TEST_SUITE(SerializerTest); + CPPUNIT_TEST(serializerTest); + CPPUNIT_TEST_SUITE_END(); +}; + +// ----------------------------------------------------------------------------- +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(BasicTest, "BasicTest"); +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(SerializerTest, "SerializerTest"); +} + + +// ----------------------------------------------------------------------------- + +// this macro creates an empty function, which will called by the RegisterAllFunctions() +// to let the user the possibility to also register some functions by hand. +NOADDITIONAL; + diff --git a/unoxml/test/export.map b/unoxml/test/export.map new file mode 100644 index 000000000000..bfd13f454044 --- /dev/null +++ b/unoxml/test/export.map @@ -0,0 +1,38 @@ +#************************************************************************* +# +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. +# +# Copyright 2008 by Sun Microsystems, Inc. +# +# OpenOffice.org - a multi-platform office productivity suite +# +# $RCSfile: export.map,v $ +# +# $Revision: 1.2 $ +# +# This file is part of OpenOffice.org. +# +# OpenOffice.org is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License version 3 +# only, as published by the Free Software Foundation. +# +# OpenOffice.org is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License version 3 for more details +# (a copy is included in the LICENSE file that accompanied this code). +# +# You should have received a copy of the GNU Lesser General Public License +# version 3 along with OpenOffice.org. If not, see +# <http://www.openoffice.org/license.html> +# for a copy of the LGPLv3 License. +# +#************************************************************************* + +UDK_3.1 { + global: + registerAllTestFunction; + + local: + *; +}; diff --git a/unoxml/test/makefile.mk b/unoxml/test/makefile.mk new file mode 100644 index 000000000000..781f8a46bf96 --- /dev/null +++ b/unoxml/test/makefile.mk @@ -0,0 +1,106 @@ +#************************************************************************* +# +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. +# +# Copyright 2008 by Sun Microsystems, Inc. +# +# OpenOffice.org - a multi-platform office productivity suite +# +# $RCSfile: makefile.mk,v $ +# +# $Revision: 1.9 $ +# +# This file is part of OpenOffice.org. +# +# OpenOffice.org is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License version 3 +# only, as published by the Free Software Foundation. +# +# OpenOffice.org is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License version 3 for more details +# (a copy is included in the LICENSE file that accompanied this code). +# +# You should have received a copy of the GNU Lesser General Public License +# version 3 along with OpenOffice.org. If not, see +# <http://www.openoffice.org/license.html> +# for a copy of the LGPLv3 License. +# +#************************************************************************* + +PRJ=.. + +PRJNAME=unoxml +TARGET=tests +TARGETTYPE=GUI + +ENABLE_EXCEPTIONS=TRUE + +# --- Settings ----------------------------------------------------- + +.INCLUDE : settings.mk + +# --- Common ---------------------------------------------------------- + +# BEGIN ---------------------------------------------------------------- +# auto generated Target:tests by codegen.pl +SHL1OBJS= \ + $(SLO)$/domtest.obj + +# the following three libs are a bit of a hack: cannot link against +# unoxml here, because not yet delivered (and does not export +# ~anything). Need the functionality to test, so we're linking it in +# statically. Need to keep this in sync with +# source/services/makefile.mk +SHL1LIBS= \ + $(SLB)$/domimpl.lib \ + $(SLB)$/xpathimpl.lib \ + $(SLB)$/eventsimpl.lib + +SHL1TARGET= tests +SHL1STDLIBS= \ + $(UCBHELPERLIB) \ + $(LIBXML2LIB) \ + $(TOOLSLIB) \ + $(COMPHELPERLIB) \ + $(CPPUHELPERLIB) \ + $(CPPUNITLIB) \ + $(CPPULIB) \ + $(SAXLIB) \ + $(SALLIB)\ + $(EXPATASCII3RDLIB) + +SHL1IMPLIB= i$(SHL1TARGET) + +DEF1NAME =$(SHL1TARGET) +SHL1VERSIONMAP = export.map + +# END ------------------------------------------------------------------ + +#------------------------------- All object files ------------------------------- +# do this here, so we get right dependencies +SLOFILES=$(SHL1OBJS) + +# --- Targets ------------------------------------------------------ + +.INCLUDE : target.mk +.INCLUDE : _cppunit.mk + +# --- Fake uno bootstrap ------------------------ + +$(BIN)$/unoxml_unittest_test.ini : makefile.mk + rm -f $@ + @echo UNO_SERVICES= > $@ + @echo UNO_TYPES=$(UNOUCRRDB:s/\/\\/) >> $@ + +# --- Enable testshl2 execution in normal build ------------------------ + +$(MISC)$/unoxml_unittest_succeeded : $(SHL1TARGETN) $(BIN)$/unoxml_unittest_test.ini + @echo ---------------------------------------------------------- + @echo - start unit test on library $(SHL1TARGETN) + @echo ---------------------------------------------------------- + testshl2 -forward $(BIN)$/ -sf $(mktmp ) $(SHL1TARGETN) + $(TOUCH) $@ + +ALLTAR : $(MISC)$/unoxml_unittest_succeeded |