Code I/O

A topnotch WordPress.com site

5 Minutes on Adobe Flex – A reusable XMLFile class

Leave a comment

Handling files in Flex is something each application will do, I hope this XMLFile class can be useful out there.  Further this class can be extend to handle specific objects like Application preferences.   The listing of PreferencesFile source is here

XMLFile.as


package foss.afxtionlib.files
{
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;

	public class XMLFile
	{
		protected var _directory:File = null;
		protected var _file:File = null;
		protected var _data:XML = null;

		public function XMLFile(fileName:String, storageDirectory:File=null)
		{
			if(storageDirectory == null)
			{
				_directory = File.documentsDirectory;
			}
			else
			{
				_directory = storageDirectory;
			}

			if(_file == null)
			{
				_file = _directory.resolvePath(fileName);
			}

			if (_file.exists) {
			    _data = readXML();
			}
		}

		public function get file():File
		{
			return(_file);
		}

		public function set file(file:File):void
		{
			_file = file;
		}

		public function get data():XML
		{
			return(_data);
		}

		public function set data(xml:XML):void
		{
			_data = xml;
		}

		public function set directory(storageDirectory:File):void
		{
			_directory = storageDirectory;
		}

		public function get directory():File
		{
			return(_directory);
		}

		public function saveXML():void
		{
			var outputString:String = '<?xml version="1.0" encoding="utf-8"?>n';
			outputString += data.toXMLString();
			outputString = outputString.replace(/n/g, File.lineEnding);

			var stream:FileStream = new FileStream();
			stream.open(file, FileMode.WRITE);
			stream.writeUTFBytes(outputString);
			stream.close();
		}

		public function readXML():XML
		{
			var stream:FileStream = new FileStream();
			stream.open(file, FileMode.READ);

			data = XML(stream.readUTFBytes(stream.bytesAvailable));
			stream.close();

			return(data);
		}
	}
}

PreferencesFile.as


package foss.afxtionlib.files
{
	import flash.filesystem.File;

	public class PreferencesFile extends XMLFile
	{
		private var _templateXML:XML = null;

		public function PreferencesFile(tx:XML)
		{
			_templateXML = tx;
			super("Preferences.xml", File.applicationStorageDirectory);

			if(file.exists == false || data == null)
			{
				setPreferences();
			}
			trace("PSP=" + file.nativePath + "ndata=" + data);
		}

		public function setPreferences(d:XML=null):void
		{
			if(d == null)
			{
				data = _templateXML;
			}
			else
			{
				data = d;
			}
		}

		public function getPreferences():XML
		{
			if(data == null)
			{
				return(_templateXML);
			}

			return(data);
		}
	}
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s