| By Yakov Fain | Article Rating: |
|
| February 11, 2007 07:00 PM EST | Reads: |
15,039 |
From Farata Systems blog
In XML, namespaces are used to avoid potential naming conflicts with other components having the same names. Since Flex lets you program in MXML and in ActionScript 3, I’ll go over the syntax of namespaces in both of them. Familiarity with Soprano family is a pre-requisite for reading this article.
Namespaces in MXML
MXML applications start with the <mx:Application> tag that includes xmlns property:
<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml” layout="absolute">
The namespace mx:xmlns refers to the URI http://www.adobe.com/2006/mxml that lists valid MXML tags. Open the file flex-config.xml, and you’ll find there an XML element that links this URI to the file mxml-manifest.mxl, which list all MXML components. Here’s an extract from this manifest file:
<component id="ButtonBar" class="mx.controls.ButtonBar"/>
<component id="Canvas" class="mx.containers.Canvas"/>
<component id="CheckBox" class="mx.controls.CheckBox"/>
<component id="ColorPicker" class="mx.controls.ColorPicker"/>
<component id="ComboBox" class="mx.controls.ComboBox"/>
If you want to use one of the standard MXML components, specify the prefix mx for each of them. For example, to use MXML Label, you write the following:
<mx:Label x="111" y="81" text="Hello World"/>
If you are going to create custom Flex components, keep them in a separate namespace to avoid naming conflicts. For example, if you are going to develop a custom Tree component, introduce another namespace, for example with the URI com.enron.controls.* as shown below:
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:lib="com.enron.controls.*" >
…
<lib:Tree id="tree" width="50%" height="100%"…>
</lib:Tree>
</mx:Application>
This sample defines two namespaces: mx and lib. The <lib:Tree> notation means that we are planning to use a Tree component from the namespace that we called lib. As you can guess, I assume that you are going to program this Tree component in the ActionScript’s package com.enron.controls and will have to provide either the code of our Tree component or the SWC library that includes this Tree.
The namespace URI tells Flex where to look for the file implementing this component. You can either create a subdirectory com/enron/controls in the application’s directory, or preferably keep it in a separate location that is included in the classpath of your application (read about flex-config.xml file and the source-path tag in Flex documentation). Since we’ve defined two namespaces here, we can use components available in any of them.
You can also specify so-called local namespace using notations like xmlns=”*” or xmlns:mylocal=”*”, which tells Flex to look for components that are located in the same directory as MXML file or, in case of Flex Data Services, in the /WEB-INF/flex/user-classes directory.
Namespaces in ActionScript 3
Namespaces in ActionScript 3 as well as in MXML are used to control/limit the scope (visibility) of the methods, properties or constants. They are also used to avoid naming conflicts in cases if you create your own custom components that may have the same names as the Flex Framework or other vendor’s counterparts.
You can think of access control keywords public, private, protected and internal as a built-in name spaces. If a method has been declared as
protected calculateTax(){}
you can say that the method calculateTax() has a protected namespace. But AS3 allows you to define your own namespaces to be used instead of these standards language qualifiers.
To introduce your own namespace, you need to perform the following steps:
- Declare a namespace
- Apply the namespace
- Reference the namespace
Let’s write a simple program for an accountant who calculates taxes, but customers that belong to mafia should pay only half of the amount. To do this, we’ll start with declaring two namespaces called regular and soprano. This is the content of the file soprano.as
package com.enron.namespaces {
public namespace soprano="http://www.enron.com/namespaces";
}
Please note that the use of a URI in the namespace declaration is optional. The listing below does not use any explicit URI, but the compiler will generate one. This is how the namespace called regular may look like (it’s defined in the ActionScript file called regular.as):
package com.enron.namespaces {
public namespace regular;
}
To apply the namespaces, we’ll define a class Tax with two methods calcTax() that will differ by the namespace access attribute and by the amount of “calculated” tax. The ActionScript class Tax may look like this:
package com.enron.tax{
import com.enron.namespaces.*;
public class Tax
{
regular static function calcTax():Number{
return 3500;
}
soprano static function calcTax():Number{
return 1750;
}
}
}
The testing class TextTax looks like this:
package com.enron.test
{
import com.enron.namespaces.*;
import com.enron.tax.Tax;
import mx.controls.Alert;
use namespace regular;
// use namespace soprano;
public class TestTax
{
public static function myTax():void {
var tax:Number;
tax=Tax.calcTax();
Alert.show("Your tax is "+ tax,"Calculation complete");
}
}
}
Since we apply the namespace for the regular customer, s/he will have to pay the tax amount of $3500. The MXML code that uses TestTax is shown below.
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute" creationComplete="initApp();">
<mx:Script>
<![CDATA[
import com.enron.test.TestTax;
public function initApp():void {
TestTax.myTax();
}
]]>
</mx:Script>
</mx:Application>
The output of this program looks as in a screenshot below.
Switch to another namespace by changing the use statement to look like
use namespace soprano;
and the amount of tax to pay will by substantially lower. Besides the directive use that affects the entire block of code, AS3 allows more fine grained notation to refer to a specific namespace with a name qualifier (a double colon). In our example, this may look like this:
tax = Tax.soprano::calcTax();
This is the output of the TextTax.mxml for regular customers. For obvious reasons I do not show the output of this program for Soprano family members.
Using namespaces provides additional means of the visibility control (especially if you have something to hide). The methods, class properties of the constants can be physically located in different packages, but marked with the same namespace qualifier, and a one-line namespace change can engage a completely different set of methods/properties across the entire application.
Published February 11, 2007 Reads 15,039
Copyright © 2007 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
About Yakov Fain
Yakov Fain is a Managing Director of Farata Systems, consulting, training and product company. He has authored several Java books, dozens of technical articles. SYS-CON Books released his latest co-authored book , Rich Internet Applications with Adobe Flex and Java: Secrets of the Masters in Spring 2007. Sun Microsystems has nominated and awarded Yakov with the title Java Champion. He leads the Princeton Java Users Group. He is an Adobe Certified Flex Instructor. Currently Yakov works on the book for O'Reilly "Enterprise Application Development with Flex". He twits at twitter.com/yfain.
- AJAX World RIA Conference & Expo Kicks Off in New York City
- Java Kicks Ruby on Rails in the Butt
- Ulitzer’s Amazing First 30 Days in Public Beta
- SYS-CON's "Government IT Expo" to Highlight Cloud Computing and SOA
- Will Ulitzer Dominate News Content on The Web? -Gartner
- Clear Toolkit 4: The Road Map
- Creating Adobe AIR Native Menu with Flash CS4
- Menu Interaction in Adobe AIR
- The Darker Sides Of Cloud Computing: Security and Availability
- Adobe AIR: Creating Dock and System Tray Icon Menus
- AJAX World RIA Conference & Expo Kicks Off in New York City
- Creating PDF Documents from Flex Applications
- Java Kicks Ruby on Rails in the Butt
- WebORB Launched for Flex, Flash, AJAX and Silverlight
- Adobe Takes LiveCycle into the Cloud
- Ulitzer’s Amazing First 30 Days in Public Beta
- Adobe Creates a Sandbox in the Sky
- AJAX and RIA Market Is Heating Up: Sun CEO
- SYS-CON's "Government IT Expo" to Highlight Cloud Computing and SOA
- The Role of an RIA in the Enterprise
- Cover Story: How to Increase the Frame Rates of Your Flash Movies
- AJAX World RIA Conference & Expo Kicks Off in New York City
- Your First Adobe Flex Application with a ColdFusion Backend
- Adobe Flex 2: Advanced DataGrid
- Adobe/Macromedia - Microsoft, Look Out!
- i-Technology Blog: Death-Knell For "Rich Media? Hardly!
- Adobe Flex Interface Customization - Themes, Styles, Skins
- Personal Branding Checklist
- How To Create a Photo Slide Show ...
- "Real-World Flex" by Adobe's Christophe Coenraets







































