LibreOffice Module sax (master) 1
xml2utf.hxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#ifndef INCLUDED_SAX_INC_XML2UTF_HXX
21#define INCLUDED_SAX_INC_XML2UTF_HXX
22
23#include <sal/config.h>
24
25#include <memory>
26
27#include <sal/types.h>
28#include <rtl/string.hxx>
29
30#include <com/sun/star/io/XInputStream.hpp>
31
32namespace sax_expatwrap {
33
35{
36
37public:
38 Text2UnicodeConverter( const OString & sEncoding );
40
41 css::uno::Sequence < sal_Unicode > convert( const css::uno::Sequence<sal_Int8> & );
42 bool canContinue() const { return m_bCanContinue; }
43
44private:
45 void init( rtl_TextEncoding encoding );
46
47 rtl_TextToUnicodeConverter m_convText2Unicode;
48 rtl_TextToUnicodeContext m_contextText2Unicode;
51 css::uno::Sequence<sal_Int8> m_seqSource;
52};
53
54/*----------------------------------------
55*
56* Unicode2TextConverter
57*
58**-----------------------------------------*/
60{
61public:
62 Unicode2TextConverter( rtl_TextEncoding encoding );
64
65 css::uno::Sequence<sal_Int8> convert( const sal_Unicode * , sal_Int32 nLength );
66
67private:
68 rtl_UnicodeToTextConverter m_convUnicode2Text;
69 rtl_UnicodeToTextContext m_contextUnicode2Text;
70 css::uno::Sequence<sal_Unicode> m_seqSource;
71};
72
73
74/*----------------------------------------
75*
76* XMLFile2UTFConverter
77*
78**-----------------------------------------*/
80{
81public:
83 m_bStarted( false )
84 {}
85
86 void setInputStream( css::uno::Reference< css::io::XInputStream > const &r ) { m_in = r; }
87 void setEncoding( const OString &s ) { m_sEncoding = s; }
88
89
90 // @param nMaxToRead The number of chars, that should be read. Note that this is no exact number. There
91 // may be returned less or more bytes than ordered.
96 sal_Int32 readAndConvert( css::uno::Sequence<sal_Int8> &seq , sal_Int32 nMaxToRead );
97
98private:
99
100 // Called only on first Sequence of bytes. Tries to figure out file format and encoding information.
101 // @return TRUE, when encoding information could be retrieved
102 // @return FALSE, when no encoding information was found in file
103 bool scanForEncoding( css::uno::Sequence<sal_Int8> &seq );
104
105 // Called only on first Sequence of bytes. Tries to figure out
106 // if enough data is available to scan encoding
107 // @return TRUE, when encoding is retrievable
108 // @return FALSE, when more data is needed
109 static bool isEncodingRecognizable( const css::uno::Sequence< sal_Int8 > & seq );
110
111 // When encoding attribute is within the text (in the first line), it is removed.
112 static void removeEncoding( css::uno::Sequence<sal_Int8> &seq );
113
114 // Initializes decoding depending on m_sEncoding setting
115 void initializeDecoding();
116private:
117 css::uno::Reference< css::io::XInputStream > m_in;
118
120 OString m_sEncoding;
121
122 std::unique_ptr<Text2UnicodeConverter> m_pText2Unicode;
123 std::unique_ptr<Unicode2TextConverter> m_pUnicode2Text;
124};
125}
126
127#endif // INCLUDED_SAX_INC_XML2UTF_HXX
128
129/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
css::uno::Sequence< sal_Int8 > m_seqSource
Definition: xml2utf.hxx:51
void init(rtl_TextEncoding encoding)
Definition: xml2utf.cxx:353
rtl_TextToUnicodeContext m_contextText2Unicode
Definition: xml2utf.hxx:48
rtl_TextToUnicodeConverter m_convText2Unicode
Definition: xml2utf.hxx:47
Text2UnicodeConverter(const OString &sEncoding)
Definition: xml2utf.cxx:328
css::uno::Sequence< sal_Unicode > convert(const css::uno::Sequence< sal_Int8 > &)
Definition: xml2utf.cxx:363
css::uno::Sequence< sal_Int8 > convert(const sal_Unicode *, sal_Int32 nLength)
Definition: xml2utf.cxx:441
Unicode2TextConverter(rtl_TextEncoding encoding)
Definition: xml2utf.cxx:427
rtl_UnicodeToTextConverter m_convUnicode2Text
Definition: xml2utf.hxx:68
rtl_UnicodeToTextContext m_contextUnicode2Text
Definition: xml2utf.hxx:69
css::uno::Sequence< sal_Unicode > m_seqSource
Definition: xml2utf.hxx:70
css::uno::Reference< css::io::XInputStream > m_in
Definition: xml2utf.hxx:117
void setEncoding(const OString &s)
Definition: xml2utf.hxx:87
bool scanForEncoding(css::uno::Sequence< sal_Int8 > &seq)
Definition: xml2utf.cxx:197
sal_Int32 readAndConvert(css::uno::Sequence< sal_Int8 > &seq, sal_Int32 nMaxToRead)
Definition: xml2utf.cxx:39
void setInputStream(css::uno::Reference< css::io::XInputStream > const &r)
Definition: xml2utf.hxx:86
std::unique_ptr< Text2UnicodeConverter > m_pText2Unicode
Definition: xml2utf.hxx:122
static void removeEncoding(css::uno::Sequence< sal_Int8 > &seq)
Definition: xml2utf.cxx:117
std::unique_ptr< Unicode2TextConverter > m_pUnicode2Text
Definition: xml2utf.hxx:123
static bool isEncodingRecognizable(const css::uno::Sequence< sal_Int8 > &seq)
Definition: xml2utf.cxx:160
sal_uInt16 sal_Unicode
sal_Int32 nLength