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 #ifndef _SCRIPTING_STORAGE_SCRIPTMETADATAIMPORTER_HXX_
24 #define _SCRIPTING_STORAGE_SCRIPTMETADATAIMPORTER_HXX_
25 
26 #include <vector>
27 
28 #include <rtl/ustring.h>
29 #include <rtl/ustrbuf.hxx>
30 #include <osl/mutex.hxx>
31 #include <cppuhelper/implbase1.hxx> // helper for component factory
32 
33 #include <com/sun/star/uno/XComponentContext.hpp>
34 #include <com/sun/star/lang/XServiceInfo.hpp>
35 #include <com/sun/star/lang/XInitialization.hpp>
36 #include <com/sun/star/xml/sax/XExtendedDocumentHandler.hpp>
37 #include <com/sun/star/io/XInputStream.hpp>
38 
39 #include "ScriptData.hxx"
40 
41 namespace scripting_impl
42 {
43 // for simplification
44 #define css ::com::sun::star
45 #define dcsssf ::drafts::com::sun::star::script::framework
46 
47 typedef ::std::vector< ScriptData > InfoImpls_vec;
48 typedef ::std::pair< ::rtl::OUString, ::std::pair< ::rtl::OUString,
49     ::rtl::OUString > > strpair_pair;
50 
51 /**
52  * Script Meta Data Importer
53  */
54 class ScriptMetadataImporter : public
55     ::cppu::WeakImplHelper1< css::xml::sax::XExtendedDocumentHandler >
56 {
57 public:
58 
59     /**
60      * This function will begin the parser and parse the meta data
61      *
62      * @param xInput The XInputStream for the parser which contains the XML
63      * @param parcelURI The parcel's URI in the document or the application
64      *
65      * @see css::io::XInputStream
66      */
67     void parseMetaData( css::uno::Reference< css::io::XInputStream >
68         const & xInput, const ::rtl::OUString & parcelURI,
69         InfoImpls_vec & io_ScriptDatas )
70         throw ( css::xml::sax::SAXException, css::io::IOException,
71             css::uno::RuntimeException );
72 
73     /**
74      * Constructor for the meta-data parser
75      *
76      * @param XComponentContext
77      */
78     explicit ScriptMetadataImporter(
79         const css::uno::Reference< css::uno::XComponentContext >&  );
80 
81     /**
82      * Destructor for the parser
83      */
84     virtual ~ScriptMetadataImporter() SAL_THROW( () );
85 
86     // XExtendedDocumentHandler impl
87     /**
88      * Function to handle the start of CDATA in XML
89      *
90      * @see com::sun::star::xml::sax::XExtendedDocumentHandler
91      */
92     virtual void SAL_CALL startCDATA()
93         throw ( css::xml::sax::SAXException, css::uno::RuntimeException );
94 
95     /**
96      * Function to handle the end of CDATA in XML
97      *
98      * @see com::sun::star::xml::sax::XExtendedDocumentHandler
99      */
100     virtual void SAL_CALL endCDATA() throw ( css::uno::RuntimeException );
101 
102     /**
103      * Function to handle comments in XML
104      *
105      * @see com::sun::star::xml::sax::XExtendedDocumentHandler
106      */
107     virtual void SAL_CALL comment( const ::rtl::OUString & sComment )
108         throw ( css::xml::sax::SAXException, css::uno::RuntimeException );
109 
110     /**
111      * Function to handle line breaks in XML
112      *
113      * @see com::sun::star::xml::sax::XExtendedDocumentHandler
114      */
115     virtual void SAL_CALL allowLineBreak()
116         throw ( css::xml::sax::SAXException, css::uno::RuntimeException );
117 
118     /**
119      * Function to handle unknowns in XML
120      *
121      * @see com::sun::star::xml::sax::XExtendedDocumentHandler
122      */
123     virtual void SAL_CALL unknown( const ::rtl::OUString & sString )
124         throw ( css::xml::sax::SAXException, css::uno::RuntimeException );
125 
126     /**
127      * Function to handle the start of XML document
128      *
129      * @see com::sun::star::xml::sax::XExtendedDocumentHandler
130      */
131     // XDocumentHandler impl
132     virtual void SAL_CALL startDocument()
133         throw ( css::xml::sax::SAXException, css::uno::RuntimeException );
134 
135     /**
136      * Function to handle the end of the XML document
137      *
138      * @see com::sun::star::xml::sax::XDocumentHandler
139      */
140     virtual void SAL_CALL endDocument()
141         throw ( css::xml::sax::SAXException, css::uno::RuntimeException );
142 
143     /**
144      * Function to handle the start of an element
145      *
146      * @see com::sun::star::xml::sax::XDocumentHandler
147      */
148     virtual void SAL_CALL startElement( const ::rtl::OUString& aName,
149         const css::uno::Reference< css::xml::sax::XAttributeList > & xAttribs )
150         throw ( css::xml::sax::SAXException,
151             css::uno::RuntimeException );
152 
153     /**
154      * Function to handle the end of an element
155      *
156      * @see com::sun::star::xml::sax::XDocumentHandler
157      */
158     virtual void SAL_CALL endElement( const ::rtl::OUString & aName )
159         throw ( css::xml::sax::SAXException, css::uno::RuntimeException );
160 
161     /**
162      * Function to handle characters in elements
163      *
164      * @see com::sun::star::xml::sax::XDocumentHandler
165      */
166     virtual void SAL_CALL characters( const ::rtl::OUString & aChars )
167         throw ( css::xml::sax::SAXException, css::uno::RuntimeException );
168 
169     /**
170      * Function to handle whitespace
171      *
172      * @see com::sun::star::xml::sax::XDocumentHandler
173      */
174     virtual void SAL_CALL ignorableWhitespace( const ::rtl::OUString & aWhitespaces )
175         throw ( css::xml::sax::SAXException, css::uno::RuntimeException );
176 
177     /**
178      * Function to handle XML processing instructions
179      *
180      * @see com::sun::star::xml::sax::XDocumentHandler
181      */
182     virtual void SAL_CALL processingInstruction(
183         const ::rtl::OUString & aTarget, const ::rtl::OUString & aData )
184         throw ( css::xml::sax::SAXException, css::uno::RuntimeException );
185 
186     /**
187      * Function to set the document locator
188      *
189      * @see com::sun::star::xml::sax::XDocumentHandler
190      */
191     virtual void SAL_CALL setDocumentLocator(
192         const css::uno::Reference< css::xml::sax::XLocator >& xLocator )
193         throw ( css::xml::sax::SAXException, css::uno::RuntimeException );
194 
195 
196 
197 private:
198 
199     /** Vector contains the ScriptData structs */
200     InfoImpls_vec* mpv_ScriptDatas;
201 
202     /** @internal */
203     osl::Mutex     m_mutex;
204 
205     /** @internal */
206     css::uno::Reference< css::uno::XComponentContext >  m_xContext;
207 
208     /** Placeholder for the parcel URI */
209     ::rtl::OUString ms_parcelURI;
210 
211     /** States for state machine during parsing */
212     enum { PARCEL, SCRIPT, LOCALE, DISPLAYNAME, DESCRIPTION, FUNCTIONNAME,
213         LOGICALNAME, LANGUAGEDEPPROPS, LANGDEPPROPS, FILESET, FILESETPROPS,
214         FILES, FILEPROPS } m_state;
215 
216     /** Build up the struct during parsing the meta data */
217     ScriptData m_ScriptData;
218 
219     /** @internal */
220     ::rtl::OUString ms_localeLang;
221     ::rtl::OUString ms_localeDisName;
222     ::rtl::OUStringBuffer *ms_localeDesc;
223 
224     props_vec mv_filesetprops;
225 
226     ::rtl::OUString ms_filename;
227     ::rtl::OUString ms_filesetname;
228 
229     props_vec mv_fileprops;
230 
231     strpairvec_map mm_files;
232 
233     InfoImpls_vec mv_ScriptDatas;
234 
235     /**
236      *   Helper function to set the state
237      *
238      *   @param tagName
239      *           The current tag being processed
240      */
241     void setState(const ::rtl::OUString & tagName);
242 }
243 ; // class ScriptMetadataImporter
244 
245 }
246 
247 #endif
248