;
private var afxns:Namespace = xml1.namespace();
private var xml2:XML = ;
private var flmns:Namespace = xml2.namespace();
private var fxgns:Namespace = new Namespace("http://ns.adobe.com/fxg/2008");
private var originalNameQName:QName = new QName(flmns, "originalName");
private function refactor():void
{
if (Capabilities.os.indexOf("Windows") != -1)
slash = "\\";
else
slash = "/";
sourceFile.nativePath = sourceFileTI.text;
sourcePath = sourceFile.nativePath;
var c:int = sourcePath.lastIndexOf(slash);
sourceDir = sourcePath.substring(0, c);
sourceStream = new FileStream();
sourceStream.open(sourceFile, FileMode.READ);
sourceString = sourceStream.readUTFBytes(sourceStream.bytesAvailable);
sourceXML = XML(sourceString);
callLater(makeFXGFiles);
}
private var topTags:XMLList;
private var numTags:int;
private var curTag:int = 0;
private var symbols:Object = {};
private var symbolList:Array = [];
private function makeFXGFiles():void
{
default xml namespace = fxgns;
var topGroups:XMLList = sourceXML.Group;
if (topGroups.length() > 1)
errorLabel.text = "Unexpected: More than one top-level group";
topTags = topGroups.children();
numTags = topTags.length();
var syms:XMLList = sourceXML.Library.children();
var n:int = syms.length();
for (var i:int = 0; i < n; i++)
{
var node:XML = syms[i];
symbols[node.@name] = node;
}
callLater(makeFXGFile);
}
private function makeFXGFile():void
{
if (curTag == numTags)
{
callLater(makeMainClass);
return;
}
var tag:XML = topTags[curTag++];
var tagName:String = tag.localName();
if (tagName == "Group")
{
callLater(makeFXGFile);
return;
}
if (tagName == "RichText")
{
callLater(makeFXGFile);
return;
}
if (tagName.indexOf("not_for_export") != -1)
{
callLater(makeFXGFile);
return;
}
var definitions:XMLList = sourceXML.Library.Definition.(@name == tagName);
if (definitions.length() != 1)
{
errorLabel.text = definitions.length().toString() + " definitions of " + tagName;
return;
}
var newName:String = tagName;
var definition:XML = definitions[0];
var originalNames:XMLList = definition.attribute(originalNameQName);
if (originalNames.length() == 1)
{
var originalName:String = originalNames.toString();
var c:int = originalName.lastIndexOf("/");
newName = originalName;
if (c != -1)
newName = originalName.substring(c + 1);
}
var newFXG:XML = definition.copy();
// change name from 'Definition' to 'Graphic'
newFXG.setName("Graphic");
newFXG.addNamespace(afxns);
newFXG.addNamespace(flmns);
// delete name attribute
delete newFXG.@name;
// add version
newFXG.@version = "2.0";
// add afx:className and baseclassName
newFXG.@afxns::className = newName;
newFXG.@afxns::baseClassName = "flash.display.Sprite";
addLibrarySymbols(newFXG, newFXG);
symbolList.push(newName);
// write out file
var destFile:File = new File(sourceDir + slash + newName + ".fxg");
var writeStream:FileStream = new FileStream();
writeStream.open(destFile, FileMode.WRITE);
writeStream.writeUTFBytes(xmlheader);
writeStream.writeUTFBytes(newFXG.toXMLString());
writeStream.close();
callLater(makeFXGFile);
errorLabel.text = "Wrote " + destFile.nativePath;
}
private function addLibrarySymbols(xml:XML, symbol:XML):void
{
// find dependent library entries
var allChildren:XMLList = symbol.descendants();
var n:int = allChildren.length();
for (var i:int = 0; i < n; i++)
{
var node:XML = allChildren[i];
var nodeName:String = node.localName();
if (symbols[nodeName])
{
if (xml.Library.length() == 0)
xml.insertChildBefore(xml.children()[0], );
var child:XML = symbols[nodeName];
if (xml.Library.children().length() == 0)
xml.Library.appendChild(child);
else if (xml.Library[nodeName].length() == 0)
xml.Library.insertChildBefore(xml.Library.children()[0], child);
addLibrarySymbols(xml, child);
}
}
}
private var firstPart:String = "package {\nimport flash.display.Sprite;\npublic class flex_skins extends Sprite\n{\n\tpublic function flex_skins(){}\n\nprivate var assets:Array = [\n";
private var secondPart:String = "];\n}\n}\n";
private function makeMainClass():void
{
// write out file
var destFile:File = new File(sourceDir + slash + "flex_skins.as");
var writeStream:FileStream = new FileStream();
writeStream.open(destFile, FileMode.WRITE);
writeStream.writeUTFBytes(asheader);
writeStream.writeUTFBytes(firstPart);
var n:int = symbolList.length - 1;
for (var i:int = 0; i < n; i++)
{
writeStream.writeUTFBytes(symbolList[i] + ",\n");
}
writeStream.writeUTFBytes(symbolList[i] + "\n");
writeStream.writeUTFBytes(secondPart);
writeStream.close();
errorLabel.text = "Wrote " + destFile.nativePath;
}
]]>