1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #ifndef INCLUDED_SVTOOLS_PARHTML_HXX
21 #define INCLUDED_SVTOOLS_PARHTML_HXX
23 #include <svtools/svtdllapi.h>
24 #include <svtools/svparser.hxx>
26 #include <boost/ptr_container/ptr_vector.hpp>
28 namespace com
{ namespace sun
{ namespace star
{
30 class XDocumentProperties
;
35 class SvNumberFormatter
;
36 class SvKeyValueIterator
;
38 #define HTMLFONTSZ1_DFLT 7
39 #define HTMLFONTSZ2_DFLT 10
40 #define HTMLFONTSZ3_DFLT 12
41 #define HTMLFONTSZ4_DFLT 14
42 #define HTMLFONTSZ5_DFLT 18
43 #define HTMLFONTSZ6_DFLT 24
44 #define HTMLFONTSZ7_DFLT 36
46 enum HTMLTableFrame
{ HTML_TF_VOID
, HTML_TF_ABOVE
, HTML_TF_BELOW
,
47 HTML_TF_HSIDES
, HTML_TF_LHS
, HTML_TF_RHS
, HTML_TF_VSIDES
, HTML_TF_BOX
};
49 enum HTMLTableRules
{ HTML_TR_NONE
, HTML_TR_GROUPS
, HTML_TR_ROWS
,
50 HTML_TR_COLS
, HTML_TR_ALL
};
55 HTML_IT_PASSWORD
= 0x02,
56 HTML_IT_CHECKBOX
= 0x03,
59 HTML_IT_SCRIBBLE
= 0x06,
61 HTML_IT_HIDDEN
= 0x08,
62 HTML_IT_SUBMIT
= 0x09,
68 enum HTMLScriptLanguage
77 const sal_Char
*pName
; // Wert einer HTML-Option
78 sal_uInt16 nValue
; // und der dazugehoerige Wert eines Enums
81 // Repraesentation einer HTML-Option (=Atrribut in einem Start-Tag)
82 // Die Werte der Optionen werden immer als String gespeichert.
83 // Die Methoden GetNumber, ... duerfen nur aufgerufen werden, wenn
84 // die Option auch numerisch, ... ist.
86 class SVT_DLLPUBLIC HTMLOption
88 OUString aValue
; // der Wert der Option (immer als String)
89 OUString aToken
; // der Name der Option als String
90 sal_uInt16 nToken
; // und das entsprechende Token
94 HTMLOption( sal_uInt16 nTyp
, const OUString
& rToken
, const OUString
& rValue
);
96 // der Name der Option ...
97 sal_uInt16
GetToken() const { return nToken
; } // ... als Enum
98 const OUString
& GetTokenString() const { return aToken
; } // ... als String
100 // der Wert der Option ...
101 const OUString
& GetString() const { return aValue
; } // ... als String
103 sal_uInt32
GetNumber() const; // ... als Zahl
104 sal_Int32
GetSNumber() const; // ... als Zahl
105 void GetNumbers( std::vector
<sal_uInt32
> &rNumbers
, // ... als Zahlen
106 bool bSpaceDelim
=false ) const;
107 void GetColor( Color
& ) const; // ... als Farbe
109 // ... als Enum pOptEnums ist ein HTMLOptionEnum-Array
110 sal_uInt16
GetEnum( const HTMLOptionEnum
*pOptEnums
,
111 sal_uInt16 nDflt
=0 ) const;
112 bool GetEnum( sal_uInt16
&rEnum
, const HTMLOptionEnum
*pOptEnums
) const;
114 // ... und als ein par spezielle Enums
115 HTMLInputType
GetInputType() const; // <INPUT TYPE=...>
116 HTMLTableFrame
GetTableFrame() const; // <TABLE FRAME=...>
117 HTMLTableRules
GetTableRules() const; // <TABLE RULES=...>
118 //SvxAdjust GetAdjust() const; // <P,TH,TD ALIGN=>
121 typedef ::boost::ptr_vector
<HTMLOption
> HTMLOptions
;
123 class SVT_DLLPUBLIC HTMLParser
: public SvParser
126 mutable HTMLOptions maOptions
; // die Optionen des Start-Tags
128 bool bNewDoc
: 1; // neues Doc lesen ?
129 bool bIsInHeader
: 1; // scanne Header-Bereich
130 bool bIsInBody
: 1; // scanne Body-Bereich
131 bool bReadListing
: 1; // Lese Listings
132 bool bReadXMP
: 1; // Lese XMP
133 bool bReadPRE
: 1; // Lese preformatted Text
134 bool bReadTextArea
: 1; // Lese TEXTAREA
135 bool bReadScript
: 1; // Lesen von <SCRIPT>
136 bool bReadStyle
: 1; // Lesen von <STYLE>
137 bool bEndTokenFound
: 1; // </SCRIPT> oder </STYLE> gefunden
139 bool bPre_IgnoreNewPara
: 1; // Flags fuers lesen von PRE-Absaetzen
140 bool bReadNextChar
: 1; // true: NextChar nochmals lesen (JavaScript!)
141 bool bReadComment
: 1; // true: NextChar nochmals lesen (JavaScript!)
143 sal_uInt32 nPre_LinePos
; // Pos in der Line im PRE-Tag
145 int mnPendingOffToken
; ///< OFF token pending for a <XX.../> ON/OFF ON token
150 OUString sSaveToken
; // das gelesene Tag als String
152 int ScanText( const sal_Unicode cBreak
= 0U );
154 int _GetNextRawToken();
156 // scanne das naechste Token,
157 virtual int _GetNextToken() SAL_OVERRIDE
;
159 virtual ~HTMLParser();
161 void FinishHeader( bool bBody
) { bIsInHeader
= false; bIsInBody
= bBody
; }
164 HTMLParser( SvStream
& rIn
, bool bReadNewDoc
= true );
166 virtual SvParserState
CallParser() SAL_OVERRIDE
; // Aufruf des Parsers
168 bool IsNewDoc() const { return bNewDoc
; }
169 bool IsInHeader() const { return bIsInHeader
; }
170 bool IsInBody() const { return bIsInBody
; }
171 bool IsValidSyntax() const { return true; }
172 bool IsReadListing() const { return bReadListing
; }
173 bool IsReadXMP() const { return bReadXMP
; }
174 bool IsReadPRE() const { return bReadPRE
; }
175 bool IsReadScript() const { return bReadScript
; }
176 bool IsReadStyle() const { return bReadStyle
; }
178 void SetReadNextChar() { bReadNextChar
= true; }
180 // PRE-/LISTING oder XMP-Modus starten/beenden oder Tags entsprechend
182 inline void StartPRE( bool bRestart
=false );
183 void FinishPRE() { bReadPRE
= false; }
184 int FilterPRE( int nToken
);
186 inline void StartListing( bool bRestart
=false );
187 void FinishListing() { bReadListing
= false; }
188 int FilterListing( int nToken
);
190 inline void StartXMP( bool bRestart
=false );
191 void FinishXMP() { bReadXMP
= false; }
192 int FilterXMP( int nToken
);
194 void FinishTextArea() { bReadTextArea
= false; }
196 // PRE-/LSITING- und XMP-Modus beenden
197 void FinishPREListingXMP() { bReadPRE
= bReadListing
= bReadXMP
= false; }
199 // Das aktuelle Token dem aktuellen Modus (PRE, XMP, ...) entsprechend
200 // Filtern und die Flags setzen. Wird von Continue aufgerufen, bevor
201 // NextToken gerufen wird. Wer eigene Schleifen implementiert bzw.
202 // selbst NextToken aufruft, sollte diese Methode vorher rufen.
203 int FilterToken( int nToken
);
205 // Scannen eines Scripts beenden (sollte nur unmittelbar nach dem
206 // Lesen eines <SCRIPT> aufgerufen werden
207 void EndScanScript() { bReadScript
= false; }
209 void ReadRawData( const OUString
&rEndToken
) { aEndToken
= rEndToken
; }
211 // Token ohne \-Sequenzen
212 void UnescapeToken();
214 // Ermitteln der Optionen. pNoConvertToken ist das optionale Token
215 // einer Option, fuer die CR/LFs nicht aus dem Wert der Option
217 const HTMLOptions
& GetOptions( sal_uInt16
*pNoConvertToken
=0 );
219 // fuers asynchrone lesen aus dem SvStream
220 virtual void Continue( int nToken
) SAL_OVERRIDE
;
225 static rtl_TextEncoding
GetEncodingByMIME( const OUString
& rMime
);
227 /// template method: called when ParseMetaOptions adds a user-defined meta
228 virtual void AddMetaUserDefined( OUString
const & i_rMetaName
);
231 /// parse meta options into XDocumentProperties and encoding
232 bool ParseMetaOptionsImpl( const ::com::sun::star::uno::Reference
<
233 ::com::sun::star::document::XDocumentProperties
>&,
236 rtl_TextEncoding
& rEnc
);
239 /// overriding method must call this implementation!
240 virtual bool ParseMetaOptions( const ::com::sun::star::uno::Reference
<
241 ::com::sun::star::document::XDocumentProperties
>&,
242 SvKeyValueIterator
* );
244 // Ist der uebergebene 0-terminierte String (vermutlich) der Anfang
245 // eines HTML-Files? Er sollte mind. 80 Zeichen lang sein.
246 // Mit Ausnahme des Falls, dass SwitchToUCS2==false und
247 // SVPAR_CS_DONTKNOW uebergeben wird muss der String mit zwei(!)
248 // 0-Bytes an einer geraden(!) Position terminiert sein.
249 static bool IsHTMLFormat( const sal_Char
* pHeader
,
250 bool bSwitchToUCS2
= false,
251 rtl_TextEncoding eEnc
=RTL_TEXTENCODING_DONTKNOW
);
253 bool ParseScriptOptions( OUString
& rLangString
, const OUString
&, HTMLScriptLanguage
& rLang
,
254 OUString
& rSrc
, OUString
& rLibrary
, OUString
& rModule
);
256 // Einen Kommentar um den Inhalt von <SCRIPT> oder <STYLE> entfernen
257 // Bei 'bFull' wird ggf. die gesammte Zeile hinter einem "<!--"
258 // entfernt (fuer JavaSript)
259 static void RemoveSGMLComment( OUString
&rString
, bool bFull
);
261 static bool InternalImgToPrivateURL( OUString
& rURL
);
262 static rtl_TextEncoding
GetEncodingByHttpHeader( SvKeyValueIterator
*pHTTPHeader
);
263 bool SetEncodingByHTTPHeader( SvKeyValueIterator
*pHTTPHeader
);
266 inline void HTMLParser::StartPRE( bool bRestart
)
269 bPre_IgnoreNewPara
= !bRestart
;
273 inline void HTMLParser::StartListing( bool bRestart
)
276 bPre_IgnoreNewPara
= !bRestart
;
280 inline void HTMLParser::StartXMP( bool bRestart
)
283 bPre_IgnoreNewPara
= !bRestart
;
289 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */