1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir #include "precompiled_xmloff.hxx"
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir #include "RDFaImportHelper.hxx"
31*cdf0e10cSrcweir 
32*cdf0e10cSrcweir #include <xmloff/xmlimp.hxx>
33*cdf0e10cSrcweir #include <xmloff/nmspmap.hxx>
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir #include <comphelper/sequenceasvector.hxx>
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir #include <tools/string.hxx> // for GetAbsoluteReference
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir #include <com/sun/star/rdf/URI.hpp>
40*cdf0e10cSrcweir #include <com/sun/star/rdf/XDocumentMetadataAccess.hpp>
41*cdf0e10cSrcweir #include <com/sun/star/rdf/XDocumentRepository.hpp>
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir #include <rtl/ustring.hxx>
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir #include <boost/bind.hpp>
46*cdf0e10cSrcweir #include <boost/iterator_adaptors.hpp>
47*cdf0e10cSrcweir #ifndef BOOST_ITERATOR_ADAPTOR_DWA053000_HPP_ // from iterator_adaptors.hpp
48*cdf0e10cSrcweir // N.B.: the check for the header guard _of a specific version of boost_
49*cdf0e10cSrcweir //       is here so this may work on different versions of boost,
50*cdf0e10cSrcweir //       which sadly put the goods in different header files
51*cdf0e10cSrcweir #include <boost/iterator/transform_iterator.hpp>
52*cdf0e10cSrcweir #endif
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir #include <map>
55*cdf0e10cSrcweir #include <iterator>
56*cdf0e10cSrcweir #include <functional>
57*cdf0e10cSrcweir #include <algorithm>
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir using namespace ::com::sun::star;
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir namespace xmloff {
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir /** a bit of context for parsing RDFa attributes */
65*cdf0e10cSrcweir class SAL_DLLPRIVATE RDFaReader
66*cdf0e10cSrcweir {
67*cdf0e10cSrcweir     const SvXMLImport & m_rImport;
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir     const SvXMLImport & GetImport() const { return m_rImport; }
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir     //FIXME: this is an ugly hack to workaround buggy SvXMLImport::GetAbsolute
72*cdf0e10cSrcweir     ::rtl::OUString GetAbsoluteReference(::rtl::OUString const & i_rURI) const
73*cdf0e10cSrcweir     {
74*cdf0e10cSrcweir         if (!i_rURI.getLength() || i_rURI[0] == '#')
75*cdf0e10cSrcweir         {
76*cdf0e10cSrcweir             return GetImport().GetBaseURL() + i_rURI;
77*cdf0e10cSrcweir         }
78*cdf0e10cSrcweir         else
79*cdf0e10cSrcweir         {
80*cdf0e10cSrcweir             return GetImport().GetAbsoluteReference(i_rURI);
81*cdf0e10cSrcweir         }
82*cdf0e10cSrcweir     }
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir public:
85*cdf0e10cSrcweir     RDFaReader(SvXMLImport const & i_rImport)
86*cdf0e10cSrcweir         : m_rImport(i_rImport)
87*cdf0e10cSrcweir     { }
88*cdf0e10cSrcweir 
89*cdf0e10cSrcweir     // returns URI or blank node!
90*cdf0e10cSrcweir     ::rtl::OUString ReadCURIE(::rtl::OUString const & i_rCURIE) const;
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir     std::vector< ::rtl::OUString >
93*cdf0e10cSrcweir     ReadCURIEs(::rtl::OUString const & i_rCURIEs) const;
94*cdf0e10cSrcweir 
95*cdf0e10cSrcweir     ::rtl::OUString
96*cdf0e10cSrcweir     ReadURIOrSafeCURIE( ::rtl::OUString const & i_rURIOrSafeCURIE) const;
97*cdf0e10cSrcweir };
98*cdf0e10cSrcweir 
99*cdf0e10cSrcweir /** helper to insert RDFa statements into the RDF repository */
100*cdf0e10cSrcweir class SAL_DLLPRIVATE RDFaInserter
101*cdf0e10cSrcweir {
102*cdf0e10cSrcweir     const uno::Reference<uno::XComponentContext> m_xContext;
103*cdf0e10cSrcweir     uno::Reference< rdf::XDocumentRepository > m_xRepository;
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir     typedef ::std::map< ::rtl::OUString, uno::Reference< rdf::XBlankNode > >
106*cdf0e10cSrcweir         BlankNodeMap_t;
107*cdf0e10cSrcweir 
108*cdf0e10cSrcweir     BlankNodeMap_t m_BlankNodeMap;
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir public:
111*cdf0e10cSrcweir     RDFaInserter(uno::Reference<uno::XComponentContext> const & i_xContext,
112*cdf0e10cSrcweir             uno::Reference< rdf::XDocumentRepository > const & i_xRepository)
113*cdf0e10cSrcweir         : m_xContext(i_xContext)
114*cdf0e10cSrcweir         , m_xRepository(i_xRepository)
115*cdf0e10cSrcweir     {}
116*cdf0e10cSrcweir 
117*cdf0e10cSrcweir     uno::Reference< rdf::XBlankNode >
118*cdf0e10cSrcweir     LookupBlankNode(::rtl::OUString const & i_rNodeId );
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir     uno::Reference< rdf::XURI >
121*cdf0e10cSrcweir     MakeURI( ::rtl::OUString const & i_rURI) const;
122*cdf0e10cSrcweir 
123*cdf0e10cSrcweir     uno::Reference< rdf::XResource>
124*cdf0e10cSrcweir     MakeResource( ::rtl::OUString const & i_rResource);
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir     void InsertRDFaEntry(struct RDFaEntry const & i_rEntry);
127*cdf0e10cSrcweir };
128*cdf0e10cSrcweir 
129*cdf0e10cSrcweir /** store parsed RDFa attributes */
130*cdf0e10cSrcweir struct SAL_DLLPRIVATE ParsedRDFaAttributes
131*cdf0e10cSrcweir {
132*cdf0e10cSrcweir     ::rtl::OUString m_About;
133*cdf0e10cSrcweir     ::std::vector< ::rtl::OUString > m_Properties;
134*cdf0e10cSrcweir     ::rtl::OUString m_Content;
135*cdf0e10cSrcweir     ::rtl::OUString m_Datatype;
136*cdf0e10cSrcweir 
137*cdf0e10cSrcweir     ParsedRDFaAttributes(
138*cdf0e10cSrcweir             ::rtl::OUString const & i_rAbout,
139*cdf0e10cSrcweir             ::std::vector< ::rtl::OUString > const & i_rProperties,
140*cdf0e10cSrcweir             ::rtl::OUString const & i_rContent,
141*cdf0e10cSrcweir             ::rtl::OUString const & i_rDatatype)
142*cdf0e10cSrcweir         : m_About(i_rAbout)
143*cdf0e10cSrcweir         , m_Properties(i_rProperties)
144*cdf0e10cSrcweir         , m_Content(i_rContent)
145*cdf0e10cSrcweir         , m_Datatype(i_rDatatype)
146*cdf0e10cSrcweir     { }
147*cdf0e10cSrcweir };
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir /** store metadatable object and its RDFa attributes */
150*cdf0e10cSrcweir struct SAL_DLLPRIVATE RDFaEntry
151*cdf0e10cSrcweir {
152*cdf0e10cSrcweir     uno::Reference<rdf::XMetadatable> m_xObject;
153*cdf0e10cSrcweir     ::boost::shared_ptr<ParsedRDFaAttributes> m_pRDFaAttributes;
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir     RDFaEntry(uno::Reference<rdf::XMetadatable> const & i_xObject,
156*cdf0e10cSrcweir             ::boost::shared_ptr<ParsedRDFaAttributes> const& i_pRDFaAttributes)
157*cdf0e10cSrcweir         : m_xObject(i_xObject)
158*cdf0e10cSrcweir         , m_pRDFaAttributes(i_pRDFaAttributes)
159*cdf0e10cSrcweir     { }
160*cdf0e10cSrcweir };
161*cdf0e10cSrcweir 
162*cdf0e10cSrcweir ////////////////////////////////////////////////////////////////////////////
163*cdf0e10cSrcweir 
164*cdf0e10cSrcweir 
165*cdf0e10cSrcweir static inline bool isWS(const sal_Unicode i_Char)
166*cdf0e10cSrcweir {
167*cdf0e10cSrcweir     return ('\t' == i_Char) || ('\n' == i_Char) || ('\r' == i_Char)
168*cdf0e10cSrcweir         || (' ' == i_Char);
169*cdf0e10cSrcweir }
170*cdf0e10cSrcweir 
171*cdf0e10cSrcweir static ::rtl::OUString splitAtWS(::rtl::OUString & io_rString)
172*cdf0e10cSrcweir {
173*cdf0e10cSrcweir     const sal_Int32 len( io_rString.getLength() );
174*cdf0e10cSrcweir     sal_Int32 idxstt(0);
175*cdf0e10cSrcweir     while ((idxstt < len) && ( isWS(io_rString[idxstt])))
176*cdf0e10cSrcweir         ++idxstt; // skip leading ws
177*cdf0e10cSrcweir     sal_Int32 idxend(idxstt);
178*cdf0e10cSrcweir     while ((idxend < len) && (!isWS(io_rString[idxend])))
179*cdf0e10cSrcweir         ++idxend; // the CURIE
180*cdf0e10cSrcweir     const ::rtl::OUString ret(io_rString.copy(idxstt, idxend - idxstt));
181*cdf0e10cSrcweir     io_rString = io_rString.copy(idxend); // rest
182*cdf0e10cSrcweir     return ret;
183*cdf0e10cSrcweir }
184*cdf0e10cSrcweir 
185*cdf0e10cSrcweir ::rtl::OUString
186*cdf0e10cSrcweir RDFaReader::ReadCURIE(::rtl::OUString const & i_rCURIE) const
187*cdf0e10cSrcweir {
188*cdf0e10cSrcweir     // the RDFa spec says that a prefix is required (it may be empty: ":foo")
189*cdf0e10cSrcweir     const sal_Int32 idx( i_rCURIE.indexOf(':') );
190*cdf0e10cSrcweir     if (idx >= 0)
191*cdf0e10cSrcweir     {
192*cdf0e10cSrcweir         ::rtl::OUString Prefix;
193*cdf0e10cSrcweir         ::rtl::OUString LocalName;
194*cdf0e10cSrcweir         ::rtl::OUString Namespace;
195*cdf0e10cSrcweir         sal_uInt16 nKey( GetImport().GetNamespaceMap()._GetKeyByAttrName(
196*cdf0e10cSrcweir             i_rCURIE, &Prefix, &LocalName, &Namespace) );
197*cdf0e10cSrcweir         if (Prefix.equalsAscii("_"))
198*cdf0e10cSrcweir         {
199*cdf0e10cSrcweir             // eeek, it's a bnode!
200*cdf0e10cSrcweir             // "_" is not a valid URI scheme => we can identify bnodes
201*cdf0e10cSrcweir             return i_rCURIE;
202*cdf0e10cSrcweir         }
203*cdf0e10cSrcweir         else
204*cdf0e10cSrcweir         {
205*cdf0e10cSrcweir             OSL_ENSURE(XML_NAMESPACE_NONE != nKey, "no namespace?");
206*cdf0e10cSrcweir             if ((XML_NAMESPACE_UNKNOWN != nKey) &&
207*cdf0e10cSrcweir                 (XML_NAMESPACE_XMLNS   != nKey))
208*cdf0e10cSrcweir             {
209*cdf0e10cSrcweir                 // N.B.: empty LocalName is valid!
210*cdf0e10cSrcweir                 const ::rtl::OUString URI(Namespace + LocalName);
211*cdf0e10cSrcweir //                return GetImport().GetAbsoluteReference(URI);
212*cdf0e10cSrcweir                 return GetAbsoluteReference(URI);
213*cdf0e10cSrcweir             }
214*cdf0e10cSrcweir             else
215*cdf0e10cSrcweir             {
216*cdf0e10cSrcweir                 OSL_TRACE( "ReadCURIE: invalid CURIE: invalid prefix" );
217*cdf0e10cSrcweir                 return ::rtl::OUString();
218*cdf0e10cSrcweir             }
219*cdf0e10cSrcweir         }
220*cdf0e10cSrcweir     }
221*cdf0e10cSrcweir     else
222*cdf0e10cSrcweir     {
223*cdf0e10cSrcweir         OSL_TRACE( "ReadCURIE: invalid CURIE: no prefix" );
224*cdf0e10cSrcweir         return ::rtl::OUString();
225*cdf0e10cSrcweir     }
226*cdf0e10cSrcweir }
227*cdf0e10cSrcweir 
228*cdf0e10cSrcweir ::std::vector< ::rtl::OUString >
229*cdf0e10cSrcweir RDFaReader::ReadCURIEs(::rtl::OUString const & i_rCURIEs) const
230*cdf0e10cSrcweir {
231*cdf0e10cSrcweir     std::vector< ::rtl::OUString > vec;
232*cdf0e10cSrcweir     ::rtl::OUString CURIEs(i_rCURIEs);
233*cdf0e10cSrcweir     do {
234*cdf0e10cSrcweir       ::rtl::OUString curie( splitAtWS(CURIEs) );
235*cdf0e10cSrcweir       if (curie.getLength())
236*cdf0e10cSrcweir       {
237*cdf0e10cSrcweir           const ::rtl::OUString uri(ReadCURIE(curie));
238*cdf0e10cSrcweir           if (uri.getLength())
239*cdf0e10cSrcweir           {
240*cdf0e10cSrcweir               vec.push_back(uri);
241*cdf0e10cSrcweir           }
242*cdf0e10cSrcweir       }
243*cdf0e10cSrcweir     }
244*cdf0e10cSrcweir     while (CURIEs.getLength());
245*cdf0e10cSrcweir     if (!vec.size())
246*cdf0e10cSrcweir     {
247*cdf0e10cSrcweir         OSL_TRACE( "ReadCURIEs: invalid CURIEs" );
248*cdf0e10cSrcweir     }
249*cdf0e10cSrcweir     return vec;
250*cdf0e10cSrcweir }
251*cdf0e10cSrcweir 
252*cdf0e10cSrcweir ::rtl::OUString
253*cdf0e10cSrcweir RDFaReader::ReadURIOrSafeCURIE(::rtl::OUString const & i_rURIOrSafeCURIE) const
254*cdf0e10cSrcweir {
255*cdf0e10cSrcweir     const sal_Int32 len(i_rURIOrSafeCURIE.getLength());
256*cdf0e10cSrcweir     if (len && (i_rURIOrSafeCURIE[0] == '['))
257*cdf0e10cSrcweir     {
258*cdf0e10cSrcweir         if ((len >= 2) && (i_rURIOrSafeCURIE[len - 1] == ']'))
259*cdf0e10cSrcweir         {
260*cdf0e10cSrcweir             return ReadCURIE(i_rURIOrSafeCURIE.copy(1, len - 2));
261*cdf0e10cSrcweir         }
262*cdf0e10cSrcweir         else
263*cdf0e10cSrcweir         {
264*cdf0e10cSrcweir             OSL_TRACE( "ReadURIOrSafeCURIE: invalid SafeCURIE" );
265*cdf0e10cSrcweir             return ::rtl::OUString();
266*cdf0e10cSrcweir         }
267*cdf0e10cSrcweir     }
268*cdf0e10cSrcweir     else
269*cdf0e10cSrcweir     {
270*cdf0e10cSrcweir         if (i_rURIOrSafeCURIE.matchAsciiL("_:", 2)) // blank node
271*cdf0e10cSrcweir         {
272*cdf0e10cSrcweir             OSL_TRACE( "ReadURIOrSafeCURIE: invalid URI: scheme is _" );
273*cdf0e10cSrcweir             return ::rtl::OUString();
274*cdf0e10cSrcweir         }
275*cdf0e10cSrcweir         else
276*cdf0e10cSrcweir         {
277*cdf0e10cSrcweir //            return GetImport().GetAbsoluteReference(i_rURIOrSafeCURIE);
278*cdf0e10cSrcweir             return GetAbsoluteReference(i_rURIOrSafeCURIE);
279*cdf0e10cSrcweir         }
280*cdf0e10cSrcweir     }
281*cdf0e10cSrcweir }
282*cdf0e10cSrcweir 
283*cdf0e10cSrcweir ////////////////////////////////////////////////////////////////////////////
284*cdf0e10cSrcweir 
285*cdf0e10cSrcweir uno::Reference< rdf::XBlankNode >
286*cdf0e10cSrcweir RDFaInserter::LookupBlankNode(::rtl::OUString const & i_rNodeId )
287*cdf0e10cSrcweir {
288*cdf0e10cSrcweir     uno::Reference< rdf::XBlankNode > & rEntry( m_BlankNodeMap[ i_rNodeId ] );
289*cdf0e10cSrcweir     if (!rEntry.is())
290*cdf0e10cSrcweir     {
291*cdf0e10cSrcweir         rEntry = m_xRepository->createBlankNode();
292*cdf0e10cSrcweir     }
293*cdf0e10cSrcweir     return rEntry;
294*cdf0e10cSrcweir }
295*cdf0e10cSrcweir 
296*cdf0e10cSrcweir uno::Reference< rdf::XURI >
297*cdf0e10cSrcweir RDFaInserter::MakeURI( ::rtl::OUString const & i_rURI) const
298*cdf0e10cSrcweir {
299*cdf0e10cSrcweir     if (i_rURI.matchAsciiL("_:", 2)) // blank node
300*cdf0e10cSrcweir     {
301*cdf0e10cSrcweir         OSL_TRACE("MakeURI: cannot create URI for blank node");
302*cdf0e10cSrcweir         return 0;
303*cdf0e10cSrcweir     }
304*cdf0e10cSrcweir     else
305*cdf0e10cSrcweir     {
306*cdf0e10cSrcweir         try
307*cdf0e10cSrcweir         {
308*cdf0e10cSrcweir             return rdf::URI::create( m_xContext, i_rURI );
309*cdf0e10cSrcweir         }
310*cdf0e10cSrcweir         catch (uno::Exception &)
311*cdf0e10cSrcweir         {
312*cdf0e10cSrcweir             OSL_ENSURE(false, "MakeURI: cannot create URI");
313*cdf0e10cSrcweir             return 0;
314*cdf0e10cSrcweir         }
315*cdf0e10cSrcweir     }
316*cdf0e10cSrcweir }
317*cdf0e10cSrcweir 
318*cdf0e10cSrcweir uno::Reference< rdf::XResource>
319*cdf0e10cSrcweir RDFaInserter::MakeResource( ::rtl::OUString const & i_rResource)
320*cdf0e10cSrcweir {
321*cdf0e10cSrcweir     if (i_rResource.matchAsciiL("_:", 2)) // blank node
322*cdf0e10cSrcweir     {
323*cdf0e10cSrcweir         // we cannot use the blank node label as-is: it must be distinct
324*cdf0e10cSrcweir         // from labels in other graphs, so create fresh ones per XML stream
325*cdf0e10cSrcweir         // N.B.: content.xml and styles.xml are distinct graphs
326*cdf0e10cSrcweir         ::rtl::OUString name( i_rResource.copy(2) );
327*cdf0e10cSrcweir         const uno::Reference< rdf::XBlankNode > xBNode( LookupBlankNode(name) );
328*cdf0e10cSrcweir         OSL_ENSURE(xBNode.is(), "no blank node?");
329*cdf0e10cSrcweir         return uno::Reference<rdf::XResource>( xBNode, uno::UNO_QUERY);
330*cdf0e10cSrcweir     }
331*cdf0e10cSrcweir     else
332*cdf0e10cSrcweir     {
333*cdf0e10cSrcweir         return uno::Reference<rdf::XResource>( MakeURI( i_rResource ),
334*cdf0e10cSrcweir             uno::UNO_QUERY);
335*cdf0e10cSrcweir     }
336*cdf0e10cSrcweir }
337*cdf0e10cSrcweir 
338*cdf0e10cSrcweir /** i wrote this because c++ implementations cannot agree on which variant
339*cdf0e10cSrcweir     of boost::bind and std::mem_fun_ref applied to Reference::is compiles */
340*cdf0e10cSrcweir class ref_is_null :
341*cdf0e10cSrcweir     public ::std::unary_function<sal_Bool, const uno::Reference<rdf::XURI> & >
342*cdf0e10cSrcweir {
343*cdf0e10cSrcweir public:
344*cdf0e10cSrcweir     sal_Bool operator() (const uno::Reference<rdf::XURI> & i_rRef)
345*cdf0e10cSrcweir     {
346*cdf0e10cSrcweir         return !i_rRef.is();
347*cdf0e10cSrcweir     }
348*cdf0e10cSrcweir };
349*cdf0e10cSrcweir 
350*cdf0e10cSrcweir void RDFaInserter::InsertRDFaEntry(
351*cdf0e10cSrcweir     struct RDFaEntry const & i_rEntry)
352*cdf0e10cSrcweir {
353*cdf0e10cSrcweir     OSL_ENSURE(i_rEntry.m_xObject.is(),
354*cdf0e10cSrcweir         "InsertRDFaEntry: invalid arg: null object");
355*cdf0e10cSrcweir     if (!i_rEntry.m_xObject.is()) return;
356*cdf0e10cSrcweir 
357*cdf0e10cSrcweir     const uno::Reference< rdf::XResource > xSubject(
358*cdf0e10cSrcweir         MakeResource( i_rEntry.m_pRDFaAttributes->m_About ) );
359*cdf0e10cSrcweir     if (!xSubject.is())
360*cdf0e10cSrcweir     {
361*cdf0e10cSrcweir         return; // invalid
362*cdf0e10cSrcweir     }
363*cdf0e10cSrcweir 
364*cdf0e10cSrcweir     ::comphelper::SequenceAsVector< uno::Reference< rdf::XURI > > predicates;
365*cdf0e10cSrcweir 
366*cdf0e10cSrcweir     predicates.reserve(i_rEntry.m_pRDFaAttributes->m_Properties.size());
367*cdf0e10cSrcweir 
368*cdf0e10cSrcweir     ::std::remove_copy_if(
369*cdf0e10cSrcweir         ::boost::make_transform_iterator(
370*cdf0e10cSrcweir             i_rEntry.m_pRDFaAttributes->m_Properties.begin(),
371*cdf0e10cSrcweir             ::boost::bind(&RDFaInserter::MakeURI, this, _1)),
372*cdf0e10cSrcweir         // argh, this must be the same type :(
373*cdf0e10cSrcweir         ::boost::make_transform_iterator(
374*cdf0e10cSrcweir             i_rEntry.m_pRDFaAttributes->m_Properties.end(),
375*cdf0e10cSrcweir             ::boost::bind(&RDFaInserter::MakeURI, this, _1)),
376*cdf0e10cSrcweir         ::std::back_inserter(predicates),
377*cdf0e10cSrcweir         ref_is_null() );
378*cdf0e10cSrcweir         // compiles only on wntmsci12
379*cdf0e10cSrcweir //        ::boost::bind( ::std::logical_not<sal_Bool>(), ::boost::bind<sal_Bool>(&uno::Reference<rdf::XURI>::is, _1)));
380*cdf0e10cSrcweir         // compiles on unxsoli4, wntsci12, but not unxlngi6
381*cdf0e10cSrcweir //        ::boost::bind( ::std::logical_not<sal_Bool>(), ::boost::bind<sal_Bool, com::sun::star::uno::Reference<rdf::XURI> >(&uno::Reference<rdf::XURI>::is, _1)));
382*cdf0e10cSrcweir         // compiles on unxsoli4, unxlngi6, but not wntsci12
383*cdf0e10cSrcweir //        ::std::not1( ::std::mem_fun_ref(&uno::Reference<rdf::XURI>::is)) );
384*cdf0e10cSrcweir 
385*cdf0e10cSrcweir     if (!predicates.size())
386*cdf0e10cSrcweir     {
387*cdf0e10cSrcweir         return; // invalid
388*cdf0e10cSrcweir     }
389*cdf0e10cSrcweir 
390*cdf0e10cSrcweir     uno::Reference<rdf::XURI> xDatatype;
391*cdf0e10cSrcweir     if (i_rEntry.m_pRDFaAttributes->m_Datatype.getLength())
392*cdf0e10cSrcweir     {
393*cdf0e10cSrcweir         xDatatype = MakeURI( i_rEntry.m_pRDFaAttributes->m_Datatype );
394*cdf0e10cSrcweir     }
395*cdf0e10cSrcweir 
396*cdf0e10cSrcweir     try
397*cdf0e10cSrcweir     {
398*cdf0e10cSrcweir         // N.B.: this will call xMeta->ensureMetadataReference, which is why
399*cdf0e10cSrcweir         // this must be done _after_ importing the whole XML file,
400*cdf0e10cSrcweir         // to prevent collision between generated ids and ids in the file
401*cdf0e10cSrcweir         m_xRepository->setStatementRDFa(xSubject, predicates.getAsConstList(),
402*cdf0e10cSrcweir             i_rEntry.m_xObject,
403*cdf0e10cSrcweir             i_rEntry.m_pRDFaAttributes->m_Content, xDatatype);
404*cdf0e10cSrcweir     }
405*cdf0e10cSrcweir     catch (uno::Exception &)
406*cdf0e10cSrcweir     {
407*cdf0e10cSrcweir         OSL_ENSURE(false, "InsertRDFaEntry: setStatementRDFa failed?");
408*cdf0e10cSrcweir     }
409*cdf0e10cSrcweir }
410*cdf0e10cSrcweir 
411*cdf0e10cSrcweir ////////////////////////////////////////////////////////////////////////////
412*cdf0e10cSrcweir 
413*cdf0e10cSrcweir RDFaImportHelper::RDFaImportHelper(const SvXMLImport & i_rImport)
414*cdf0e10cSrcweir     : m_rImport(i_rImport)
415*cdf0e10cSrcweir {
416*cdf0e10cSrcweir }
417*cdf0e10cSrcweir 
418*cdf0e10cSrcweir RDFaImportHelper::~RDFaImportHelper()
419*cdf0e10cSrcweir {
420*cdf0e10cSrcweir }
421*cdf0e10cSrcweir 
422*cdf0e10cSrcweir ::boost::shared_ptr<ParsedRDFaAttributes>
423*cdf0e10cSrcweir RDFaImportHelper::ParseRDFa(
424*cdf0e10cSrcweir     ::rtl::OUString const & i_rAbout,
425*cdf0e10cSrcweir     ::rtl::OUString const & i_rProperty,
426*cdf0e10cSrcweir     ::rtl::OUString const & i_rContent,
427*cdf0e10cSrcweir     ::rtl::OUString const & i_rDatatype)
428*cdf0e10cSrcweir {
429*cdf0e10cSrcweir     if (!i_rProperty.getLength())
430*cdf0e10cSrcweir     {
431*cdf0e10cSrcweir         OSL_TRACE("AddRDFa: invalid input: xhtml:property empty");
432*cdf0e10cSrcweir         return ::boost::shared_ptr<ParsedRDFaAttributes>();
433*cdf0e10cSrcweir     }
434*cdf0e10cSrcweir     // must parse CURIEs here: need namespace declaration context
435*cdf0e10cSrcweir     RDFaReader reader(GetImport());
436*cdf0e10cSrcweir     const ::rtl::OUString about( reader.ReadURIOrSafeCURIE(i_rAbout) );
437*cdf0e10cSrcweir     if (!about.getLength()) {
438*cdf0e10cSrcweir         return ::boost::shared_ptr<ParsedRDFaAttributes>();
439*cdf0e10cSrcweir     }
440*cdf0e10cSrcweir     const ::std::vector< ::rtl::OUString > properties(
441*cdf0e10cSrcweir         reader.ReadCURIEs(i_rProperty) );
442*cdf0e10cSrcweir     if (!properties.size()) {
443*cdf0e10cSrcweir         return ::boost::shared_ptr<ParsedRDFaAttributes>();
444*cdf0e10cSrcweir     }
445*cdf0e10cSrcweir     const ::rtl::OUString datatype( i_rDatatype.getLength()
446*cdf0e10cSrcweir         ?   reader.ReadCURIE(i_rDatatype)
447*cdf0e10cSrcweir         :   ::rtl::OUString() );
448*cdf0e10cSrcweir     return ::boost::shared_ptr<ParsedRDFaAttributes>(
449*cdf0e10cSrcweir             new ParsedRDFaAttributes(about, properties, i_rContent, datatype));
450*cdf0e10cSrcweir }
451*cdf0e10cSrcweir 
452*cdf0e10cSrcweir void
453*cdf0e10cSrcweir RDFaImportHelper::AddRDFa(
454*cdf0e10cSrcweir     uno::Reference<rdf::XMetadatable> const & i_xObject,
455*cdf0e10cSrcweir     ::boost::shared_ptr<ParsedRDFaAttributes> & i_pRDFaAttributes)
456*cdf0e10cSrcweir {
457*cdf0e10cSrcweir     if (!i_xObject.is())
458*cdf0e10cSrcweir     {
459*cdf0e10cSrcweir         OSL_ENSURE(false, "AddRDFa: invalid arg: null textcontent");
460*cdf0e10cSrcweir         return;
461*cdf0e10cSrcweir     }
462*cdf0e10cSrcweir     if (!i_pRDFaAttributes.get())
463*cdf0e10cSrcweir     {
464*cdf0e10cSrcweir         OSL_ENSURE(false, "AddRDFa: invalid arg: null RDFa attributes");
465*cdf0e10cSrcweir         return;
466*cdf0e10cSrcweir     }
467*cdf0e10cSrcweir     m_RDFaEntries.push_back(RDFaEntry(i_xObject, i_pRDFaAttributes));
468*cdf0e10cSrcweir }
469*cdf0e10cSrcweir 
470*cdf0e10cSrcweir void
471*cdf0e10cSrcweir RDFaImportHelper::ParseAndAddRDFa(
472*cdf0e10cSrcweir     uno::Reference<rdf::XMetadatable> const & i_xObject,
473*cdf0e10cSrcweir     ::rtl::OUString const & i_rAbout,
474*cdf0e10cSrcweir     ::rtl::OUString const & i_rProperty,
475*cdf0e10cSrcweir     ::rtl::OUString const & i_rContent,
476*cdf0e10cSrcweir     ::rtl::OUString const & i_rDatatype)
477*cdf0e10cSrcweir {
478*cdf0e10cSrcweir     ::boost::shared_ptr<ParsedRDFaAttributes> pAttributes(
479*cdf0e10cSrcweir         ParseRDFa(i_rAbout, i_rProperty, i_rContent, i_rDatatype) );
480*cdf0e10cSrcweir     if (pAttributes.get())
481*cdf0e10cSrcweir     {
482*cdf0e10cSrcweir         AddRDFa(i_xObject, pAttributes);
483*cdf0e10cSrcweir     }
484*cdf0e10cSrcweir }
485*cdf0e10cSrcweir 
486*cdf0e10cSrcweir void RDFaImportHelper::InsertRDFa(
487*cdf0e10cSrcweir     uno::Reference< rdf::XRepositorySupplier> const & i_xModel)
488*cdf0e10cSrcweir {
489*cdf0e10cSrcweir     OSL_ENSURE(i_xModel.is(), "InsertRDFa: invalid arg: model null");
490*cdf0e10cSrcweir     if (!i_xModel.is()) return;
491*cdf0e10cSrcweir     const uno::Reference< rdf::XDocumentRepository > xRepository(
492*cdf0e10cSrcweir         i_xModel->getRDFRepository(), uno::UNO_QUERY);
493*cdf0e10cSrcweir     OSL_ENSURE(xRepository.is(), "InsertRDFa: no DocumentRepository?");
494*cdf0e10cSrcweir     if (!xRepository.is()) return;
495*cdf0e10cSrcweir     RDFaInserter inserter(GetImport().GetComponentContext(), xRepository);
496*cdf0e10cSrcweir     ::std::for_each(m_RDFaEntries.begin(), m_RDFaEntries.end(),
497*cdf0e10cSrcweir         ::boost::bind(&RDFaInserter::InsertRDFaEntry, &inserter, _1));
498*cdf0e10cSrcweir }
499*cdf0e10cSrcweir 
500*cdf0e10cSrcweir } // namespace xmloff
501*cdf0e10cSrcweir 
502