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_helpbackenddb.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/help-registry/2010"
47 #define NS_PREFIX "help"
48 #define ROOT_ELEMENT_NAME "help-backend-db"
49 #define KEY_ELEMENT_NAME "help"
50 
51 namespace dp_registry {
52 namespace backend {
53 namespace help {
54 
55 HelpBackendDb::HelpBackendDb(
56     Reference<XComponentContext> const &  xContext,
57     ::rtl::OUString const & url):BackendDb(xContext, url)
58 {
59 
60 }
61 
62 OUString HelpBackendDb::getDbNSName()
63 {
64     return OUSTR(EXTENSION_REG_NS);
65 }
66 
67 OUString HelpBackendDb::getNSPrefix()
68 {
69     return OUSTR(NS_PREFIX);
70 }
71 
72 OUString HelpBackendDb::getRootElementName()
73 {
74     return OUSTR(ROOT_ELEMENT_NAME);
75 }
76 
77 OUString HelpBackendDb::getKeyElementName()
78 {
79     return OUSTR(KEY_ELEMENT_NAME);
80 }
81 
82 
83 void HelpBackendDb::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             save();
93         }
94     }
95     catch (css::deployment::DeploymentException& )
96     {
97         throw;
98     }
99     catch(css::uno::Exception &)
100     {
101         Any exc( ::cppu::getCaughtException() );
102         throw css::deployment::DeploymentException(
103             OUSTR("Extension Manager: failed to write data entry in help backend db: ") +
104             m_urlDb, 0, exc);
105     }
106 }
107 
108 
109 ::boost::optional<HelpBackendDb::Data>
110 HelpBackendDb::getEntry(::rtl::OUString const & url)
111 {
112     try
113     {
114         HelpBackendDb::Data retData;
115         Reference<css::xml::dom::XNode> aNode = getKeyElement(url);
116         if (aNode.is())
117         {
118             retData.dataUrl = readSimpleElement(OUSTR("data-url"), aNode);
119         }
120         else
121         {
122             return ::boost::optional<Data>();
123         }
124         return ::boost::optional<Data>(retData);
125     }
126     catch (css::deployment::DeploymentException& )
127     {
128         throw;
129     }
130     catch(css::uno::Exception &)
131     {
132         Any exc( ::cppu::getCaughtException() );
133         throw css::deployment::DeploymentException(
134             OUSTR("Extension Manager: failed to read data entry in help backend db: ") +
135             m_urlDb, 0, exc);
136     }
137 }
138 
139 ::std::list<OUString> HelpBackendDb::getAllDataUrls()
140 {
141     try
142     {
143         ::std::list<OUString> listRet;
144         Reference<css::xml::dom::XDocument> doc = getDocument();
145         Reference<css::xml::dom::XNode> root = doc->getFirstChild();
146 
147         Reference<css::xml::xpath::XXPathAPI> xpathApi = getXPathAPI();
148         const OUString sPrefix = getNSPrefix();
149         OUString sExpression(
150             sPrefix + OUSTR(":help/") + sPrefix + OUSTR(":data-url/text()"));
151         Reference<css::xml::dom::XNodeList> nodes =
152             xpathApi->selectNodeList(root, sExpression);
153         if (nodes.is())
154         {
155             sal_Int32 length = nodes->getLength();
156             for (sal_Int32 i = 0; i < length; i++)
157                 listRet.push_back(nodes->item(i)->getNodeValue());
158         }
159         return listRet;
160     }
161     catch (css::deployment::DeploymentException& )
162     {
163         throw;
164     }
165     catch(css::uno::Exception &)
166     {
167         Any exc( ::cppu::getCaughtException() );
168         throw css::deployment::DeploymentException(
169             OUSTR("Extension Manager: failed to read data entry in help backend db: ") +
170             m_urlDb, 0, exc);
171     }
172 }
173 
174 
175 } // namespace help
176 } // namespace backend
177 } // namespace dp_registry
178 
179