LibreOffice Module desktop (master)  1
dp_configurationbackenddb.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 
21 #include <cppuhelper/exc_hlp.hxx>
22 #include <com/sun/star/deployment/DeploymentException.hpp>
23 #include <com/sun/star/uno/XComponentContext.hpp>
24 #include <com/sun/star/xml/xpath/XXPathAPI.hpp>
25 
27 
28 
29 using namespace ::com::sun::star::uno;
30 
31 #define EXTENSION_REG_NS "http://openoffice.org/extensionmanager/configuration-registry/2010"
32 #define NS_PREFIX "conf"
33 #define ROOT_ELEMENT_NAME "configuration-backend-db"
34 #define KEY_ELEMENT_NAME "configuration"
35 
37 
40  OUString const & url):BackendDb(xContext, url)
41 {
42 
43 }
44 
46 {
47  return EXTENSION_REG_NS;
48 }
49 
51 {
52  return NS_PREFIX;
53 }
54 
56 {
57  return ROOT_ELEMENT_NAME;
58 }
59 
61 {
62  return KEY_ELEMENT_NAME;
63 }
64 
65 
66 void ConfigurationBackendDb::addEntry(OUString const & url, Data const & data)
67 {
68  try{
69  if (!activateEntry(url))
70  {
71  Reference<css::xml::dom::XNode> helpNode
72  = writeKeyElement(url);
73 
74  writeSimpleElement("data-url", data.dataUrl, helpNode);
75  writeSimpleElement("ini-entry", data.iniEntry, helpNode);
76  save();
77  }
78  }
79  catch ( const css::deployment::DeploymentException& )
80  {
81  throw;
82  }
83  catch(const css::uno::Exception &)
84  {
85  Any exc( ::cppu::getCaughtException() );
86  throw css::deployment::DeploymentException(
87  "Extension Manager: failed to write data entry in configuration backend db: " +
88  m_urlDb, nullptr, exc);
89  }
90 }
91 
92 
93 ::o3tl::optional<ConfigurationBackendDb::Data>
94 ConfigurationBackendDb::getEntry(OUString const & url)
95 {
96  try
97  {
99  Reference<css::xml::dom::XNode> aNode = getKeyElement(url);
100  if (aNode.is())
101  {
102  retData.dataUrl = readSimpleElement("data-url", aNode);
103  retData.iniEntry = readSimpleElement("ini-entry", aNode);
104  }
105  else
106  {
107  return ::o3tl::optional<Data>();
108  }
109  return ::o3tl::optional<Data>(retData);
110  }
111  catch ( const css::deployment::DeploymentException& )
112  {
113  throw;
114  }
115  catch(const css::uno::Exception &)
116  {
117  Any exc( ::cppu::getCaughtException() );
118  throw css::deployment::DeploymentException(
119  "Extension Manager: failed to read data entry in configuration backend db: " +
120  m_urlDb, nullptr, exc);
121  }
122 }
123 
125 {
126  try
127  {
128  std::vector<OUString> listRet;
129  Reference<css::xml::dom::XDocument> doc = getDocument();
130  Reference<css::xml::dom::XNode> root = doc->getFirstChild();
131 
132  Reference<css::xml::xpath::XXPathAPI> xpathApi = getXPathAPI();
133  const OUString sPrefix = getNSPrefix();
134  OUString sExpression(
135  sPrefix + ":configuration/" + sPrefix + ":data-url/text()");
136  Reference<css::xml::dom::XNodeList> nodes =
137  xpathApi->selectNodeList(root, sExpression);
138  if (nodes.is())
139  {
140  sal_Int32 length = nodes->getLength();
141  for (sal_Int32 i = 0; i < length; i++)
142  listRet.push_back(nodes->item(i)->getNodeValue());
143  }
144  return listRet;
145  }
146  catch ( const css::deployment::DeploymentException& )
147  {
148  throw;
149  }
150  catch(const css::uno::Exception &)
151  {
152  Any exc( ::cppu::getCaughtException() );
153  throw css::deployment::DeploymentException(
154  "Extension Manager: failed to read data entry in configuration backend db: " +
155  m_urlDb, nullptr, exc);
156  }
157 }
158 
159 } // namespace dp_registry::backend::configuration
160 
161 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define NS_PREFIX
css::uno::Reference< css::xml::dom::XDocument > const & getDocument()
css::uno::Reference< css::xml::dom::XNode > writeKeyElement(OUString const &url)
The key elements have a url attribute and are always children of the root element.
Any SAL_CALL getCaughtException()
length
css::uno::Reference< css::xml::dom::XNode > getKeyElement(OUString const &url)
OUString sPrefix
ConfigurationBackendDb(css::uno::Reference< css::uno::XComponentContext > const &xContext, OUString const &url)
int i
void writeSimpleElement(OUString const &sElementName, OUString const &value, css::uno::Reference< css::xml::dom::XNode > const &xParent)
#define ROOT_ELEMENT_NAME
static uno::Reference< css::uno::XComponentContext > xContext
Definition: init.cxx:2084
#define EXTENSION_REG_NS
#define KEY_ELEMENT_NAME
css::uno::Reference< css::xml::xpath::XXPathAPI > const & getXPathAPI()
OUString readSimpleElement(OUString const &sElementName, css::uno::Reference< css::xml::dom::XNode > const &xParent)
bool activateEntry(OUString const &url)