You are viewing the Articles in the News Category

Embedding assets with application/octet-stream

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.

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

Of the many APIs I have developed and published as Open Source to the Flex community, the AS3 HashMap has been one of the most popular. My assumption is that this popularity is in part due many developers expecting a HashMap to be provided as part of the Flex Framework, 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:

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.

Principle of Least Knowledge

One very important (yet often overlooked) design guideline which I advocate is the Principle of least knowledge.

The Principle of Least knowledge, also known as The law of Demeter, or more precisely, the Law of Demeter for Functions/Methods (LoD-F) is a design principle which provides guidelines for designing a system with minimal dependencies. It is typically summarized as “Only talk to your immediate friends.”

What this means is a client should only have knowledge of an objects members, and not have access to properties and methods of other objects via the members. To put it in simple terms you should only have access to the members of the object, and nothing beyond that. Think if it like this: if you use more than 1 dot you are violating the principle.

Consider the following: We have three classes: ClassA, ClassB and ClassC. ClassA has an instance member of type ClassB. ClassB has an instance member of type ClassC. This can be designed in such a way which allows direct access all the way down the dependency chain to ClassC or beyond, as in the following example:

The above example is quite common, however it violates The Principle of Least Knowledge as it creates multiple dependencies, thus reducing maintainability as should the internal structure of ClassA need to change so would all instances of ClassA.

Now keep in mind that in all software development there are trade-offs to some degree. Sometimes performance trumps scalability or vice-versa, other times readability trumps both. A perfect example of where you would not want to use The Principle of Least Knowledge is in a Cairngorm ModelLocator implementation. The Cairngorm ModelLocator violates the Principle of least knowledge for good reason – it simply would not be practical to write wrapper methods for every object on the ModelLocator. This is the main drawback of the Principle of least Knowledge; the need to create wrapper methods for each object, which are more formally known as Demeter Transmogrifiers.

The goal of good software design is to minimize dependencies, and by carefully following the guidelines provided by The Principle of Least Knowledge this becomes much easier to accomplish.

SQLite Administrator

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

Quick Tip: Flex 3 IDE

I have added a new Category to my blog called “Quick Tips” from which I plan to post various quick and simple, yet useful little things I come across from time to time.

This first Quick Tip has to do with the default state of the “Mark occurrences feature” in Flex Builder 3 (the feature which causes all methods, properties etc to be highlighted in blue).

Upon installation of the latest Flex 3 beta one of the initial features I noticed was the Mark occurrences feature. Personally I found it to be quite annoying and somewhat intrusive. In addition the feature was also pretty slow (however I believe this is currently being addressed).

In any event I wanted to disable this feature, problem was I couldn’t figure out exactly how to turn it off! After some exploring in the preferences panel I was still unable to find where the Mark occurrences feature could be disabled, so I posted a new topic on the Flex 3 pre-release forum, and luckily I got a quick response from Laurence Mclister explaining where the feature could be disabled – it was right there in the toolbar!

Mark occurrences can be turned off via the toolbar button which looks like a yellow highlighter it is typically toward the center of the toolbar). Below I have highlighted in red where the Mark occurrences feature is located on the toolbar.

Mark Occurrences

Hope that saves someone some time down the road.

Running ASDoc against AIR projects

At first glance one might think the ability to run asdoc.exe against an AIR project would be pretty much the same as compiling ASDocs for a typical Flex project – but it’s not.

I recently completed updating my AIRCairngorm project and decided to compile the source code documentation as I always do, however when I attempted to do so my build failed. Based on the exceptions asdoc.exe was throwing it was pretty obvious the build was failing because none of the classes in the AIR API could be resolved.

I did a quick Google search and unfortunately there wasn’t much out there explaining how to resolve the errors. After a little more digging around and exploring of the sdk I was able to fix the build. Basically asdoc.exe needs to point to air-config.xml as well as the location of the air swc’s.

Below is the final build I am using which will allow you to compile ASDocs for your AIR projects:

You will also need the asdoc.properties file to go along with it:

So if you happen to run into this problem in the future you can use the ASDocAntTask project to compile ASDocs for your AIR projects without the headaches.