xref: /aoo41x/main/unoxml/source/rdf/CLiteral.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #include "CNodes.hxx"
29 
30 #include <cppuhelper/implbase3.hxx>
31 #include <com/sun/star/lang/XServiceInfo.hpp>
32 #include <com/sun/star/lang/XInitialization.hpp>
33 #include <com/sun/star/rdf/XLiteral.hpp>
34 
35 #include <com/sun/star/lang/IllegalArgumentException.hpp>
36 
37 #include <rtl/ustrbuf.hxx>
38 
39 
40 /// anonymous implementation namespace
41 namespace {
42 
43 namespace css = ::com::sun::star;
44 
45 class CLiteral:
46     public ::cppu::WeakImplHelper3<
47         css::lang::XServiceInfo,
48         css::lang::XInitialization,
49         css::rdf::XLiteral>
50 {
51 public:
52     explicit CLiteral(css::uno::Reference< css::uno::XComponentContext > const & context);
53     virtual ~CLiteral() {}
54 
55     // ::com::sun::star::lang::XServiceInfo:
56     virtual ::rtl::OUString SAL_CALL getImplementationName() throw (css::uno::RuntimeException);
57     virtual ::sal_Bool SAL_CALL supportsService(const ::rtl::OUString & ServiceName) throw (css::uno::RuntimeException);
58     virtual css::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames() throw (css::uno::RuntimeException);
59 
60     // ::com::sun::star::lang::XInitialization:
61     virtual void SAL_CALL initialize(const css::uno::Sequence< ::com::sun::star::uno::Any > & aArguments) throw (css::uno::RuntimeException, css::uno::Exception);
62 
63     // ::com::sun::star::rdf::XNode:
64     virtual ::rtl::OUString SAL_CALL getStringValue() throw (css::uno::RuntimeException);
65 
66     // ::com::sun::star::rdf::XLiteral:
67     virtual ::rtl::OUString SAL_CALL getValue() throw (css::uno::RuntimeException);
68     virtual ::rtl::OUString SAL_CALL getLanguage() throw (css::uno::RuntimeException);
69     virtual css::uno::Reference< css::rdf::XURI > SAL_CALL getDatatype() throw (css::uno::RuntimeException);
70 
71 private:
72     CLiteral(const CLiteral &); // not defined
73     CLiteral& operator=(const CLiteral &); // not defined
74 
75     css::uno::Reference< css::uno::XComponentContext > m_xContext;
76 
77     ::rtl::OUString m_Value;
78     ::rtl::OUString m_Language;
79     css::uno::Reference< css::rdf::XURI > m_xDatatype;
80 };
81 
82 CLiteral::CLiteral(css::uno::Reference< css::uno::XComponentContext > const & context) :
83     m_xContext(context), m_Value(), m_Language(), m_xDatatype()
84 {}
85 
86 // com.sun.star.uno.XServiceInfo:
87 ::rtl::OUString SAL_CALL CLiteral::getImplementationName() throw (css::uno::RuntimeException)
88 {
89     return comp_CLiteral::_getImplementationName();
90 }
91 
92 ::sal_Bool SAL_CALL CLiteral::supportsService(::rtl::OUString const & serviceName) throw (css::uno::RuntimeException)
93 {
94     css::uno::Sequence< ::rtl::OUString > serviceNames = comp_CLiteral::_getSupportedServiceNames();
95     for (::sal_Int32 i = 0; i < serviceNames.getLength(); ++i) {
96         if (serviceNames[i] == serviceName)
97             return sal_True;
98     }
99     return sal_False;
100 }
101 
102 css::uno::Sequence< ::rtl::OUString > SAL_CALL CLiteral::getSupportedServiceNames() throw (css::uno::RuntimeException)
103 {
104     return comp_CLiteral::_getSupportedServiceNames();
105 }
106 
107 // ::com::sun::star::lang::XInitialization:
108 void SAL_CALL CLiteral::initialize(const css::uno::Sequence< ::com::sun::star::uno::Any > & aArguments) throw (css::uno::RuntimeException, css::uno::Exception)
109 {
110     const sal_Int32 len( aArguments.getLength() );
111     if (len < 1 || len > 2) {
112             throw css::lang::IllegalArgumentException(
113                 ::rtl::OUString::createFromAscii("CLiteral::initialize: "
114                     "must give 1 or 2 argument(s)"), *this, 2);
115     }
116 
117     ::rtl::OUString arg0;
118     if (!(aArguments[0] >>= arg0)) {
119         throw css::lang::IllegalArgumentException(
120             ::rtl::OUString::createFromAscii("CLiteral::initialize: "
121                 "argument must be string"), *this, 0);
122     }
123     //FIXME: what is legal?
124     if (true) {
125         m_Value = arg0;
126     } else {
127         throw css::lang::IllegalArgumentException(
128             ::rtl::OUString::createFromAscii("CLiteral::initialize: "
129                 "argument is not valid literal value"), *this, 0);
130     }
131 
132     if (len > 1) {
133         ::rtl::OUString arg1;
134         css::uno::Reference< css::rdf::XURI > xURI;
135         if ((aArguments[1] >>= arg1)) {
136             if (arg1.getLength() > 0) {
137                 m_Language = arg1;
138             } else {
139                 throw css::lang::IllegalArgumentException(
140                     ::rtl::OUString::createFromAscii("CLiteral::initialize: "
141                         "argument is not valid language"), *this, 1);
142             }
143         } else if ((aArguments[1] >>= xURI)) {
144             if (xURI.is()) {
145                 m_xDatatype = xURI;
146             } else {
147                 throw css::lang::IllegalArgumentException(
148                     ::rtl::OUString::createFromAscii("CLiteral::initialize: "
149                         "argument is null"), *this, 1);
150             }
151         } else {
152             throw css::lang::IllegalArgumentException(
153                 ::rtl::OUString::createFromAscii("CLiteral::initialize: "
154                     "argument must be string or URI"), *this, 1);
155         }
156     }
157 }
158 
159 // ::com::sun::star::rdf::XNode:
160 ::rtl::OUString SAL_CALL CLiteral::getStringValue() throw (css::uno::RuntimeException)
161 {
162     if (!m_Language.equalsAscii("")) {
163         ::rtl::OUStringBuffer buf(m_Value);
164         buf.appendAscii("@");
165         buf.append(m_Language);
166         return buf.makeStringAndClear();
167     } else if (m_xDatatype.is()) {
168         ::rtl::OUStringBuffer buf(m_Value);
169         buf.appendAscii("^^");
170         buf.append(m_xDatatype->getStringValue());
171         return buf.makeStringAndClear();
172     } else {
173         return m_Value;
174     }
175 }
176 
177 // ::com::sun::star::rdf::XLiteral:
178 ::rtl::OUString SAL_CALL CLiteral::getValue() throw (css::uno::RuntimeException)
179 {
180     return m_Value;
181 }
182 
183 ::rtl::OUString SAL_CALL CLiteral::getLanguage() throw (css::uno::RuntimeException)
184 {
185     return m_Language;
186 }
187 
188 css::uno::Reference< css::rdf::XURI > SAL_CALL CLiteral::getDatatype() throw (css::uno::RuntimeException)
189 {
190     return m_xDatatype;
191 }
192 
193 } // closing anonymous implementation namespace
194 
195 
196 
197 // component helper namespace
198 namespace comp_CLiteral {
199 
200 ::rtl::OUString SAL_CALL _getImplementationName() {
201     return ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
202         "CLiteral"));
203 }
204 
205 css::uno::Sequence< ::rtl::OUString > SAL_CALL _getSupportedServiceNames()
206 {
207     css::uno::Sequence< ::rtl::OUString > s(1);
208     s[0] = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
209         "com.sun.star.rdf.Literal"));
210     return s;
211 }
212 
213 css::uno::Reference< css::uno::XInterface > SAL_CALL _create(
214     const css::uno::Reference< css::uno::XComponentContext > & context)
215         SAL_THROW((css::uno::Exception))
216 {
217     return static_cast< ::cppu::OWeakObject * >(new CLiteral(context));
218 }
219 
220 } // closing component helper namespace
221 
222