merge the formfield patch from ooo-build
[ooovba.git] / unoxml / source / rdf / CLiteral.cxx
blob25a8267f5cc5d2c1db3054fc700ca729e6478cc7
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: CLiteral.cxx,v $
10 * $Revision: 1.2 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include "CNodes.hxx"
33 #include <cppuhelper/implbase3.hxx>
34 #include <com/sun/star/lang/XServiceInfo.hpp>
35 #include <com/sun/star/lang/XInitialization.hpp>
36 #include <com/sun/star/rdf/XLiteral.hpp>
38 #include <com/sun/star/lang/IllegalArgumentException.hpp>
40 #include <rtl/ustrbuf.hxx>
43 /// anonymous implementation namespace
44 namespace {
46 namespace css = ::com::sun::star;
48 class CLiteral:
49 public ::cppu::WeakImplHelper3<
50 css::lang::XServiceInfo,
51 css::lang::XInitialization,
52 css::rdf::XLiteral>
54 public:
55 explicit CLiteral(css::uno::Reference< css::uno::XComponentContext > const & context);
56 virtual ~CLiteral() {}
58 // ::com::sun::star::lang::XServiceInfo:
59 virtual ::rtl::OUString SAL_CALL getImplementationName() throw (css::uno::RuntimeException);
60 virtual ::sal_Bool SAL_CALL supportsService(const ::rtl::OUString & ServiceName) throw (css::uno::RuntimeException);
61 virtual css::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames() throw (css::uno::RuntimeException);
63 // ::com::sun::star::lang::XInitialization:
64 virtual void SAL_CALL initialize(const css::uno::Sequence< ::com::sun::star::uno::Any > & aArguments) throw (css::uno::RuntimeException, css::uno::Exception);
66 // ::com::sun::star::rdf::XNode:
67 virtual ::rtl::OUString SAL_CALL getStringValue() throw (css::uno::RuntimeException);
69 // ::com::sun::star::rdf::XLiteral:
70 virtual ::rtl::OUString SAL_CALL getValue() throw (css::uno::RuntimeException);
71 virtual ::rtl::OUString SAL_CALL getLanguage() throw (css::uno::RuntimeException);
72 virtual css::uno::Reference< css::rdf::XURI > SAL_CALL getDatatype() throw (css::uno::RuntimeException);
74 private:
75 CLiteral(const CLiteral &); // not defined
76 CLiteral& operator=(const CLiteral &); // not defined
78 css::uno::Reference< css::uno::XComponentContext > m_xContext;
80 ::rtl::OUString m_Value;
81 ::rtl::OUString m_Language;
82 css::uno::Reference< css::rdf::XURI > m_xDatatype;
85 CLiteral::CLiteral(css::uno::Reference< css::uno::XComponentContext > const & context) :
86 m_xContext(context), m_Value(), m_Language(), m_xDatatype()
89 // com.sun.star.uno.XServiceInfo:
90 ::rtl::OUString SAL_CALL CLiteral::getImplementationName() throw (css::uno::RuntimeException)
92 return comp_CLiteral::_getImplementationName();
95 ::sal_Bool SAL_CALL CLiteral::supportsService(::rtl::OUString const & serviceName) throw (css::uno::RuntimeException)
97 css::uno::Sequence< ::rtl::OUString > serviceNames = comp_CLiteral::_getSupportedServiceNames();
98 for (::sal_Int32 i = 0; i < serviceNames.getLength(); ++i) {
99 if (serviceNames[i] == serviceName)
100 return sal_True;
102 return sal_False;
105 css::uno::Sequence< ::rtl::OUString > SAL_CALL CLiteral::getSupportedServiceNames() throw (css::uno::RuntimeException)
107 return comp_CLiteral::_getSupportedServiceNames();
110 // ::com::sun::star::lang::XInitialization:
111 void SAL_CALL CLiteral::initialize(const css::uno::Sequence< ::com::sun::star::uno::Any > & aArguments) throw (css::uno::RuntimeException, css::uno::Exception)
113 const sal_Int32 len( aArguments.getLength() );
114 if (len < 1 || len > 2) {
115 throw css::lang::IllegalArgumentException(
116 ::rtl::OUString::createFromAscii("CLiteral::initialize: "
117 "must give 1 or 2 argument(s)"), *this, 2);
120 ::rtl::OUString arg0;
121 if (!(aArguments[0] >>= arg0)) {
122 throw css::lang::IllegalArgumentException(
123 ::rtl::OUString::createFromAscii("CLiteral::initialize: "
124 "argument must be string"), *this, 0);
126 //FIXME: what is legal?
127 if (true) {
128 m_Value = arg0;
129 } else {
130 throw css::lang::IllegalArgumentException(
131 ::rtl::OUString::createFromAscii("CLiteral::initialize: "
132 "argument is not valid literal value"), *this, 0);
135 if (len > 1) {
136 ::rtl::OUString arg1;
137 css::uno::Reference< css::rdf::XURI > xURI;
138 if ((aArguments[1] >>= arg1)) {
139 if (arg1.getLength() > 0) {
140 m_Language = arg1;
141 } else {
142 throw css::lang::IllegalArgumentException(
143 ::rtl::OUString::createFromAscii("CLiteral::initialize: "
144 "argument is not valid language"), *this, 1);
146 } else if ((aArguments[1] >>= xURI)) {
147 if (xURI.is()) {
148 m_xDatatype = xURI;
149 } else {
150 throw css::lang::IllegalArgumentException(
151 ::rtl::OUString::createFromAscii("CLiteral::initialize: "
152 "argument is null"), *this, 1);
154 } else {
155 throw css::lang::IllegalArgumentException(
156 ::rtl::OUString::createFromAscii("CLiteral::initialize: "
157 "argument must be string or URI"), *this, 1);
162 // ::com::sun::star::rdf::XNode:
163 ::rtl::OUString SAL_CALL CLiteral::getStringValue() throw (css::uno::RuntimeException)
165 if (!m_Language.equalsAscii("")) {
166 ::rtl::OUStringBuffer buf(m_Value);
167 buf.appendAscii("@");
168 buf.append(m_Language);
169 return buf.makeStringAndClear();
170 } else if (m_xDatatype.is()) {
171 ::rtl::OUStringBuffer buf(m_Value);
172 buf.appendAscii("^^");
173 buf.append(m_xDatatype->getStringValue());
174 return buf.makeStringAndClear();
175 } else {
176 return m_Value;
180 // ::com::sun::star::rdf::XLiteral:
181 ::rtl::OUString SAL_CALL CLiteral::getValue() throw (css::uno::RuntimeException)
183 return m_Value;
186 ::rtl::OUString SAL_CALL CLiteral::getLanguage() throw (css::uno::RuntimeException)
188 return m_Language;
191 css::uno::Reference< css::rdf::XURI > SAL_CALL CLiteral::getDatatype() throw (css::uno::RuntimeException)
193 return m_xDatatype;
196 } // closing anonymous implementation namespace
200 // component helper namespace
201 namespace comp_CLiteral {
203 ::rtl::OUString SAL_CALL _getImplementationName() {
204 return ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
205 "CLiteral"));
208 css::uno::Sequence< ::rtl::OUString > SAL_CALL _getSupportedServiceNames()
210 css::uno::Sequence< ::rtl::OUString > s(1);
211 s[0] = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
212 "com.sun.star.rdf.Literal"));
213 return s;
216 css::uno::Reference< css::uno::XInterface > SAL_CALL _create(
217 const css::uno::Reference< css::uno::XComponentContext > & context)
218 SAL_THROW((css::uno::Exception))
220 return static_cast< ::cppu::OWeakObject * >(new CLiteral(context));
223 } // closing component helper namespace