Line data Source code
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 : #include "xmlFileBasedDatabase.hxx"
21 : #include "xmlfilter.hxx"
22 : #include <xmloff/xmltoken.hxx>
23 : #include <xmloff/xmlnmspe.hxx>
24 : #include <xmloff/nmspmap.hxx>
25 : #include "xmlEnums.hxx"
26 : #include "xmlstrings.hrc"
27 : #include <tools/debug.hxx>
28 : #include <tools/diagnose_ex.h>
29 : #include <comphelper/sequence.hxx>
30 : #include <svl/filenotation.hxx>
31 : #include <unotools/pathoptions.hxx>
32 : #include "dsntypes.hxx"
33 : namespace dbaxml
34 : {
35 : using namespace ::com::sun::star::uno;
36 : using namespace ::com::sun::star::xml::sax;
37 : DBG_NAME(OXMLFileBasedDatabase)
38 :
39 0 : OXMLFileBasedDatabase::OXMLFileBasedDatabase( ODBFilter& rImport,
40 : sal_uInt16 nPrfx, const ::rtl::OUString& _sLocalName,
41 : const Reference< XAttributeList > & _xAttrList) :
42 0 : SvXMLImportContext( rImport, nPrfx, _sLocalName )
43 : {
44 : DBG_CTOR(OXMLFileBasedDatabase,NULL);
45 :
46 : OSL_ENSURE(_xAttrList.is(),"Attribute list is NULL!");
47 0 : const SvXMLNamespaceMap& rMap = rImport.GetNamespaceMap();
48 0 : const SvXMLTokenMap& rTokenMap = rImport.GetDataSourceElemTokenMap();
49 :
50 0 : Reference<XPropertySet> xDataSource = rImport.getDataSource();
51 :
52 0 : PropertyValue aProperty;
53 :
54 0 : const sal_Int16 nLength = (xDataSource.is() && _xAttrList.is()) ? _xAttrList->getLength() : 0;
55 0 : ::rtl::OUString sLocation,sMediaType,sFileTypeExtension;
56 0 : for(sal_Int16 i = 0; i < nLength; ++i)
57 : {
58 0 : ::rtl::OUString sLocalName;
59 0 : const rtl::OUString sAttrName = _xAttrList->getNameByIndex( i );
60 0 : const sal_uInt16 nPrefix = rMap.GetKeyByAttrName( sAttrName,&sLocalName );
61 0 : const rtl::OUString sValue = _xAttrList->getValueByIndex( i );
62 :
63 0 : aProperty.Name = ::rtl::OUString();
64 0 : aProperty.Value = Any();
65 :
66 0 : switch( rTokenMap.Get( nPrefix, sLocalName ) )
67 : {
68 : case XML_TOK_DB_HREF:
69 : {
70 0 : SvtPathOptions aPathOptions;
71 0 : rtl::OUString sFileName = aPathOptions.SubstituteVariable(sValue);
72 0 : if ( sValue == sFileName )
73 : {
74 0 : const sal_Int32 nFileNameLength = sFileName.getLength();
75 0 : if ( ( nFileNameLength > 0 ) && ( sFileName.getStr()[ nFileNameLength - 1 ] == '/' ) )
76 0 : sFileName = sFileName.copy( 0, nFileNameLength - 1 );
77 :
78 0 : sLocation = ::svt::OFileNotation( rImport.GetAbsoluteReference( sFileName ) ).get( ::svt::OFileNotation::N_SYSTEM );
79 : }
80 :
81 0 : if ( sLocation.isEmpty() )
82 0 : sLocation = sValue;
83 : }
84 0 : break;
85 : case XML_TOK_MEDIA_TYPE:
86 0 : sMediaType = sValue;
87 0 : break;
88 : case XML_TOK_EXTENSION:
89 0 : aProperty.Name = INFO_TEXTFILEEXTENSION;
90 0 : sFileTypeExtension = sValue;
91 0 : break;
92 : }
93 0 : if ( !aProperty.Name.isEmpty() )
94 : {
95 0 : if ( !aProperty.Value.hasValue() )
96 0 : aProperty.Value <<= sValue;
97 0 : rImport.addInfo(aProperty);
98 : }
99 0 : }
100 0 : if ( !(sLocation.isEmpty() || sMediaType.isEmpty()) )
101 : {
102 0 : ::dbaccess::ODsnTypeCollection aTypeCollection(rImport.getORB());
103 0 : ::rtl::OUString sURL(aTypeCollection.getDatasourcePrefixFromMediaType(sMediaType,sFileTypeExtension));
104 0 : sURL += sLocation;
105 : try
106 : {
107 0 : xDataSource->setPropertyValue(PROPERTY_URL,makeAny(sURL));
108 : }
109 0 : catch(const Exception&)
110 : {
111 : DBG_UNHANDLED_EXCEPTION();
112 0 : }
113 0 : }
114 0 : }
115 : // -----------------------------------------------------------------------------
116 :
117 0 : OXMLFileBasedDatabase::~OXMLFileBasedDatabase()
118 : {
119 :
120 : DBG_DTOR(OXMLFileBasedDatabase,NULL);
121 0 : }
122 : // -----------------------------------------------------------------------------
123 : //----------------------------------------------------------------------------
124 : } // namespace dbaxml
125 : // -----------------------------------------------------------------------------
126 :
127 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|