1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_xmlscript.hxx"
30 #include "imp_share.hxx"
31 
32 #include <osl/diagnose.h>
33 
34 #include <rtl/ustrbuf.hxx>
35 
36 #include <xmlscript/xml_import.hxx>
37 #include <comphelper/processfactory.hxx>
38 
39 
40 namespace xmlscript
41 {
42 
43 //##################################################################################################
44 
45 //__________________________________________________________________________________________________
46 Reference< xml::input::XElement > ModuleElement::getParent()
47     throw (RuntimeException)
48 {
49     return static_cast< xml::input::XElement * >( _pParent );
50 }
51 //__________________________________________________________________________________________________
52 OUString ModuleElement::getLocalName()
53     throw (RuntimeException)
54 {
55     return _aLocalName;
56 }
57 //__________________________________________________________________________________________________
58 sal_Int32 ModuleElement::getUid()
59     throw (RuntimeException)
60 {
61     return _pImport->XMLNS_SCRIPT_UID;
62 }
63 //__________________________________________________________________________________________________
64 Reference< xml::input::XAttributes > ModuleElement::getAttributes()
65     throw (RuntimeException)
66 {
67     return _xAttributes;
68 }
69 //__________________________________________________________________________________________________
70 void ModuleElement::ignorableWhitespace(
71     OUString const & /*rWhitespaces*/ )
72     throw (xml::sax::SAXException, RuntimeException)
73 {
74     // not used
75 }
76 //__________________________________________________________________________________________________
77 void ModuleElement::characters( OUString const & rChars )
78     throw (xml::sax::SAXException, RuntimeException)
79 {
80     _StrBuffer.append( rChars );
81 }
82 //__________________________________________________________________________________________________
83 void ModuleElement::processingInstruction(
84     OUString const & /*rTarget*/, OUString const & /*rData*/ )
85     throw (xml::sax::SAXException, RuntimeException)
86 {
87 }
88 //__________________________________________________________________________________________________
89 void ModuleElement::endElement()
90     throw (xml::sax::SAXException, RuntimeException)
91 {
92     _pImport->mrModuleDesc.aCode = _StrBuffer.makeStringAndClear();
93 }
94 //__________________________________________________________________________________________________
95 Reference< xml::input::XElement > ModuleElement::startChildElement(
96     sal_Int32 /*nUid*/, OUString const & /*rLocalName*/,
97     Reference< xml::input::XAttributes > const & /*xAttributes*/ )
98     throw (xml::sax::SAXException, RuntimeException)
99 {
100     throw xml::sax::SAXException(
101         OUString( RTL_CONSTASCII_USTRINGPARAM("unexpected element!") ),
102         Reference< XInterface >(), Any() );
103 }
104 
105 //__________________________________________________________________________________________________
106 ModuleElement::ModuleElement(
107     OUString const & rLocalName,
108     Reference< xml::input::XAttributes > const & xAttributes,
109     ModuleElement * pParent, ModuleImport * pImport )
110     SAL_THROW( () )
111     : _pImport( pImport )
112     , _pParent( pParent )
113     , _aLocalName( rLocalName )
114     , _xAttributes( xAttributes )
115 {
116     _pImport->acquire();
117 
118     if (_pParent)
119     {
120         _pParent->acquire();
121     }
122 }
123 //__________________________________________________________________________________________________
124 ModuleElement::~ModuleElement()
125     SAL_THROW( () )
126 {
127     _pImport->release();
128 
129     if (_pParent)
130     {
131         _pParent->release();
132     }
133 
134 #if OSL_DEBUG_LEVEL > 1
135     OString aStr( OUStringToOString( _aLocalName, RTL_TEXTENCODING_ASCII_US ) );
136     OSL_TRACE( "ModuleElement::~ModuleElement(): %s\n", aStr.getStr() );
137 #endif
138 }
139 
140 //##################################################################################################
141 
142 // XRoot
143 
144 //______________________________________________________________________________
145 void ModuleImport::startDocument(
146     Reference< xml::input::XNamespaceMapping > const & xNamespaceMapping )
147     throw (xml::sax::SAXException, RuntimeException)
148 {
149     XMLNS_SCRIPT_UID = xNamespaceMapping->getUidByUri(
150         OUSTR(XMLNS_SCRIPT_URI) );
151     XMLNS_LIBRARY_UID = xNamespaceMapping->getUidByUri(
152         OUSTR(XMLNS_LIBRARY_URI) );
153     XMLNS_XLINK_UID = xNamespaceMapping->getUidByUri(
154         OUSTR(XMLNS_XLINK_URI) );
155 }
156 
157 //__________________________________________________________________________________________________
158 void ModuleImport::endDocument()
159     throw (xml::sax::SAXException, RuntimeException)
160 {
161     // ignored
162 }
163 //__________________________________________________________________________________________________
164 void ModuleImport::processingInstruction(
165     OUString const & /*rTarget*/, OUString const & /*rData*/ )
166     throw (xml::sax::SAXException, RuntimeException)
167 {
168 }
169 //__________________________________________________________________________________________________
170 void ModuleImport::setDocumentLocator(
171     Reference< xml::sax::XLocator > const & /*xLocator*/ )
172     throw (xml::sax::SAXException, RuntimeException)
173 {
174 }
175 //__________________________________________________________________________________________________
176 Reference< xml::input::XElement > ModuleImport::startRootElement(
177     sal_Int32 nUid, OUString const & rLocalName,
178     Reference< xml::input::XAttributes > const & xAttributes )
179     throw (xml::sax::SAXException, RuntimeException)
180 {
181     if (XMLNS_SCRIPT_UID != nUid)
182     {
183         throw xml::sax::SAXException(
184             OUString( RTL_CONSTASCII_USTRINGPARAM("illegal namespace!") ),
185             Reference< XInterface >(), Any() );
186     }
187     // window
188     else if (rLocalName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("module") ))
189     {
190         mrModuleDesc.aName = xAttributes->getValueByUidName(
191             XMLNS_SCRIPT_UID,
192             OUString( RTL_CONSTASCII_USTRINGPARAM("name") ) );
193         mrModuleDesc.aLanguage = xAttributes->getValueByUidName(
194             XMLNS_SCRIPT_UID,
195             OUString( RTL_CONSTASCII_USTRINGPARAM("language") ) );
196         mrModuleDesc.aModuleType = xAttributes->getValueByUidName(
197             XMLNS_SCRIPT_UID,
198             OUString( RTL_CONSTASCII_USTRINGPARAM("moduleType") ) );
199 
200         return new ModuleElement( rLocalName, xAttributes, 0, this );
201     }
202     else
203     {
204         throw xml::sax::SAXException(
205             OUString( RTL_CONSTASCII_USTRINGPARAM(
206                           "illegal root element (expected module) given: ") ) +
207             rLocalName, Reference< XInterface >(), Any() );
208     }
209 }
210 //__________________________________________________________________________________________________
211 ModuleImport::~ModuleImport()
212     SAL_THROW( () )
213 {
214 #if OSL_DEBUG_LEVEL > 1
215     OSL_TRACE( "ModuleImport::~ModuleImport().\n" );
216 #endif
217 }
218 
219 //##################################################################################################
220 
221 Reference< xml::sax::XDocumentHandler >
222 SAL_CALL importScriptModule( ModuleDescriptor& rMod )
223     SAL_THROW( (Exception) )
224 {
225     return ::xmlscript::createDocumentHandler(
226         static_cast< xml::input::XRoot * >( new ModuleImport( rMod ) ) );
227 }
228 
229 }
230 
231