2 * Copyright (C) 2007 Francis Tyers
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <transliterate.h>
22 #include <XMLParseUtil.h>
28 wstring
const Transliterator::TRANSLITERATOR_TRANSLITERATOR_ELEM
= L
"transliterator";
29 wstring
const Transliterator::TRANSLITERATOR_TABLE_ELEM
= L
"table";
30 wstring
const Transliterator::TRANSLITERATOR_TABLE_NAME_ATTR
= L
"name";
31 wstring
const Transliterator::TRANSLITERATOR_TRANSFORM_ELEM
= L
"transform";
33 wstring
const Transliterator::TRANSLITERATOR_LEFT_ATTR
= L
"left";
34 wstring
const Transliterator::TRANSLITERATOR_RIGHT_ATTR
= L
"right";
36 Transliterator::Transliterator(string
const &file
, string
const &_direction
, wstring
const &_name
)
38 init(file
, _direction
, _name
);
41 Transliterator::Transliterator(string
const &file
, string
const &_direction
)
43 wstring
const _name
= L
"default";
44 init(file
, _direction
, _name
);
47 Transliterator::Transliterator(string
const &file
)
49 wstring
const _name
= L
"default";
50 string
const _direction
= "lr";
51 init(file
, _direction
, _name
);
54 void Transliterator::init(string
const &file
, string
const &_direction
, wstring
const &_name
)
56 reader
= xmlReaderForFile(file
.c_str(), NULL
, 0);
58 direction
= _direction
;
62 int ret
= xmlTextReaderRead(reader
);
66 ret
= xmlTextReaderRead(reader
);
70 wcerr
<< L
"Error: Parse error at the end of input." << endl
;
73 xmlFreeTextReader(reader
);
77 Transliterator::~Transliterator()
79 // wcout << L"--" << endl << "~Transliterator()" << endl;
83 * Transliterate a string.
86 Transliterator::transliterate(wstring in
)
91 // iterate through the string 1 char at a time.
92 for(unsigned int i
= 0; i
< in
.length(); i
++) {
96 // iterate through the transliteration table
97 for(unsigned int j
= 0; j
< table
.size(); j
++) {
98 Entry e
= table
.at(j
);
100 if(direction
== "rl") {
101 if(in
[i
] == (unsigned int)(e
.right())[0]) {
105 if(in
[i
] == (unsigned int)(e
.left())[0]) {
118 Transliterator::process_node(void)
120 wstring nombre
= XMLParseUtil::towstring(xmlTextReaderConstName(reader
));
121 // this needs to be static as it must remain through all calls.
122 wstring
static table_name
= L
"";
124 if(nombre
== L
"#text") {
126 } else if(nombre
== TRANSLITERATOR_TRANSLITERATOR_ELEM
) {
127 // wcout << L"Transliterator" << endl;
128 } else if(nombre
== TRANSLITERATOR_TABLE_ELEM
) {
129 table_name
= XMLParseUtil::attrib(reader
, TRANSLITERATOR_TABLE_NAME_ATTR
);
130 // wcout << L" Table " << table_name << endl;
131 } else if(nombre
== TRANSLITERATOR_TRANSFORM_ELEM
) {
133 if(table_name
== name
) {
134 wstring
const &left
= XMLParseUtil::attrib(reader
, TRANSLITERATOR_LEFT_ATTR
);
135 wstring
const &right
= XMLParseUtil::attrib(reader
, TRANSLITERATOR_RIGHT_ATTR
);
136 // wcout << L" Replace " << left << L" --> " << right << endl;
138 Entry e
= Entry(left
, right
);
139 iter
= table
.insert(iter
, e
);
141 } else if(nombre
== L
"#comment") {
144 wcerr
<< "Error (" << xmlTextReaderGetParserLineNumber(reader
);
145 wcerr
<< "): Invalid node '<" << nombre
<< ">'." << endl
;