Archive for the 'Flex Error' Category

05
Jul

Data Binding, and Regular Expressions

Parsing XML has never been fun. Java programmers use way too many different parsers and APIs.
Java 6 includes Java Architecture for XML Binding (JAXB 2.0) and the implementation of the XML
Data Binding Specification (JSR 31) that maps JavaBeans and XML data. To be more accurate, JAXB
2.0 offers two-directional mapping between JavaBeans and XML Schema (see the javax.xml.bind
package). Java 6 comes with a new tool called xjc that takes an XML Schema as an input and generates
the required JavaBeans as an output.
ActionScript 3.0 supports E4X, which is an ECMA standard for working with XML (see http://www.
ecma-international.org/publications/files/ECMA-ST/ECMA-357.pdf ). It’s very powerful and yet
simple to use. You can forget about these SAX and DOM parsers. E4X is a step towards making XML
a programming language.
For example, an MXML program can read the XML file people.xml (or any other XML source) shown
in Listing 4.23 into a variable with only one line (without worrying about error processing):
<mx:XML id=”myXmlFile” source=”people.xml”/>
You’ll need another line of code to populate the data grid using Flex data binding (remember, tying
the data from a source to a destination). In our case myXmlFile is the source that populates the data
grid aka destination:
<mx:DataGrid dataProvider=”{myXmlFile.person}”>
This line means that each element <person> will populate one row in the data grid. Let’s make the
XML from Listing 4.23 a bit more complex: we’ll introduce nesting in the name element. Now it
consists of separate <first> and <last> elements.
<?xml version=”1.0” encoding=”UTF-8”?>
<people>
<person>
<name>
<first>Yakov</first>
<last>Fain</last>
</name>
<age>22</age>
<skills>java, HTML, SQL</skills>
CHAPTER 4
RIA WITH ADOBE FLEX AND JAVA 123
</person>
<person>
<name>
<first>Victor</first>
<last>Rasputnis</last>
</name>
<age>21</age>
<skills>PowerScript, JavaScript, ActionScript</skills>
</person>
<person>
<name>
<first>Anatole</first>
<last>Tartakovsky</last>
</name>
<age>20</age>
<skills>SQL, C++, Java</skills>
</person>
</people>
Listing 4.32 The XML file FarataSystems_skills.xml
Our goal is to produce a window that looks like this:
Figure 4.9 A data grid populated from people.xml
Please note that we also want to concatenate the values from the <first> and <last> XML elements
for the data grid’s Name column. A small program in Listing 4.30 does exactly this. The fullName
method concatenates the first and last names, and since we specified the labelFunction property
in the name column, the data rendering will be controlled by the fullName() function. We’ll return
to labelFunction in Chapter 11.
<?xml version=”1.0” encoding=”utf-8”?>
<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml”>
Learning Flex Through Applications
124 RIA WITH ADOBE FLEX AND JAVA
<mx:XML id=”myXmlFile” source=”FarataSystems_Skills.xml”/>
<mx:Label text=”Java Developers” fontSize=”18”/>
<mx:DataGrid dataProvider=”{myXmlFile.person}” width=”500”>
<mx:columns>
<mx:DataGridColumn dataField=”name” headerText=”Name” labelFunction=”fullName”/>
<mx:DataGridColumn dataField=”age” headerText=”Age”/>
<mx:DataGridColumn dataField=”skills” headerText=”Skills”/>
</mx:columns>
</mx:DataGrid>
<mx:Script>
<![CDATA[
private function fullName(item:Object, column:DataGridColumn):String {
return item.name.first + “ “ + item.name.last;
}
]]>
</mx:Script>
</mx:Application>
Listing 4.33 Populating a data grid from an XML file
The next step is to add regular expressions to filter the data while populating the data grid. There’s
a nice example of predicate filtering with E4X and RegExp in Darron Schall’s blog at http://www.
darronschall.com/weblog/archives/000214.cfm.
Let’s imagine that a recruiter wants to do a quick search in our XML file to identify people with Java
skills. A small one-line change will do this trick, or at least will be a step in the right direction.
The RegExp class lets you create an instance of the object per the specified pattern, and then find
the substring(s) with this pattern and perform the manipulations with the found substrings, if
any.
In AS3, you can create an instance of the RegExp by using a familiar syntax with a constructor. For
example:
var javaPattern:RegExp = new RegExp(“Java”, “i”);
This is a pattern for finding occurrences of “Java,” ignoring the letter case.
Here’s another way of creating this instance:
var javaPattern:RegExp = /Java/i;
We’ll use the latter syntax by feeding the E4X output to this RegExp instance and the result will be
used as a data provider for the data grid. Let’s modify the MXML code for the <mx:DataGrid> tag to
CHAPTER 4
RIA WITH ADOBE FLEX AND JAVA 125
apply this regular expression to the XML element called skills:
<mx:DataGrid dataProvider=”{myXmlFile.person.(/Java/.test( skills ))}” >
In the line above, /Java/ creates an instance of the RegEx object and the test(skills) method will
ensure that only those XML elements that contain Java are included in the myXmlFile. Now the
resulting window will look as follows:
Figure 4.10 Displaying FarataSystems_skills.xml with Java skills
We still don’t like a couple of things here. First, this output didn’t include Yakov Fain because the
word Java was written in lower case in his skills element. Adding the ignore case option “i” to our
RegExp instance will help:
<mx:DataGrid dataProvider=”{myXmlFile.person.(/Java/i.test(skills))}” >
The output will again look like Figure 4.9.
The next step is to filter out people who were included in this list just because of JavaScript, which
has very little to do with Java. One of the ways to do this is by requesting that there should be a
space or a comma in the regular expression after the word Java:
<mx:DataGrid
dataProvider=”{myXmlFile.person.(/Java? ?,/i.test(skills))}” >
Now we’ve lost both Victor and Anatole. Even though Anatole knows Java, there’s no space or comma
after the word Java in his list of skills. Adding an OR (|) condition that means we’re also interested
in people with the word Java as the last word in the string will help.
<mx:DataGrid
dataProvider=”{myXmlFile.person.(/Java? ?, | Java$/i.test(skills))}”
Learning Flex Through Applications
126 RIA WITH ADOBE FLEX AND JAVA
Figure 4.11 Finding Java programmers
Today, E4X doesn’t support XML Schema, and all the XML elements are returned as text, but it’ll
change in future versions of the ECMAScript for XML. Meanwhile, the implementation of E4X by
any programming language makes it more appealing to developers who have to deal with XML.

24
Jun

Learn Flex Breakpoints and Stepping

In the main Debug view in the Flex Debugging perspective, you will find a row of buttons. These buttons provide the ability to resume, suspend, terminate, step into, step over, and step return. Breakpoints provide single points in the code to automatically stop when debugging. The question is what to do when you need to continue. This is where you have multiple options, especially the stepping functionality. Continue reading ‘Learn Flex Breakpoints and Stepping’

24
Jun

Flex Debugging and Testing

Testing and debugging an application go hand in hand. The basic requirement to debug is to receive or view in real time messages from the system regarding the state of the execution stack. From logging simple messages to viewing the state of the execution stack, this chapter will help you debug and test both on the client side and the server side of development.

Logging Overview

There are two areas of logging in an application’s life cycle. The first is application- and compiler-generated messages, and the second is developer-created messages. To be effective in debugging and testing, you need to learn both areas and how they overlap. Most logging is something that is done during development, and should be disabled when put into production, but that doesn’t mean certain critical messages shouldn’t be taken out of live production applications. Continue reading ‘Flex Debugging and Testing’

12
Jun

Deployment Directory Structure

With the exception of the FDS2 deployment that was discussed earlier, the deployment of a Flex 2 application is very similar to Flash deployment. Flex Builder, by default, sets up a deployment directory that is used when debugging, although you can customize your deployment as you see fit. The only thing to keep in mind when customizing a deployment is that you must keep your pathing correct - meaning that, if you nest your XML files in an /xml folder for run-time parsing, you must ensure that your Flex application is pointing to that /xml folder. This applies to any external, nonembedded assets.

The following table shows a basic customized deployment directory structure.

Open table as spreadsheet

Directory/File Description
deploy/ The location of the root directory. Many developers choose a /source, /deploy directory structure, although you can use whatever you’d like.
/images The location of any nonembedded images such as PNG, JPG, and GIF.
/xml The location of any nonembedded, run-time-specific data that is wrapped in XML.
/swfs The location of any nonembedded SWFs used by your application.
/js The location of any JavaScript files.
myApp.swf Your application.
myApp.html Your application’s HTML wrapper.

Deployment with Flex Builder 2

If you are using Flex Builder 2 or the Flex Builder plugin for Eclipse, as mentioned before, both create a deployment directory for you. Located in your application project’s root directory is a /bin directory. This is where all of the wrappers are kept. The following table describes this structure.

Open table as spreadsheet

Directory/File Description
{appName}/bin/ Your application deployment root directory
AppName.swf The default SWF file for your application
AppName.html The default HTML wrapper for your application
AppName-debug.swf The default debugger SWF for your application that is used by the debugging version of Flash Player 9
AppName-debug.html The default HTML wrapper for your application’s debug file
history.htm The default wrapper for the history-management SWF
history.swf The history-management SWF
history.js The history-management JavaScript file
playerProductInstall.swf The Flash player detection/installation SWF for Express install

As you can see, by default, Flex Builder’s deployment isn’t very organized, but it works well for testing. However, if you’re going to use your own wrappers and directory structure with Flex Builder, keep in mind that you have to tell Flex Builder to not build out the wrappers by default, and redirect Flex Builder’s RUN and DEBUG commands to your custom wrappers.

Figure 18-9 illustrates how to disable HTML wrapper generation within the properties of your application. To do so, uncheck “Generate HTML wrapper file.”




 

September 2008
M T W T F S S
« Aug    
1234567
891011121314
15161718192021
22232425262728
2930  

Badge Farm

  • Textmate