merge the formfield patch from ooo-build
[ooovba.git] / jvmfwk / source / elements.cxx
blob38b91dbab7271691459daef2f3c479aa3438b82a
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: elements.cxx,v $
10 * $Revision: 1.23.10.1 $
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 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_jvmfwk.hxx"
33 #include "elements.hxx"
34 #include "osl/mutex.hxx"
35 #include "osl/file.hxx"
36 #include "osl/time.h"
37 #include "fwkutil.hxx"
38 #include "fwkbase.hxx"
39 #include "framework.hxx"
40 #include "libxmlutil.hxx"
41 #include "osl/thread.hxx"
42 #include <algorithm>
43 #include "libxml/parser.h"
44 #include "libxml/xpath.h"
45 #include "libxml/xpathInternals.h"
46 #include "rtl/bootstrap.hxx"
47 #include "boost/optional.hpp"
48 #include <string.h>
49 // #define NS_JAVA_FRAMEWORK "http://openoffice.org/2004/java/framework/1.0"
50 // #define NS_SCHEMA_INSTANCE "http://www.w3.org/2001/XMLSchema-instance"
53 using namespace osl;
54 namespace jfw
57 rtl::OString getElementUpdated()
59 //Prepare the xml document and context
60 rtl::OString sSettingsPath = jfw::getVendorSettingsPath();
61 OSL_ASSERT(sSettingsPath.getLength() > 0);
62 jfw::CXmlDocPtr doc(xmlParseFile(sSettingsPath.getStr()));
63 if (doc == NULL)
64 throw FrameworkException(
65 JFW_E_ERROR,
66 rtl::OString("[Java framework] Error in function getElementUpdated "
67 "(elements.cxx)"));
69 jfw::CXPathContextPtr context(xmlXPathNewContext(doc));
70 if (xmlXPathRegisterNs(context, (xmlChar*) "jf",
71 (xmlChar*) NS_JAVA_FRAMEWORK) == -1)
72 throw FrameworkException(
73 JFW_E_ERROR,
74 rtl::OString("[Java framework] Error in function getElementUpdated "
75 "(elements.cxx)"));
76 CXPathObjectPtr pathObj;
77 pathObj = xmlXPathEvalExpression(
78 (xmlChar*)"/jf:javaSelection/jf:updated/text()", context);
79 if (xmlXPathNodeSetIsEmpty(pathObj->nodesetval))
80 throw FrameworkException(
81 JFW_E_ERROR,
82 rtl::OString("[Java framework] Error in function getElementUpdated "
83 "(elements.cxx)"));
84 rtl::OString sValue = (sal_Char*) pathObj->nodesetval->nodeTab[0]->content;
85 return sValue;
89 void createSettingsStructure(xmlDoc * document, bool * bNeedsSave)
91 rtl::OString sExcMsg("[Java framework] Error in function createSettingsStructure "
92 "(elements.cxx).");
93 xmlNode * root = xmlDocGetRootElement(document);
94 if (root == NULL)
95 throw FrameworkException(JFW_E_ERROR, sExcMsg);
96 bool bFound = false;
97 xmlNode * cur = root->children;
98 while (cur != NULL)
100 if (xmlStrcmp(cur->name, (xmlChar*) "enabled") == 0)
102 bFound = true;
103 break;
105 cur = cur->next;
107 if (bFound)
109 bNeedsSave = false;
110 return;
112 //We will modify this document
113 *bNeedsSave = true;
114 // Now we create the child elements ------------------
115 //Get xsi:nil namespace
116 xmlNs* nsXsi = xmlSearchNsByHref(
117 document, root,(xmlChar*) NS_SCHEMA_INSTANCE);
119 //<enabled xsi:nil="true"
120 xmlNode * nodeEn = xmlNewTextChild(
121 root,NULL, (xmlChar*) "enabled", (xmlChar*) "");
122 if (nodeEn == NULL)
123 throw FrameworkException(JFW_E_ERROR, sExcMsg);
124 xmlSetNsProp(nodeEn,nsXsi,(xmlChar*) "nil",(xmlChar*) "true");
125 //add a new line
126 xmlNode * nodeCrLf = xmlNewText((xmlChar*) "\n");
127 xmlAddChild(root, nodeCrLf);
129 //<userClassPath xsi:nil="true">
130 xmlNode * nodeUs = xmlNewTextChild(
131 root,NULL, (xmlChar*) "userClassPath", (xmlChar*) "");
132 if (nodeUs == NULL)
133 throw FrameworkException(JFW_E_ERROR, sExcMsg);
134 xmlSetNsProp(nodeUs,nsXsi,(xmlChar*) "nil",(xmlChar*) "true");
135 //add a new line
136 nodeCrLf = xmlNewText((xmlChar*) "\n");
137 xmlAddChild(root, nodeCrLf);
139 //<vmParameters xsi:nil="true">
140 xmlNode * nodeVm = xmlNewTextChild(
141 root,NULL, (xmlChar*) "vmParameters", (xmlChar*) "");
142 if (nodeVm == NULL)
143 throw FrameworkException(JFW_E_ERROR, sExcMsg);
144 xmlSetNsProp(nodeVm,nsXsi,(xmlChar*) "nil",(xmlChar*) "true");
145 //add a new line
146 nodeCrLf = xmlNewText((xmlChar*) "\n");
147 xmlAddChild(root, nodeCrLf);
149 //<jreLocations xsi:nil="true">
150 xmlNode * nodeJre = xmlNewTextChild(
151 root,NULL, (xmlChar*) "jreLocations", (xmlChar*) "");
152 if (nodeJre == NULL)
153 throw FrameworkException(JFW_E_ERROR, sExcMsg);
154 xmlSetNsProp(nodeJre,nsXsi,(xmlChar*) "nil",(xmlChar*) "true");
155 //add a new line
156 nodeCrLf = xmlNewText((xmlChar*) "\n");
157 xmlAddChild(root, nodeCrLf);
159 //<javaInfo xsi:nil="true" autoSelect="true">
160 xmlNode * nodeJava = xmlNewTextChild(
161 root,NULL, (xmlChar*) "javaInfo", (xmlChar*) "");
162 if (nodeJava == NULL)
163 throw FrameworkException(JFW_E_ERROR, sExcMsg);
164 xmlSetNsProp(nodeJava,nsXsi,(xmlChar*) "nil",(xmlChar*) "true");
165 // xmlSetProp(nodeJava,(xmlChar*) "autoSelect",(xmlChar*) "true");
166 //add a new line
167 nodeCrLf = xmlNewText((xmlChar*) "\n");
168 xmlAddChild(root, nodeCrLf);
172 //====================================================================
173 VersionInfo::VersionInfo(): arVersions(NULL)
177 VersionInfo::~VersionInfo()
179 delete [] arVersions;
182 void VersionInfo::addExcludeVersion(const rtl::OUString& sVersion)
184 vecExcludeVersions.push_back(sVersion);
187 rtl_uString** VersionInfo::getExcludeVersions()
189 osl::MutexGuard guard(FwkMutex::get());
190 if (arVersions != NULL)
191 return arVersions;
193 arVersions = new rtl_uString*[vecExcludeVersions.size()];
194 int j=0;
195 typedef std::vector<rtl::OUString>::const_iterator it;
196 for (it i = vecExcludeVersions.begin(); i != vecExcludeVersions.end();
197 i++, j++)
199 arVersions[j] = vecExcludeVersions[j].pData;
201 return arVersions;
204 sal_Int32 VersionInfo::getExcludeVersionSize()
206 return vecExcludeVersions.size();
208 //==================================================================
210 NodeJava::NodeJava(Layer layer):
211 m_layer(layer)
213 //This class reads and write to files which should only be done in
214 //application mode
215 if (getMode() == JFW_MODE_DIRECT)
216 throw FrameworkException(
217 JFW_E_DIRECT_MODE,
218 "[Java framework] Trying to access settings files in direct mode.");
220 if (USER_OR_INSTALL == m_layer)
222 if (BootParams::getInstallData().getLength() > 0)
223 m_layer = INSTALL;
224 else
225 m_layer = USER;
227 else
229 m_layer = layer;
234 void NodeJava::load()
236 const rtl::OString sExcMsg("[Java framework] Error in function NodeJava::load"
237 "(elements.cxx).");
238 if (SHARED == m_layer)
240 //we do not support yet to write into the shared installation
242 //check if shared settings exist at all.
243 jfw::FileStatus s = checkFileURL(BootParams::getSharedData());
244 if (s == FILE_INVALID)
245 throw FrameworkException(
246 JFW_E_ERROR,
247 "[Java framework] Invalid file for shared Java settings.");
248 else if (s == FILE_DOES_NOT_EXIST)
249 //Writing shared data is not supported yet.
250 return;
252 else if (USER == m_layer || INSTALL == m_layer)
254 prepareSettingsDocument();
256 else
258 OSL_ASSERT("[Java framework] Unknown enum used.");
262 //Read the user elements
263 rtl::OString sSettingsPath = getSettingsPath();
264 //There must not be a share settings file
265 CXmlDocPtr docUser(xmlParseFile(sSettingsPath.getStr()));
266 if (docUser == NULL)
267 throw FrameworkException(JFW_E_ERROR, sExcMsg);
269 xmlNode * cur = xmlDocGetRootElement(docUser);
270 if (cur == NULL || cur->children == NULL)
271 throw FrameworkException(JFW_E_ERROR, sExcMsg);
273 CXmlCharPtr sNil;
274 cur = cur->children;
275 while (cur != NULL)
277 if (xmlStrcmp(cur->name, (xmlChar*) "enabled") == 0)
279 //only overwrite share settings if xsi:nil="false"
280 sNil = xmlGetNsProp(
281 cur, (xmlChar*) "nil", (xmlChar*) NS_SCHEMA_INSTANCE);
282 if (sNil == NULL)
283 throw FrameworkException(JFW_E_ERROR, sExcMsg);;
284 if (xmlStrcmp(sNil, (xmlChar*) "false") == 0)
286 CXmlCharPtr sEnabled( xmlNodeListGetString(
287 docUser, cur->children, 1));
288 if (xmlStrcmp(sEnabled, (xmlChar*) "true") == 0)
289 m_enabled = boost::optional<sal_Bool>(sal_True);
290 else if (xmlStrcmp(sEnabled, (xmlChar*) "false") == 0)
291 m_enabled = boost::optional<sal_Bool>(sal_False);
294 else if (xmlStrcmp(cur->name, (xmlChar*) "userClassPath") == 0)
296 sNil = xmlGetNsProp(
297 cur, (xmlChar*) "nil", (xmlChar*) NS_SCHEMA_INSTANCE);
298 if (sNil == NULL)
299 throw FrameworkException(JFW_E_ERROR, sExcMsg);
300 if (xmlStrcmp(sNil, (xmlChar*) "false") == 0)
302 CXmlCharPtr sUser(xmlNodeListGetString(
303 docUser, cur->children, 1));
304 m_userClassPath = boost::optional<rtl::OUString>(rtl::OUString(sUser));
307 else if (xmlStrcmp(cur->name, (xmlChar*) "javaInfo") == 0)
309 sNil = xmlGetNsProp(
310 cur, (xmlChar*) "nil", (xmlChar*) NS_SCHEMA_INSTANCE);
311 if (sNil == NULL)
312 throw FrameworkException(JFW_E_ERROR, sExcMsg);
314 if (xmlStrcmp(sNil, (xmlChar*) "false") == 0)
316 if (! m_javaInfo)
317 m_javaInfo = boost::optional<CNodeJavaInfo>(CNodeJavaInfo());
318 m_javaInfo->loadFromNode(docUser, cur);
321 else if (xmlStrcmp(cur->name, (xmlChar*) "vmParameters") == 0)
323 sNil = xmlGetNsProp(
324 cur, (xmlChar*) "nil", (xmlChar*) NS_SCHEMA_INSTANCE);
325 if (sNil == NULL)
326 throw FrameworkException(JFW_E_ERROR, sExcMsg);
327 if (xmlStrcmp(sNil, (xmlChar*) "false") == 0)
329 if ( ! m_vmParameters)
330 m_vmParameters = boost::optional<std::vector<rtl::OUString> >(
331 std::vector<rtl::OUString> ());
333 xmlNode * pOpt = cur->children;
334 while (pOpt != NULL)
336 if (xmlStrcmp(pOpt->name, (xmlChar*) "param") == 0)
338 CXmlCharPtr sOpt;
339 sOpt = xmlNodeListGetString(
340 docUser, pOpt->children, 1);
341 m_vmParameters->push_back(sOpt);
343 pOpt = pOpt->next;
347 else if (xmlStrcmp(cur->name, (xmlChar*) "jreLocations") == 0)
349 sNil = xmlGetNsProp(
350 cur, (xmlChar*) "nil", (xmlChar*) NS_SCHEMA_INSTANCE);
351 if (sNil == NULL)
352 throw FrameworkException(JFW_E_ERROR, sExcMsg);
353 if (xmlStrcmp(sNil, (xmlChar*) "false") == 0)
355 if (! m_JRELocations)
356 m_JRELocations = boost::optional<std::vector<rtl::OUString> >(
357 std::vector<rtl::OUString>());
359 xmlNode * pLoc = cur->children;
360 while (pLoc != NULL)
362 if (xmlStrcmp(pLoc->name, (xmlChar*) "location") == 0)
364 CXmlCharPtr sLoc;
365 sLoc = xmlNodeListGetString(
366 docUser, pLoc->children, 1);
367 m_JRELocations->push_back(sLoc);
369 pLoc = pLoc->next;
373 cur = cur->next;
377 ::rtl::OString NodeJava::getSettingsPath() const
379 ::rtl::OString ret;
380 switch (m_layer)
382 case USER: ret = getUserSettingsPath(); break;
383 case INSTALL: ret = getInstallSettingsPath(); break;
384 case SHARED: ret = getSharedSettingsPath(); break;
385 default:
386 OSL_ASSERT("[Java framework] NodeJava::getSettingsPath()");
388 return ret;
391 ::rtl::OUString NodeJava::getSettingsURL() const
393 ::rtl::OUString ret;
394 switch (m_layer)
396 case USER: ret = BootParams::getUserData(); break;
397 case INSTALL: ret = BootParams::getInstallData(); break;
398 case SHARED: ret = BootParams::getSharedData(); break;
399 default:
400 OSL_ASSERT("[Java framework] NodeJava::getSettingsURL()");
402 return ret;
405 void NodeJava::prepareSettingsDocument() const
407 rtl::OString sExcMsg(
408 "[Java framework] Error in function prepareSettingsDocument"
409 " (elements.cxx).");
410 createSettingsDocument();
411 rtl::OString sSettings = getSettingsPath();
412 CXmlDocPtr doc(xmlParseFile(sSettings.getStr()));
413 if (!doc)
414 throw FrameworkException(JFW_E_ERROR, sExcMsg);
416 bool bNeedsSave = false;
417 createSettingsStructure(doc, & bNeedsSave);
418 if (bNeedsSave)
420 if (xmlSaveFormatFileEnc(
421 sSettings.getStr(), doc,"UTF-8", 1) == -1)
422 throw FrameworkException(JFW_E_ERROR, sExcMsg);
426 void NodeJava::write() const
428 rtl::OString sExcMsg("[Java framework] Error in function NodeJava::writeSettings "
429 "(elements.cxx).");
430 CXmlDocPtr docUser;
431 CXPathContextPtr contextUser;
432 CXPathObjectPtr pathObj;
434 prepareSettingsDocument();
436 //Read the user elements
437 rtl::OString sSettingsPath = getSettingsPath();
438 docUser = xmlParseFile(sSettingsPath.getStr());
439 if (docUser == NULL)
440 throw FrameworkException(JFW_E_ERROR, sExcMsg);
441 contextUser = xmlXPathNewContext(docUser);
442 if (xmlXPathRegisterNs(contextUser, (xmlChar*) "jf",
443 (xmlChar*) NS_JAVA_FRAMEWORK) == -1)
444 throw FrameworkException(JFW_E_ERROR, sExcMsg);
446 xmlNode * root = xmlDocGetRootElement(docUser);
447 //Get xsi:nil namespace
448 xmlNs* nsXsi = xmlSearchNsByHref(docUser,
449 root,
450 (xmlChar*) NS_SCHEMA_INSTANCE);
452 //set the <enabled> element
453 //The element must exist
454 if (m_enabled)
456 rtl::OString sExpression= rtl::OString(
457 "/jf:java/jf:enabled");
458 pathObj = xmlXPathEvalExpression((xmlChar*) sExpression.getStr(),
459 contextUser);
460 if ( ! pathObj || xmlXPathNodeSetIsEmpty(pathObj->nodesetval))
461 throw FrameworkException(JFW_E_ERROR, sExcMsg);
463 xmlNode * nodeEnabled = pathObj->nodesetval->nodeTab[0];
464 xmlSetNsProp(nodeEnabled,
465 nsXsi,
466 (xmlChar*) "nil",
467 (xmlChar*) "false");
469 if (m_enabled == boost::optional<sal_Bool>(sal_True))
470 xmlNodeSetContent(nodeEnabled,(xmlChar*) "true");
471 else
472 xmlNodeSetContent(nodeEnabled,(xmlChar*) "false");
475 //set the <userClassPath> element
476 //The element must exist
477 if (m_userClassPath)
479 rtl::OString sExpression= rtl::OString(
480 "/jf:java/jf:userClassPath");
481 pathObj = xmlXPathEvalExpression((xmlChar*) sExpression.getStr(),
482 contextUser);
483 if ( ! pathObj || xmlXPathNodeSetIsEmpty(pathObj->nodesetval))
484 throw FrameworkException(JFW_E_ERROR, sExcMsg);
486 xmlNode * nodeEnabled = pathObj->nodesetval->nodeTab[0];
487 xmlSetNsProp(nodeEnabled, nsXsi, (xmlChar*) "nil",(xmlChar*) "false");
488 xmlNodeSetContent(nodeEnabled,(xmlChar*) CXmlCharPtr(*m_userClassPath));
491 //set <javaInfo> element
492 if (m_javaInfo)
494 rtl::OString sExpression= rtl::OString(
495 "/jf:java/jf:javaInfo");
496 pathObj = xmlXPathEvalExpression((xmlChar*) sExpression.getStr(),
497 contextUser);
498 if ( ! pathObj || xmlXPathNodeSetIsEmpty(pathObj->nodesetval))
499 throw FrameworkException(JFW_E_ERROR, sExcMsg);
500 m_javaInfo->writeToNode(
501 docUser, pathObj->nodesetval->nodeTab[0]);
504 //set <vmParameters> element
505 if (m_vmParameters)
507 rtl::OString sExpression= rtl::OString(
508 "/jf:java/jf:vmParameters");
509 pathObj = xmlXPathEvalExpression((xmlChar*) sExpression.getStr(),
510 contextUser);
511 if ( ! pathObj || xmlXPathNodeSetIsEmpty(pathObj->nodesetval))
512 throw FrameworkException(JFW_E_ERROR, sExcMsg);
513 xmlNode* vmParameters = pathObj->nodesetval->nodeTab[0];
514 //set xsi:nil = false;
515 xmlSetNsProp(vmParameters, nsXsi,(xmlChar*) "nil",
516 (xmlChar*) "false");
518 //remove option elements
519 xmlNode* cur = vmParameters->children;
520 while (cur != NULL)
522 xmlNode* lastNode = cur;
523 cur = cur->next;
524 xmlUnlinkNode(lastNode);
525 xmlFreeNode(lastNode);
527 //add a new line after <vmParameters>
528 if (m_vmParameters->size() > 0)
530 xmlNode * nodeCrLf = xmlNewText((xmlChar*) "\n");
531 xmlAddChild(vmParameters, nodeCrLf);
534 typedef std::vector<rtl::OUString>::const_iterator cit;
535 for (cit i = m_vmParameters->begin(); i != m_vmParameters->end(); i++)
537 xmlNewTextChild(vmParameters, NULL, (xmlChar*) "param",
538 CXmlCharPtr(*i));
539 //add a new line
540 xmlNode * nodeCrLf = xmlNewText((xmlChar*) "\n");
541 xmlAddChild(vmParameters, nodeCrLf);
545 //set <jreLocations> element
546 if (m_JRELocations)
548 rtl::OString sExpression= rtl::OString(
549 "/jf:java/jf:jreLocations");
550 pathObj = xmlXPathEvalExpression((xmlChar*) sExpression.getStr(),
551 contextUser);
552 if ( ! pathObj || xmlXPathNodeSetIsEmpty(pathObj->nodesetval))
553 throw FrameworkException(JFW_E_ERROR, sExcMsg);
554 xmlNode* jreLocationsNode = pathObj->nodesetval->nodeTab[0];
555 //set xsi:nil = false;
556 xmlSetNsProp(jreLocationsNode, nsXsi,(xmlChar*) "nil",
557 (xmlChar*) "false");
559 //remove option elements
560 xmlNode* cur = jreLocationsNode->children;
561 while (cur != NULL)
563 xmlNode* lastNode = cur;
564 cur = cur->next;
565 xmlUnlinkNode(lastNode);
566 xmlFreeNode(lastNode);
568 //add a new line after <vmParameters>
569 if (m_JRELocations->size() > 0)
571 xmlNode * nodeCrLf = xmlNewText((xmlChar*) "\n");
572 xmlAddChild(jreLocationsNode, nodeCrLf);
575 typedef std::vector<rtl::OUString>::const_iterator cit;
576 for (cit i = m_JRELocations->begin(); i != m_JRELocations->end(); i++)
578 xmlNewTextChild(jreLocationsNode, NULL, (xmlChar*) "location",
579 CXmlCharPtr(*i));
580 //add a new line
581 xmlNode * nodeCrLf = xmlNewText((xmlChar*) "\n");
582 xmlAddChild(jreLocationsNode, nodeCrLf);
585 if (xmlSaveFormatFile(sSettingsPath.getStr(), docUser, 1) == -1)
586 throw FrameworkException(JFW_E_ERROR, sExcMsg);
589 void NodeJava::setEnabled(sal_Bool bEnabled)
591 m_enabled = boost::optional<sal_Bool>(bEnabled);
595 void NodeJava::setUserClassPath(const rtl::OUString & sClassPath)
597 m_userClassPath = boost::optional<rtl::OUString>(sClassPath);
600 void NodeJava::setJavaInfo(const JavaInfo * pInfo, bool bAutoSelect)
602 if (!m_javaInfo)
603 m_javaInfo = boost::optional<CNodeJavaInfo>(CNodeJavaInfo());
604 m_javaInfo->bAutoSelect = bAutoSelect;
605 m_javaInfo->bNil = false;
607 if (pInfo != NULL)
609 m_javaInfo->m_bEmptyNode = false;
610 m_javaInfo->sVendor = pInfo->sVendor;
611 m_javaInfo->sLocation = pInfo->sLocation;
612 m_javaInfo->sVersion = pInfo->sVersion;
613 m_javaInfo->nFeatures = pInfo->nFeatures;
614 m_javaInfo->nRequirements = pInfo->nRequirements;
615 m_javaInfo->arVendorData = pInfo->arVendorData;
617 else
619 m_javaInfo->m_bEmptyNode = true;
620 rtl::OUString sEmpty;
621 m_javaInfo->sVendor = sEmpty;
622 m_javaInfo->sLocation = sEmpty;
623 m_javaInfo->sVersion = sEmpty;
624 m_javaInfo->nFeatures = 0;
625 m_javaInfo->nRequirements = 0;
626 m_javaInfo->arVendorData = rtl::ByteSequence();
630 void NodeJava::setVmParameters(rtl_uString * * arOptions, sal_Int32 size)
632 OSL_ASSERT( !(arOptions == 0 && size != 0));
633 if ( ! m_vmParameters)
634 m_vmParameters = boost::optional<std::vector<rtl::OUString> >(
635 std::vector<rtl::OUString>());
636 m_vmParameters->clear();
637 if (arOptions != NULL)
639 for (int i = 0; i < size; i++)
641 const rtl::OUString sOption(static_cast<rtl_uString*>(arOptions[i]));
642 m_vmParameters->push_back(sOption);
647 void NodeJava::setJRELocations(rtl_uString * * arLocations, sal_Int32 size)
649 OSL_ASSERT( !(arLocations == 0 && size != 0));
650 if (! m_JRELocations)
651 m_JRELocations = boost::optional<std::vector<rtl::OUString> > (
652 std::vector<rtl::OUString>());
653 m_JRELocations->clear();
654 if (arLocations != NULL)
656 for (int i = 0; i < size; i++)
658 const rtl::OUString & sLocation = static_cast<rtl_uString*>(arLocations[i]);
660 //only add the path if not already present
661 std::vector<rtl::OUString>::const_iterator it =
662 std::find(m_JRELocations->begin(), m_JRELocations->end(),
663 sLocation);
664 if (it == m_JRELocations->end())
665 m_JRELocations->push_back(sLocation);
670 void NodeJava::addJRELocation(rtl_uString * sLocation)
672 OSL_ASSERT( sLocation);
673 if (!m_JRELocations)
674 m_JRELocations = boost::optional<std::vector<rtl::OUString> >(
675 std::vector<rtl::OUString> ());
676 //only add the path if not already present
677 std::vector<rtl::OUString>::const_iterator it =
678 std::find(m_JRELocations->begin(), m_JRELocations->end(),
679 rtl::OUString(sLocation));
680 if (it == m_JRELocations->end())
681 m_JRELocations->push_back(rtl::OUString(sLocation));
684 const boost::optional<sal_Bool> & NodeJava::getEnabled() const
686 return m_enabled;
689 const boost::optional<std::vector<rtl::OUString> >&
690 NodeJava::getJRELocations() const
692 return m_JRELocations;
695 const boost::optional<rtl::OUString> & NodeJava::getUserClassPath() const
697 return m_userClassPath;
700 const boost::optional<std::vector<rtl::OUString> > & NodeJava::getVmParameters() const
702 return m_vmParameters;
705 const boost::optional<CNodeJavaInfo> & NodeJava::getJavaInfo() const
707 return m_javaInfo;
710 jfw::FileStatus NodeJava::checkSettingsFileStatus() const
712 jfw::FileStatus ret = FILE_DOES_NOT_EXIST;
714 const rtl::OUString sURL = getSettingsURL();
715 //check the file time
716 ::osl::DirectoryItem item;
717 File::RC rc = ::osl::DirectoryItem::get(sURL, item);
718 if (File::E_None == rc)
720 ::osl::FileStatus stat(
721 FileStatusMask_Validate
722 | FileStatusMask_CreationTime
723 | FileStatusMask_ModifyTime);
724 File::RC rc_stat = item.getFileStatus(stat);
725 if (File::E_None == rc_stat)
727 //ToDo we remove the file and create it shortly after. This
728 //function may be called multiple times when a java is started.
729 //If the expiretime is too small then we may loop because everytime
730 //the file is deleted and we need to search for a java again.
731 if (INSTALL == m_layer)
733 //file exists. Check if it is too old
734 //Do not use the creation time. On Windows 2003 server I noticed
735 //that after removing the file and shortly later creating it again
736 //did not change the creation time. That is the newly created file
737 //had the creation time of the former file.
738 // ::TimeValue time = stat.getCreationTime();
739 ::TimeValue modTime = stat.getModifyTime();
740 ::TimeValue curTime = {0,0};
741 if (sal_True == ::osl_getSystemTime(& curTime))
743 if ( curTime.Seconds - modTime.Seconds >
744 BootParams::getInstallDataExpiration())
746 #if OSL_DEBUG_LEVEL >=2
747 rtl::OString s = rtl::OUStringToOString(sURL, osl_getThreadTextEncoding());
748 fprintf(stderr, "[Java framework] Deleting settings file at \n%s\n", s.getStr());
749 #endif
750 //delete file
751 // File::RC rc_rem = File::remove(sURL);
752 File f(sURL);
753 if (File::E_None == f.open(OpenFlag_Write | OpenFlag_Read)
754 && File::E_None == f.setPos(0, 0)
755 && File::E_None == f.setSize(0))
756 ret = FILE_DOES_NOT_EXIST;
757 else
758 ret = FILE_INVALID;
760 else
762 ret = FILE_OK;
765 else // osl_getSystemTime
767 ret = FILE_INVALID;
770 else // INSTALL == m_layer
772 ret = FILE_OK;
775 else if (File::E_NOENT == rc_stat)
777 ret = FILE_DOES_NOT_EXIST;
779 else
781 ret = FILE_INVALID;
784 else if(File::E_NOENT == rc)
786 ret = FILE_DOES_NOT_EXIST;
788 else
790 ret = FILE_INVALID;
792 return ret;
795 void NodeJava::createSettingsDocument() const
797 const rtl::OUString sURL = getSettingsURL();
798 //make sure there is a user directory
799 rtl::OString sExcMsg("[Java framework] Error in function createSettingsDocument "
800 "(elements.cxx).");
801 // check if javasettings.xml already exist
802 if (FILE_OK == checkSettingsFileStatus())
803 return;
805 //make sure that the directories are created in case they do not exist
806 FileBase::RC rcFile = Directory::createPath(getDirFromFile(sURL));
807 if (rcFile != FileBase::E_EXIST && rcFile != FileBase::E_None)
808 throw FrameworkException(JFW_E_ERROR, sExcMsg);
810 //javasettings.xml does not exist yet
811 CXmlDocPtr doc(xmlNewDoc((xmlChar *)"1.0"));
812 if (! doc)
813 throw FrameworkException(JFW_E_ERROR, sExcMsg);
814 //Create a comment
815 xmlNewDocComment(
816 doc, (xmlChar *) "This is a generated file. Do not alter this file!");
818 //Create the root element and name spaces
819 xmlNodePtr root = xmlNewDocNode(
820 doc, NULL, (xmlChar *) "java", (xmlChar *) "\n");
822 if (root == NULL)
823 throw FrameworkException(JFW_E_ERROR, sExcMsg);
825 if (xmlNewNs(root, (xmlChar *) NS_JAVA_FRAMEWORK,NULL) == NULL)
826 throw FrameworkException(JFW_E_ERROR, sExcMsg);
827 if (xmlNewNs(root,(xmlChar*) NS_SCHEMA_INSTANCE,(xmlChar*)"xsi") == NULL)
828 throw FrameworkException(JFW_E_ERROR, sExcMsg);
829 xmlDocSetRootElement(doc, root);
831 //Create a comment
832 xmlNodePtr com = xmlNewComment(
833 (xmlChar *) "This is a generated file. Do not alter this file!");
834 if (com == NULL)
835 throw FrameworkException(JFW_E_ERROR, sExcMsg);
837 if (xmlAddPrevSibling(root, com) == NULL)
838 throw FrameworkException(JFW_E_ERROR, sExcMsg);
840 const rtl::OString path = getSettingsPath();
841 if (xmlSaveFormatFileEnc(path.getStr(), doc,"UTF-8", 1) == -1)
842 throw FrameworkException(JFW_E_ERROR, sExcMsg);
845 //=====================================================================
846 CNodeJavaInfo::CNodeJavaInfo() :
847 m_bEmptyNode(false), bNil(true), bAutoSelect(true),
848 nFeatures(0), nRequirements(0)
852 CNodeJavaInfo::~CNodeJavaInfo()
856 void CNodeJavaInfo::loadFromNode(xmlDoc * pDoc, xmlNode * pJavaInfo)
858 rtl::OString sExcMsg("[Java framework] Error in function NodeJavaInfo::loadFromNode "
859 "(elements.cxx).");
861 OSL_ASSERT(pJavaInfo && pDoc);
862 if (pJavaInfo->children == NULL)
863 return;
864 //Get the xsi:nil attribute;
865 CXmlCharPtr sNil;
866 sNil = xmlGetNsProp(
867 pJavaInfo, (xmlChar*) "nil", (xmlChar*) NS_SCHEMA_INSTANCE);
868 if ( ! sNil)
869 throw FrameworkException(JFW_E_ERROR, sExcMsg);
871 if (xmlStrcmp(sNil, (xmlChar*) "true") == 0)
872 bNil = true;
873 else if (xmlStrcmp(sNil, (xmlChar*) "false") == 0)
874 bNil = false;
875 else
876 throw FrameworkException(JFW_E_ERROR, sExcMsg);
877 if (bNil == true)
878 return;
880 //Get javaInfo@manuallySelected attribute
881 CXmlCharPtr sAutoSelect;
882 sAutoSelect = xmlGetProp(
883 pJavaInfo, (xmlChar*) "autoSelect");
884 if ( ! sAutoSelect)
885 throw FrameworkException(JFW_E_ERROR, sExcMsg);
887 if (xmlStrcmp(sAutoSelect, (xmlChar*) "true") == 0)
888 bAutoSelect = true;
889 else if (xmlStrcmp(sAutoSelect, (xmlChar*) "false") == 0)
890 bAutoSelect = false;
891 else
892 throw FrameworkException(JFW_E_ERROR, sExcMsg);
894 xmlNode * cur = pJavaInfo->children;
896 while (cur != NULL)
898 if (xmlStrcmp(cur->name, (xmlChar*) "vendor") == 0)
900 CXmlCharPtr xmlVendor;
901 xmlVendor = xmlNodeListGetString(
902 pDoc, cur->children, 1);
903 if (! xmlVendor)
904 return;
905 sVendor = xmlVendor;
907 else if (xmlStrcmp(cur->name, (xmlChar*) "location") == 0)
909 CXmlCharPtr xmlLocation;
910 xmlLocation = xmlNodeListGetString(
911 pDoc, cur->children, 1);
912 sLocation = xmlLocation;
914 else if (xmlStrcmp(cur->name, (xmlChar*) "version") == 0)
916 CXmlCharPtr xmlVersion;
917 xmlVersion = xmlNodeListGetString(
918 pDoc, cur->children, 1);
919 sVersion = xmlVersion;
921 else if (xmlStrcmp(cur->name, (xmlChar*) "features")== 0)
923 CXmlCharPtr xmlFeatures;
924 xmlFeatures = xmlNodeListGetString(
925 pDoc, cur->children, 1);
926 rtl::OUString sFeatures = xmlFeatures;
927 nFeatures = sFeatures.toInt64(16);
929 else if (xmlStrcmp(cur->name, (xmlChar*) "requirements") == 0)
931 CXmlCharPtr xmlRequire;
932 xmlRequire = xmlNodeListGetString(
933 pDoc, cur->children, 1);
934 rtl::OUString sRequire = xmlRequire;
935 nRequirements = sRequire.toInt64(16);
937 else if (xmlStrcmp(cur->name, (xmlChar*) "vendorData") == 0)
939 CXmlCharPtr xmlData;
940 xmlData = xmlNodeListGetString(
941 pDoc, cur->children, 1);
942 xmlChar* _data = (xmlChar*) xmlData;
943 if (_data)
945 rtl::ByteSequence seq((sal_Int8*) _data, strlen((char*)_data));
946 arVendorData = decodeBase16(seq);
949 cur = cur->next;
952 if (sVendor.getLength() == 0)
953 m_bEmptyNode = true;
954 //Get the javainfo attributes
955 CXmlCharPtr sVendorUpdate;
956 sVendorUpdate = xmlGetProp(pJavaInfo,
957 (xmlChar*) "vendorUpdate");
958 if ( ! sVendorUpdate)
959 throw FrameworkException(JFW_E_ERROR, sExcMsg);
960 sAttrVendorUpdate = sVendorUpdate;
964 void CNodeJavaInfo::writeToNode(xmlDoc* pDoc,
965 xmlNode* pJavaInfoNode) const
968 OSL_ASSERT(pJavaInfoNode && pDoc);
969 rtl::OString sExcMsg("[Java framework] Error in function NodeJavaInfo::writeToNode "
970 "(elements.cxx).");
972 //write the attribute vendorSettings
974 //javaInfo@vendorUpdate
975 //creates the attribute if necessary
976 rtl::OString sUpdated = getElementUpdated();
978 xmlSetProp(pJavaInfoNode, (xmlChar*)"vendorUpdate",
979 (xmlChar*) sUpdated.getStr());
981 //javaInfo@autoSelect
982 xmlSetProp(pJavaInfoNode, (xmlChar*)"autoSelect",
983 (xmlChar*) (bAutoSelect == true ? "true" : "false"));
985 //Set xsi:nil in javaInfo element to false
986 //the xmlNs pointer must not be destroyed
987 xmlNs* nsXsi = xmlSearchNsByHref((xmlDoc*) pDoc,
988 pJavaInfoNode,
989 (xmlChar*) NS_SCHEMA_INSTANCE);
991 xmlSetNsProp(pJavaInfoNode,
992 nsXsi,
993 (xmlChar*) "nil",
994 (xmlChar*) "false");
996 //Delete the children of JavaInfo
997 xmlNode* cur = pJavaInfoNode->children;
998 while (cur != NULL)
1000 xmlNode* lastNode = cur;
1001 cur = cur->next;
1002 xmlUnlinkNode(lastNode);
1003 xmlFreeNode(lastNode);
1006 //If the JavaInfo was set with an empty value,
1007 //then we are done.
1008 if (m_bEmptyNode)
1009 return;
1011 //add a new line after <javaInfo>
1012 xmlNode * nodeCrLf = xmlNewText((xmlChar*) "\n");
1013 xmlAddChild(pJavaInfoNode, nodeCrLf);
1015 //Create the vendor element
1016 xmlNewTextChild(pJavaInfoNode, NULL, (xmlChar*) "vendor",
1017 CXmlCharPtr(sVendor));
1018 //add a new line for better readability
1019 nodeCrLf = xmlNewText((xmlChar*) "\n");
1020 xmlAddChild(pJavaInfoNode, nodeCrLf);
1022 //Create the location element
1023 xmlNewTextChild(pJavaInfoNode, NULL, (xmlChar*) "location",
1024 CXmlCharPtr(sLocation));
1025 //add a new line for better readability
1026 nodeCrLf = xmlNewText((xmlChar*) "\n");
1027 xmlAddChild(pJavaInfoNode, nodeCrLf);
1029 //Create the version element
1030 xmlNewTextChild(pJavaInfoNode, NULL, (xmlChar*) "version",
1031 CXmlCharPtr(sVersion));
1032 //add a new line for better readability
1033 nodeCrLf = xmlNewText((xmlChar*) "\n");
1034 xmlAddChild(pJavaInfoNode, nodeCrLf);
1036 //Create the features element
1037 rtl::OUString sFeatures = rtl::OUString::valueOf(
1038 (sal_Int64)nFeatures, 16);
1039 xmlNewTextChild(pJavaInfoNode, NULL, (xmlChar*) "features",
1040 CXmlCharPtr(sFeatures));
1041 //add a new line for better readability
1042 nodeCrLf = xmlNewText((xmlChar*) "\n");
1043 xmlAddChild(pJavaInfoNode, nodeCrLf);
1046 //Create the requirements element
1047 rtl::OUString sRequirements = rtl::OUString::valueOf(
1048 (sal_Int64) nRequirements, 16);
1049 xmlNewTextChild(pJavaInfoNode, NULL, (xmlChar*) "requirements",
1050 CXmlCharPtr(sRequirements));
1051 //add a new line for better readability
1052 nodeCrLf = xmlNewText((xmlChar*) "\n");
1053 xmlAddChild(pJavaInfoNode, nodeCrLf);
1056 //Create the features element
1057 rtl::ByteSequence data = encodeBase16(arVendorData);
1058 xmlNode* dataNode = xmlNewChild(pJavaInfoNode, NULL,
1059 (xmlChar*) "vendorData",
1060 (xmlChar*) "");
1061 xmlNodeSetContentLen(dataNode,
1062 (xmlChar*) data.getArray(), data.getLength());
1063 //add a new line for better readability
1064 nodeCrLf = xmlNewText((xmlChar*) "\n");
1065 xmlAddChild(pJavaInfoNode, nodeCrLf);
1068 JavaInfo * CNodeJavaInfo::makeJavaInfo() const
1070 if (bNil == true || m_bEmptyNode == true)
1071 return NULL;
1072 JavaInfo * pInfo = (JavaInfo*) rtl_allocateMemory(sizeof(JavaInfo));
1073 if (pInfo == NULL)
1074 return NULL;
1075 memset(pInfo, 0, sizeof(JavaInfo));
1076 pInfo->sVendor = sVendor.pData;
1077 rtl_uString_acquire(pInfo->sVendor);
1078 pInfo->sLocation = sLocation.pData;
1079 rtl_uString_acquire(pInfo->sLocation);
1080 pInfo->sVersion = sVersion.pData;
1081 rtl_uString_acquire(pInfo->sVersion);
1082 pInfo->nFeatures = nFeatures;
1083 pInfo->nRequirements = nRequirements;
1084 pInfo->arVendorData = arVendorData.getHandle();
1085 rtl_byte_sequence_acquire(pInfo->arVendorData);
1086 return pInfo;
1090 //================================================================================
1091 MergedSettings::MergedSettings():
1092 m_bEnabled(sal_False),
1093 m_sClassPath(),
1094 m_vmParams(),
1095 m_JRELocations(),
1096 m_javaInfo()
1098 NodeJava settings(NodeJava::USER_OR_INSTALL);
1099 settings.load();
1101 //Check if UNO_JAVA_JFW_INSTALL_DATA is set. If so, then we need not use user and
1102 //shared data.
1103 const ::rtl::OUString sInstall = BootParams::getInstallData();
1105 if (sInstall.getLength() == 0)
1107 NodeJava sharedSettings(NodeJava::SHARED);
1108 sharedSettings.load();
1109 merge(sharedSettings, settings);
1111 else
1113 merge(NodeJava(), settings);
1117 MergedSettings::~MergedSettings()
1121 void MergedSettings::merge(const NodeJava & share, const NodeJava & user)
1123 if (user.getEnabled())
1124 m_bEnabled = * user.getEnabled();
1125 else if (share.getEnabled())
1126 m_bEnabled = * share.getEnabled();
1127 else
1128 m_bEnabled = sal_True;
1130 if (user.getUserClassPath())
1131 m_sClassPath = * user.getUserClassPath();
1132 else if (share.getUserClassPath())
1133 m_sClassPath = * share.getUserClassPath();
1135 if (user.getJavaInfo())
1136 m_javaInfo = * user.getJavaInfo();
1137 else if (share.getJavaInfo())
1138 m_javaInfo = * share.getJavaInfo();
1140 if (user.getVmParameters())
1141 m_vmParams = * user.getVmParameters();
1142 else if (share.getVmParameters())
1143 m_vmParams = * share.getVmParameters();
1145 if (user.getJRELocations())
1146 m_JRELocations = * user.getJRELocations();
1147 else if (share.getJRELocations())
1148 m_JRELocations = * share.getJRELocations();
1151 sal_Bool MergedSettings::getEnabled() const
1153 return m_bEnabled;
1155 const rtl::OUString& MergedSettings::getUserClassPath() const
1157 return m_sClassPath;
1160 ::std::vector< ::rtl::OString> MergedSettings::getVmParametersUtf8() const
1162 ::std::vector< ::rtl::OString> ret;
1163 typedef ::std::vector< ::rtl::OUString>::const_iterator cit;
1164 for (cit i = m_vmParams.begin(); i < m_vmParams.end(); i++)
1166 ret.push_back( ::rtl::OUStringToOString(*i, RTL_TEXTENCODING_UTF8));
1168 return ret;
1171 const ::rtl::OString & MergedSettings::getJavaInfoAttrVendorUpdate() const
1173 return m_javaInfo.sAttrVendorUpdate;
1177 JavaInfo * MergedSettings::createJavaInfo() const
1179 return m_javaInfo.makeJavaInfo();
1181 #ifdef WNT
1182 bool MergedSettings::getJavaInfoAttrAutoSelect() const
1184 return m_javaInfo.bAutoSelect;
1186 #endif
1187 void MergedSettings::getVmParametersArray(
1188 rtl_uString *** parParams, sal_Int32 * size) const
1190 osl::MutexGuard guard(FwkMutex::get());
1191 OSL_ASSERT(parParams != NULL && size != NULL);
1193 *parParams = (rtl_uString **)
1194 rtl_allocateMemory(sizeof(rtl_uString*) * m_vmParams.size());
1195 if (*parParams == NULL)
1196 return;
1198 int j=0;
1199 typedef std::vector<rtl::OUString>::const_iterator it;
1200 for (it i = m_vmParams.begin(); i != m_vmParams.end();
1201 i++, j++)
1203 (*parParams)[j] = i->pData;
1204 rtl_uString_acquire(i->pData);
1206 *size = m_vmParams.size();
1209 void MergedSettings::getJRELocations(
1210 rtl_uString *** parLocations, sal_Int32 * size) const
1212 osl::MutexGuard guard(FwkMutex::get());
1213 OSL_ASSERT(parLocations != NULL && size != NULL);
1215 *parLocations = (rtl_uString **)
1216 rtl_allocateMemory(sizeof(rtl_uString*) * m_JRELocations.size());
1217 if (*parLocations == NULL)
1218 return;
1220 int j=0;
1221 typedef std::vector<rtl::OUString>::const_iterator it;
1222 for (it i = m_JRELocations.begin(); i != m_JRELocations.end();
1223 i++, j++)
1225 (*parLocations)[j] = i->pData;
1226 rtl_uString_acquire(i->pData);
1228 *size = m_JRELocations.size();
1230 const std::vector<rtl::OUString> & MergedSettings::getJRELocations() const
1232 return m_JRELocations;