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_XMLCOMPONENT_HXX
27 #include "xmlComponent.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 _COM_SUN_STAR_BEANS_PROPERTYVALUE_HPP_
48 #include <com/sun/star/beans/PropertyValue.hpp>
49 #endif
50 #ifndef _COM_SUN_STAR_CONTAINER_XNAMECONTAINER_HPP_
51 #include <com/sun/star/container/XNameContainer.hpp>
52 #endif
53 #ifndef _TOOLS_DEBUG_HXX
54 #include <tools/debug.hxx>
55 #endif
56 #ifndef TOOLS_DIAGNOSE_EX_H
57 #include <tools/diagnose_ex.h>
58 #endif
59 
60 namespace dbaxml
61 {
62 	using namespace ::com::sun::star::uno;
63 	using namespace ::com::sun::star::beans;
64 	using namespace ::com::sun::star::container;
65 	using namespace ::com::sun::star::xml::sax;
DBG_NAME(OXMLComponent)66 DBG_NAME(OXMLComponent)
67 
68 OXMLComponent::OXMLComponent( ODBFilter& rImport
69 				,sal_uInt16 nPrfx
70 				,const ::rtl::OUString& _sLocalName
71 				,const Reference< XAttributeList > & _xAttrList
72 				,const Reference< XNameAccess >& _xParentContainer
73 				,const ::rtl::OUString& _sComponentServiceName
74 				) :
75 	SvXMLImportContext( rImport, nPrfx, _sLocalName )
76 	,m_bAsTemplate(sal_False)
77 {
78     DBG_CTOR(OXMLComponent,NULL);
79 
80 	OSL_ENSURE(_xAttrList.is(),"Attribute list is NULL!");
81 	const SvXMLNamespaceMap& rMap = rImport.GetNamespaceMap();
82 	const SvXMLTokenMap& rTokenMap = rImport.GetComponentElemTokenMap();
83 
84 	sal_Int16 nLength = (_xAttrList.is()) ? _xAttrList->getLength() : 0;
85 	static const ::rtl::OUString s_sTRUE = ::xmloff::token::GetXMLToken(XML_TRUE);
86 	for(sal_Int16 i = 0; i < nLength; ++i)
87 	{
88 		::rtl::OUString sLocalName;
89 		rtl::OUString sAttrName = _xAttrList->getNameByIndex( i );
90 		sal_uInt16 nPrefix = rMap.GetKeyByAttrName( sAttrName,&sLocalName );
91 		rtl::OUString sValue = _xAttrList->getValueByIndex( i );
92 
93 		switch( rTokenMap.Get( nPrefix, sLocalName ) )
94 		{
95 			case XML_TOK_HREF:
96 				m_sHREF = sValue;
97 				break;
98 			case XML_TOK_COMPONENT_NAME:
99 				m_sName = sValue;
100                 // sanitize the name. Previously, we allowed to create forms/reports/queries which contain
101                 // a / in their name, which nowadays is forbidden. To not lose such objects if they're contained
102                 // in older files, we replace the slash with something less offending.
103                 m_sName = m_sName.replace( '/', '_' );
104 				break;
105 			case XML_TOK_AS_TEMPLATE:
106 				m_bAsTemplate = (sValue == s_sTRUE ? sal_True : sal_False);
107 				break;
108 		}
109 	}
110 	if ( m_sHREF.getLength() && m_sName.getLength() && _xParentContainer.is() )
111 	{
112 		Sequence< Any > aArguments(3);
113 		PropertyValue aValue;
114 		// set as folder
115 		aValue.Name = PROPERTY_NAME;
116 		aValue.Value <<= m_sName;
117 		aArguments[0] <<= aValue;
118 
119 		aValue.Name = PROPERTY_PERSISTENT_NAME;
120 		sal_Int32 nIndex = m_sHREF.lastIndexOf('/')+1;
121 		aValue.Value <<= m_sHREF.getToken(0,'/',nIndex);
122 		aArguments[1] <<= aValue;
123 
124 		aValue.Name = PROPERTY_AS_TEMPLATE;
125 		aValue.Value <<= m_bAsTemplate;
126 		aArguments[2] <<= aValue;
127 
128 		try
129 		{
130 			Reference< XMultiServiceFactory > xORB( _xParentContainer, UNO_QUERY_THROW );
131 			Reference< XInterface > xComponent( xORB->createInstanceWithArguments( _sComponentServiceName, aArguments ) );
132 			Reference< XNameContainer > xNameContainer( _xParentContainer, UNO_QUERY_THROW );
133 			xNameContainer->insertByName( m_sName, makeAny( xComponent ) );
134 		}
135 		catch(Exception&)
136 		{
137             DBG_UNHANDLED_EXCEPTION();
138 		}
139 	}
140 }
141 // -----------------------------------------------------------------------------
142 
~OXMLComponent()143 OXMLComponent::~OXMLComponent()
144 {
145 
146     DBG_DTOR(OXMLComponent,NULL);
147 }
148 // -----------------------------------------------------------------------------
149 //----------------------------------------------------------------------------
150 } // namespace dbaxml
151 // -----------------------------------------------------------------------------
152