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_XMLSERVERDATABASE_HXX_INCLUDED
27 #include "xmlServerDatabase.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 
54 namespace dbaxml
55 {
56 	using namespace ::com::sun::star::uno;
57 	using namespace ::com::sun::star::xml::sax;
DBG_NAME(OXMLServerDatabase)58 DBG_NAME(OXMLServerDatabase)
59 
60 OXMLServerDatabase::OXMLServerDatabase( ODBFilter& rImport,
61 				sal_uInt16 nPrfx, const ::rtl::OUString& _sLocalName,
62 				const Reference< XAttributeList > & _xAttrList) :
63 	SvXMLImportContext( rImport, nPrfx, _sLocalName )
64 {
65     DBG_CTOR(OXMLServerDatabase,NULL);
66 
67 	OSL_ENSURE(_xAttrList.is(),"Attribute list is NULL!");
68 	const SvXMLNamespaceMap& rMap = rImport.GetNamespaceMap();
69 	const SvXMLTokenMap& rTokenMap = rImport.GetDataSourceElemTokenMap();
70 
71 	Reference<XPropertySet> xDataSource = rImport.getDataSource();
72 
73 	PropertyValue aProperty;
74 
75 	const sal_Int16 nLength = (xDataSource.is() && _xAttrList.is()) ? _xAttrList->getLength() : 0;
76     ::rtl::OUString sType,sHostName,sPortNumber,sDatabaseName;
77 	for(sal_Int16 i = 0; i < nLength; ++i)
78 	{
79 		::rtl::OUString sLocalName;
80 		const rtl::OUString sAttrName = _xAttrList->getNameByIndex( i );
81 		const sal_uInt16 nPrefix = rMap.GetKeyByAttrName( sAttrName,&sLocalName );
82 		const rtl::OUString sValue = _xAttrList->getValueByIndex( i );
83 
84 		switch( rTokenMap.Get( nPrefix, sLocalName ) )
85 		{
86 			case XML_TOK_DB_TYPE:
87                 sType = sValue;
88 				break;
89 			case XML_TOK_HOSTNAME:
90                 sHostName = sValue;
91 				break;
92             case XML_TOK_PORT:
93                 sPortNumber = sValue;
94 				break;
95 			case XML_TOK_LOCAL_SOCKET:
96 			    aProperty.Name = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("LocalSocket"));
97 			    aProperty.Value <<= sValue;
98 			    rImport.addInfo(aProperty);
99 				break;
100             case XML_TOK_DATABASE_NAME:
101                 sDatabaseName = sValue;
102                 break;
103 		}
104 	}
105     if ( sType.getLength() )
106     {
107         ::rtl::OUStringBuffer sURL;
108         if  (   sType.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( "sdbc:mysql:jdbc" ) )
109             ||  sType.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( "sdbc:mysqlc" ) )
110             ||  sType.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( "sdbc:mysql:mysqlc" ) )
111             )
112         {
113             sURL.append( sType );
114             sURL.append( sal_Unicode( ':' ) );
115             sURL.append(sHostName);
116             if ( sPortNumber.getLength() )
117             {
118                 sURL.appendAscii(":");
119                 sURL.append(sPortNumber);
120             }
121             if ( sDatabaseName.getLength() )
122             {
123                 sURL.appendAscii("/");
124                 sURL.append(sDatabaseName);
125             }
126         }
127         else if ( sType.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( "jdbc:oracle:thin" ) ) )
128         {
129             sURL.appendAscii("jdbc:oracle:thin:@");
130             sURL.append(sHostName);
131             if ( sPortNumber.getLength() )
132             {
133                 sURL.appendAscii(":");
134                 sURL.append(sPortNumber);
135             }
136             if ( sDatabaseName.getLength() )
137             {
138                 sURL.appendAscii(":");
139                 sURL.append(sDatabaseName);
140             }
141         }
142         else if ( sType.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( "sdbc:address:ldap" ) ) )
143         {
144             sURL.appendAscii("sdbc:address:ldap:");
145             sURL.append(sHostName);
146             if ( sPortNumber.getLength() )
147             {
148                 sURL.appendAscii(":");
149                 sURL.append(sPortNumber);
150             }
151         }
152         else
153         {
154             sURL.append(sType);
155             sURL.appendAscii(":");
156             sURL.append(sHostName);
157             if ( sPortNumber.getLength() )
158             {
159                 sURL.appendAscii(":");
160                 sURL.append(sPortNumber);
161             }
162             if ( sDatabaseName.getLength() )
163             {
164                 sURL.appendAscii(":");
165                 sURL.append(sDatabaseName);
166             }
167         }
168         try
169 		{
170 			xDataSource->setPropertyValue(PROPERTY_URL,makeAny(sURL.makeStringAndClear()));
171 		}
172 		catch(Exception)
173 		{
174             DBG_UNHANDLED_EXCEPTION();
175 		}
176     }
177 }
178 // -----------------------------------------------------------------------------
179 
~OXMLServerDatabase()180 OXMLServerDatabase::~OXMLServerDatabase()
181 {
182 
183     DBG_DTOR(OXMLServerDatabase,NULL);
184 }
185 // -----------------------------------------------------------------------------
186 
187 //----------------------------------------------------------------------------
188 } // namespace dbaxml
189 // -----------------------------------------------------------------------------
190