xref: /trunk/main/svtools/inc/svtools/parhtml.hxx (revision 01aa44aa)
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 _PARHTML_HXX
25 #define _PARHTML_HXX
26 
27 #include "svtools/svtdllapi.h"
28 #include <tools/solar.h>
29 #include <tools/string.hxx>
30 #include <svl/svarray.hxx>
31 #include <svtools/svparser.hxx>
32 
33 
34 namespace com { namespace sun { namespace star {
35     namespace document {
36         class XDocumentProperties;
37     }
38 } } }
39 
40 class Color;
41 class SvNumberFormatter;
42 class SvULongs;
43 class SvKeyValueIterator;
44 
45 #define HTMLFONTSZ1_DFLT 7
46 #define HTMLFONTSZ2_DFLT 10
47 #define HTMLFONTSZ3_DFLT 12
48 #define HTMLFONTSZ4_DFLT 14
49 #define HTMLFONTSZ5_DFLT 18
50 #define HTMLFONTSZ6_DFLT 24
51 #define HTMLFONTSZ7_DFLT 36
52 
53 enum HTMLTableFrame { HTML_TF_VOID, HTML_TF_ABOVE, HTML_TF_BELOW,
54 	HTML_TF_HSIDES, HTML_TF_LHS, HTML_TF_RHS, HTML_TF_VSIDES, HTML_TF_BOX };
55 
56 enum HTMLTableRules { HTML_TR_NONE, HTML_TR_GROUPS, HTML_TR_ROWS,
57 	HTML_TR_COLS, HTML_TR_ALL };
58 
59 enum HTMLInputType
60 {
61 	HTML_IT_TEXT =		0x01,
62 	HTML_IT_PASSWORD =	0x02,
63 	HTML_IT_CHECKBOX =	0x03,
64 	HTML_IT_RADIO =		0x04,
65 	HTML_IT_RANGE =		0x05,
66 	HTML_IT_SCRIBBLE =	0x06,
67 	HTML_IT_FILE =		0x07,
68 	HTML_IT_HIDDEN =	0x08,
69 	HTML_IT_SUBMIT =	0x09,
70 	HTML_IT_IMAGE =		0x0a,
71 	HTML_IT_RESET =		0x0b,
72 	HTML_IT_BUTTON = 	0x0c
73 };
74 
75 enum HTMLScriptLanguage
76 {
77 	HTML_SL_STARBASIC,
78 	HTML_SL_JAVASCRIPT,
79 	HTML_SL_UNKNOWN
80 };
81 
82 struct HTMLOptionEnum
83 {
84 	const sal_Char *pName;	// Wert einer HTML-Option
85 	sal_uInt16 nValue;		// und der dazugehoerige Wert eines Enums
86 };
87 
88 // Repraesentation einer HTML-Option (=Atrribut in einem Start-Tag)
89 // Die Werte der Optionen werden immer als String gespeichert.
90 // Die Methoden GetNumber, ... duerfen nur aufgerufen werden, wenn
91 // die Option auch numerisch, ... ist.
92 
93 class SVT_DLLPUBLIC HTMLOption
94 {
95 	String aValue;			// der Wert der Option (immer als String)
96 	String aToken;			// der Name der Option als String
97 	sal_uInt16 nToken;			// und das entsprechende Token
98 
99 public:
100 
101 	HTMLOption( sal_uInt16 nTyp, const String& rToken, const String& rValue );
102 
103 	// der Name der Option ...
GetToken() const104 	sal_uInt16 GetToken() const { return nToken; }	// ... als Enum
GetTokenString() const105 	const String& GetTokenString() const { return aToken; } // ... als String
106 
107 	// der Wert der Option ...
GetString() const108 	const String& GetString() const { return aValue; }	// ... als String
109 
110 	sal_uInt32 GetNumber() const;                          	// ... als Zahl
111 	sal_Int32 GetSNumber() const;                          	// ... als Zahl
112 	void GetNumbers( SvULongs &rLongs, 					// ... als Zahlen
113 					 sal_Bool bSpaceDelim=sal_False ) const;
114 	void GetColor( Color& ) const;						// ... als Farbe
115 
116 	// ... als Enum pOptEnums ist ein HTMLOptionEnum-Array
117 	sal_uInt16 GetEnum( const HTMLOptionEnum *pOptEnums,
118 						sal_uInt16 nDflt=0 ) const;
119 	sal_Bool GetEnum( sal_uInt16 &rEnum, const HTMLOptionEnum *pOptEnums ) const;
120 
121 	// ... und als ein par spezielle Enums
122 	HTMLInputType GetInputType() const;				   	// <INPUT TYPE=...>
123 	HTMLTableFrame GetTableFrame() const;				// <TABLE FRAME=...>
124 	HTMLTableRules GetTableRules() const;				// <TABLE RULES=...>
125 	//SvxAdjust GetAdjust() const;						// <P,TH,TD ALIGN=>
126 };
127 
128 typedef HTMLOption* HTMLOptionPtr;
129 SV_DECL_PTRARR(HTMLOptions,HTMLOptionPtr,16,16)
130 
131 class SVT_DLLPUBLIC HTMLParser : public SvParser
132 {
133 	sal_Bool bNewDoc 		: 1;		// neues Doc lesen ?
134 	sal_Bool bIsInHeader 	: 1;		// scanne Header-Bereich
135 	sal_Bool bIsInBody 		: 1;		// scanne Body-Bereich
136 	sal_Bool bReadListing	: 1;		// Lese Listings
137 	sal_Bool bReadXMP		: 1;		// Lese XMP
138 	sal_Bool bReadPRE		: 1;		// Lese preformatted Text
139 	sal_Bool bReadTextArea	: 1;		// Lese TEXTAREA
140 	sal_Bool bReadScript	: 1;		// Lesen von <SCRIPT>
141 	sal_Bool bReadStyle		: 1;		// Lesen von <STYLE>
142 	sal_Bool bEndTokenFound : 1;		// </SCRIPT> oder </STYLE> gefunden
143 
144 	sal_Bool bPre_IgnoreNewPara : 1;	// Flags fuers lesen von PRE-Absaetzen
145 	sal_Bool bReadNextChar : 1;			// sal_True: NextChar nochmals lesen (JavaScript!)
146 	sal_Bool bReadComment : 1;			// sal_True: NextChar nochmals lesen (JavaScript!)
147 
148 	sal_uInt32 nPre_LinePos;			// Pos in der Line im PRE-Tag
149 
150 	HTMLOptions *pOptions;			// die Optionen des Start-Tags
151 	String aEndToken;
152 
153 protected:
154 	String sSaveToken;				// das gelesene Tag als String
155 
156 	int ScanText( const sal_Unicode cBreak = 0U );
157 
158 	int _GetNextRawToken();
159 
160 	// scanne das naechste Token,
161 	virtual int _GetNextToken();
162 
163 	virtual ~HTMLParser();
164 
FinishHeader(sal_Bool bBody)165 	void FinishHeader( sal_Bool bBody ) { bIsInHeader = sal_False; bIsInBody = bBody; }
166 
167 public:
168 	HTMLParser( SvStream& rIn, int bReadNewDoc = sal_True );
169 
170 	virtual SvParserState CallParser();   // Aufruf des Parsers
171 
IsNewDoc() const172 	sal_Bool IsNewDoc() const 		{ return bNewDoc; }
IsInHeader() const173 	sal_Bool IsInHeader() const 	{ return bIsInHeader; }
IsInBody() const174 	sal_Bool IsInBody() const 		{ return bIsInBody; }
IsValidSyntax() const175 	sal_Bool IsValidSyntax() const 	{ return sal_True; }
IsReadListing() const176 	sal_Bool IsReadListing() const 	{ return bReadListing; }
IsReadXMP() const177 	sal_Bool IsReadXMP() const 		{ return bReadXMP; }
IsReadPRE() const178 	sal_Bool IsReadPRE() const 		{ return bReadPRE; }
IsReadScript() const179 	sal_Bool IsReadScript() const 	{ return bReadScript; }
IsReadStyle() const180 	sal_Bool IsReadStyle() const 	{ return bReadStyle; }
181 
SetReadNextChar()182 	void SetReadNextChar() 		{ bReadNextChar = sal_True; }
183 
184 	// PRE-/LISTING oder XMP-Modus starten/beenden oder Tags entsprechend
185 	// filtern
186 	inline void StartPRE( sal_Bool bRestart=sal_False );
FinishPRE()187 	void FinishPRE() { bReadPRE = sal_False; }
188 	int FilterPRE( int nToken );
189 
190 	inline void StartListing( sal_Bool bRestart=sal_False );
FinishListing()191 	void FinishListing() { bReadListing = sal_False; }
192 	int FilterListing( int nToken );
193 
194 	inline void StartXMP( sal_Bool bRestart=sal_False );
FinishXMP()195 	void FinishXMP() { bReadXMP = sal_False; }
196 	int FilterXMP( int nToken );
197 
FinishTextArea()198 	void FinishTextArea() { bReadTextArea = sal_False; }
199 
200 	// PRE-/LSITING- und XMP-Modus beenden
FinishPREListingXMP()201 	void FinishPREListingXMP() { bReadPRE = bReadListing = bReadXMP = sal_False; }
202 
203 	// Das aktuelle Token dem aktuellen Modus (PRE, XMP, ...) entsprechend
204 	// Filtern und die Flags setzen. Wird von Continue aufgerufen, bevor
205 	// NextToken gerufen wird. Wer eigene Schleifen implementiert bzw.
206 	// selbst NextToken aufruft, sollte diese Methode vorher rufen.
207 	int FilterToken( int nToken );
208 
209 	// Scannen eines Scripts beenden (sollte nur unmittelbar nach dem
210 	// Lesen eines <SCRIPT> aufgerufen werden
EndScanScript()211 	void EndScanScript() { bReadScript = sal_False; }
212 
ReadRawData(const sal_Char * pEndToken)213 	void ReadRawData( const sal_Char *pEndToken ) { aEndToken.AssignAscii(pEndToken); }
214 
215 	// Token ohne \-Sequenzen
216 	void UnescapeToken();
217 
218 	// Ermitteln der Optionen. pNoConvertToken ist das optionale Token
219 	// einer Option, fuer die CR/LFs nicht aus dem Wert der Option
220 	// geloescht werden.
221 	const HTMLOptions *GetOptions( sal_uInt16 *pNoConvertToken=0 ) const;
222 
223 	// fuers asynchrone lesen aus dem SvStream
224 //	virtual void SaveState( int nToken );
225 //	virtual void RestoreState();
226 	virtual void Continue( int nToken );
227 
228 
229 protected:
230 
231     static rtl_TextEncoding GetEncodingByMIME( const String& rMime );
232 
233     /// template method: called when ParseMetaOptions adds a user-defined meta
234     virtual void AddMetaUserDefined( ::rtl::OUString const & i_rMetaName );
235 
236 private:
237     /// parse meta options into XDocumentProperties and encoding
238     bool ParseMetaOptionsImpl( const ::com::sun::star::uno::Reference<
239                 ::com::sun::star::document::XDocumentProperties>&,
240             SvKeyValueIterator*,
241             const HTMLOptions*,
242             rtl_TextEncoding& rEnc );
243 
244 public:
245     /// overriding method must call this implementation!
246     virtual bool ParseMetaOptions( const ::com::sun::star::uno::Reference<
247                 ::com::sun::star::document::XDocumentProperties>&,
248             SvKeyValueIterator* );
249 
250 	// Ist der uebergebene 0-terminierte String (vermutlich) der Anfang
251 	// eines HTML-Files? Er sollte mind. 80 Zeichen lang sein.
252 	// Mit Ausnahme des Falls, dass SwitchToUCS2==sal_False und
253 	// SVPAR_CS_DONTKNOW uebergeben wird muss der String mit zwei(!)
254 	// 0-Bytes an einer geraden(!) Position terminiert sein.
255 	static FASTBOOL IsHTMLFormat( const sal_Char* pHeader,
256 					  sal_Bool bSwitchToUCS2 = sal_False,
257 					  rtl_TextEncoding eEnc=RTL_TEXTENCODING_DONTKNOW );
258 
259     sal_Bool ParseScriptOptions( String& rLangString, const String&, HTMLScriptLanguage& rLang,
260 							 String& rSrc, String& rLibrary, String& rModule );
261 
262 	// Einen Kommentar um den Inhalt von <SCRIPT> oder <STYLE> entfernen
263 	// Bei 'bFull' wird ggf. die gesammte Zeile hinter einem "<!--"
264 	// entfernt (fuer JavaSript)
265 	static void RemoveSGMLComment( String &rString, sal_Bool bFull );
266 
267 	static sal_Bool InternalImgToPrivateURL( String& rURL );
268     static rtl_TextEncoding GetEncodingByHttpHeader( SvKeyValueIterator *pHTTPHeader );
269 	sal_Bool SetEncodingByHTTPHeader( SvKeyValueIterator *pHTTPHeader );
270 };
271 
StartPRE(sal_Bool bRestart)272 inline void HTMLParser::StartPRE( sal_Bool bRestart )
273 {
274 	bReadPRE = sal_True;
275 	bPre_IgnoreNewPara = !bRestart;
276 	nPre_LinePos = 0UL;
277 }
278 
StartListing(sal_Bool bRestart)279 inline void HTMLParser::StartListing( sal_Bool bRestart )
280 {
281 	bReadListing = sal_True;
282 	bPre_IgnoreNewPara = !bRestart;
283 	nPre_LinePos = 0UL;
284 }
285 
StartXMP(sal_Bool bRestart)286 inline void HTMLParser::StartXMP( sal_Bool bRestart )
287 {
288 	bReadXMP = sal_True;
289 	bPre_IgnoreNewPara = !bRestart;
290 	nPre_LinePos = 0UL;
291 }
292 
293 #endif
294