Move tests
[lttoolbox.git] / lttoolbox / tmx_preprocessor.cc
blob8559bbae6ed6960f056f3b4f6951da7811654274
1 /*
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
17 * 02111-1307, USA.
19 #include <lttoolbox/tmx_preprocessor.h>
20 #include <lttoolbox/lt_locale.h>
21 #include <lttoolbox/xml_parse_util.h>
23 #include <cstdlib>
24 #include <iostream>
25 #include <libxml/encoding.h>
27 using namespace std;
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()
84 void
85 TMXPreprocessor::parse(string const &filename, wstring const &dir)
87 reader = xmlReaderForFile(filename.c_str(), NULL, 0);
88 if(reader == NULL)
90 cerr << "Error: Cannot open '";
91 cerr << filename;
92 cerr << "'." << endl;
93 exit(EXIT_FAILURE);
96 int ret = xmlTextReaderRead(reader);
97 while(ret == 1)
99 procNode();
100 ret = xmlTextReaderRead(reader);
103 if(ret != 0)
105 wcerr << L"Error: Parse error at the end of input." << endl;
108 xmlFreeTextReader(reader);
109 xmlCleanupParser();
112 void
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;
119 exit(EXIT_FAILURE);
123 bool
124 TMXPreprocessor::allBlanks()
126 bool flag = true;
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]);
134 return flag;
137 void
138 TMXPreprocessor::skipBlanks(wstring &name)
140 while(name == L"#text" || name == L"#comment")
142 if(name != L"#comment")
144 if(!allBlanks())
146 wcerr << L"Error (" << xmlTextReaderGetParserLineNumber(reader);
147 wcerr << L"): Invalid construction." << endl;
148 exit(EXIT_FAILURE);
152 xmlTextReaderRead(reader);
153 name = XMLParseUtil::towstring(xmlTextReaderConstName(reader));
157 void
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")
167 if(!allBlanks())
169 wcerr << L"Error (" << xmlTextReaderGetParserLineNumber(reader);
170 wcerr << L"): Invalid construction." << endl;
171 exit(EXIT_FAILURE);
174 xmlTextReaderRead(reader);
175 name = XMLParseUtil::towstring(xmlTextReaderConstName(reader));
178 if(name != elem)
180 wcerr << L"Error (" << xmlTextReaderGetParserLineNumber(reader);
181 wcerr << L"): Expected '<" << elem << L">'." << endl;
182 exit(EXIT_FAILURE);
186 wstring
187 TMXPreprocessor::attrib(wstring const &name)
189 return XMLParseUtil::attrib(reader, name);
192 void
193 TMXPreprocessor::requireAttribute(wstring const &value, wstring const &attrname,
194 wstring const &elemname)
196 if(value == L"")
198 wcerr << L"Error (" << xmlTextReaderGetParserLineNumber(reader);
199 wcerr << L"): '<" << elemname;
200 wcerr << L"' element must specify non-void '";
201 wcerr << attrname << L"' attribute." << endl;
202 exit(EXIT_FAILURE);
206 void
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;
219 else
221 wcerr << L"Error (" << xmlTextReaderGetParserLineNumber(reader);
222 wcerr << L"): Invalid node '<" << nombre << L">'." << endl;
223 exit(EXIT_FAILURE);
227 int main(int argc, char *argv[])
229 TMXPreprocessor tmxp;
230 if(argc == 3)
232 tmxp.parse(argv[1], L"LR");
234 return 0;