1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: XML resource compiler
4 // Author: Vaclav Slavik
7 // Copyright: (c) 2000 Vaclav Slavik
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #if defined(__GNUG__) && !defined(__APPLE__)
12 #pragma implementation
16 // For compilers that support precompilation, includes "wx/wx.h".
17 #include "wx/wxprec.h"
23 // for all others, include the necessary headers (this file is usually all you
24 // need because it includes almost all "standard" wxWindows headers
29 #include "wx/cmdline.h"
30 #include "wx/xml/xml.h"
32 #include "wx/filename.h"
33 #include "wx/wfstream.h"
36 class XmlResApp
: public wxAppConsole
44 void ParseParams(const wxCmdLineParser
& cmdline
);
46 wxArrayString
PrepareTempFiles();
47 void FindFilesInXML(wxXmlNode
*node
, wxArrayString
& flist
, const wxString
& inputPath
);
49 wxString
GetInternalFileName(const wxString
& name
, const wxArrayString
& flist
);
50 void DeleteTempFiles(const wxArrayString
& flist
);
51 void MakePackageZIP(const wxArrayString
& flist
);
52 void MakePackageCPP(const wxArrayString
& flist
);
53 void MakePackagePython(const wxArrayString
& flist
);
56 wxArrayString
FindStrings();
57 wxArrayString
FindStrings(wxXmlNode
*node
);
59 bool flagVerbose
, flagCPP
, flagPython
, flagGettext
;
60 wxString parOutput
, parFuncname
, parOutputPath
;
61 wxArrayString parFiles
;
65 IMPLEMENT_APP(XmlResApp
)
67 int XmlResApp::OnRun()
69 static const wxCmdLineEntryDesc cmdLineDesc
[] =
71 { wxCMD_LINE_SWITCH
, _T("h"), _T("help"), _T("show help message"),
72 wxCMD_LINE_VAL_NONE
, wxCMD_LINE_OPTION_HELP
},
73 { wxCMD_LINE_SWITCH
, _T("v"), _T("verbose"), _T("be verbose") },
74 { wxCMD_LINE_SWITCH
, _T("c"), _T("cpp-code"), _T("output C++ source rather than .rsc file") },
75 { wxCMD_LINE_SWITCH
, _T("p"), _T("python-code"), _T("output wxPython source rather than .rsc file") },
76 { wxCMD_LINE_SWITCH
, _T("g"), _T("gettext"), _T("output list of translatable strings (to stdout or file if -o used)") },
77 { wxCMD_LINE_OPTION
, _T("n"), _T("function"), _T("C++/Python function name (with -c or -p) [InitXmlResource]") },
78 { wxCMD_LINE_OPTION
, _T("o"), _T("output"), _T("output file [resource.xrs/cpp]") },
79 #if 0 // not yet implemented
80 { wxCMD_LINE_OPTION
, _T("l"), _T("list-of-handlers", _T("output list of neccessary handlers to this file" },
82 { wxCMD_LINE_PARAM
, NULL
, NULL
, _T("input file(s)"),
83 wxCMD_LINE_VAL_STRING
,
84 wxCMD_LINE_PARAM_MULTIPLE
| wxCMD_LINE_OPTION_MANDATORY
},
89 wxCmdLineParser
parser(cmdLineDesc
, argc
, argv
);
91 switch (parser
.Parse())
116 void XmlResApp::ParseParams(const wxCmdLineParser
& cmdline
)
118 flagGettext
= cmdline
.Found(_T("g"));
119 flagVerbose
= cmdline
.Found(_T("v"));
120 flagCPP
= cmdline
.Found(_T("c"));
121 flagPython
= cmdline
.Found(_T("p"));
123 if (!cmdline
.Found(_T("o"), &parOutput
))
126 parOutput
= wxEmptyString
;
130 parOutput
= _T("resource.cpp");
132 parOutput
= _T("resource.py");
134 parOutput
= _T("resource.xrs");
137 wxFileName
fn(parOutput
);
139 parOutput
= fn
.GetFullPath();
140 parOutputPath
= wxPathOnly(parOutput
);
141 if (!parOutputPath
) parOutputPath
= _T(".");
143 if (!cmdline
.Found(_T("n"), &parFuncname
))
144 parFuncname
= _T("InitXmlResource");
146 for (size_t i
= 0; i
< cmdline
.GetParamCount(); i
++)
149 wxString fn
=wxFindFirstFile(cmdline
.GetParam(i
), wxFILE
);
150 while (!fn
.IsEmpty())
156 parFiles
.Add(cmdline
.GetParam(i
));
164 void XmlResApp::CompileRes()
166 wxArrayString files
= PrepareTempFiles();
168 wxRemoveFile(parOutput
);
173 MakePackageCPP(files
);
175 MakePackagePython(files
);
177 MakePackageZIP(files
);
180 DeleteTempFiles(files
);
184 wxString
XmlResApp::GetInternalFileName(const wxString
& name
, const wxArrayString
& flist
)
186 wxString name2
= name
;
187 name2
.Replace(_T(":"), _T("_"));
188 name2
.Replace(_T("/"), _T("_"));
189 name2
.Replace(_T("\\"), _T("_"));
190 name2
.Replace(_T("*"), _T("_"));
191 name2
.Replace(_T("?"), _T("_"));
193 wxString s
= wxFileNameFromPath(parOutput
) + _T("$") + name2
;
195 if (wxFileExists(s
) && flist
.Index(s
) == wxNOT_FOUND
)
197 for (int i
= 0;; i
++)
199 s
.Printf(wxFileNameFromPath(parOutput
) + _T("$%03i-") + name2
, i
);
200 if (!wxFileExists(s
) || flist
.Index(s
) != wxNOT_FOUND
)
207 wxArrayString
XmlResApp::PrepareTempFiles()
211 for (size_t i
= 0; i
< parFiles
.Count(); i
++)
214 wxPrintf(_T("processing ") + parFiles
[i
] + _T("...\n"));
218 if (!doc
.Load(parFiles
[i
]))
220 wxLogError(_T("Error parsing file ") + parFiles
[i
]);
225 wxString name
, ext
, path
;
226 wxSplitPath(parFiles
[i
], &path
, &name
, &ext
);
228 FindFilesInXML(doc
.GetRoot(), flist
, path
);
230 wxString internalName
= GetInternalFileName(parFiles
[i
], flist
);
232 doc
.Save(parOutputPath
+ wxFILE_SEP_PATH
+ internalName
);
233 flist
.Add(internalName
);
240 // Does 'node' contain filename information at all?
241 static bool NodeContainsFilename(wxXmlNode
*node
)
244 if (node
->GetName() == _T("bitmap"))
247 // URLs in wxHtmlWindow:
248 if (node
->GetName() == _T("url"))
252 wxXmlNode
*parent
= node
->GetParent();
253 if (parent
!= NULL
&&
254 parent
->GetPropVal(_T("class"), _T("")) == _T("wxBitmapButton") &&
255 (node
->GetName() == _T("focus") ||
256 node
->GetName() == _T("disabled") ||
257 node
->GetName() == _T("selected")))
260 // wxBitmap or wxIcon toplevel resources:
261 if (node
->GetName() == _T("object"))
263 wxString klass
= node
->GetPropVal(_T("class"), wxEmptyString
);
264 if (klass
== _T("wxBitmap") || klass
== _T("wxIcon"))
271 // find all files mentioned in structure, e.g. <bitmap>filename</bitmap>
272 void XmlResApp::FindFilesInXML(wxXmlNode
*node
, wxArrayString
& flist
, const wxString
& inputPath
)
274 // Is 'node' XML node element?
275 if (node
== NULL
) return;
276 if (node
->GetType() != wxXML_ELEMENT_NODE
) return;
278 bool containsFilename
= NodeContainsFilename(node
);
280 wxXmlNode
*n
= node
->GetChildren();
283 if (containsFilename
&&
284 (n
->GetType() == wxXML_TEXT_NODE
||
285 n
->GetType() == wxXML_CDATA_SECTION_NODE
))
288 if (wxIsAbsolutePath(n
->GetContent()) || inputPath
.empty())
289 fullname
= n
->GetContent();
291 fullname
= inputPath
+ wxFILE_SEP_PATH
+ n
->GetContent();
294 wxPrintf(_T("adding ") + fullname
+ _T("...\n"));
296 wxString filename
= GetInternalFileName(n
->GetContent(), flist
);
297 n
->SetContent(filename
);
299 if (flist
.Index(filename
) == wxNOT_FOUND
)
302 wxFileInputStream
sin(fullname
);
303 wxFileOutputStream
sout(parOutputPath
+ wxFILE_SEP_PATH
+ filename
);
304 sin
.Read(sout
); // copy the stream
308 if (n
->GetType() == wxXML_ELEMENT_NODE
)
309 FindFilesInXML(n
, flist
, inputPath
);
317 void XmlResApp::DeleteTempFiles(const wxArrayString
& flist
)
319 for (size_t i
= 0; i
< flist
.Count(); i
++)
320 wxRemoveFile(parOutputPath
+ wxFILE_SEP_PATH
+ flist
[i
]);
325 void XmlResApp::MakePackageZIP(const wxArrayString
& flist
)
329 for (size_t i
= 0; i
< flist
.Count(); i
++)
330 files
+= flist
[i
] + _T(" ");
334 wxPrintf(_T("compressing ") + parOutput
+ _T("...\n"));
336 wxString cwd
= wxGetCwd();
337 wxSetWorkingDirectory(parOutputPath
);
338 int execres
= wxExecute(_T("zip -9 -j ") +
339 wxString(flagVerbose
? _T("") : _T("-q ")) +
340 parOutput
+ _T(" ") + files
, TRUE
);
341 wxSetWorkingDirectory(cwd
);
344 wxLogError(_T("Unable to execute zip program. Make sure it is in the path."));
345 wxLogError(_T("You can download it at http://www.cdrom.com/pub/infozip/"));
354 static wxString
FileToCppArray(wxString filename
, int num
)
359 wxFFile
file(filename
, wxT("rb"));
360 size_t lng
= file
.Length();
362 snum
.Printf(_T("%i"), num
);
363 output
.Printf(_T("static size_t xml_res_size_") + snum
+ _T(" = %i;\n"), lng
);
364 output
+= _T("static unsigned char xml_res_file_") + snum
+ _T("[] = {\n");
365 // we cannot use string literals because MSVC is dumb wannabe compiler
366 // with arbitrary limitation to 2048 strings :(
368 unsigned char *buffer
= new unsigned char[lng
];
369 file
.Read(buffer
, lng
);
371 for (size_t i
= 0, linelng
= 0; i
< lng
; i
++)
373 tmp
.Printf(_T("%i"), buffer
[i
]);
374 if (i
!= 0) output
<< _T(',');
381 linelng
+= tmp
.Length()+1;
386 output
+= _T("};\n\n");
392 void XmlResApp::MakePackageCPP(const wxArrayString
& flist
)
394 wxFFile
file(parOutput
, wxT("wt"));
398 wxPrintf(_T("creating C++ source file ") + parOutput
+ _T("...\n"));
402 _T("// This file was automatically generated by wxrc, do not edit by hand.\n")
404 _T("#include <wx/wxprec.h>\n")
406 _T("#ifdef __BORLANDC__\n")
407 _T(" #pragma hdrstop\n")
410 _T("#ifndef WX_PRECOMP\n")
411 _T(" #include <wx/wx.h>\n")
414 _T("#include <wx/filesys.h>\n")
415 _T("#include <wx/fs_mem.h>\n")
416 _T("#include <wx/xrc/xmlres.h>\n")
417 _T("#include <wx/xrc/xh_all.h>\n")
420 for (i
= 0; i
< flist
.Count(); i
++)
422 FileToCppArray(parOutputPath
+ wxFILE_SEP_PATH
+ flist
[i
], i
));
425 _T("void ") + parFuncname
+ wxT("()\n")
428 _T(" // Check for memory FS. If not present, load the handler:\n")
430 _T(" wxMemoryFSHandler::AddFile(wxT(\"XRC_resource/dummy_file\"), wxT(\"dummy one\"));\n")
431 _T(" wxFileSystem fsys;\n")
432 _T(" wxFSFile *f = fsys.OpenFile(wxT(\"memory:XRC_resource/dummy_file\"));\n")
433 _T(" wxMemoryFSHandler::RemoveFile(wxT(\"XRC_resource/dummy_file\"));\n")
434 _T(" if (f) delete f;\n")
435 _T(" else wxFileSystem::AddHandler(new wxMemoryFSHandler);\n")
439 for (i
= 0; i
< flist
.Count(); i
++)
442 s
.Printf(_T(" wxMemoryFSHandler::AddFile(wxT(\"XRC_resource/") + flist
[i
] +
443 _T("\"), xml_res_file_%i, xml_res_size_%i);\n"), i
, i
);
447 for (i
= 0; i
< parFiles
.Count(); i
++)
449 file
.Write(_T(" wxXmlResource::Get()->Load(wxT(\"memory:XRC_resource/") +
450 GetInternalFileName(parFiles
[i
], flist
) + _T("\"));\n"));
453 file
.Write(_T("}\n"));
458 static wxString
FileToPythonArray(wxString filename
, int num
)
463 wxFFile
file(filename
, wxT("rb"));
464 size_t lng
= file
.Length();
466 snum
.Printf(_T("%i"), num
);
467 output
= _T(" xml_res_file_") + snum
+ _T(" = \"\"\"\\\n");
469 unsigned char *buffer
= new unsigned char[lng
];
470 file
.Read(buffer
, lng
);
472 for (size_t i
= 0, linelng
= 0; i
< lng
; i
++)
474 unsigned char c
= buffer
[i
];
480 else if (c
< 32 || c
> 127)
481 tmp
.Printf(_T("\\x%02x"), c
);
489 output
<< _T("\\\n");
492 linelng
+= tmp
.Length();
497 output
+= _T("\"\"\"\n\n");
503 void XmlResApp::MakePackagePython(const wxArrayString
& flist
)
505 wxFFile
file(parOutput
, wxT("wt"));
509 wxPrintf(_T("creating Python source file ") + parOutput
+ _T("...\n"));
513 _T("# This file was automatically generated by wxrc, do not edit by hand.\n")
515 _T("from wxPython.wx import *\n")
516 _T("from wxPython.xrc import *\n\n")
520 file
.Write(_T("def ") + parFuncname
+ _T("():\n"));
522 for (i
= 0; i
< flist
.Count(); i
++)
524 FileToPythonArray(parOutputPath
+ wxFILE_SEP_PATH
+ flist
[i
], i
));
526 for (i
= 0; i
< flist
.Count(); i
++)
529 s
.Printf(_T(" wxXmlResource_Get().LoadFromString(xml_res_file_%i)\n"), i
);
536 void XmlResApp::OutputGettext()
538 wxArrayString str
= FindStrings();
541 if (!parOutput
) fout
.Attach(stdout
);
542 else fout
.Open(parOutput
, wxT("wt"));
544 for (size_t i
= 0; i
< str
.GetCount(); i
++)
545 fout
.Write(_T("_(\"") + str
[i
] + _T("\");\n"));
547 if (!parOutput
) fout
.Detach();
552 wxArrayString
XmlResApp::FindStrings()
554 wxArrayString arr
, a2
;
556 for (size_t i
= 0; i
< parFiles
.Count(); i
++)
559 wxPrintf(_T("processing ") + parFiles
[i
] + _T("...\n"));
562 if (!doc
.Load(parFiles
[i
]))
564 wxLogError(_T("Error parsing file ") + parFiles
[i
]);
568 a2
= FindStrings(doc
.GetRoot());
569 WX_APPEND_ARRAY(arr
, a2
);
577 static wxString
ConvertText(const wxString
& str
)
582 for (dt
= str
.c_str(); *dt
; dt
++)
586 if ( *(++dt
) == wxT('_') )
589 str2
<< wxT('&') << *dt
;
595 case wxT('\n') : str2
<< wxT("\\n"); break;
596 case wxT('\t') : str2
<< wxT("\\t"); break;
597 case wxT('\r') : str2
<< wxT("\\r"); break;
598 case wxT('\\') : if ((*(dt
+1) != 'n') &&
605 case wxT('"') : str2
<< wxT("\\\""); break;
606 default : str2
<< *dt
; break;
615 wxArrayString
XmlResApp::FindStrings(wxXmlNode
*node
)
620 if (n
== NULL
) return arr
;
621 n
= n
->GetChildren();
625 if ((node
->GetType() == wxXML_ELEMENT_NODE
) &&
626 // parent is an element, i.e. has subnodes...
627 (n
->GetType() == wxXML_TEXT_NODE
||
628 n
->GetType() == wxXML_CDATA_SECTION_NODE
) &&
629 // ...it is textnode...
631 node
/*not n!*/->GetName() == _T("label") ||
632 (node
/*not n!*/->GetName() == _T("value") &&
633 !n
->GetContent().IsNumber()) ||
634 node
/*not n!*/->GetName() == _T("help") ||
635 node
/*not n!*/->GetName() == _T("longhelp") ||
636 node
/*not n!*/->GetName() == _T("tooltip") ||
637 node
/*not n!*/->GetName() == _T("htmlcode") ||
638 node
/*not n!*/->GetName() == _T("title") ||
639 node
/*not n!*/->GetName() == _T("item")
641 // ...and known to contain translatable string
643 arr
.Add(ConvertText(n
->GetContent()));
647 if (n
->GetType() == wxXML_ELEMENT_NODE
)
649 wxArrayString a2
= FindStrings(n
);
650 WX_APPEND_ARRAY(arr
, a2
);