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/xrc/xml.h"
32 #include "wx/filename.h"
33 #include "wx/wfstream.h"
41 #error "You must compile the resource compiler with wxBase!"
45 class XmlResApp
: public wxApp
57 void ParseParams(const wxCmdLineParser
& cmdline
);
59 wxArrayString
PrepareTempFiles();
60 void FindFilesInXML(wxXmlNode
*node
, wxArrayString
& flist
, const wxString
& inputPath
);
62 wxString
GetInternalFileName(const wxString
& name
, const wxArrayString
& flist
);
63 void DeleteTempFiles(const wxArrayString
& flist
);
64 void MakePackageZIP(const wxArrayString
& flist
);
65 void MakePackageCPP(const wxArrayString
& flist
);
66 void MakePackagePython(const wxArrayString
& flist
);
69 wxArrayString
FindStrings();
70 wxArrayString
FindStrings(wxXmlNode
*node
);
72 bool flagVerbose
, flagCPP
, flagPython
, flagGettext
;
73 wxString parOutput
, parFuncname
, parOutputPath
;
74 wxArrayString parFiles
;
78 IMPLEMENT_APP(XmlResApp
)
81 bool XmlResApp::OnInit()
83 int XmlResApp::OnRun()
86 static const wxCmdLineEntryDesc cmdLineDesc
[] =
88 { wxCMD_LINE_SWITCH
, _T("h"), _T("help"), _T("show help message"),
89 wxCMD_LINE_VAL_NONE
, wxCMD_LINE_OPTION_HELP
},
90 { wxCMD_LINE_SWITCH
, _T("v"), _T("verbose"), _T("be verbose") },
91 { wxCMD_LINE_SWITCH
, _T("c"), _T("cpp-code"), _T("output C++ source rather than .rsc file") },
92 { wxCMD_LINE_SWITCH
, _T("p"), _T("python-code"), _T("output wxPython source rather than .rsc file") },
93 { wxCMD_LINE_SWITCH
, _T("g"), _T("gettext"), _T("output list of translatable strings (to stdout or file if -o used)") },
94 { wxCMD_LINE_OPTION
, _T("n"), _T("function"), _T("C++/Python function name (with -c or -p) [InitXmlResource]") },
95 { wxCMD_LINE_OPTION
, _T("o"), _T("output"), _T("output file [resource.xrs/cpp]") },
96 #if 0 // not yet implemented
97 { wxCMD_LINE_OPTION
, _T("l"), _T("list-of-handlers", _T("output list of neccessary handlers to this file" },
99 { wxCMD_LINE_PARAM
, NULL
, NULL
, _T("input file(s)"),
100 wxCMD_LINE_VAL_STRING
,
101 wxCMD_LINE_PARAM_MULTIPLE
| wxCMD_LINE_OPTION_MANDATORY
},
106 wxCmdLineParser
parser(cmdLineDesc
, argc
, argv
);
108 switch (parser
.Parse())
141 void XmlResApp::ParseParams(const wxCmdLineParser
& cmdline
)
143 flagGettext
= cmdline
.Found(_T("g"));
144 flagVerbose
= cmdline
.Found(_T("v"));
145 flagCPP
= cmdline
.Found(_T("c"));
146 flagPython
= cmdline
.Found(_T("p"));
148 if (!cmdline
.Found(_T("o"), &parOutput
))
151 parOutput
= wxEmptyString
;
155 parOutput
= _T("resource.cpp");
157 parOutput
= _T("resource.py");
159 parOutput
= _T("resource.xrs");
162 wxFileName
fn(parOutput
);
164 parOutput
= fn
.GetFullPath();
165 parOutputPath
= wxPathOnly(parOutput
);
166 if (!parOutputPath
) parOutputPath
= _T(".");
168 if (!cmdline
.Found(_T("n"), &parFuncname
))
169 parFuncname
= _T("InitXmlResource");
171 for (size_t i
= 0; i
< cmdline
.GetParamCount(); i
++)
172 parFiles
.Add(cmdline
.GetParam(i
));
178 void XmlResApp::CompileRes()
180 wxArrayString files
= PrepareTempFiles();
182 wxRemoveFile(parOutput
);
187 MakePackageCPP(files
);
189 MakePackagePython(files
);
191 MakePackageZIP(files
);
194 DeleteTempFiles(files
);
198 wxString
XmlResApp::GetInternalFileName(const wxString
& name
, const wxArrayString
& flist
)
200 wxString name2
= name
;
201 name2
.Replace(_T(":"), _T("_"));
202 name2
.Replace(_T("/"), _T("_"));
203 name2
.Replace(_T("\\"), _T("_"));
204 name2
.Replace(_T("*"), _T("_"));
205 name2
.Replace(_T("?"), _T("_"));
207 wxString s
= wxFileNameFromPath(parOutput
) + _T("$") + name2
;
209 if (wxFileExists(s
) && flist
.Index(s
) == wxNOT_FOUND
)
211 for (int i
= 0;; i
++)
213 s
.Printf(wxFileNameFromPath(parOutput
) + _T("$%03i-") + name2
, i
);
214 if (!wxFileExists(s
) || flist
.Index(s
) != wxNOT_FOUND
)
221 wxArrayString
XmlResApp::PrepareTempFiles()
225 for (size_t i
= 0; i
< parFiles
.Count(); i
++)
228 wxPrintf(_T("processing ") + parFiles
[i
] + _T("...\n"));
232 if (!doc
.Load(parFiles
[i
]))
234 wxLogError(_T("Error parsing file ") + parFiles
[i
]);
239 wxString name
, ext
, path
;
240 wxSplitPath(parFiles
[i
], &path
, &name
, &ext
);
242 FindFilesInXML(doc
.GetRoot(), flist
, path
);
244 wxString internalName
= GetInternalFileName(parFiles
[i
], flist
);
246 doc
.Save(parOutputPath
+ wxFILE_SEP_PATH
+ internalName
);
247 flist
.Add(internalName
);
254 // Does 'node' contain filename information at all?
255 static bool NodeContainsFilename(wxXmlNode
*node
)
258 if (node
->GetName() == _T("bitmap"))
261 // URLs in wxHtmlWindow:
262 if (node
->GetName() == _T("url"))
266 wxXmlNode
*parent
= node
->GetParent();
267 if (parent
!= NULL
&&
268 parent
->GetPropVal(_T("class"), _T("")) == _T("wxBitmapButton") &&
269 (node
->GetName() == _T("focus") ||
270 node
->GetName() == _T("disabled") ||
271 node
->GetName() == _T("selected")))
274 // wxBitmap or wxIcon toplevel resources:
275 if (node
->GetName() == _T("object"))
277 wxString klass
= node
->GetPropVal(_T("class"), wxEmptyString
);
278 if (klass
== _T("wxBitmap") || klass
== _T("wxIcon"))
285 // find all files mentioned in structure, e.g. <bitmap>filename</bitmap>
286 void XmlResApp::FindFilesInXML(wxXmlNode
*node
, wxArrayString
& flist
, const wxString
& inputPath
)
288 // Is 'node' XML node element?
289 if (node
== NULL
) return;
290 if (node
->GetType() != wxXML_ELEMENT_NODE
) return;
292 bool containsFilename
= NodeContainsFilename(node
);
294 wxXmlNode
*n
= node
->GetChildren();
297 if (containsFilename
&&
298 (n
->GetType() == wxXML_TEXT_NODE
||
299 n
->GetType() == wxXML_CDATA_SECTION_NODE
))
302 if (wxIsAbsolutePath(n
->GetContent()) || inputPath
.empty())
303 fullname
= n
->GetContent();
305 fullname
= inputPath
+ wxFILE_SEP_PATH
+ n
->GetContent();
308 wxPrintf(_T("adding ") + fullname
+ _T("...\n"));
310 wxString filename
= GetInternalFileName(n
->GetContent(), flist
);
311 n
->SetContent(filename
);
313 if (flist
.Index(filename
) == wxNOT_FOUND
)
316 wxFileInputStream
sin(fullname
);
317 wxFileOutputStream
sout(parOutputPath
+ wxFILE_SEP_PATH
+ filename
);
318 sin
.Read(sout
); // copy the stream
322 if (n
->GetType() == wxXML_ELEMENT_NODE
)
323 FindFilesInXML(n
, flist
, inputPath
);
331 void XmlResApp::DeleteTempFiles(const wxArrayString
& flist
)
333 for (size_t i
= 0; i
< flist
.Count(); i
++)
334 wxRemoveFile(parOutputPath
+ wxFILE_SEP_PATH
+ flist
[i
]);
339 void XmlResApp::MakePackageZIP(const wxArrayString
& flist
)
343 for (size_t i
= 0; i
< flist
.Count(); i
++)
344 files
+= flist
[i
] + _T(" ");
348 wxPrintf(_T("compressing ") + parOutput
+ _T("...\n"));
350 wxString cwd
= wxGetCwd();
351 wxSetWorkingDirectory(parOutputPath
);
352 int execres
= wxExecute(_T("zip -9 -j ") +
353 wxString(flagVerbose
? _T("") : _T("-q ")) +
354 parOutput
+ _T(" ") + files
, TRUE
);
355 wxSetWorkingDirectory(cwd
);
358 wxLogError(_T("Unable to execute zip program. Make sure it is in the path."));
359 wxLogError(_T("You can download it at http://www.cdrom.com/pub/infozip/"));
368 static wxString
FileToCppArray(wxString filename
, int num
)
373 wxFFile
file(filename
, wxT("rb"));
374 size_t lng
= file
.Length();
376 snum
.Printf(_T("%i"), num
);
377 output
.Printf(_T("static size_t xml_res_size_") + snum
+ _T(" = %i;\n"), lng
);
378 output
+= _T("static unsigned char xml_res_file_") + snum
+ _T("[] = {\n");
379 // we cannot use string literals because MSVC is dumb wannabe compiler
380 // with arbitrary limitation to 2048 strings :(
382 unsigned char *buffer
= new unsigned char[lng
];
383 file
.Read(buffer
, lng
);
385 for (size_t i
= 0, linelng
= 0; i
< lng
; i
++)
387 tmp
.Printf(_T("%i"), buffer
[i
]);
388 if (i
!= 0) output
<< _T(',');
395 linelng
+= tmp
.Length()+1;
400 output
+= _T("};\n\n");
406 void XmlResApp::MakePackageCPP(const wxArrayString
& flist
)
408 wxFFile
file(parOutput
, wxT("wt"));
412 wxPrintf(_T("creating C++ source file ") + parOutput
+ _T("...\n"));
416 _T("// This file was automatically generated by wxrc, do not edit by hand.\n")
418 _T("#include <wx/wxprec.h>\n")
420 _T("#ifdef __BORLANDC__\n")
421 _T(" #pragma hdrstop\n")
424 _T("#ifndef WX_PRECOMP\n")
425 _T(" #include <wx/wx.h>\n")
428 _T("#include <wx/filesys.h>\n")
429 _T("#include <wx/fs_mem.h>\n")
430 _T("#include <wx/xrc/xmlres.h>\n")
431 _T("#include <wx/xrc/xh_all.h>\n")
434 for (i
= 0; i
< flist
.Count(); i
++)
436 FileToCppArray(parOutputPath
+ wxFILE_SEP_PATH
+ flist
[i
], i
));
439 _T("void ") + parFuncname
+ wxT("()\n")
442 _T(" // Check for memory FS. If not present, load the handler:\n")
444 _T(" wxMemoryFSHandler::AddFile(wxT(\"XRC_resource/dummy_file\"), wxT(\"dummy one\"));\n")
445 _T(" wxFileSystem fsys;\n")
446 _T(" wxFSFile *f = fsys.OpenFile(wxT(\"memory:XRC_resource/dummy_file\"));\n")
447 _T(" wxMemoryFSHandler::RemoveFile(wxT(\"XRC_resource/dummy_file\"));\n")
448 _T(" if (f) delete f;\n")
449 _T(" else wxFileSystem::AddHandler(new wxMemoryFSHandler);\n")
453 for (i
= 0; i
< flist
.Count(); i
++)
456 s
.Printf(_T(" wxMemoryFSHandler::AddFile(wxT(\"XRC_resource/") + flist
[i
] +
457 _T("\"), xml_res_file_%i, xml_res_size_%i);\n"), i
, i
);
461 for (i
= 0; i
< parFiles
.Count(); i
++)
463 file
.Write(_T(" wxXmlResource::Get()->Load(wxT(\"memory:XRC_resource/") +
464 GetInternalFileName(parFiles
[i
], flist
) + _T("\"));\n"));
467 file
.Write(_T("}\n"));
472 static wxString
FileToPythonArray(wxString filename
, int num
)
477 wxFFile
file(filename
, wxT("rb"));
478 size_t lng
= file
.Length();
480 snum
.Printf(_T("%i"), num
);
481 output
= _T(" xml_res_file_") + snum
+ _T(" = \"\"\"\\\n");
483 unsigned char *buffer
= new unsigned char[lng
];
484 file
.Read(buffer
, lng
);
486 for (size_t i
= 0, linelng
= 0; i
< lng
; i
++)
488 unsigned char c
= buffer
[i
];
494 else if (c
< 32 || c
> 127)
495 tmp
.Printf(_T("\\x%02x"), c
);
503 output
<< _T("\\\n");
506 linelng
+= tmp
.Length();
511 output
+= _T("\"\"\"\n\n");
517 void XmlResApp::MakePackagePython(const wxArrayString
& flist
)
519 wxFFile
file(parOutput
, wxT("wt"));
523 wxPrintf(_T("creating Python source file ") + parOutput
+ _T("...\n"));
527 _T("# This file was automatically generated by wxrc, do not edit by hand.\n")
529 _T("from wxPython.wx import *\n")
530 _T("from wxPython.xrc import *\n\n")
534 file
.Write(_T("def ") + parFuncname
+ _T("():\n"));
536 for (i
= 0; i
< flist
.Count(); i
++)
538 FileToPythonArray(parOutputPath
+ wxFILE_SEP_PATH
+ flist
[i
], i
));
540 for (i
= 0; i
< flist
.Count(); i
++)
543 s
.Printf(_T(" wxXmlResource_Get().LoadFromString(xml_res_file_%i)\n"), i
);
550 void XmlResApp::OutputGettext()
552 wxArrayString str
= FindStrings();
555 if (!parOutput
) fout
.Attach(stdout
);
556 else fout
.Open(parOutput
, wxT("wt"));
558 for (size_t i
= 0; i
< str
.GetCount(); i
++)
559 fout
.Write(_T("_(\"") + str
[i
] + _T("\");\n"));
561 if (!parOutput
) fout
.Detach();
566 wxArrayString
XmlResApp::FindStrings()
568 wxArrayString arr
, a2
;
570 for (size_t i
= 0; i
< parFiles
.Count(); i
++)
573 wxPrintf(_T("processing ") + parFiles
[i
] + _T("...\n"));
576 if (!doc
.Load(parFiles
[i
]))
578 wxLogError(_T("Error parsing file ") + parFiles
[i
]);
582 a2
= FindStrings(doc
.GetRoot());
583 WX_APPEND_ARRAY(arr
, a2
);
591 static wxString
ConvertText(const wxString
& str
)
596 for (dt
= str
.c_str(); *dt
; dt
++)
600 if ( *(++dt
) == wxT('_') )
603 str2
<< wxT('&') << *dt
;
609 case wxT('\n') : str2
<< wxT("\\n"); break;
610 case wxT('\t') : str2
<< wxT("\\t"); break;
611 case wxT('\r') : str2
<< wxT("\\r"); break;
612 case wxT('\\') : if ((*(dt
+1) != 'n') &&
619 case wxT('"') : str2
<< wxT("\\\""); break;
620 default : str2
<< *dt
; break;
629 wxArrayString
XmlResApp::FindStrings(wxXmlNode
*node
)
634 if (n
== NULL
) return arr
;
635 n
= n
->GetChildren();
639 if ((node
->GetType() == wxXML_ELEMENT_NODE
) &&
640 // parent is an element, i.e. has subnodes...
641 (n
->GetType() == wxXML_TEXT_NODE
||
642 n
->GetType() == wxXML_CDATA_SECTION_NODE
) &&
643 // ...it is textnode...
645 node
/*not n!*/->GetName() == _T("label") ||
646 (node
/*not n!*/->GetName() == _T("value") &&
647 !n
->GetContent().IsNumber()) ||
648 node
/*not n!*/->GetName() == _T("help") ||
649 node
/*not n!*/->GetName() == _T("longhelp") ||
650 node
/*not n!*/->GetName() == _T("tooltip") ||
651 node
/*not n!*/->GetName() == _T("htmlcode") ||
652 node
/*not n!*/->GetName() == _T("title") ||
653 node
/*not n!*/->GetName() == _T("item")
655 // ...and known to contain translatable string
657 arr
.Add(ConvertText(n
->GetContent()));
661 if (n
->GetType() == wxXML_ELEMENT_NODE
)
663 wxArrayString a2
= FindStrings(n
);
664 WX_APPEND_ARRAY(arr
, a2
);