2 * Copyright (C) 2005 Universitat d'Alacant / Universidad de Alicante
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * 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
19 #include <lttoolbox/tmx_preprocessor.h>
20 #include <lttoolbox/lt_locale.h>
21 #include <lttoolbox/xml_parse_util.h>
25 #include <libxml/encoding.h>
29 wstring
const TMXPreprocessor::TMX_BODY_ELEM
= L
"body";
30 wstring
const TMXPreprocessor::TMX_HEADER_ELEM
= L
"header";
31 wstring
const TMXPreprocessor::TMX_MAP_ELEM
= L
"map";
32 wstring
const TMXPreprocessor::TMX_NOTE_ELEM
= L
"note";
33 wstring
const TMXPreprocessor::TMX_PROP_ELEM
= L
"prop";
34 wstring
const TMXPreprocessor::TMX_SEG_ELEM
= L
"seg";
35 wstring
const TMXPreprocessor::TMX_TMX_ELEM
= L
"tmx";
36 wstring
const TMXPreprocessor::TMX_TU_ELEM
= L
"tu";
37 wstring
const TMXPreprocessor::TMX_TUV_ELEM
= L
"tuv";
38 wstring
const TMXPreprocessor::TMX_UDE_ELEM
= L
"ude";
39 wstring
const TMXPreprocessor::TMX_BPT_ELEM
= L
"bpt";
40 wstring
const TMXPreprocessor::TMX_EPT_ELEM
= L
"ept";
41 wstring
const TMXPreprocessor::TMX_HI_ELEM
= L
"hi";
42 wstring
const TMXPreprocessor::TMX_IT_ELEM
= L
"it";
43 wstring
const TMXPreprocessor::TMX_PH_ELEM
= L
"ph";
44 wstring
const TMXPreprocessor::TMX_SUB_ELEM
= L
"sub";
45 wstring
const TMXPreprocessor::TMX_UT_ELEM
= L
"ut";
46 wstring
const TMXPreprocessor::TMX_ADMINLANG_ATTR
= L
"adminlang";
47 wstring
const TMXPreprocessor::TMX_ASSOC_ATTR
= L
"assoc";
48 wstring
const TMXPreprocessor::TMX_BASE_ATTR
= L
"base";
49 wstring
const TMXPreprocessor::TMX_CHANGEDATE_ATTR
= L
"changedate";
50 wstring
const TMXPreprocessor::TMX_CHANGEID_ATTR
= L
"changeid";
51 wstring
const TMXPreprocessor::TMX_CODE_ATTR
= L
"code";
52 wstring
const TMXPreprocessor::TMX_CREATIONDATE_ATTR
= L
"creationdate";
53 wstring
const TMXPreprocessor::TMX_CREATIONID_ATTR
= L
"creationid";
54 wstring
const TMXPreprocessor::TMX_CREATIONTOOL_ATTR
= L
"creationtool";
55 wstring
const TMXPreprocessor::TMX_CREATIONTOOLVERSION_ATTR
= L
"creationtoolversion";
56 wstring
const TMXPreprocessor::TMX_DATATYPE_ATTR
= L
"datatype";
57 wstring
const TMXPreprocessor::TMX_ENT_ATTR
= L
"ent";
58 wstring
const TMXPreprocessor::TMX_I_ATTR
= L
"i";
59 wstring
const TMXPreprocessor::TMX_LASTUSAGEDATE_ATTR
= L
"lastusagedate";
60 wstring
const TMXPreprocessor::TMX_NAME_ATTR
= L
"name";
61 wstring
const TMXPreprocessor::TMX_O_ENCODING_ATTR
= L
"o-encoding";
62 wstring
const TMXPreprocessor::TMX_O_TMF_ATTR
= L
"o-tmf";
63 wstring
const TMXPreprocessor::TMX_POS_ATTR
= L
"pos";
64 wstring
const TMXPreprocessor::TMX_SEGTYPE_ATTR
= L
"segtype";
65 wstring
const TMXPreprocessor::TMX_SRCLANG_ATTR
= L
"srclang";
66 wstring
const TMXPreprocessor::TMX_SUBST_ATTR
= L
"subst";
67 wstring
const TMXPreprocessor::TMX_TUID_ATTR
= L
"tuid";
68 wstring
const TMXPreprocessor::TMX_TYPE_ATTR
= L
"type";
69 wstring
const TMXPreprocessor::TMX_UNICODE_ATTR
= L
"unicode";
70 wstring
const TMXPreprocessor::TMX_USAGECOUNT_ATTR
= L
"usagecount";
71 wstring
const TMXPreprocessor::TMX_VERSION_ATTR
= L
"version";
72 wstring
const TMXPreprocessor::TMX_X_ATTR
= L
"x";
73 wstring
const TMXPreprocessor::TMX_XML_LANG_ATTR
= L
"xml:lang";
75 TMXPreprocessor::TMXPreprocessor()
77 LtLocale::tryToSetLocale();
80 TMXPreprocessor::~TMXPreprocessor()
85 TMXPreprocessor::parse(string
const &filename
, wstring
const &dir
)
87 reader
= xmlReaderForFile(filename
.c_str(), NULL
, 0);
90 cerr
<< "Error: Cannot open '";
96 int ret
= xmlTextReaderRead(reader
);
100 ret
= xmlTextReaderRead(reader
);
105 wcerr
<< L
"Error: Parse error at the end of input." << endl
;
108 xmlFreeTextReader(reader
);
113 TMXPreprocessor::requireEmptyError(wstring
const &name
)
115 if(!xmlTextReaderIsEmptyElement(reader
))
117 wcerr
<< L
"Error (" << xmlTextReaderGetParserLineNumber(reader
);
118 wcerr
<< L
"): Non-empty element '<" << name
<< L
">' should be empty." << endl
;
124 TMXPreprocessor::allBlanks()
127 wstring text
= XMLParseUtil::towstring(xmlTextReaderConstValue(reader
));
129 for(unsigned int i
= 0, limit
= text
.size(); i
< limit
; i
++)
131 flag
= flag
&& iswspace(text
[i
]);
138 TMXPreprocessor::skipBlanks(wstring
&name
)
140 while(name
== L
"#text" || name
== L
"#comment")
142 if(name
!= L
"#comment")
146 wcerr
<< L
"Error (" << xmlTextReaderGetParserLineNumber(reader
);
147 wcerr
<< L
"): Invalid construction." << endl
;
152 xmlTextReaderRead(reader
);
153 name
= XMLParseUtil::towstring(xmlTextReaderConstName(reader
));
158 TMXPreprocessor::skip(wstring
&name
, wstring
const &elem
)
160 xmlTextReaderRead(reader
);
161 name
= XMLParseUtil::towstring(xmlTextReaderConstName(reader
));
163 while(name
== L
"#text" || name
== L
"#comment")
165 if(name
!= L
"#comment")
169 wcerr
<< L
"Error (" << xmlTextReaderGetParserLineNumber(reader
);
170 wcerr
<< L
"): Invalid construction." << endl
;
174 xmlTextReaderRead(reader
);
175 name
= XMLParseUtil::towstring(xmlTextReaderConstName(reader
));
180 wcerr
<< L
"Error (" << xmlTextReaderGetParserLineNumber(reader
);
181 wcerr
<< L
"): Expected '<" << elem
<< L
">'." << endl
;
187 TMXPreprocessor::attrib(wstring
const &name
)
189 return XMLParseUtil::attrib(reader
, name
);
193 TMXPreprocessor::requireAttribute(wstring
const &value
, wstring
const &attrname
,
194 wstring
const &elemname
)
198 wcerr
<< L
"Error (" << xmlTextReaderGetParserLineNumber(reader
);
199 wcerr
<< L
"): '<" << elemname
;
200 wcerr
<< L
"' element must specify non-void '";
201 wcerr
<< attrname
<< L
"' attribute." << endl
;
207 TMXPreprocessor::procNode()
209 xmlChar
const *xnombre
= xmlTextReaderConstName(reader
);
210 wstring nombre
= XMLParseUtil::towstring(xnombre
);
212 wcout
<< nombre
<< endl
;
215 if(nombre
== TMX_TMX_ELEM
)
217 wcout
<< nombre
<< endl
;
221 wcerr
<< L
"Error (" << xmlTextReaderGetParserLineNumber(reader
);
222 wcerr
<< L
"): Invalid node '<" << nombre
<< L
">'." << endl
;
227 int main(int argc
, char *argv
[])
229 TMXPreprocessor tmxp
;
232 tmxp
.parse(argv
[1], L
"LR");