Bump for 4.0-11
[LibreOffice.git] / unoxml / source / rdf / CLiteral.cxx
blob2261d45819bd76926dafa4253dc1971e9b5b3b7c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "CNodes.hxx"
22 #include <cppuhelper/implbase3.hxx>
23 #include <com/sun/star/lang/XServiceInfo.hpp>
24 #include <com/sun/star/lang/XInitialization.hpp>
25 #include <com/sun/star/rdf/XLiteral.hpp>
27 #include <com/sun/star/lang/IllegalArgumentException.hpp>
29 #include <rtl/ustrbuf.hxx>
32 /// anonymous implementation namespace
33 namespace {
35 class CLiteral:
36 public ::cppu::WeakImplHelper3<
37 css::lang::XServiceInfo,
38 css::lang::XInitialization,
39 css::rdf::XLiteral>
41 public:
42 explicit CLiteral(css::uno::Reference< css::uno::XComponentContext > const & context);
43 virtual ~CLiteral() {}
45 // ::com::sun::star::lang::XServiceInfo:
46 virtual ::rtl::OUString SAL_CALL getImplementationName() throw (css::uno::RuntimeException);
47 virtual ::sal_Bool SAL_CALL supportsService(const ::rtl::OUString & ServiceName) throw (css::uno::RuntimeException);
48 virtual css::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames() throw (css::uno::RuntimeException);
50 // ::com::sun::star::lang::XInitialization:
51 virtual void SAL_CALL initialize(const css::uno::Sequence< ::com::sun::star::uno::Any > & aArguments) throw (css::uno::RuntimeException, css::uno::Exception);
53 // ::com::sun::star::rdf::XNode:
54 virtual ::rtl::OUString SAL_CALL getStringValue() throw (css::uno::RuntimeException);
56 // ::com::sun::star::rdf::XLiteral:
57 virtual ::rtl::OUString SAL_CALL getValue() throw (css::uno::RuntimeException);
58 virtual ::rtl::OUString SAL_CALL getLanguage() throw (css::uno::RuntimeException);
59 virtual css::uno::Reference< css::rdf::XURI > SAL_CALL getDatatype() throw (css::uno::RuntimeException);
61 private:
62 CLiteral(const CLiteral &); // not defined
63 CLiteral& operator=(const CLiteral &); // not defined
65 css::uno::Reference< css::uno::XComponentContext > m_xContext;
67 ::rtl::OUString m_Value;
68 ::rtl::OUString m_Language;
69 css::uno::Reference< css::rdf::XURI > m_xDatatype;
72 CLiteral::CLiteral(css::uno::Reference< css::uno::XComponentContext > const & context) :
73 m_xContext(context), m_Value(), m_Language(), m_xDatatype()
76 // com.sun.star.uno.XServiceInfo:
77 ::rtl::OUString SAL_CALL CLiteral::getImplementationName() throw (css::uno::RuntimeException)
79 return comp_CLiteral::_getImplementationName();
82 ::sal_Bool SAL_CALL CLiteral::supportsService(::rtl::OUString const & serviceName) throw (css::uno::RuntimeException)
84 css::uno::Sequence< ::rtl::OUString > serviceNames = comp_CLiteral::_getSupportedServiceNames();
85 for (::sal_Int32 i = 0; i < serviceNames.getLength(); ++i) {
86 if (serviceNames[i] == serviceName)
87 return sal_True;
89 return sal_False;
92 css::uno::Sequence< ::rtl::OUString > SAL_CALL CLiteral::getSupportedServiceNames() throw (css::uno::RuntimeException)
94 return comp_CLiteral::_getSupportedServiceNames();
97 // ::com::sun::star::lang::XInitialization:
98 void SAL_CALL CLiteral::initialize(const css::uno::Sequence< ::com::sun::star::uno::Any > & aArguments) throw (css::uno::RuntimeException, css::uno::Exception)
100 const sal_Int32 len( aArguments.getLength() );
101 if (len < 1 || len > 2) {
102 throw css::lang::IllegalArgumentException(
103 ::rtl::OUString("CLiteral::initialize: "
104 "must give 1 or 2 argument(s)"), *this, 2);
107 ::rtl::OUString arg0;
108 if (!(aArguments[0] >>= arg0)) {
109 throw css::lang::IllegalArgumentException(
110 ::rtl::OUString("CLiteral::initialize: "
111 "argument must be string"), *this, 0);
113 //FIXME: what is legal?
114 if (true) {
115 m_Value = arg0;
116 } else {
117 throw css::lang::IllegalArgumentException(
118 ::rtl::OUString("CLiteral::initialize: "
119 "argument is not valid literal value"), *this, 0);
122 if (len > 1) {
123 ::rtl::OUString arg1;
124 css::uno::Reference< css::rdf::XURI > xURI;
125 if ((aArguments[1] >>= arg1)) {
126 if (!arg1.isEmpty()) {
127 m_Language = arg1;
128 } else {
129 throw css::lang::IllegalArgumentException(
130 ::rtl::OUString("CLiteral::initialize: "
131 "argument is not valid language"), *this, 1);
133 } else if ((aArguments[1] >>= xURI)) {
134 if (xURI.is()) {
135 m_xDatatype = xURI;
136 } else {
137 throw css::lang::IllegalArgumentException(
138 ::rtl::OUString("CLiteral::initialize: "
139 "argument is null"), *this, 1);
141 } else {
142 throw css::lang::IllegalArgumentException(
143 ::rtl::OUString("CLiteral::initialize: "
144 "argument must be string or URI"), *this, 1);
149 // ::com::sun::star::rdf::XNode:
150 ::rtl::OUString SAL_CALL CLiteral::getStringValue() throw (css::uno::RuntimeException)
152 if (!m_Language.isEmpty()) {
153 ::rtl::OUStringBuffer buf(m_Value);
154 buf.appendAscii("@");
155 buf.append(m_Language);
156 return buf.makeStringAndClear();
157 } else if (m_xDatatype.is()) {
158 ::rtl::OUStringBuffer buf(m_Value);
159 buf.appendAscii("^^");
160 buf.append(m_xDatatype->getStringValue());
161 return buf.makeStringAndClear();
162 } else {
163 return m_Value;
167 // ::com::sun::star::rdf::XLiteral:
168 ::rtl::OUString SAL_CALL CLiteral::getValue() throw (css::uno::RuntimeException)
170 return m_Value;
173 ::rtl::OUString SAL_CALL CLiteral::getLanguage() throw (css::uno::RuntimeException)
175 return m_Language;
178 css::uno::Reference< css::rdf::XURI > SAL_CALL CLiteral::getDatatype() throw (css::uno::RuntimeException)
180 return m_xDatatype;
183 } // closing anonymous implementation namespace
187 // component helper namespace
188 namespace comp_CLiteral {
190 ::rtl::OUString SAL_CALL _getImplementationName() {
191 return ::rtl::OUString( "CLiteral");
194 css::uno::Sequence< ::rtl::OUString > SAL_CALL _getSupportedServiceNames()
196 css::uno::Sequence< ::rtl::OUString > s(1);
197 s[0] = ::rtl::OUString( "com.sun.star.rdf.Literal");
198 return s;
201 css::uno::Reference< css::uno::XInterface > SAL_CALL _create(
202 const css::uno::Reference< css::uno::XComponentContext > & context)
203 SAL_THROW((css::uno::Exception))
205 return static_cast< ::cppu::OWeakObject * >(new CLiteral(context));
208 } // closing component helper namespace
210 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */