2 Copyright (C) 1999 Lars Knoll (knoll@mpi-hd.mpg.de)
3 Copyright (C) 2006 Alexey Proskuryakov (ap@nypop.com)
4 Copyright (C) 2006, 2008 Apple Inc. All rights reserved.
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public License
17 along with this library; see the file COPYING.LIB. If not, write to
18 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA.
23 #ifndef TextResourceDecoder_h
24 #define TextResourceDecoder_h
26 #include "TextEncoding.h"
30 class TextResourceDecoder : public RefCounted<TextResourceDecoder> {
35 EncodingFromXMLHeader,
37 EncodingFromCSSCharset,
38 EncodingFromHTTPHeader,
42 static PassRefPtr<TextResourceDecoder> create(const String& mimeType, const TextEncoding& defaultEncoding = TextEncoding())
44 return adoptRef(new TextResourceDecoder(mimeType, defaultEncoding));
46 ~TextResourceDecoder();
48 void setEncoding(const TextEncoding&, EncodingSource);
49 const TextEncoding& encoding() const { return m_encoding; }
51 String decode(const char* data, size_t length);
54 void useLenientXMLDecoding() { m_useLenientXMLDecoding = true; }
55 bool sawError() const { return m_sawError; }
58 TextResourceDecoder(const String& mimeType, const TextEncoding& defaultEncoding);
60 enum ContentType { PlainText, HTML, XML, CSS }; // PlainText only checks for BOM.
61 static ContentType determineContentType(const String& mimeType);
62 static const TextEncoding& defaultEncoding(ContentType, const TextEncoding& defaultEncoding);
64 size_t checkForBOM(const char*, size_t);
65 bool checkForCSSCharset(const char*, size_t, bool& movedDataToBuffer);
66 bool checkForHeadCharset(const char*, size_t, bool& movedDataToBuffer);
67 void detectJapaneseEncoding(const char*, size_t);
69 ContentType m_contentType;
70 TextEncoding m_encoding;
71 OwnPtr<TextCodec> m_codec;
72 EncodingSource m_source;
73 Vector<char> m_buffer;
75 bool m_checkedForCSSCharset;
76 bool m_checkedForHeadCharset;
77 bool m_useLenientXMLDecoding; // Don't stop on XML decoding errors.