1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_dbaccess.hxx"
26 #ifndef DBA_XMLFILEBASEDDATABASE_HXX_INCLUDED
27 #include "xmlFileBasedDatabase.hxx"
28 #endif
29 #ifndef DBA_XMLFILTER_HXX
30 #include "xmlfilter.hxx"
31 #endif
32 #ifndef _XMLOFF_XMLTOKEN_HXX
33 #include <xmloff/xmltoken.hxx>
34 #endif
35 #ifndef _XMLOFF_XMLNMSPE_HXX
36 #include <xmloff/xmlnmspe.hxx>
37 #endif
38 #ifndef _XMLOFF_NMSPMAP_HXX
39 #include <xmloff/nmspmap.hxx>
40 #endif
41 #ifndef DBA_XMLENUMS_HXX
42 #include "xmlEnums.hxx"
43 #endif
44 #ifndef DBACCESS_SHARED_XMLSTRINGS_HRC
45 #include "xmlstrings.hrc"
46 #endif
47 #ifndef _TOOLS_DEBUG_HXX
48 #include <tools/debug.hxx>
49 #endif
50 #ifndef TOOLS_DIAGNOSE_EX_H
51 #include <tools/diagnose_ex.h>
52 #endif
53 #include <comphelper/sequence.hxx>
54 #include <svl/filenotation.hxx>
55 #include <unotools/pathoptions.hxx>
56 #include "dsntypes.hxx"
57 namespace dbaxml
58 {
59 	using namespace ::com::sun::star::uno;
60 	using namespace ::com::sun::star::xml::sax;
DBG_NAME(OXMLFileBasedDatabase)61 DBG_NAME(OXMLFileBasedDatabase)
62 
63 OXMLFileBasedDatabase::OXMLFileBasedDatabase( ODBFilter& rImport,
64 				sal_uInt16 nPrfx, const ::rtl::OUString& _sLocalName,
65 				const Reference< XAttributeList > & _xAttrList) :
66 	SvXMLImportContext( rImport, nPrfx, _sLocalName )
67 {
68     DBG_CTOR(OXMLFileBasedDatabase,NULL);
69 
70 	OSL_ENSURE(_xAttrList.is(),"Attribute list is NULL!");
71 	const SvXMLNamespaceMap& rMap = rImport.GetNamespaceMap();
72 	const SvXMLTokenMap& rTokenMap = rImport.GetDataSourceElemTokenMap();
73 
74 	Reference<XPropertySet> xDataSource = rImport.getDataSource();
75 
76 	PropertyValue aProperty;
77 
78 	const sal_Int16 nLength = (xDataSource.is() && _xAttrList.is()) ? _xAttrList->getLength() : 0;
79     ::rtl::OUString sLocation,sMediaType,sFileTypeExtension;
80 	for(sal_Int16 i = 0; i < nLength; ++i)
81 	{
82 		::rtl::OUString sLocalName;
83 		const rtl::OUString sAttrName = _xAttrList->getNameByIndex( i );
84 		const sal_uInt16 nPrefix = rMap.GetKeyByAttrName( sAttrName,&sLocalName );
85 		const rtl::OUString sValue = _xAttrList->getValueByIndex( i );
86 
87 		aProperty.Name = ::rtl::OUString();
88 		aProperty.Value = Any();
89 
90 		switch( rTokenMap.Get( nPrefix, sLocalName ) )
91 		{
92 			case XML_TOK_DB_HREF:
93                 {
94                     SvtPathOptions aPathOptions;
95                     rtl::OUString sFileName = aPathOptions.SubstituteVariable(sValue);
96                     if ( sValue == sFileName )
97                     {
98                         const sal_Int32 nFileNameLength = sFileName.getLength();
99                         if ( ( nFileNameLength > 0 ) && ( sFileName.getStr()[ nFileNameLength - 1 ] == '/' ) )
100                             sFileName = sFileName.copy( 0, nFileNameLength - 1 );
101 
102                         sLocation = ::svt::OFileNotation( rImport.GetAbsoluteReference( sFileName ) ).get( ::svt::OFileNotation::N_SYSTEM );
103                     }
104 
105                     if ( sLocation.getLength() == 0 )
106                         sLocation = sValue;
107                 }
108 				break;
109 			case XML_TOK_MEDIA_TYPE:
110                 sMediaType = sValue;
111 				break;
112 			case XML_TOK_EXTENSION:
113 				aProperty.Name = INFO_TEXTFILEEXTENSION;
114                 sFileTypeExtension = sValue;
115 				break;
116 		}
117 		if ( aProperty.Name.getLength() )
118 		{
119 			if ( !aProperty.Value.hasValue() )
120 				aProperty.Value <<= sValue;
121 			rImport.addInfo(aProperty);
122 		}
123 	}
124     if ( sLocation.getLength() && sMediaType.getLength() )
125     {
126         ::dbaccess::ODsnTypeCollection aTypeCollection(rImport.getORB());
127         ::rtl::OUString sURL(aTypeCollection.getDatasourcePrefixFromMediaType(sMediaType,sFileTypeExtension));
128         sURL += sLocation;
129         try
130 		{
131 			xDataSource->setPropertyValue(PROPERTY_URL,makeAny(sURL));
132 		}
133 		catch(Exception)
134 		{
135             DBG_UNHANDLED_EXCEPTION();
136 		}
137     }
138 }
139 // -----------------------------------------------------------------------------
140 
~OXMLFileBasedDatabase()141 OXMLFileBasedDatabase::~OXMLFileBasedDatabase()
142 {
143 
144     DBG_DTOR(OXMLFileBasedDatabase,NULL);
145 }
146 // -----------------------------------------------------------------------------
147 //----------------------------------------------------------------------------
148 } // namespace dbaxml
149 // -----------------------------------------------------------------------------
150