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_dbaxml.hxx"
26 #ifndef DBA_XMLTABLE_HXX
27 #include "xmlTable.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 DBA_XMLSTYLEIMPORT_HXX
45 #include "xmlStyleImport.hxx"
46 #endif
47 #ifndef DBA_XMLHIERARCHYCOLLECTION_HXX
48 #include "xmlHierarchyCollection.hxx"
49 #endif
50 #ifndef DBACCESS_SHARED_XMLSTRINGS_HRC
51 #include "xmlstrings.hrc"
52 #endif
53 #ifndef _UCBHELPER_CONTENT_HXX
54 #include <ucbhelper/content.hxx>
55 #endif
56 #ifndef _COM_SUN_STAR_UCB_XCOMMANDENVIRONMENT_HPP_
57 #include <com/sun/star/ucb/XCommandEnvironment.hpp>
58 #endif
59 #ifndef _COM_SUN_STAR_SDBCX_XCOLUMNSSUPPLIER_HPP_
60 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
61 #endif
62 #ifndef _COMPHELPER_NAMECONTAINER_HXX_
63 #include <comphelper/namecontainer.hxx>
64 #endif
65 #ifndef _TOOLS_DEBUG_HXX
66 #include <tools/debug.hxx>
67 #endif
68 
69 namespace dbaxml
70 {
71 	using namespace ::com::sun::star;
72 	using namespace ::com::sun::star::uno;
73 	using namespace ::com::sun::star::sdbcx;
74 	using namespace ::com::sun::star::xml::sax;
DBG_NAME(OXMLTable)75 DBG_NAME(OXMLTable)
76 
77 OXMLTable::OXMLTable( ODBFilter& _rImport
78 				,sal_uInt16 nPrfx
79 				,const ::rtl::OUString& _sLocalName
80 				,const uno::Reference< XAttributeList > & _xAttrList
81 				,const uno::Reference< ::com::sun::star::container::XNameAccess >& _xParentContainer
82 				,const ::rtl::OUString& _sServiceName
83 				)
84     :SvXMLImportContext( _rImport, nPrfx, _sLocalName )
85 	,m_xParentContainer(_xParentContainer)
86 	,m_sServiceName(_sServiceName)
87 	,m_bApplyFilter(sal_False)
88 	,m_bApplyOrder(sal_False)
89 {
90     DBG_CTOR(OXMLTable,NULL);
91 
92 	OSL_ENSURE(_xAttrList.is(),"Attribute list is NULL!");
93 	const SvXMLNamespaceMap& rMap = GetOwnImport().GetNamespaceMap();
94 	const SvXMLTokenMap& rTokenMap = GetOwnImport().GetQueryElemTokenMap();
95 
96 	sal_Int16 nLength = (_xAttrList.is()) ? _xAttrList->getLength() : 0;
97 	for(sal_Int16 i = 0; i < nLength; ++i)
98 	{
99 		::rtl::OUString sLocalName;
100 		rtl::OUString sAttrName = _xAttrList->getNameByIndex( i );
101 		sal_uInt16 nPrefix = rMap.GetKeyByAttrName( sAttrName,&sLocalName );
102 		rtl::OUString sValue = _xAttrList->getValueByIndex( i );
103 
104 		switch( rTokenMap.Get( nPrefix, sLocalName ) )
105 		{
106 			case XML_TOK_QUERY_NAME:
107 				m_sName = sValue;
108 				break;
109 			case XML_TOK_CATALOG_NAME:
110 				m_sCatalog = sValue;
111 				break;
112 			case XML_TOK_SCHEMA_NAME:
113 				m_sSchema = sValue;
114 				break;
115 			case XML_TOK_STYLE_NAME:
116 				m_sStyleName = sValue;
117 				break;
118 			case XML_TOK_APPLY_FILTER:
119 				m_bApplyFilter = sValue.equalsAscii("true");
120 				break;
121 			case XML_TOK_APPLY_ORDER:
122 				m_bApplyOrder = sValue.equalsAscii("true");
123 				break;
124 		}
125 	}
126 	Sequence< Any > aArguments(2);
127 	PropertyValue aValue;
128 	// set as folder
129 	aValue.Name = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Name"));
130 	aValue.Value <<= m_sName;
131 	aArguments[0] <<= aValue;
132 	//parent
133 	aValue.Name = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Parent"));
134 	aValue.Value <<= m_xParentContainer;
135 	aArguments[1] <<= aValue;
136 	m_xTable.set(GetOwnImport().getORB()->createInstanceWithArguments(m_sServiceName,aArguments),UNO_QUERY);
137 }
138 // -----------------------------------------------------------------------------
139 
~OXMLTable()140 OXMLTable::~OXMLTable()
141 {
142 
143     DBG_DTOR(OXMLTable,NULL);
144 }
145 // -----------------------------------------------------------------------------
CreateChildContext(sal_uInt16 nPrefix,const::rtl::OUString & rLocalName,const uno::Reference<XAttributeList> & xAttrList)146 SvXMLImportContext* OXMLTable::CreateChildContext(
147 		sal_uInt16 nPrefix,
148 		const ::rtl::OUString& rLocalName,
149 		const uno::Reference< XAttributeList > & xAttrList )
150 {
151 	SvXMLImportContext *pContext = 0;
152 	const SvXMLTokenMap&	rTokenMap	= GetOwnImport().GetQueryElemTokenMap();
153 
154 	switch( rTokenMap.Get( nPrefix, rLocalName ) )
155 	{
156 		case XML_TOK_FILTER_STATEMENT:
157 			{
158 				GetOwnImport().GetProgressBarHelper()->Increment( PROGRESS_BAR_STEP );
159 				::rtl::OUString s1,s2,s3;
160 				fillAttributes(nPrefix, rLocalName,xAttrList,m_sFilterStatement,s1,s2,s3);
161 			}
162 			break;
163 		case XML_TOK_ORDER_STATEMENT:
164 			{
165 				GetOwnImport().GetProgressBarHelper()->Increment( PROGRESS_BAR_STEP );
166 				::rtl::OUString s1,s2,s3;
167 				fillAttributes(nPrefix, rLocalName,xAttrList,m_sOrderStatement,s1,s2,s3);
168 			}
169 			break;
170 
171 		case XML_TOK_COLUMNS:
172 			{
173 				GetOwnImport().GetProgressBarHelper()->Increment( PROGRESS_BAR_STEP );
174 				uno::Reference< XColumnsSupplier > xColumnsSup(m_xTable,UNO_QUERY);
175 				uno::Reference< XNameAccess > xColumns;
176 				if ( xColumnsSup.is() )
177 				{
178 					xColumns = xColumnsSup->getColumns();
179 				}
180 				pContext = new OXMLHierarchyCollection( GetOwnImport(), nPrefix, rLocalName ,xColumns,m_xTable);
181 			}
182 			break;
183 	}
184 
185 	if( !pContext )
186 		pContext = new SvXMLImportContext( GetImport(), nPrefix, rLocalName );
187 
188 	return pContext;
189 }
190 // -----------------------------------------------------------------------------
GetOwnImport()191 ODBFilter& OXMLTable::GetOwnImport()
192 {
193 	return static_cast<ODBFilter&>(GetImport());
194 }
195 // -----------------------------------------------------------------------------
setProperties(uno::Reference<XPropertySet> & _xProp)196 void OXMLTable::setProperties(uno::Reference< XPropertySet > & _xProp )
197 {
198 	try
199 	{
200 		if ( _xProp.is() )
201 		{
202 			_xProp->setPropertyValue(PROPERTY_APPLYFILTER,makeAny(m_bApplyFilter));
203 			_xProp->setPropertyValue(PROPERTY_FILTER,makeAny(m_sFilterStatement));
204 
205 			if ( _xProp->getPropertySetInfo()->hasPropertyByName(PROPERTY_APPLYORDER) )
206 				_xProp->setPropertyValue(PROPERTY_APPLYORDER,makeAny(m_bApplyOrder));
207 			_xProp->setPropertyValue(PROPERTY_ORDER,makeAny(m_sOrderStatement));
208 		}
209 	}
210 	catch(Exception&)
211 	{
212 		OSL_ENSURE(0,"OXMLTable::EndElement -> exception catched");
213 	}
214 }
215 // -----------------------------------------------------------------------------
EndElement()216 void OXMLTable::EndElement()
217 {
218 	uno::Reference<XNameContainer> xNameContainer(m_xParentContainer,UNO_QUERY);
219 	if ( xNameContainer.is() )
220 	{
221 		try
222 		{
223 			if ( m_xTable.is() )
224 			{
225 				setProperties(m_xTable);
226 
227 				if ( m_sStyleName.getLength() )
228 				{
229 					const SvXMLStylesContext* pAutoStyles = GetOwnImport().GetAutoStyles();
230 					if ( pAutoStyles )
231 					{
232 						OTableStyleContext* pAutoStyle = PTR_CAST(OTableStyleContext,pAutoStyles->FindStyleChildContext(XML_STYLE_FAMILY_TABLE_TABLE,m_sStyleName));
233 						if ( pAutoStyle )
234 						{
235 							pAutoStyle->FillPropertySet(m_xTable);
236 						}
237 					}
238 				}
239 
240 				xNameContainer->insertByName(m_sName,makeAny(m_xTable));
241 			}
242 		}
243 		catch(Exception&)
244 		{
245 			OSL_ENSURE(0,"OXMLQuery::EndElement -> exception catched");
246 		}
247 	}
248 
249 }
250 // -----------------------------------------------------------------------------
fillAttributes(sal_uInt16,const::rtl::OUString &,const uno::Reference<XAttributeList> & _xAttrList,::rtl::OUString & _rsCommand,::rtl::OUString & _rsTableName,::rtl::OUString & _rsTableSchema,::rtl::OUString & _rsTableCatalog)251 void OXMLTable::fillAttributes(sal_uInt16 /*nPrfx*/
252 								,const ::rtl::OUString& /*_sLocalName*/
253 								,const uno::Reference< XAttributeList > & _xAttrList
254 								, ::rtl::OUString& _rsCommand
255 								,::rtl::OUString& _rsTableName
256 								,::rtl::OUString& _rsTableSchema
257 								,::rtl::OUString& _rsTableCatalog
258 								)
259 {
260 	OSL_ENSURE(_xAttrList.is(),"Attribute list is NULL!");
261 	const SvXMLNamespaceMap& rMap = GetOwnImport().GetNamespaceMap();
262 	const SvXMLTokenMap& rTokenMap = GetOwnImport().GetQueryElemTokenMap();
263 
264 	sal_Int16 nLength = (_xAttrList.is()) ? _xAttrList->getLength() : 0;
265 	for(sal_Int16 i = 0; i < nLength; ++i)
266 	{
267 		::rtl::OUString sLocalName;
268 		rtl::OUString sAttrName = _xAttrList->getNameByIndex( i );
269 		sal_uInt16 nPrefix = rMap.GetKeyByAttrName( sAttrName,&sLocalName );
270 		rtl::OUString sValue = _xAttrList->getValueByIndex( i );
271 
272 		switch( rTokenMap.Get( nPrefix, sLocalName ) )
273 		{
274 			case XML_TOK_COMMAND:
275 				_rsCommand = sValue;
276 				break;
277 			case XML_TOK_CATALOG_NAME:
278 				_rsTableCatalog = sValue;
279 				break;
280 			case XML_TOK_SCHEMA_NAME:
281 				_rsTableSchema = sValue;
282 				break;
283 			case XML_TOK_QUERY_NAME:
284 				_rsTableName = sValue;
285 				break;
286 		}
287 	}
288 }
289 //----------------------------------------------------------------------------
290 } // namespace dbaxml
291 // -----------------------------------------------------------------------------
292