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 
27 #include <xmloff/xmlscripti.hxx>
28 #include "xmloff/xmlnmspe.hxx"
29 #include <xmloff/xmltoken.hxx>
30 #include <xmloff/xmlimp.hxx>
31 #include <xmloff/nmspmap.hxx>
32 #include <xmloff/XMLEventsImportContext.hxx>
33 #include "xmlbasici.hxx"
34 
35 #include <com/sun/star/document/XEventsSupplier.hpp>
36 #include <com/sun/star/document/XEmbeddedScripts.hpp>
37 
38 using ::rtl::OUString;
39 using namespace com::sun::star;
40 using namespace com::sun::star::uno;
41 using namespace com::sun::star::lang;
42 using namespace com::sun::star::frame;
43 using namespace com::sun::star::document;
44 using namespace com::sun::star::xml::sax;
45 using namespace ::xmloff::token;
46 
47 
48 // =============================================================================
49 // XMLScriptChildContext: context for <office:script> element
50 // =============================================================================
51 
52 class XMLScriptChildContext : public SvXMLImportContext
53 {
54 private:
55     ::com::sun::star::uno::Reference< ::com::sun::star::frame::XModel >                 m_xModel;
56     ::com::sun::star::uno::Reference< ::com::sun::star::document::XEmbeddedScripts >    m_xDocumentScripts;
57     ::rtl::OUString m_aLanguage;
58 
59 public:
60     XMLScriptChildContext( SvXMLImport& rImport, sal_uInt16 nPrfx, const ::rtl::OUString& rLName,
61         const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XModel>& rxModel,
62         const ::rtl::OUString& rLanguage );
63     virtual ~XMLScriptChildContext();
64 
65     virtual SvXMLImportContext* CreateChildContext( sal_uInt16 nPrefix, const ::rtl::OUString& rLocalName,
66         const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XAttributeList >& xAttrList );
67 
68 	virtual void EndElement();
69 };
70 
71 // -----------------------------------------------------------------------------
72 
XMLScriptChildContext(SvXMLImport & rImport,sal_uInt16 nPrfx,const::rtl::OUString & rLName,const Reference<frame::XModel> & rxModel,const::rtl::OUString & rLanguage)73 XMLScriptChildContext::XMLScriptChildContext( SvXMLImport& rImport, sal_uInt16 nPrfx, const ::rtl::OUString& rLName,
74         const Reference< frame::XModel >& rxModel, const ::rtl::OUString& rLanguage )
75     :SvXMLImportContext( rImport, nPrfx, rLName )
76     ,m_xModel( rxModel )
77     ,m_xDocumentScripts( rxModel, UNO_QUERY )
78     ,m_aLanguage( rLanguage )
79 {
80 }
81 
82 // -----------------------------------------------------------------------------
83 
~XMLScriptChildContext()84 XMLScriptChildContext::~XMLScriptChildContext()
85 {
86 }
87 
88 // -----------------------------------------------------------------------------
89 
CreateChildContext(sal_uInt16 nPrefix,const::rtl::OUString & rLocalName,const Reference<xml::sax::XAttributeList> & xAttrList)90 SvXMLImportContext* XMLScriptChildContext::CreateChildContext(
91     sal_uInt16 nPrefix, const ::rtl::OUString& rLocalName,
92     const Reference< xml::sax::XAttributeList >& xAttrList )
93 {
94     SvXMLImportContext* pContext = NULL;
95 
96     if ( m_xDocumentScripts.is() )
97     {   // document supports embedding scripts/macros
98         ::rtl::OUString aBasic( GetImport().GetNamespaceMap().GetPrefixByKey( XML_NAMESPACE_OOO ) );
99         aBasic += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ":Basic" ) );
100 
101         if ( m_aLanguage == aBasic && nPrefix == XML_NAMESPACE_OOO && IsXMLToken( rLocalName, XML_LIBRARIES ) )
102             pContext = new XMLBasicImportContext( GetImport(), nPrefix, rLocalName, m_xModel );
103     }
104 
105     if ( !pContext )
106         pContext = SvXMLImportContext::CreateChildContext( nPrefix, rLocalName, xAttrList );
107 
108     return pContext;
109 }
110 
111 // -----------------------------------------------------------------------------
112 
EndElement()113 void XMLScriptChildContext::EndElement()
114 {
115 }
116 
117 // =============================================================================
118 // XMLScriptContext: context for <office:scripts> element
119 // =============================================================================
120 
XMLScriptContext(SvXMLImport & rImport,sal_uInt16 nPrfx,const OUString & rLName,const Reference<XModel> & rDocModel)121 XMLScriptContext::XMLScriptContext( SvXMLImport& rImport, sal_uInt16 nPrfx, const OUString& rLName,
122         const Reference<XModel>& rDocModel )
123     :SvXMLImportContext( rImport, nPrfx, rLName )
124     ,m_xModel( rDocModel )
125 {
126 }
127 
128 // -----------------------------------------------------------------------------
129 
~XMLScriptContext()130 XMLScriptContext::~XMLScriptContext()
131 {
132 }
133 
134 // -----------------------------------------------------------------------------
135 
CreateChildContext(sal_uInt16 nPrefix,const OUString & rLName,const Reference<XAttributeList> & xAttrList)136 SvXMLImportContext* XMLScriptContext::CreateChildContext(
137     sal_uInt16 nPrefix, const OUString& rLName,
138     const Reference<XAttributeList>& xAttrList )
139 {
140 	SvXMLImportContext* pContext = NULL;
141 
142     if ( nPrefix == XML_NAMESPACE_OFFICE )
143     {
144         if ( IsXMLToken( rLName, XML_EVENT_LISTENERS ) )
145         {
146             Reference< XEventsSupplier> xSupplier( GetImport().GetModel(), UNO_QUERY );
147             pContext = new XMLEventsImportContext( GetImport(), nPrefix, rLName, xSupplier );
148         }
149         else if ( IsXMLToken( rLName, XML_SCRIPT ) )
150         {
151             ::rtl::OUString aAttrName( GetImport().GetNamespaceMap().GetPrefixByKey( XML_NAMESPACE_SCRIPT ) );
152             aAttrName += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ":language" ) );
153             if ( xAttrList.is() )
154             {
155                 ::rtl::OUString aLanguage = xAttrList->getValueByName( aAttrName );
156 
157 				if ( m_xModel.is() )
158 				{
159 					uno::Sequence< beans::PropertyValue > aMedDescr = m_xModel->getArgs();
160 					sal_Int32 nNewLen = aMedDescr.getLength() + 1;
161 					aMedDescr.realloc( nNewLen );
162 					aMedDescr[nNewLen-1].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "BreakMacroSignature" ) );
163 					aMedDescr[nNewLen-1].Value <<= (sal_Bool)sal_True;
164 					m_xModel->attachResource( m_xModel->getURL(), aMedDescr );
165 
166                 	pContext = new XMLScriptChildContext( GetImport(), nPrefix, rLName, m_xModel, aLanguage );
167 				}
168             }
169         }
170 	}
171 
172     if ( !pContext )
173         pContext = SvXMLImportContext::CreateChildContext( nPrefix, rLName, xAttrList);
174 
175 	return pContext;
176 }
177 
178 // -----------------------------------------------------------------------------
179 
EndElement()180 void XMLScriptContext::EndElement()
181 {
182 }
183 
184 // -----------------------------------------------------------------------------
185