I had been asked by my manager on how much time will it take to convert the intranet Flex 1.5 application to Flex2. Instead of doing the basic stuff for converting the app manually, I thought of writing a utility of replacing code.
The utility takes care of the following
I . Finds and replace
1. application namespace
2. Void with void
3. newline with \n in .as file
4. _root with Application.application
5. Application.alert with mx.controls.Alert
II. package the class
Following are the regular expressions I have used to replace the content (not sure if this is the best way to do the task)
file_content = Regex.Replace(file_content, "xmlns:mx=\"http://www.macromedia.com/2003/mxml\"", "xmlns:mx=\"http://www.adobe.com/2006/mxml\"");
file_content = Regex.Replace(file_content, "\bvoid\b", "void",RegexOptions.IgnoreCase);
if (fi.Extension == ".as")
{
file_content = Regex.Replace(file_content, "\bnewline\b", "\"\n\"", RegexOptions.IgnoreCase);
}
else
{
file_content = Regex.Replace(file_content, "newline", "#&13;", RegexOptions.IgnoreCase);
}
file_content = Regex.Replace(file_content, "\b_root[.]\b", "Application.application.", RegexOptions.IgnoreCase);
file_content = Regex.Replace(file_content, "\bApplication.alert\(", "mx.controls.Alert.show(", RegexOptions.IgnoreCase);
String packagename;
if (fi.Extension == ".as")
{
Match m = Regex.Match(file_content, @"(?<class>class)(?<packages>.*[.])(?<classname>.*)");
file_content = Regex.Replace(file_content, @"(?<class>class)(?<packages>.*[.])(?<classname>.*)", "${class} ${classname}");
packagename = m.Groups["packages"].Value;
file_content = "package " + packagename.Substring(0, packagename.Length - 1) + "{n" + file_content;
file_content = file_content + "n}n";
}