xref: /trunk/main/unoxml/source/rdf/CLiteral.cxx (revision e9cbe144)
1*e9cbe144SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*e9cbe144SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*e9cbe144SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*e9cbe144SAndrew Rist  * distributed with this work for additional information
6*e9cbe144SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*e9cbe144SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*e9cbe144SAndrew Rist  * "License"); you may not use this file except in compliance
9*e9cbe144SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*e9cbe144SAndrew Rist  *
11*e9cbe144SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*e9cbe144SAndrew Rist  *
13*e9cbe144SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*e9cbe144SAndrew Rist  * software distributed under the License is distributed on an
15*e9cbe144SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*e9cbe144SAndrew Rist  * KIND, either express or implied.  See the License for the
17*e9cbe144SAndrew Rist  * specific language governing permissions and limitations
18*e9cbe144SAndrew Rist  * under the License.
19*e9cbe144SAndrew Rist  *
20*e9cbe144SAndrew Rist  *************************************************************/
21*e9cbe144SAndrew Rist 
22*e9cbe144SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include "CNodes.hxx"
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #include <cppuhelper/implbase3.hxx>
27cdf0e10cSrcweir #include <com/sun/star/lang/XServiceInfo.hpp>
28cdf0e10cSrcweir #include <com/sun/star/lang/XInitialization.hpp>
29cdf0e10cSrcweir #include <com/sun/star/rdf/XLiteral.hpp>
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #include <com/sun/star/lang/IllegalArgumentException.hpp>
32cdf0e10cSrcweir 
33cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
34cdf0e10cSrcweir 
35cdf0e10cSrcweir 
36cdf0e10cSrcweir /// anonymous implementation namespace
37cdf0e10cSrcweir namespace {
38cdf0e10cSrcweir 
39cdf0e10cSrcweir namespace css = ::com::sun::star;
40cdf0e10cSrcweir 
41cdf0e10cSrcweir class CLiteral:
42cdf0e10cSrcweir     public ::cppu::WeakImplHelper3<
43cdf0e10cSrcweir         css::lang::XServiceInfo,
44cdf0e10cSrcweir         css::lang::XInitialization,
45cdf0e10cSrcweir         css::rdf::XLiteral>
46cdf0e10cSrcweir {
47cdf0e10cSrcweir public:
48cdf0e10cSrcweir     explicit CLiteral(css::uno::Reference< css::uno::XComponentContext > const & context);
~CLiteral()49cdf0e10cSrcweir     virtual ~CLiteral() {}
50cdf0e10cSrcweir 
51cdf0e10cSrcweir     // ::com::sun::star::lang::XServiceInfo:
52cdf0e10cSrcweir     virtual ::rtl::OUString SAL_CALL getImplementationName() throw (css::uno::RuntimeException);
53cdf0e10cSrcweir     virtual ::sal_Bool SAL_CALL supportsService(const ::rtl::OUString & ServiceName) throw (css::uno::RuntimeException);
54cdf0e10cSrcweir     virtual css::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames() throw (css::uno::RuntimeException);
55cdf0e10cSrcweir 
56cdf0e10cSrcweir     // ::com::sun::star::lang::XInitialization:
57cdf0e10cSrcweir     virtual void SAL_CALL initialize(const css::uno::Sequence< ::com::sun::star::uno::Any > & aArguments) throw (css::uno::RuntimeException, css::uno::Exception);
58cdf0e10cSrcweir 
59cdf0e10cSrcweir     // ::com::sun::star::rdf::XNode:
60cdf0e10cSrcweir     virtual ::rtl::OUString SAL_CALL getStringValue() throw (css::uno::RuntimeException);
61cdf0e10cSrcweir 
62cdf0e10cSrcweir     // ::com::sun::star::rdf::XLiteral:
63cdf0e10cSrcweir     virtual ::rtl::OUString SAL_CALL getValue() throw (css::uno::RuntimeException);
64cdf0e10cSrcweir     virtual ::rtl::OUString SAL_CALL getLanguage() throw (css::uno::RuntimeException);
65cdf0e10cSrcweir     virtual css::uno::Reference< css::rdf::XURI > SAL_CALL getDatatype() throw (css::uno::RuntimeException);
66cdf0e10cSrcweir 
67cdf0e10cSrcweir private:
68cdf0e10cSrcweir     CLiteral(const CLiteral &); // not defined
69cdf0e10cSrcweir     CLiteral& operator=(const CLiteral &); // not defined
70cdf0e10cSrcweir 
71cdf0e10cSrcweir     css::uno::Reference< css::uno::XComponentContext > m_xContext;
72cdf0e10cSrcweir 
73cdf0e10cSrcweir     ::rtl::OUString m_Value;
74cdf0e10cSrcweir     ::rtl::OUString m_Language;
75cdf0e10cSrcweir     css::uno::Reference< css::rdf::XURI > m_xDatatype;
76cdf0e10cSrcweir };
77cdf0e10cSrcweir 
CLiteral(css::uno::Reference<css::uno::XComponentContext> const & context)78cdf0e10cSrcweir CLiteral::CLiteral(css::uno::Reference< css::uno::XComponentContext > const & context) :
79cdf0e10cSrcweir     m_xContext(context), m_Value(), m_Language(), m_xDatatype()
80cdf0e10cSrcweir {}
81cdf0e10cSrcweir 
82cdf0e10cSrcweir // com.sun.star.uno.XServiceInfo:
getImplementationName()83cdf0e10cSrcweir ::rtl::OUString SAL_CALL CLiteral::getImplementationName() throw (css::uno::RuntimeException)
84cdf0e10cSrcweir {
85cdf0e10cSrcweir     return comp_CLiteral::_getImplementationName();
86cdf0e10cSrcweir }
87cdf0e10cSrcweir 
supportsService(::rtl::OUString const & serviceName)88cdf0e10cSrcweir ::sal_Bool SAL_CALL CLiteral::supportsService(::rtl::OUString const & serviceName) throw (css::uno::RuntimeException)
89cdf0e10cSrcweir {
90cdf0e10cSrcweir     css::uno::Sequence< ::rtl::OUString > serviceNames = comp_CLiteral::_getSupportedServiceNames();
91cdf0e10cSrcweir     for (::sal_Int32 i = 0; i < serviceNames.getLength(); ++i) {
92cdf0e10cSrcweir         if (serviceNames[i] == serviceName)
93cdf0e10cSrcweir             return sal_True;
94cdf0e10cSrcweir     }
95cdf0e10cSrcweir     return sal_False;
96cdf0e10cSrcweir }
97cdf0e10cSrcweir 
getSupportedServiceNames()98cdf0e10cSrcweir css::uno::Sequence< ::rtl::OUString > SAL_CALL CLiteral::getSupportedServiceNames() throw (css::uno::RuntimeException)
99cdf0e10cSrcweir {
100cdf0e10cSrcweir     return comp_CLiteral::_getSupportedServiceNames();
101cdf0e10cSrcweir }
102cdf0e10cSrcweir 
103cdf0e10cSrcweir // ::com::sun::star::lang::XInitialization:
initialize(const css::uno::Sequence<::com::sun::star::uno::Any> & aArguments)104cdf0e10cSrcweir void SAL_CALL CLiteral::initialize(const css::uno::Sequence< ::com::sun::star::uno::Any > & aArguments) throw (css::uno::RuntimeException, css::uno::Exception)
105cdf0e10cSrcweir {
106cdf0e10cSrcweir     const sal_Int32 len( aArguments.getLength() );
107cdf0e10cSrcweir     if (len < 1 || len > 2) {
108cdf0e10cSrcweir             throw css::lang::IllegalArgumentException(
109cdf0e10cSrcweir                 ::rtl::OUString::createFromAscii("CLiteral::initialize: "
110cdf0e10cSrcweir                     "must give 1 or 2 argument(s)"), *this, 2);
111cdf0e10cSrcweir     }
112cdf0e10cSrcweir 
113cdf0e10cSrcweir     ::rtl::OUString arg0;
114cdf0e10cSrcweir     if (!(aArguments[0] >>= arg0)) {
115cdf0e10cSrcweir         throw css::lang::IllegalArgumentException(
116cdf0e10cSrcweir             ::rtl::OUString::createFromAscii("CLiteral::initialize: "
117cdf0e10cSrcweir                 "argument must be string"), *this, 0);
118cdf0e10cSrcweir     }
119cdf0e10cSrcweir     //FIXME: what is legal?
120cdf0e10cSrcweir     if (true) {
121cdf0e10cSrcweir         m_Value = arg0;
122cdf0e10cSrcweir     } else {
123cdf0e10cSrcweir         throw css::lang::IllegalArgumentException(
124cdf0e10cSrcweir             ::rtl::OUString::createFromAscii("CLiteral::initialize: "
125cdf0e10cSrcweir                 "argument is not valid literal value"), *this, 0);
126cdf0e10cSrcweir     }
127cdf0e10cSrcweir 
128cdf0e10cSrcweir     if (len > 1) {
129cdf0e10cSrcweir         ::rtl::OUString arg1;
130cdf0e10cSrcweir         css::uno::Reference< css::rdf::XURI > xURI;
131cdf0e10cSrcweir         if ((aArguments[1] >>= arg1)) {
132cdf0e10cSrcweir             if (arg1.getLength() > 0) {
133cdf0e10cSrcweir                 m_Language = arg1;
134cdf0e10cSrcweir             } else {
135cdf0e10cSrcweir                 throw css::lang::IllegalArgumentException(
136cdf0e10cSrcweir                     ::rtl::OUString::createFromAscii("CLiteral::initialize: "
137cdf0e10cSrcweir                         "argument is not valid language"), *this, 1);
138cdf0e10cSrcweir             }
139cdf0e10cSrcweir         } else if ((aArguments[1] >>= xURI)) {
140cdf0e10cSrcweir             if (xURI.is()) {
141cdf0e10cSrcweir                 m_xDatatype = xURI;
142cdf0e10cSrcweir             } else {
143cdf0e10cSrcweir                 throw css::lang::IllegalArgumentException(
144cdf0e10cSrcweir                     ::rtl::OUString::createFromAscii("CLiteral::initialize: "
145cdf0e10cSrcweir                         "argument is null"), *this, 1);
146cdf0e10cSrcweir             }
147cdf0e10cSrcweir         } else {
148cdf0e10cSrcweir             throw css::lang::IllegalArgumentException(
149cdf0e10cSrcweir                 ::rtl::OUString::createFromAscii("CLiteral::initialize: "
150cdf0e10cSrcweir                     "argument must be string or URI"), *this, 1);
151cdf0e10cSrcweir         }
152cdf0e10cSrcweir     }
153cdf0e10cSrcweir }
154cdf0e10cSrcweir 
155cdf0e10cSrcweir // ::com::sun::star::rdf::XNode:
getStringValue()156cdf0e10cSrcweir ::rtl::OUString SAL_CALL CLiteral::getStringValue() throw (css::uno::RuntimeException)
157cdf0e10cSrcweir {
158cdf0e10cSrcweir     if (!m_Language.equalsAscii("")) {
159cdf0e10cSrcweir         ::rtl::OUStringBuffer buf(m_Value);
160cdf0e10cSrcweir         buf.appendAscii("@");
161cdf0e10cSrcweir         buf.append(m_Language);
162cdf0e10cSrcweir         return buf.makeStringAndClear();
163cdf0e10cSrcweir     } else if (m_xDatatype.is()) {
164cdf0e10cSrcweir         ::rtl::OUStringBuffer buf(m_Value);
165cdf0e10cSrcweir         buf.appendAscii("^^");
166cdf0e10cSrcweir         buf.append(m_xDatatype->getStringValue());
167cdf0e10cSrcweir         return buf.makeStringAndClear();
168cdf0e10cSrcweir     } else {
169cdf0e10cSrcweir         return m_Value;
170cdf0e10cSrcweir     }
171cdf0e10cSrcweir }
172cdf0e10cSrcweir 
173cdf0e10cSrcweir // ::com::sun::star::rdf::XLiteral:
getValue()174cdf0e10cSrcweir ::rtl::OUString SAL_CALL CLiteral::getValue() throw (css::uno::RuntimeException)
175cdf0e10cSrcweir {
176cdf0e10cSrcweir     return m_Value;
177cdf0e10cSrcweir }
178cdf0e10cSrcweir 
getLanguage()179cdf0e10cSrcweir ::rtl::OUString SAL_CALL CLiteral::getLanguage() throw (css::uno::RuntimeException)
180cdf0e10cSrcweir {
181cdf0e10cSrcweir     return m_Language;
182cdf0e10cSrcweir }
183cdf0e10cSrcweir 
getDatatype()184cdf0e10cSrcweir css::uno::Reference< css::rdf::XURI > SAL_CALL CLiteral::getDatatype() throw (css::uno::RuntimeException)
185cdf0e10cSrcweir {
186cdf0e10cSrcweir     return m_xDatatype;
187cdf0e10cSrcweir }
188cdf0e10cSrcweir 
189cdf0e10cSrcweir } // closing anonymous implementation namespace
190cdf0e10cSrcweir 
191cdf0e10cSrcweir 
192cdf0e10cSrcweir 
193cdf0e10cSrcweir // component helper namespace
194cdf0e10cSrcweir namespace comp_CLiteral {
195cdf0e10cSrcweir 
_getImplementationName()196cdf0e10cSrcweir ::rtl::OUString SAL_CALL _getImplementationName() {
197cdf0e10cSrcweir     return ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
198cdf0e10cSrcweir         "CLiteral"));
199cdf0e10cSrcweir }
200cdf0e10cSrcweir 
_getSupportedServiceNames()201cdf0e10cSrcweir css::uno::Sequence< ::rtl::OUString > SAL_CALL _getSupportedServiceNames()
202cdf0e10cSrcweir {
203cdf0e10cSrcweir     css::uno::Sequence< ::rtl::OUString > s(1);
204cdf0e10cSrcweir     s[0] = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
205cdf0e10cSrcweir         "com.sun.star.rdf.Literal"));
206cdf0e10cSrcweir     return s;
207cdf0e10cSrcweir }
208cdf0e10cSrcweir 
_create(const css::uno::Reference<css::uno::XComponentContext> & context)209cdf0e10cSrcweir css::uno::Reference< css::uno::XInterface > SAL_CALL _create(
210cdf0e10cSrcweir     const css::uno::Reference< css::uno::XComponentContext > & context)
211cdf0e10cSrcweir         SAL_THROW((css::uno::Exception))
212cdf0e10cSrcweir {
213cdf0e10cSrcweir     return static_cast< ::cppu::OWeakObject * >(new CLiteral(context));
214cdf0e10cSrcweir }
215cdf0e10cSrcweir 
216cdf0e10cSrcweir } // closing component helper namespace
217cdf0e10cSrcweir 
218