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/wfstream.h"
40 #error "You must compile the resource compiler with wxBase!"
44 class XmlResApp
: public wxApp
56 void ParseParams(const wxCmdLineParser
& cmdline
);
58 wxArrayString
PrepareTempFiles();
59 void FindFilesInXML(wxXmlNode
*node
, wxArrayString
& flist
, const wxString
& inputPath
);
61 wxString
GetInternalFileName(const wxString
& name
, const wxArrayString
& flist
);
62 void DeleteTempFiles(const wxArrayString
& flist
);
63 void MakePackageZIP(const wxArrayString
& flist
);
64 void MakePackageCPP(const wxArrayString
& flist
);
65 void MakePackagePython(const wxArrayString
& flist
);
68 wxArrayString
FindStrings();
69 wxArrayString
FindStrings(wxXmlNode
*node
);
71 bool flagVerbose
, flagCPP
, flagPython
, flagGettext
;
72 wxString parOutput
, parFuncname
, parOutputPath
;
73 wxArrayString parFiles
;
77 IMPLEMENT_APP(XmlResApp
)
80 bool XmlResApp::OnInit()
82 int XmlResApp::OnRun()
85 static const wxCmdLineEntryDesc cmdLineDesc
[] =
87 { wxCMD_LINE_SWITCH
, _T("h"), _T("help"), _T("show help message"),
88 wxCMD_LINE_VAL_NONE
, wxCMD_LINE_OPTION_HELP
},
89 { wxCMD_LINE_SWITCH
, _T("v"), _T("verbose"), _T("be verbose") },
90 { wxCMD_LINE_SWITCH
, _T("c"), _T("cpp-code"), _T("output C++ source rather than .rsc file") },
91 { wxCMD_LINE_SWITCH
, _T("p"), _T("python-code"), _T("output wxPython source rather than .rsc file") },
92 { wxCMD_LINE_SWITCH
, _T("g"), _T("gettext"), _T("output list of translatable strings (to stdout or file if -o used)") },
93 { wxCMD_LINE_OPTION
, _T("n"), _T("function"), _T("C++/Python function name (with -c or -p) [InitXmlResource]") },
94 { wxCMD_LINE_OPTION
, _T("o"), _T("output"), _T("output file [resource.xrs/cpp]") },
95 #if 0 // not yet implemented
96 { wxCMD_LINE_OPTION
, _T("l"), _T("list-of-handlers", _T("output list of neccessary handlers to this file" },
98 { wxCMD_LINE_PARAM
, NULL
, NULL
, _T("input file(s)"),
99 wxCMD_LINE_VAL_STRING
,
100 wxCMD_LINE_PARAM_MULTIPLE
| wxCMD_LINE_OPTION_MANDATORY
},
105 wxCmdLineParser
parser(cmdLineDesc
, argc
, argv
);
107 switch (parser
.Parse())
140 void XmlResApp::ParseParams(const wxCmdLineParser
& cmdline
)
142 flagGettext
= cmdline
.Found(_T("g"));
143 flagVerbose
= cmdline
.Found(_T("v"));
144 flagCPP
= cmdline
.Found(_T("c"));
145 flagPython
= cmdline
.Found(_T("p"));
147 if (!cmdline
.Found(_T("o"), &parOutput
))
150 parOutput
= wxEmptyString
;
154 parOutput
= _T("resource.cpp");
156 parOutput
= _T("resource.py");
158 parOutput
= _T("resource.xrs");
161 parOutputPath
= wxPathOnly(parOutput
);
162 if (!parOutputPath
) parOutputPath
= _T(".");
164 if (!cmdline
.Found(_T("n"), &parFuncname
))
165 parFuncname
= _T("InitXmlResource");
167 for (size_t i
= 0; i
< cmdline
.GetParamCount(); i
++)
168 parFiles
.Add(cmdline
.GetParam(i
));
174 void XmlResApp::CompileRes()
176 wxArrayString files
= PrepareTempFiles();
178 wxRemoveFile(parOutput
);
183 MakePackageCPP(files
);
185 MakePackagePython(files
);
187 MakePackageZIP(files
);
190 DeleteTempFiles(files
);
194 wxString
XmlResApp::GetInternalFileName(const wxString
& name
, const wxArrayString
& flist
)
196 wxString name2
= name
;
197 name2
.Replace(_T(":"), _T("_"));
198 name2
.Replace(_T("/"), _T("_"));
199 name2
.Replace(_T("\\"), _T("_"));
200 name2
.Replace(_T("*"), _T("_"));
201 name2
.Replace(_T("?"), _T("_"));
203 wxString s
= wxFileNameFromPath(parOutput
) + _T("$") + name2
;
205 if (wxFileExists(s
) && flist
.Index(s
) == wxNOT_FOUND
)
207 for (int i
= 0;; i
++)
209 s
.Printf(wxFileNameFromPath(parOutput
) + _T("$%03i-") + name2
, i
);
210 if (!wxFileExists(s
) || flist
.Index(s
) != wxNOT_FOUND
)
217 wxArrayString
XmlResApp::PrepareTempFiles()
221 for (size_t i
= 0; i
< parFiles
.Count(); i
++)
224 wxPrintf(_T("processing ") + parFiles
[i
] + _T("...\n"));
228 if (!doc
.Load(parFiles
[i
]))
230 wxLogError(_T("Error parsing file ") + parFiles
[i
]);
235 wxString name
, ext
, path
;
236 wxSplitPath(parFiles
[i
], &path
, &name
, &ext
);
238 FindFilesInXML(doc
.GetRoot(), flist
, path
);
240 wxString internalName
= GetInternalFileName(parFiles
[i
], flist
);
242 doc
.Save(parOutputPath
+ _T("/") + internalName
);
243 flist
.Add(internalName
);
251 // find all files mentioned in structure, e.g. <bitmap>filename</bitmap>
252 void XmlResApp::FindFilesInXML(wxXmlNode
*node
, wxArrayString
& flist
, const wxString
& inputPath
)
254 // Is 'node' XML node element?
255 if (node
== NULL
) return;
256 if (node
->GetType() != wxXML_ELEMENT_NODE
) return;
258 // Does 'node' contain filename information at all?
259 bool containsFilename
= (
261 (node
->GetName() == _T("bitmap")) ||
262 // URLs in wxHtmlWindow:
263 (node
->GetName() == _T("url")) ||
265 (node
->GetParent() != NULL
&&
266 node
->GetParent()->GetPropVal(_T("class"), _T("")) == _T("wxBitmapButton") &&
267 (node
->GetName() == _T("focus") ||
268 node
->GetName() == _T("disabled") ||
269 node
->GetName() == _T("selected")))
272 wxXmlNode
*n
= node
->GetChildren();
275 if (containsFilename
&&
276 (n
->GetType() == wxXML_TEXT_NODE
||
277 n
->GetType() == wxXML_CDATA_SECTION_NODE
))
280 if (wxIsAbsolutePath(n
->GetContent()) || inputPath
.empty())
281 fullname
= n
->GetContent();
283 fullname
= inputPath
+ _T("/") + n
->GetContent();
286 wxPrintf(_T("adding ") + fullname
+ _T("...\n"));
288 wxString filename
= GetInternalFileName(n
->GetContent(), flist
);
289 n
->SetContent(filename
);
291 if (flist
.Index(filename
) == wxNOT_FOUND
)
294 wxFileInputStream
sin(fullname
);
295 wxFileOutputStream
sout(parOutputPath
+ _T("/") + filename
);
296 sin
.Read(sout
); // copy the stream
300 if (n
->GetType() == wxXML_ELEMENT_NODE
)
301 FindFilesInXML(n
, flist
, inputPath
);
309 void XmlResApp::DeleteTempFiles(const wxArrayString
& flist
)
311 for (size_t i
= 0; i
< flist
.Count(); i
++)
312 wxRemoveFile(parOutputPath
+ _T("/") + flist
[i
]);
317 void XmlResApp::MakePackageZIP(const wxArrayString
& flist
)
321 for (size_t i
= 0; i
< flist
.Count(); i
++)
322 files
+= flist
[i
] + _T(" ");
326 wxPrintf(_T("compressing ") + parOutput
+ _T("...\n"));
328 if (wxExecute(_T("zip -9 -j ") + wxString(flagVerbose
? _T("") : _T("-q ")) +
329 parOutput
+ _T(" ") + files
, TRUE
) == -1)
331 wxLogError(_T("Unable to execute zip program. Make sure it is in the path."));
332 wxLogError(_T("You can download it at http://www.cdrom.com/pub/infozip/"));
341 static wxString
FileToCppArray(wxString filename
, int num
)
346 wxFFile
file(filename
, "rb");
347 size_t lng
= file
.Length();
349 snum
.Printf(_T("%i"), num
);
350 output
.Printf(_T("static size_t xml_res_size_") + snum
+ _T(" = %i;\n"), lng
);
351 output
+= _T("static unsigned char xml_res_file_") + snum
+ _T("[] = {\n");
352 // we cannot use string literals because MSVC is dumb wannabe compiler
353 // with arbitrary limitation to 2048 strings :(
355 unsigned char *buffer
= new unsigned char[lng
];
356 file
.Read(buffer
, lng
);
358 for (size_t i
= 0, linelng
= 0; i
< lng
; i
++)
360 tmp
.Printf(_T("%i"), buffer
[i
]);
361 if (i
!= 0) output
<< _T(',');
368 linelng
+= tmp
.Length()+1;
373 output
+= _T("};\n\n");
379 void XmlResApp::MakePackageCPP(const wxArrayString
& flist
)
381 wxFFile
file(parOutput
, "wt");
385 wxPrintf(_T("creating C++ source file ") + parOutput
+ _T("...\n"));
389 _T("// This file was automatically generated by wxrc, do not edit by hand.\n")
391 _T("#include <wx/wxprec.h>\n")
393 _T("#ifdef __BORLANDC__\n")
394 _T(" #pragma hdrstop\n")
397 _T("#ifndef WX_PRECOMP\n")
398 _T(" #include <wx/wx.h>\n")
401 _T("#include <wx/filesys.h>\n")
402 _T("#include <wx/fs_mem.h>\n")
403 _T("#include <wx/xrc/xmlres.h>\n")
404 _T("#include <wx/xrc/xh_all.h>\n")
407 for (i
= 0; i
< flist
.Count(); i
++)
408 file
.Write(FileToCppArray(flist
[i
], i
));
411 _T("void " + parFuncname
+ "()\n")
414 _T(" // Check for memory FS. If not present, load the handler:\n")
416 _T(" wxMemoryFSHandler::AddFile(\"XRC_resource/dummy_file\", \"dummy one\");\n")
417 _T(" wxFileSystem fsys;\n")
418 _T(" wxFSFile *f = fsys.OpenFile(\"memory:XRC_resource/dummy_file\");\n")
419 _T(" wxMemoryFSHandler::RemoveFile(\"XRC_resource/dummy_file\");\n")
420 _T(" if (f) delete f;\n")
421 _T(" else wxFileSystem::AddHandler(new wxMemoryFSHandler);\n")
425 for (i
= 0; i
< flist
.Count(); i
++)
428 s
.Printf(_T(" wxMemoryFSHandler::AddFile(\"XRC_resource/") + flist
[i
] +
429 _T("\", xml_res_file_%i, xml_res_size_%i);\n"), i
, i
);
433 for (i
= 0; i
< parFiles
.Count(); i
++)
435 file
.Write(_T(" wxXmlResource::Get()->Load(\"memory:XRC_resource/") +
436 GetInternalFileName(parFiles
[i
], flist
) + _T("\");\n"));
439 file
.Write(_T("}\n"));
444 static wxString
FileToPythonArray(wxString filename
, int num
)
449 wxFFile
file(filename
, "rb");
450 size_t lng
= file
.Length();
452 snum
.Printf(_T("%i"), num
);
453 output
= _T(" xml_res_file_") + snum
+ _T(" = \"\"\"\\\n");
455 unsigned char *buffer
= new unsigned char[lng
];
456 file
.Read(buffer
, lng
);
458 for (size_t i
= 0, linelng
= 0; i
< lng
; i
++)
460 unsigned char c
= buffer
[i
];
466 else if (c
< 32 || c
> 127)
467 tmp
.Printf(_T("\\x%02x"), c
);
475 output
<< _T("\\\n");
478 linelng
+= tmp
.Length();
483 output
+= _T("\"\"\"\n\n");
489 void XmlResApp::MakePackagePython(const wxArrayString
& flist
)
491 wxFFile
file(parOutput
, "wt");
495 wxPrintf(_T("creating Python source file ") + parOutput
+ _T("...\n"));
499 _T("# This file was automatically generated by wxrc, do not edit by hand.\n")
501 _T("from wxPython.wx import *\n")
502 _T("from wxPython.xrc import *\n\n")
506 file
.Write(_T("def ") + parFuncname
+ _T("():\n"));
508 for (i
= 0; i
< flist
.Count(); i
++)
509 file
.Write(FileToPythonArray(flist
[i
], i
));
511 for (i
= 0; i
< flist
.Count(); i
++)
514 s
.Printf(_T(" wxXmlResource_Get().LoadFromString(xml_res_file_%i)\n"), i
);
521 void XmlResApp::OutputGettext()
523 wxArrayString str
= FindStrings();
526 if (!parOutput
) fout
.Attach(stdout
);
527 else fout
.Open(parOutput
, "wt");
529 for (size_t i
= 0; i
< str
.GetCount(); i
++)
530 fout
.Write(_T("_(\"") + str
[i
] + _T("\");\n"));
532 if (!parOutput
) fout
.Detach();
537 wxArrayString
XmlResApp::FindStrings()
539 wxArrayString arr
, a2
;
541 for (size_t i
= 0; i
< parFiles
.Count(); i
++)
544 wxPrintf(_T("processing ") + parFiles
[i
] + _T("...\n"));
547 if (!doc
.Load(parFiles
[i
]))
549 wxLogError(_T("Error parsing file ") + parFiles
[i
]);
553 a2
= FindStrings(doc
.GetRoot());
554 WX_APPEND_ARRAY(arr
, a2
);
562 static wxString
ConvertText(const wxString
& str
)
567 for (dt
= str
.c_str(); *dt
; dt
++)
571 if ( *(++dt
) == wxT('_') )
574 str2
<< wxT('&') << *dt
;
580 case wxT('\n') : str2
<< wxT("\\n"); break;
581 case wxT('\t') : str2
<< wxT("\\t"); break;
582 case wxT('\r') : str2
<< wxT("\\r"); break;
583 case wxT('\\') : if ((*(dt
+1) != 'n') &&
590 case wxT('"') : str2
<< wxT("\\\""); break;
591 default : str2
<< *dt
; break;
600 wxArrayString
XmlResApp::FindStrings(wxXmlNode
*node
)
605 if (n
== NULL
) return arr
;
606 n
= n
->GetChildren();
610 if ((node
->GetType() == wxXML_ELEMENT_NODE
) &&
611 // parent is an element, i.e. has subnodes...
612 (n
->GetType() == wxXML_TEXT_NODE
||
613 n
->GetType() == wxXML_CDATA_SECTION_NODE
) &&
614 // ...it is textnode...
616 node
/*not n!*/->GetName() == _T("label") ||
617 (node
/*not n!*/->GetName() == _T("value") &&
618 !n
->GetContent().IsNumber()) ||
619 node
/*not n!*/->GetName() == _T("help") ||
620 node
/*not n!*/->GetName() == _T("longhelp") ||
621 node
/*not n!*/->GetName() == _T("tooltip") ||
622 node
/*not n!*/->GetName() == _T("htmlcode") ||
623 node
/*not n!*/->GetName() == _T("title") ||
624 node
/*not n!*/->GetName() == _T("item")
626 // ...and known to contain translatable string
628 arr
.Add(ConvertText(n
->GetContent()));
632 if (n
->GetType() == wxXML_ELEMENT_NODE
)
634 wxArrayString a2
= FindStrings(n
);
635 WX_APPEND_ARRAY(arr
, a2
);