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 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_desktop.hxx"
30 
31 #include "rtl/string.h"
32 #include "rtl/bootstrap.hxx"
33 #include "cppuhelper/exc_hlp.hxx"
34 #include "com/sun/star/uno/XComponentContext.hpp"
35 #include "com/sun/star/xml/dom/XDocumentBuilder.hpp"
36 #include "com/sun/star/xml/xpath/XXPathAPI.hpp"
37 #include "dp_misc.h"
38 
39 #include "dp_configurationbackenddb.hxx"
40 
41 
42 namespace css = ::com::sun::star;
43 using namespace ::com::sun::star::uno;
44 using ::rtl::OUString;
45 
46 #define EXTENSION_REG_NS "http://openoffice.org/extensionmanager/configuration-registry/2010"
47 #define NS_PREFIX "conf"
48 #define ROOT_ELEMENT_NAME "configuration-backend-db"
49 #define KEY_ELEMENT_NAME "configuration"
50 
51 namespace dp_registry {
52 namespace backend {
53 namespace configuration {
54 
55 ConfigurationBackendDb::ConfigurationBackendDb(
56     Reference<XComponentContext> const &  xContext,
57     ::rtl::OUString const & url):BackendDb(xContext, url)
58 {
59 
60 }
61 
62 OUString ConfigurationBackendDb::getDbNSName()
63 {
64     return OUSTR(EXTENSION_REG_NS);
65 }
66 
67 OUString ConfigurationBackendDb::getNSPrefix()
68 {
69     return OUSTR(NS_PREFIX);
70 }
71 
72 OUString ConfigurationBackendDb::getRootElementName()
73 {
74     return OUSTR(ROOT_ELEMENT_NAME);
75 }
76 
77 OUString ConfigurationBackendDb::getKeyElementName()
78 {
79     return OUSTR(KEY_ELEMENT_NAME);
80 }
81 
82 
83 void ConfigurationBackendDb::addEntry(::rtl::OUString const & url, Data const & data)
84 {
85     try{
86         if (!activateEntry(url))
87         {
88             Reference<css::xml::dom::XNode> helpNode
89                 = writeKeyElement(url);
90 
91             writeSimpleElement(OUSTR("data-url"), data.dataUrl, helpNode);
92             writeSimpleElement(OUSTR("ini-entry"), data.iniEntry, helpNode);
93             save();
94         }
95     }
96     catch (css::deployment::DeploymentException& )
97     {
98         throw;
99     }
100     catch(css::uno::Exception &)
101     {
102         Any exc( ::cppu::getCaughtException() );
103         throw css::deployment::DeploymentException(
104             OUSTR("Extension Manager: failed to write data entry in configuration backend db: ") +
105             m_urlDb, 0, exc);
106     }
107 }
108 
109 
110 ::boost::optional<ConfigurationBackendDb::Data>
111 ConfigurationBackendDb::getEntry(::rtl::OUString const & url)
112 {
113     try
114     {
115         ConfigurationBackendDb::Data retData;
116         Reference<css::xml::dom::XNode> aNode = getKeyElement(url);
117         if (aNode.is())
118         {
119             retData.dataUrl = readSimpleElement(OUSTR("data-url"), aNode);
120             retData.iniEntry = readSimpleElement(OUSTR("ini-entry"), aNode);
121         }
122         else
123         {
124             return ::boost::optional<Data>();
125         }
126         return ::boost::optional<Data>(retData);
127     }
128     catch (css::deployment::DeploymentException& )
129     {
130         throw;
131     }
132     catch(css::uno::Exception &)
133     {
134         Any exc( ::cppu::getCaughtException() );
135         throw css::deployment::DeploymentException(
136             OUSTR("Extension Manager: failed to read data entry in configuration backend db: ") +
137             m_urlDb, 0, exc);
138     }
139 }
140 
141 ::std::list<OUString> ConfigurationBackendDb::getAllDataUrls()
142 {
143     try
144     {
145         ::std::list<OUString> listRet;
146         Reference<css::xml::dom::XDocument> doc = getDocument();
147         Reference<css::xml::dom::XNode> root = doc->getFirstChild();
148 
149         Reference<css::xml::xpath::XXPathAPI> xpathApi = getXPathAPI();
150         const OUString sPrefix = getNSPrefix();
151         OUString sExpression(
152             sPrefix + OUSTR(":configuration/") + sPrefix + OUSTR(":data-url/text()"));
153         Reference<css::xml::dom::XNodeList> nodes =
154             xpathApi->selectNodeList(root, sExpression);
155         if (nodes.is())
156         {
157             sal_Int32 length = nodes->getLength();
158             for (sal_Int32 i = 0; i < length; i++)
159                 listRet.push_back(nodes->item(i)->getNodeValue());
160         }
161         return listRet;
162     }
163     catch (css::deployment::DeploymentException& )
164     {
165         throw;
166     }
167     catch(css::uno::Exception &)
168     {
169         Any exc( ::cppu::getCaughtException() );
170         throw css::deployment::DeploymentException(
171             OUSTR("Extension Manager: failed to read data entry in configuration backend db: ") +
172             m_urlDb, 0, exc);
173     }
174 }
175 
176 ::std::list<OUString> ConfigurationBackendDb::getAllIniEntries()
177 {
178     return getOneChildFromAllEntries(OUSTR("ini-entry"));
179 }
180 
181 
182 
183 } // namespace configuration
184 } // namespace backend
185 } // namespace dp_registry
186 
187