1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: localfilehelper.cxx,v $
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 ************************************************************************/
32 // MARKER(update_precomp.py): autogen include statement, do not remove
33 #include "precompiled_configmgr.hxx"
34 #include "localfilehelper.hxx"
35 #include "filehelper.hxx"
36 #include <rtl/ustrbuf.hxx>
37 #include "tools/getprocessworkingdir.hxx"
44 //------------------------------------------------------------------------------
45 bool isValidFileURL (rtl::OUString
const& _sFileURL
)
47 rtl::OUString sSystemPath
;
48 return _sFileURL
.getLength() && (osl::File::E_None
== osl::File::getSystemPathFromFileURL(_sFileURL
, sSystemPath
));
50 //------------------------------------------------------------------------------
51 void validateFileURL(const rtl::OUString
& _sFileURL
, const uno::Reference
<uno::XInterface
>& pContext
)
52 throw(css::configuration::InvalidBootstrapFileException
)
54 if (!isValidFileURL( _sFileURL
))
56 rtl::OUStringBuffer sMsg
;
57 sMsg
.appendAscii(" Not a Valid File URL: \"");
58 sMsg
.append(_sFileURL
);
59 sMsg
.appendAscii("\"");
60 throw com::sun::star::configuration::InvalidBootstrapFileException(
61 sMsg
.makeStringAndClear(),pContext
, rtl::OUString() ) ;
64 //------------------------------------------------------------------------------
65 void checkFileExists(const rtl::OUString
& _sFileURL
,const uno::Reference
<uno::XInterface
>& pContext
)
66 throw (backend::CannotConnectException
)
68 if (!FileHelper::fileExists(_sFileURL
))
70 rtl::OUStringBuffer sMsg
;
71 sMsg
.appendAscii(" No Such File or Directory: \"");
72 sMsg
.append(_sFileURL
);
73 sMsg
.appendAscii("\"");
75 throw backend::CannotConnectException(sMsg
.makeStringAndClear(), pContext
, uno::Any()) ;
78 //------------------------------------------------------------------------------
79 void checkIfDirectory(const rtl::OUString
& _sFileURL
, const uno::Reference
<uno::XInterface
>& pContext
)
80 throw (backend::BackendSetupException
)
82 if (!FileHelper::dirExists(_sFileURL
))
84 rtl::OUStringBuffer sMsg
;
85 sMsg
.appendAscii(" File:\"");
86 sMsg
.append(_sFileURL
);
87 sMsg
.appendAscii("\" Must be a Directory\"");
89 throw backend::BackendSetupException(sMsg
.makeStringAndClear(),pContext
, uno::Any()) ;
93 // ---------------------------------------------------------------------------------------
94 bool implEnsureAbsoluteURL(rtl::OUString
& _rsURL
) // also strips embedded dots etc.
96 rtl::OUString sBasePath
;
97 OSL_VERIFY(tools::getProcessWorkingDir(&sBasePath
));
99 rtl::OUString sAbsolute
;
100 if ( osl::File::E_None
== osl::File::getAbsoluteFileURL(sBasePath
, _rsURL
, sAbsolute
))
107 OSL_ENSURE(false, "Could not get absolute file URL for valid URL");
111 // ---------------------------------------------------------------------------------------
112 osl::DirectoryItem::RC
implNormalizeURL(rtl::OUString
& _sURL
, osl::DirectoryItem
& aDirItem
)
114 OSL_PRECOND(aDirItem
.is(), "Opened DirItem required");
116 static const sal_uInt32 cFileStatusMask
= FileStatusMask_FileURL
;
118 osl::FileStatus
aFileStatus(cFileStatusMask
);
120 osl::DirectoryItem::RC rc
= aDirItem
.getFileStatus(aFileStatus
);
122 if (rc
== osl::DirectoryItem::E_None
)
124 rtl::OUString aNormalizedURL
= aFileStatus
.getFileURL();
126 if (aNormalizedURL
.getLength() != 0)
127 _sURL
= aNormalizedURL
;
129 rc
= osl::DirectoryItem::E_INVAL
;
134 // ---------------------------------------------------------------------------------------
136 bool normalizeURL(rtl::OUString
& _sURL
, const uno::Reference
<uno::XInterface
>& pContext
, bool bNothrow
)
137 throw (backend::InsufficientAccessRightsException
, backend::BackendAccessException
)
139 if (_sURL
.getLength() == 0)
142 // This method has no right to be so under-performing to
143 // achieve so, so little of any usefulness.
146 osl::DirectoryItem aDirItem
;
148 osl::DirectoryItem::RC rc
= osl::DirectoryItem::get(_sURL
, aDirItem
);
150 if (rc
== osl::DirectoryItem::E_None
)
151 rc
= implNormalizeURL(_sURL
,aDirItem
);
155 case osl::DirectoryItem::E_None
: return true;
157 case osl::DirectoryItem::E_NOENT
: return true;
159 case osl::DirectoryItem::E_ACCES
:
162 rtl::OUStringBuffer msg
;
163 msg
.appendAscii("LocalBackend: Cannot normalize URL \"" );
165 msg
.appendAscii("\" - InsufficientAccess");
166 throw backend::InsufficientAccessRightsException(msg
.makeStringAndClear(),pContext
,uno::Any());
173 rtl::OUStringBuffer msg
;
174 msg
.appendAscii("LocalBackend: Cannot normalize URL \"" );
176 msg
.appendAscii("\" - ").append(FileHelper::createOSLErrorString(rc
));
177 throw backend::BackendAccessException(msg
.makeStringAndClear(),pContext
,uno::Any());
184 // ---------------------------------------------------------------------------------------
185 static const sal_Unicode kComponentSeparator
= '.' ;
186 static const sal_Unicode kPathSeparator
= '/' ;
188 rtl::OUString
componentToPath(const rtl::OUString
& aComponent
)
190 rtl::OUStringBuffer retCode
;
192 retCode
.append(kPathSeparator
) ;
193 retCode
.append(aComponent
.replace(kComponentSeparator
, kPathSeparator
)) ;
194 return retCode
.makeStringAndClear() ;
196 //------------------------------------------------------------------------------
197 rtl::OUString
layeridToPath(const rtl::OUString
& aLayerId
)
199 sal_Int32
const nSplit
= aLayerId
.indexOf(k_cLayerIdSeparator
);
201 return componentToPath(aLayerId
);
203 rtl::OUString
const aComponent
= aLayerId
.copy(0,nSplit
);
204 rtl::OUString
const aSubid
= aLayerId
.copy(nSplit
+1);
206 rtl::OUStringBuffer retCode
;
208 retCode
.append(kPathSeparator
) ;
209 retCode
.append(aComponent
.replace(kComponentSeparator
, kPathSeparator
)) ;
210 retCode
.append(kPathSeparator
) ;
211 retCode
.append(aSubid
) ;
212 return retCode
.makeStringAndClear() ;
214 //------------------------------------------------------------------------------
215 bool checkOptionalArg(rtl::OUString
& aArg
)
217 if (aArg
.getLength() && aArg
[0] == sal_Unicode('?'))
227 //------------------------------------------------------------------------------
228 void fillFromBlankSeparated(const rtl::OUString
& aList
,
229 uno::Sequence
<rtl::OUString
>& aTarget
)
231 std::vector
<rtl::OUString
> tokens
;
232 sal_Int32 nextToken
= 0 ;
235 tokens
.push_back(aList
.getToken(0, ' ', nextToken
)) ;
236 } while (nextToken
>= 0) ;
237 if (tokens
.size() > 0) {
238 aTarget
.realloc(tokens
.size()) ;
239 std::vector
<rtl::OUString
>::const_iterator token
;
242 for (token
= tokens
.begin() ; token
!= tokens
.end() ; ++ token
) {
243 aTarget
[i
++] = *token
;
247 //------------------------------------------------------------------------------
249 } // namespace configmgr