1/*
2 * Copyright (C) 2007 Esmertec AG.
3 * Copyright (C) 2007 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * 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 WBXML_ENCODER_H
19#define WBXML_ENCODER_H
20
21#include <stdint.h>
22#include "wbxml_const.h"
23#include "wbxml_stl.h"
24
25
26class WbxmlHandler
27{
28public:
29    virtual ~WbxmlHandler() {}
30    virtual void wbxmlData(const char *data, uint32_t len) = 0;
31};
32
33enum EncoderError {
34    NO_ERROR = 0,
35    ERROR_NO_PUBLIC_ID = 1,
36    ERROR_UNSUPPORTED_DOCTYPE,
37    ERROR_UNSUPPORTED_TAG,
38    ERROR_UNSUPPORTED_ATTR,
39    ERROR_INVALID_DATA,
40    ERROR_INVALID_INTEGER_VALUE,
41    ERROR_INVALID_DATETIME_VALUE,
42    ERROR_INVALID_END_ELEMENT,
43};
44
45class WbxmlEncoder
46{
47public:
48    WbxmlEncoder(int publicId):mPublicId(publicId) {}
49
50    virtual ~WbxmlEncoder() {}
51
52    void setWbxmlHandler(WbxmlHandler * handler)
53    {
54        mHandler = handler;
55    }
56
57    virtual EncoderError startElement(const char *name, const char **atts) = 0;
58    virtual EncoderError characters(const char *chars, int len) = 0;
59    virtual EncoderError endElement() = 0;
60
61    /**
62     * Reset the encoder so that it may be used again. The WbxmlHandler is
63     * NOT cleared by reset().
64     */
65    virtual void reset() = 0;
66
67    static bool isXmlWhitespace(int ch);
68    static bool parseUint(const char * s, int len, uint32_t *res);
69
70protected:
71    WbxmlHandler * mHandler;
72    int mPublicId;
73
74    EncoderError encodeInteger(const char *chars, int len);
75    EncoderError encodeDatetime(const char *chars, int len);
76    void encodeInlinedStr(const char *s, int len);
77    void encodeMbuint(uint32_t i);
78
79    void clearResult()
80    {
81        mResult.clear();
82    }
83
84    void appendResult(int ch)
85    {
86        mResult += (char)ch;
87    }
88
89    void appendResult(const char *s, int len)
90    {
91        mResult.append(s, len);
92    }
93
94    void sendResult();
95
96    /**
97     * Append a string into the string table, return the index of the string in
98     * the string table.
99     */
100    int appendToStringTable(const char *s);
101
102private:
103    string mResult;
104    vector<string> mStringTable;
105};
106
107#endif
108
109