[89] | 1 | /*
|
---|
| 2 | * Licensed to the Apache Software Foundation (ASF) under one or more
|
---|
| 3 | * contributor license agreements. See the NOTICE file distributed with
|
---|
| 4 | * this work for additional information regarding copyright ownership.
|
---|
| 5 | * The ASF licenses this file to You under the Apache License, Version 2.0
|
---|
| 6 | * (the "License"); you may not use this file except in compliance with
|
---|
| 7 | * the License. You may obtain a copy of the License at
|
---|
| 8 | *
|
---|
| 9 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
| 10 | *
|
---|
| 11 | * Unless required by applicable law or agreed to in writing, software
|
---|
| 12 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
| 14 | * See the License for the specific language governing permissions and
|
---|
| 15 | * limitations under the License.
|
---|
| 16 | */
|
---|
| 17 |
|
---|
| 18 | #ifndef _LOG4CXX_HELPERS_CHARSETDECODER_H
|
---|
| 19 | #define _LOG4CXX_HELPERS_CHARSETDECODER_H
|
---|
| 20 |
|
---|
| 21 | #include <log4cxx/helpers/objectimpl.h>
|
---|
| 22 |
|
---|
| 23 | namespace log4cxx
|
---|
| 24 | {
|
---|
| 25 | namespace helpers {
|
---|
| 26 | class CharsetDecoder;
|
---|
| 27 | LOG4CXX_PTR_DEF(CharsetDecoder);
|
---|
| 28 | class ByteBuffer;
|
---|
| 29 |
|
---|
| 30 |
|
---|
| 31 | /**
|
---|
| 32 | * An abstract engine to transform a sequences of bytes in a specific charset
|
---|
| 33 | * into a LogString.
|
---|
| 34 | */
|
---|
| 35 | class LOG4CXX_EXPORT CharsetDecoder : public ObjectImpl
|
---|
| 36 | {
|
---|
| 37 | public:
|
---|
| 38 | DECLARE_ABSTRACT_LOG4CXX_OBJECT(CharsetDecoder)
|
---|
| 39 | BEGIN_LOG4CXX_CAST_MAP()
|
---|
| 40 | LOG4CXX_CAST_ENTRY(CharsetDecoder)
|
---|
| 41 | END_LOG4CXX_CAST_MAP()
|
---|
| 42 | protected:
|
---|
| 43 | /**
|
---|
| 44 | * Protected constructor.
|
---|
| 45 | */
|
---|
| 46 | CharsetDecoder();
|
---|
| 47 | public:
|
---|
| 48 | /**
|
---|
| 49 | * Destructor.
|
---|
| 50 | */
|
---|
| 51 | virtual ~CharsetDecoder();
|
---|
| 52 |
|
---|
| 53 | /**
|
---|
| 54 | * Get decoder for default charset.
|
---|
| 55 | */
|
---|
| 56 | static CharsetDecoderPtr getDefaultDecoder();
|
---|
| 57 | /**
|
---|
| 58 | * Get decoder for specified character set.
|
---|
| 59 | * @param charset the following values should be recognized:
|
---|
| 60 | * "US-ASCII", "ISO-8859-1", "UTF-8",
|
---|
| 61 | * "UTF-16BE", "UTF-16LE".
|
---|
| 62 | * @return decoder
|
---|
| 63 | * @throws IllegalArgumentException if charset is not recognized.
|
---|
| 64 | */
|
---|
| 65 | static CharsetDecoderPtr getDecoder(const LogString& charset);
|
---|
| 66 |
|
---|
| 67 | /**
|
---|
| 68 | * Get decoder for UTF-8.
|
---|
| 69 | */
|
---|
| 70 | static CharsetDecoderPtr getUTF8Decoder();
|
---|
| 71 | /**
|
---|
| 72 | * Get decoder for ISO-8859-1.
|
---|
| 73 | */
|
---|
| 74 | static CharsetDecoderPtr getISOLatinDecoder();
|
---|
| 75 |
|
---|
| 76 |
|
---|
| 77 |
|
---|
| 78 | /**
|
---|
| 79 | * Decodes as many bytes as possible from the given
|
---|
| 80 | * input buffer, writing the results to the given output string.
|
---|
| 81 | * @param in input buffer.
|
---|
| 82 | * @param out output string.
|
---|
| 83 | * @return APR_SUCCESS if not encoding errors were found.
|
---|
| 84 | */
|
---|
| 85 | virtual log4cxx_status_t decode(ByteBuffer& in,
|
---|
| 86 | LogString& out) = 0;
|
---|
| 87 |
|
---|
| 88 | /**
|
---|
| 89 | * Determins if status value indicates an invalid byte sequence.
|
---|
| 90 | */
|
---|
| 91 | inline static bool isError(log4cxx_status_t stat) {
|
---|
| 92 | return (stat != 0);
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | private:
|
---|
| 96 | /**
|
---|
| 97 | * Private copy constructor.
|
---|
| 98 | */
|
---|
| 99 | CharsetDecoder(const CharsetDecoder&);
|
---|
| 100 | /**
|
---|
| 101 | * Private assignment operator.
|
---|
| 102 | */
|
---|
| 103 | CharsetDecoder& operator=(const CharsetDecoder&);
|
---|
| 104 | /**
|
---|
| 105 | * Creates a new decoder for the default charset.
|
---|
| 106 | */
|
---|
| 107 | static CharsetDecoder* createDefaultDecoder();
|
---|
| 108 | };
|
---|
| 109 |
|
---|
| 110 | } // namespace helpers
|
---|
| 111 | } //namespace log4cxx
|
---|
| 112 |
|
---|
| 113 | #endif //_LOG4CXX_HELPERS_CHARSETENCODER_H
|
---|