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