xref: /aoo4110/main/svtools/inc/svtools/svparser.hxx (revision b1cdbd2c)
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 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil -*- */
25 
26 #ifndef _SVPARSER_HXX
27 #define _SVPARSER_HXX
28 
29 #include "svtools/svtdllapi.h"
30 #include <tools/string.hxx>
31 #include <tools/ref.hxx>
32 #include <rtl/textenc.h>
33 #include <boost/utility.hpp>
34 
35 
36 struct SvParser_Impl;
37 class SvStream;
38 class SvUShorts;
39 
40 enum SvParserState
41 {
42 	SVPAR_ACCEPTED = 0,
43 	SVPAR_NOTSTARTED,
44 	SVPAR_WORKING,
45 	SVPAR_PENDING,
46 	SVPAR_WAITFORDATA,
47 	SVPAR_ERROR
48 };
49 
50 class SVT_DLLPUBLIC SvParser : public SvRefBase
51 {
52 	DECL_STATIC_LINK( SvParser, NewDataRead, void* );
53 
54 protected:
55 	SvStream&		rInput;
56 	String 	  		aToken;				// gescanntes Token
57 	sal_uLong	  		nlLineNr;			// akt. Zeilen Nummer
58 	sal_uLong	  		nlLinePos;			// akt. Spalten Nummer
59 
60 	SvParser_Impl	*pImplData;			// interne Daten
61 	long           	nTokenValue;		// zusaetzlicher Wert (RTF)
62 	sal_Bool            bTokenHasValue;     // indicates whether nTokenValue is valid
63 	SvParserState	eState;				// Status auch in abgl. Klassen
64 
65 	rtl_TextEncoding	eSrcEnc;		// Source encoding
66 
67     sal_uLong nNextChPos;
68 	sal_Unicode	nNextCh;    		    // Akt. Zeichen fuer die "lex"
69 
70 
71 	sal_Bool			bDownloadingFile : 1;// sal_True: Es wird gerade ein externes
72 										//		 File geladen. d.h. alle
73 										// 		 DataAvailable Links muessen
74 										//		 ignoriert werden.
75 										// Wenn keibes der folgenden
76 										// Flags gesetzt ist, wird der
77 										// Stream als ANSI gelesen,
78 										// aber als CharSet DONTKNOW
79 										// zurueckgegeben.
80 	sal_Bool			bUCS2BSrcEnc : 1;	// oder als big-endian UCS2
81 	sal_Bool			bSwitchToUCS2 : 1;	// Umschalten des ist erlaubt
82 
83 	sal_Bool			bRTF_InTextRead : 1;  // only for RTF-Parser!!!
84 
85 	struct TokenStackType
86 	{
87 		String	sToken;
88 		long 	nTokenValue;
89 		sal_Bool	bTokenHasValue;
90 		int 	nTokenId;
91 
TokenStackTypeSvParser::TokenStackType92 		inline TokenStackType() { nTokenId = 0; }
~TokenStackTypeSvParser::TokenStackType93 		inline ~TokenStackType() { }
94 	};
95 
96 	// Methoden fuer Token-Stack
97 	int SkipToken( short nCnt = -1 );		// n Tokens zurueck "skippen"
98 	TokenStackType* GetStackPtr( short nCnt );
99 	inline sal_uInt8 GetStackPos() const;
100 
101 	// scanne das naechste Token:
102 	//	Tokenstack abarbeiten und ggfs. _GetNextToken() rufen. Diese
103 	//	ist fuers erkennen von neuen Token verantwortlich
104 	int GetNextToken();
105 	virtual int _GetNextToken() = 0;
106 
107 	// wird fuer jedes Token gerufen, das in CallParser erkannt wird
108 	virtual void NextToken( int nToken );
109 
110 	// zu Zeiten der SvRefBase-Ableitung darf nicht jeder loeschen
111 	virtual	~SvParser();
112 
113 	void ClearTxtConvContext();
114 
115 private:
116 	TokenStackType* pTokenStack;
117 	TokenStackType *pTokenStackPos;
118 	sal_uInt8 nTokenStackSize, nTokenStackPos;
119 
120 public:
121 	// Konstruktor
122 	SvParser( SvStream& rIn, sal_uInt8 nStackSize = 3 );
123 
124 	virtual	 SvParserState CallParser() = 0;	// Aufruf des Parsers
125 
GetStatus() const126 	inline SvParserState GetStatus() const	{ return eState; }	// StatusInfo
127 
GetLineNr() const128 	inline sal_uLong	GetLineNr() const		{ return nlLineNr; }
GetLinePos() const129 	inline sal_uLong	GetLinePos() const		{ return nlLinePos; }
IncLineNr()130 	inline sal_uLong	IncLineNr() 			{ return ++nlLineNr; }
IncLinePos()131 	inline sal_uLong	IncLinePos()			{ return ++nlLinePos; }
132 	inline sal_uLong	SetLineNr( sal_uLong nlNum );			// inline unten
133 	inline sal_uLong	SetLinePos( sal_uLong nlPos );			// inline unten
134 
135 	sal_Unicode	GetNextChar();
136     void RereadLookahead();
137 
IsParserWorking() const138 	inline int 	IsParserWorking() const	{ return SVPAR_WORKING == eState; }
139 
GetAsynchCallLink() const140 	Link GetAsynchCallLink() const
141 		{ return STATIC_LINK( this, SvParser, NewDataRead ); }
142 
CallAsyncCallLink()143 	long CallAsyncCallLink() { return NewDataRead( this, 0 ); }
144 
145 	// fuers asynchrone lesen aus dem SvStream
146 	/*virtual*/ void SaveState( int nToken );
147 	/*virtual*/ void RestoreState();
148 	virtual void Continue( int nToken );
149 
SetDownloadingFile(sal_Bool bSet)150 	inline void SetDownloadingFile( sal_Bool bSet ) { bDownloadingFile = bSet; }
IsDownloadingFile() const151 	inline sal_Bool IsDownloadingFile() const { return bDownloadingFile; }
152 
153 	// Set/get source encoding. The UCS2BEncoding flag is valid if source
154 	// encoding is UCS2. It specifies a big endian encoding.
155 	void SetSrcEncoding( rtl_TextEncoding eSrcEnc );
GetSrcEncoding() const156 	rtl_TextEncoding GetSrcEncoding() const { return eSrcEnc; }
157 
SetSrcUCS2BEncoding(sal_Bool bSet)158 	void SetSrcUCS2BEncoding( sal_Bool bSet ) { bUCS2BSrcEnc = bSet; }
IsSrcUCS2BEncoding() const159 	sal_Bool IsSrcUCS2BEncoding() const { return bUCS2BSrcEnc; }
160 
161 	// Darf der Zeichensatz auf UCS/2 umgeschaltet werden, wenn
162 	// in den ersten beiden Zeichen im Stream eine BOM steht?
SetSwitchToUCS2(sal_Bool bSet)163 	void SetSwitchToUCS2( sal_Bool bSet ) { bSwitchToUCS2 = bSet; }
IsSwitchToUCS2() const164 	sal_Bool IsSwitchToUCS2() const { return bSwitchToUCS2; }
165 
166 	// Aus wie vielen Bytes betseht ein Zeichen
167 	inline sal_uInt16 GetCharSize() const;
168 
169 	int GetSaveToken() const;
170 
171 	// Aufbau einer Which-Map 'rWhichMap' aus einem Array von
172 	// 'pWhichIds' von Which-Ids. Es hat die Lange 'nWhichIds'.
173 	// Die Which-Map wird nicht geloescht.
174 	static void BuildWhichTbl( SvUShorts &rWhichMap,
175 							   sal_uInt16 *pWhichIds,
176 							   sal_uInt16 nWhichIds );
177 };
178 
179 
180 #ifndef GOODIES_DECL_SVPARSER_DEFINED
181 #define GOODIES_DECL_SVPARSER_DEFINED
182 SV_DECL_REF(SvParser)
183 #endif
SV_IMPL_REF(SvParser)184 SV_IMPL_REF(SvParser)
185 
186 
187 
188 inline sal_uLong SvParser::SetLineNr( sal_uLong nlNum )
189 {	sal_uLong nlOld = nlLineNr; nlLineNr = nlNum; return nlOld; }
190 
SetLinePos(sal_uLong nlPos)191 inline sal_uLong SvParser::SetLinePos( sal_uLong nlPos )
192 {	sal_uLong nlOld = nlLinePos; nlLinePos = nlPos; return nlOld; }
193 
GetStackPos() const194 inline sal_uInt8 SvParser::GetStackPos() const
195 {	return nTokenStackPos; }
196 
GetCharSize() const197 inline sal_uInt16 SvParser::GetCharSize() const
198 {
199 	return (RTL_TEXTENCODING_UCS2 == eSrcEnc) ? 2 : 1;
200 }
201 
202 
203 /*========================================================================
204  *
205  * SvKeyValue.
206  *
207  *======================================================================*/
208 
209 SV_DECL_REF(SvKeyValueIterator)
210 
211 class SvKeyValue
212 {
213 	/** Representation.
214 	*/
215 	String m_aKey;
216 	String m_aValue;
217 
218 public:
219 	/** Construction.
220 	*/
SvKeyValue(void)221 	SvKeyValue (void)
222 	{}
223 
SvKeyValue(const String & rKey,const String & rValue)224 	SvKeyValue (const String &rKey, const String &rValue)
225 		: m_aKey (rKey), m_aValue (rValue)
226 	{}
227 
SvKeyValue(const SvKeyValue & rOther)228 	SvKeyValue (const SvKeyValue &rOther)
229 		: m_aKey (rOther.m_aKey), m_aValue (rOther.m_aValue)
230 	{}
231 
232 	/** Assignment.
233 	*/
operator =(SvKeyValue & rOther)234 	SvKeyValue& operator= (SvKeyValue &rOther)
235 	{
236 		m_aKey   = rOther.m_aKey;
237 		m_aValue = rOther.m_aValue;
238 		return *this;
239 	}
240 
241 	/** Operation.
242 	*/
GetKey(void) const243 	const String& GetKey   (void) const { return m_aKey; }
GetValue(void) const244 	const String& GetValue (void) const { return m_aValue; }
245 
SetKey(const String & rKey)246 	void SetKey   (const String &rKey  ) { m_aKey = rKey; }
SetValue(const String & rValue)247 	void SetValue (const String &rValue) { m_aValue = rValue; }
248 };
249 
250 /*========================================================================
251  *
252  * SvKeyValueIterator.
253  *
254  *======================================================================*/
255 class SvKeyValueList_Impl;
256 class SVT_DLLPUBLIC SvKeyValueIterator : public SvRefBase,
257     private boost::noncopyable
258 {
259     /** Representation.
260     */
261     SvKeyValueList_Impl* m_pList;
262     sal_uInt16               m_nPos;
263 
264 public:
265     /** Construction/Destruction.
266     */
267     SvKeyValueIterator (void);
268     virtual ~SvKeyValueIterator (void);
269 
270     /** Operation.
271     */
272     virtual sal_Bool GetFirst (SvKeyValue &rKeyVal);
273     virtual sal_Bool GetNext  (SvKeyValue &rKeyVal);
274     virtual void Append   (const SvKeyValue &rKeyVal);
275 };
276 
277 SV_IMPL_REF(SvKeyValueIterator);
278 
279 #endif //_SVPARSER_HXX
280 
281 /* vi:set tabstop=4 shiftwidth=4 expandtab: */
282