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 #ifndef _SAX_SAXPARSER_HXX_ 25 #define _SAX_SAXPARSER_HXX_ 26 27 #include "sax/dllapi.h" 28 #include <com/sun/star/xml/sax/InputSource.hpp> 29 #include <com/sun/star/xml/sax/SAXException.hpp> 30 #include <rtl/ref.hxx> 31 32 #ifndef BOOST_SHARED_PTR_HPP_INCLUDED 33 #include <boost/shared_ptr.hpp> 34 #endif 35 36 #include <map> 37 #include <memory> 38 #include "sax/tools/saxobject.hxx" 39 #include "sax/tools/attributemap.hxx" 40 41 #include <boost/scoped_ptr.hpp> 42 43 namespace sax 44 { 45 46 // -------------------------------------------------------------------- 47 48 class SaxParser; 49 class SaxContext; 50 51 typedef rtl::Reference< SaxParser > SaxParserRef; 52 typedef rtl::Reference< SaxContext > SaxContextRef; 53 54 /** base class for each implementation that handles all sax calls for an element */ 55 class SAX_DLLPUBLIC SaxContext : public SaxObject 56 { 57 public: 58 SaxContext( const SaxParserRef& xParser ); 59 virtual ~SaxContext(); 60 61 /** receives notification of the beginning of an element . 62 */ 63 virtual void StartElement( sal_uInt32 aElementToken, const AttributeMap& rAttributes ); 64 65 /** receives notification of character data. 66 */ 67 virtual void Characters( const sal_Char *pCharacters, sal_uInt32 nLength ); 68 69 /** receives notification of the end of an element. 70 */ 71 virtual void EndElement( sal_uInt32 aElementToken ); 72 73 /** is called by the SaxParser for each child element inside this instances element */ 74 virtual SaxContextRef CreateContext( sal_uInt32 aElementToken, const AttributeMap& rAttributes ); 75 getParser() const76 const SaxParserRef& getParser() const { return mxParser; } 77 private: 78 SaxParserRef mxParser; 79 }; 80 81 // -------------------------------------------------------------------- 82 83 class SaxParserImpl; 84 85 /** base class for loading a single xml stream. Derived classes must call 86 parseStream to start parsing and are notified through virtual methods 87 for sax events. */ 88 class SAX_DLLPUBLIC SaxParser : public SaxObject 89 { 90 public: 91 SaxParser(); 92 virtual ~SaxParser(); 93 94 /** returns the unicode representation of a token from the xml stream 95 that was unknown to the SaxTokenMap from the derived class. */ 96 rtl::OUString GetCustomToken( sal_uInt32 nToken ); 97 98 /** returns the unicode representation of a namespace from the xml stream 99 that was unknown to the SaxTokenMap from the derived class. */ 100 rtl::OUString GetCustomNamespace( sal_uInt32 nToken ); 101 102 /** returns the system id of the currently parsed stream */ 103 const rtl::OUString& GetSystemId() const; 104 105 /** starts parsing of the source xml stream provided in the given sax InputSource */ 106 virtual void parseStream( const ::com::sun::star::xml::sax::InputSource& rInputSource ) throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException); 107 108 /** is called once when parsing of the xml stream starts */ 109 virtual void StartDocument(); 110 111 /** is called once for each element in the xml stream. 112 Default implementation calls StartElement() on the topmost contex. */ 113 virtual void StartElement( sal_uInt32 aElementToken, const AttributeMap& rAttributes ); 114 115 /** is called for characters between elements in the xml stream. 116 Default implementation calls Characters() on the topmost contex. 117 @param pCharacters The characters in utf-8 encoding 118 @param nLength the size in bytes of the utf-8 string 119 */ 120 virtual void Characters( const sal_Char *pCharacters, sal_uInt32 nLength ); 121 122 /** is called once at the end of each element in the xml stream. 123 Default implementation calls EndElement() on the topmost contex. */ 124 virtual void EndElement( sal_uInt32 aElementToken ); 125 126 /** is called once after parsing the xml stream is finished */ 127 virtual void EndDocument(); 128 129 /** is called once for each element in the xml stream and before StartElement() is called. 130 Default implementation calls CreateContext at the topmost context. 131 Returned contexts are pushed on a stack and removed after the corresponding EndElement call. */ 132 virtual SaxContextRef CreateContext( sal_uInt32 aElementToken, const AttributeMap& rAttributes ); 133 134 /** must be implemented from derived classes. The returned SaxTokenMap is used to convert 135 element names and attribute names and values to tokens. */ 136 virtual const SaxTokenMapRef& getTokenMap() = 0; 137 138 private: 139 void AddNamespace( sal_uInt32 nNamespaceId, sal_uInt32 nNamespaceURIToken ); 140 141 boost::scoped_ptr< SaxParserImpl > mpImpl; 142 }; 143 144 } 145 146 #endif // _SAX_SAXPARSER_HXX_ 147