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