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