Archive for the 'Adobe AIR' Category

LocalPersistenceMap

Sunday, March 9th, 2008

When developing browser based Flex application SharedObject provides just about everything one needs to facilitate local persistence of application data. SharedObject is restricted by certain rules governed by the Flash Player Security Model, however it is very efficient for general use.

The data property of a SharedObject provides read/write access to the underlying data which is persisted to the SharedObject. Personally, I prefer to have a consistent API available when working with dynamic objects, thus I developed the IMap interface.

LocalPersistenceMap provides an IMap implementation into the data property of a SharedObject instance. It allows clients to work with the underlying data of a SharedObject just as one would with a HashMap, ResourceMap, etc.

Below is a basic example which demonstrates how LocalPersistenceMap can be utilized to provide an IMap implementation into a SharedObject:

var map:IMap = new LocalPersistenceMap("test", "/");
map.put("username", "efeminella");
map.put("password", "43kj5k4nr43r934hcr34hr8h3");
map.put("admin", true);

The LocalPersistenceMap constructor creates a reference to a local SharedObject based on the specified identifier and optional local path. A reference to the underlying SharedObject can also be retrieved via the sharedObjectInstance accessor.

In addition to the new LocalPersistenceMap, I have also packaged the Collections API which can be downloaded here. Complete documentation and code examples for the Collections API are also available here.

Embedding assets with application/octet-stream

Monday, February 18th, 2008

Awhile back one of the guys on my team discovered an interesting way to embed files in ActionScript using the “application/octet-stream” mimeType with the Embed metadata tag.

When using the Embed tag it is not common to explicitly assign a specific mimeType to an asset. Because of this Flex uses heuristics to determine the appropriate mimeType based on the extension of the embedded asset (i.e. file).

The Adobe Flex documentation states:

“You can use the [Embed] metadata tag to import JPEG, GIF, PNG, SVG, SWF, TTF, and MP3 files.”

However when specifying application/octet-stream (which is essentially an arbitrary byte stream ) as the assets mimeType it is also possible to embed a file of any type. If the file type is not supported natively, such as XML, developers can write custom parsers which can read the file utilizing the ByteArray (Flex 2) or ByteArrayAsset (Flex 3).

Below I have provided a basic example which demonstrates how the application/octet-stream mimeType can be utilized to embed a file, in this case an external xml document which serves as a config file.

package example
{
  import mx.core.ByteArrayAsset;
   
  public final class Config
  {
    [Embed("config.xml", mimeType="application/octet-stream")]
    private static const Config:Class;
   
    public static function getConfig() : XML
    {
      var ba:ByteArrayAsset = ByteArrayAsset( new Config()) ;
      var xml:XML = new XML( ba.readUTFBytes( ba.length ) );
       
      return xml;    
    }
  }
}
 

In the above example an XML document is embedded as a Class object from which the contents of the file are read as a string via ByteArrayAsset and converted to an XML object.

Based on this example it would also be possible to create arbitrary custom types in conjunction with custom parsers to allow additional support for numerous other file types in Flex as well.

AS3 ResourceMap API

Friday, February 8th, 2008

Out of all of the APIs I which have developed and published as Open Source to the Flex community the AS3 HashMap API has by far been one of the most popular. Additionally, based on my Firestats totals, the posts I have written over the past 2 years related to the AS3 HashMap API have also been some of the most popular as well. My assumption is this popularity is due to the fact that HashMaps were expected to be included as part of the Flex Framework by most Flex developers, and rightfully so. After all that is why I developed it in the first place!

With the upcoming release of Adobe Flex 3 there are many new additions which have been added to the Framework. These new additions and features are outside the scope of this article; however, one which is of relevance is the ability to directly access the underlying content of a ResourceBundle.

For the most part I tend to think of the content of a ResourceBundle as not begin much different than a HashMap as it essentially is just an aggregation of name / value pairs, just like a HashMap. With that in mind I have developed a ResourceMap API which allows developers to work with a ResourceBundle via an IMap implementation (a.k.a. HashMap).

ResourceMap implements the IMap interface allowing the underlying content of a ResourceBundle to have CRUD specific operations performed on it. To utilize the ResourceMap one only need to instantiate an instance of ResourceMap and pass in a ResourceBundle, as in the following:

import com.ericfeminella.collections.ResourceMap;
import com.ericfeminella.collections.IMap;
import mx.resources.ResourceBundle;

[ResourceBundle("resources")]
private static const rb:ResourceBundle;

var map:IMap = new ResourceMap( rb );

The above example is all that is required to work with a Resourcebundle as a HashMap. From there you can work with the ResourceBundle just as you would with a typical HashMap.

The benefit to this approach is it allows developers to dynamically add, remove, update and delete resources at runtime, whereas the new Flex 3 ResourceManager (much like the Open Source ResourceManager API I created last year - had to add that in) does not provide an API for setting resources. In addition, all of the getters defined by the IResourceBundle interface and implemented by ResourceBundle have been deprecated in favor of the new ResourceManager. However, IResourceBundle now provides an additional operation called getContent(); which exposes a reference to the underlying content Object which is created when a .properties file is compiled. Therefore it is possible to take advantage of this by accessing the content object.

Admittedly the thinking behind the ResourceMap API takes a somewhat “outside-of-the-box” approach to working with ResourceBundles as one typically tends to think of resources as constants, especially when working with localized applications. However with all of the new capabilities available in the Flex 3 Resource API (such as loading compiled resource swf’s at runtime, etc) the opportunity to experiment with different things is well worth it!

ResourceMap is published under the MIT license.

SQLite Administrator

Thursday, January 24th, 2008

When developing application in Adobe AIR which utilize the SQLite API it is often useful to have a quality SQLite editor available, especially during development and testing.

I have tried many different SQLite editors, most of which were quite good, however the best of the bunch you have to pay for.

Personally I recommend SQLite Administrator. SQLite Administrator is a powerful tool which allows for the design, creation and modification of local SQLite databases. The code editor is very intuitive and provides a simple UI which allows you to write SQL queries with little effort. In addition, there are many useful features which you would expect from a quality editor such as code completion and highlighting.

SQLite Administrator

So if you are looking for an high quality free SQLite Editor when working with Adobe AIR and SQLite check out SQLite Administrator