wml_compiler.c
资源名称:gateway-1.2.1 [点击查看]
上传用户:gzpyjq
上传日期:2013-01-31
资源大小:1852k
文件大小:44k
源码类别:
手机WAP编程
开发平台:
WINDOWS
- /*
- * wml_compiler.c - compiling WML to WML binary
- *
- * This is an implemention for WML compiler for compiling the WML text
- * format to WML binary format, which is used for transmitting the
- * decks to the mobile terminal to decrease the use of the bandwidth.
- *
- *
- * Tuomas Luttinen for Wapit Ltd.
- */
- #include "config.h"
- #include <time.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <string.h>
- #include <math.h>
- #include <ctype.h>
- #include <libxml/xmlmemory.h>
- #include <libxml/tree.h>
- #include <libxml/debugXML.h>
- #include <libxml/encoding.h>
- #include "gwlib/gwlib.h"
- #include "wml_compiler.h"
- #include "xml_definitions.h"
- /***********************************************************************
- * Declarations of data types.
- */
- typedef enum { NOESC, ESC, UNESC, FAILED } var_esc_t;
- /*
- * The wml token table node with two fields.
- */
- typedef struct {
- char *text;
- unsigned char token;
- } wml_table_t;
- /*
- * The wml token table node with three fields.
- */
- typedef struct {
- char *text1;
- char *text2;
- unsigned char token;
- } wml_table3_t;
- /*
- * The binary WML structure, that has been passed around between the
- * internal functions. It contains the header fields for wbxml version,
- * the WML public ID and the character set, the length of the string table,
- * the list structure implementing the string table and the octet string
- * containing the encoded WML binary.
- */
- typedef struct {
- unsigned char wbxml_version;
- unsigned char wml_public_id;
- unsigned long character_set;
- unsigned long string_table_length;
- List *string_table;
- Octstr *wbxml_string;
- } wml_binary_t;
- /*
- * The string table list node.
- */
- typedef struct {
- unsigned long offset;
- Octstr *string;
- } string_table_t;
- /*
- * The string table proposal list node.
- */
- typedef struct {
- int count;
- Octstr *string;
- } string_table_proposal_t;
- /*
- * The wml hash table node.
- */
- typedef struct {
- Octstr *item;
- unsigned char binary;
- } wml_hash_t;
- /*
- * The hash table node for attribute and values.
- */
- typedef struct {
- Octstr *attribute;
- unsigned char binary;
- List *value_list;
- } wml_attribute_t;
- #include "xml_shared.h"
- #include "wml_definitions.h"
- /***********************************************************************
- * Declarations of global variables.
- */
- Dict *wml_elements_dict;
- Dict *wml_attributes_dict;
- List *wml_attr_values_list;
- List *wml_URL_values_list;
- /***********************************************************************
- * Declarations of internal functions. These are defined at the end of
- * the file.
- */
- /*
- * Parsing functions. These funtions operate on a single node or a
- * smaller datatype. Look for more details on the functions at the
- * definitions.
- */
- static int parse_document(xmlDocPtr document, Octstr *charset,
- wml_binary_t **wbxml);
- static int parse_node(xmlNodePtr node, wml_binary_t **wbxml);
- static int parse_element(xmlNodePtr node, wml_binary_t **wbxml);
- static int parse_attribute(xmlAttrPtr attr, wml_binary_t **wbxml);
- static int parse_attr_value(Octstr *attr_value, List *tokens,
- wml_binary_t **wbxml);
- static int parse_text(xmlNodePtr node, wml_binary_t **wbxml);
- static int parse_cdata(xmlNodePtr node, wml_binary_t **wbxml);
- static int parse_st_octet_string(Octstr *ostr, int cdata, wml_binary_t **wbxml);
- static void parse_st_end(wml_binary_t **wbxml);
- static void parse_entities(Octstr *wml_source);
- /*
- * Variable functions. These functions are used to find and parse variables.
- */
- static int parse_variable(Octstr *text, int start, Octstr **output,
- wml_binary_t **wbxml);
- static Octstr *get_variable(Octstr *text, int start);
- static var_esc_t check_variable_syntax(Octstr *variable);
- /*
- * wml_binary-functions. These are used to create, destroy and modify
- * wml_binary_t.
- */
- static wml_binary_t *wml_binary_create(void);
- static void wml_binary_destroy(wml_binary_t *wbxml);
- static void wml_binary_output(Octstr *ostr, wml_binary_t *wbxml);
- /* Output into the wml_binary. */
- static void output_st_char(int byte, wml_binary_t **wbxml);
- static void output_st_octet_string(Octstr *ostr, wml_binary_t **wbxml);
- static void output_variable(Octstr *variable, Octstr **output,
- var_esc_t escaped, wml_binary_t **wbxml);
- /*
- * Memory allocation and deallocations.
- */
- static wml_hash_t *hash_create(char *text, unsigned char token);
- static wml_attribute_t *attribute_create(void);
- static void attr_dict_construct(wml_table3_t *attributes, Dict *attr_dict);
- static void hash_destroy(void *p);
- static void attribute_destroy(void *p);
- /*
- * Comparison functions for the hash tables.
- */
- static int hash_cmp(void *hash1, void *hash2);
- /*
- * Miscellaneous help functions.
- */
- static int check_do_elements(xmlNodePtr node);
- static var_esc_t check_variable_name(xmlNodePtr node);
- static Octstr *get_do_element_name(xmlNodePtr node);
- static int check_if_url(int hex);
- static int check_if_emphasis(xmlNodePtr node);
- static int wml_table_len(wml_table_t *table);
- static int wml_table3_len(wml_table3_t *table);
- /*
- * String table functions, used to add and remove strings into and from the
- * string table.
- */
- static string_table_t *string_table_create(int offset, Octstr *ostr);
- static void string_table_destroy(string_table_t *node);
- static string_table_proposal_t *string_table_proposal_create(Octstr *ostr);
- static void string_table_proposal_destroy(string_table_proposal_t *node);
- static void string_table_build(xmlNodePtr node, wml_binary_t **wbxml);
- static void string_table_collect_strings(xmlNodePtr node, List *strings);
- static List *string_table_collect_words(List *strings);
- static List *string_table_sort_list(List *start);
- static List *string_table_add_many(List *sorted, wml_binary_t **wbxml);
- static unsigned long string_table_add(Octstr *ostr, wml_binary_t **wbxml);
- static void string_table_apply(Octstr *ostr, wml_binary_t **wbxml);
- static void string_table_output(Octstr *ostr, wml_binary_t **wbxml);
- /***********************************************************************
- * Implementations of the functions declared in wml_compiler.h.
- */
- /*
- * The actual compiler function. This operates as interface to the compiler.
- * For more information, look wml_compiler.h.
- */
- int wml_compile(Octstr *wml_text,
- Octstr *charset,
- Octstr **wml_binary)
- {
- int ret = 0;
- size_t size;
- xmlDocPtr pDoc = NULL;
- char *wml_c_text;
- wml_binary_t *wbxml = NULL;
- *wml_binary = octstr_create("");
- wbxml = wml_binary_create();
- /* Remove the extra space from start and the end of the WML Document. */
- octstr_strip_blanks(wml_text);
- /* Check the WML-code for