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_xmloff.hxx"
26 #include "xmlbasici.hxx"
27 #include <xmloff/attrlist.hxx>
28 #include <xmloff/nmspmap.hxx>
29 #include <xmloff/xmlimp.hxx>
30 
31 using namespace ::com::sun::star;
32 using namespace ::com::sun::star::uno;
33 
34 
35 // =============================================================================
36 // XMLBasicImportContext
37 // =============================================================================
38 
XMLBasicImportContext(SvXMLImport & rImport,sal_uInt16 nPrfx,const::rtl::OUString & rLName,const Reference<frame::XModel> & rxModel)39 XMLBasicImportContext::XMLBasicImportContext( SvXMLImport& rImport, sal_uInt16 nPrfx, const ::rtl::OUString& rLName,
40         const Reference< frame::XModel >& rxModel )
41     :SvXMLImportContext( rImport, nPrfx, rLName )
42     ,m_xModel( rxModel )
43 {
44     Reference< lang::XMultiServiceFactory > xMSF = GetImport().getServiceFactory();
45     if ( xMSF.is() )
46     {
47         m_xHandler.set( xMSF->createInstance(
48             ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.document.XMLOasisBasicImporter" ) ) ),
49             UNO_QUERY );
50     }
51 
52     if ( m_xHandler.is() )
53     {
54         Reference< document::XImporter > xImporter( m_xHandler, UNO_QUERY );
55         if ( xImporter.is() )
56         {
57             Reference< lang::XComponent > xComp( m_xModel, UNO_QUERY );
58             xImporter->setTargetDocument( xComp );
59         }
60     }
61 }
62 
63 // -----------------------------------------------------------------------------
64 
~XMLBasicImportContext()65 XMLBasicImportContext::~XMLBasicImportContext()
66 {
67 }
68 
69 // -----------------------------------------------------------------------------
70 
CreateChildContext(sal_uInt16 nPrefix,const::rtl::OUString & rLocalName,const Reference<xml::sax::XAttributeList> &)71 SvXMLImportContext* XMLBasicImportContext::CreateChildContext(
72     sal_uInt16 nPrefix, const ::rtl::OUString& rLocalName,
73     const Reference< xml::sax::XAttributeList >& )
74 {
75     SvXMLImportContext* pContext = 0;
76 
77     if ( m_xHandler.is() )
78         pContext = new XMLBasicImportChildContext( GetImport(), nPrefix, rLocalName, m_xHandler );
79 
80     if ( !pContext )
81         pContext = new SvXMLImportContext( GetImport(), nPrefix, rLocalName );
82 
83     return pContext;
84 }
85 
86 // -----------------------------------------------------------------------------
87 
StartElement(const Reference<xml::sax::XAttributeList> & rxAttrList)88 void XMLBasicImportContext::StartElement(
89     const Reference< xml::sax::XAttributeList >& rxAttrList )
90 {
91     if ( m_xHandler.is() )
92     {
93         m_xHandler->startDocument();
94 
95         // copy namespace declarations
96         SvXMLAttributeList* pAttrList = new SvXMLAttributeList( rxAttrList );
97         Reference< xml::sax::XAttributeList > xAttrList( pAttrList );
98         const SvXMLNamespaceMap& rNamespaceMap = GetImport().GetNamespaceMap();
99         sal_uInt16 nPos = rNamespaceMap.GetFirstKey();
100         while ( nPos != USHRT_MAX )
101         {
102             ::rtl::OUString aAttrName( rNamespaceMap.GetAttrNameByKey( nPos ) );
103             if ( xAttrList->getValueByName( aAttrName ).getLength() == 0 )
104                 pAttrList->AddAttribute( aAttrName, rNamespaceMap.GetNameByKey( nPos ) );
105             nPos = rNamespaceMap.GetNextKey( nPos );
106         }
107 
108         m_xHandler->startElement(
109             GetImport().GetNamespaceMap().GetQNameByKey( GetPrefix(), GetLocalName() ),
110             xAttrList );
111     }
112 }
113 
114 // -----------------------------------------------------------------------------
115 
EndElement()116 void XMLBasicImportContext::EndElement()
117 {
118     if ( m_xHandler.is() )
119     {
120         m_xHandler->endElement(
121             GetImport().GetNamespaceMap().GetQNameByKey( GetPrefix(), GetLocalName() ) );
122         m_xHandler->endDocument();
123     }
124 }
125 
126 // -----------------------------------------------------------------------------
127 
Characters(const::rtl::OUString & rChars)128 void XMLBasicImportContext::Characters( const ::rtl::OUString& rChars )
129 {
130     if ( m_xHandler.is() )
131         m_xHandler->characters( rChars );
132 }
133 
134 
135 // =============================================================================
136 // XMLBasicImportChildContext
137 // =============================================================================
138 
XMLBasicImportChildContext(SvXMLImport & rImport,sal_uInt16 nPrfx,const::rtl::OUString & rLName,const Reference<xml::sax::XDocumentHandler> & rxHandler)139 XMLBasicImportChildContext::XMLBasicImportChildContext( SvXMLImport& rImport, sal_uInt16 nPrfx, const ::rtl::OUString& rLName,
140         const Reference< xml::sax::XDocumentHandler >& rxHandler )
141     :SvXMLImportContext( rImport, nPrfx, rLName )
142     ,m_xHandler( rxHandler )
143 {
144 }
145 
146 // -----------------------------------------------------------------------------
147 
~XMLBasicImportChildContext()148 XMLBasicImportChildContext::~XMLBasicImportChildContext()
149 {
150 }
151 
152 // -----------------------------------------------------------------------------
153 
CreateChildContext(sal_uInt16 nPrefix,const::rtl::OUString & rLocalName,const Reference<xml::sax::XAttributeList> &)154 SvXMLImportContext* XMLBasicImportChildContext::CreateChildContext(
155     sal_uInt16 nPrefix, const ::rtl::OUString& rLocalName,
156     const Reference< xml::sax::XAttributeList >& )
157 {
158     return new XMLBasicImportChildContext( GetImport(), nPrefix, rLocalName, m_xHandler );
159 }
160 
161 // -----------------------------------------------------------------------------
162 
StartElement(const Reference<xml::sax::XAttributeList> & xAttrList)163 void XMLBasicImportChildContext::StartElement(
164     const Reference< xml::sax::XAttributeList >& xAttrList )
165 {
166     if ( m_xHandler.is() )
167     {
168         m_xHandler->startElement(
169             GetImport().GetNamespaceMap().GetQNameByKey( GetPrefix(), GetLocalName() ),
170             xAttrList );
171     }
172 }
173 
174 // -----------------------------------------------------------------------------
175 
EndElement()176 void XMLBasicImportChildContext::EndElement()
177 {
178     if ( m_xHandler.is() )
179     {
180         m_xHandler->endElement(
181             GetImport().GetNamespaceMap().GetQNameByKey( GetPrefix(), GetLocalName() ) );
182     }
183 }
184 
185 // -----------------------------------------------------------------------------
186 
Characters(const::rtl::OUString & rChars)187 void XMLBasicImportChildContext::Characters( const ::rtl::OUString& rChars )
188 {
189     if ( m_xHandler.is() )
190         m_xHandler->characters( rChars );
191 }
192 
193 // -----------------------------------------------------------------------------
194