Using persistence In JavaFx

Creating Persistence Using JavaFX 1.2
There is a huge amount of features that have added to the new release of
JavaFX SDK 1.2.The most important feature added to JavaFX is persistence. The
feature allows the user to create applications that can store the data for later usage. This
increases the JavaFX to reach a more broader horizon.
This feature of JavaFX can help creating real time applications,the best
example for this feature will be when you create Games in JavaFX,you can give a
SaveAs option and store the details for that particular user,and retrieve it when the game
is loaded again.
Some other examples could be Sticky notes,Reminders and so on. It comes
down to saving histories for a particular application and using them for the next time
load.
In order to implement this JavaFx uses two classes,
1.Storage Class
2.Resource Class
Storage class is a collection of resources that are going to be used for making the
data persistent. Storage provides methods for storing data locally on the client system,
even for applications that are running in untrusted execution environment. In order to
use a storage we have two steps,
1.Declaring a Storage,
var storage:Storage=Storage
{
source: "test.txt"
}
2.Using resource to identify the source of the Storage,
var res:Resource=storage.resource;
So the Resource helps in identifying the source and making them usable. Here
the resource variable identifies the resource to be made used and returns it to res
variable.
Using resource we can open InputStream and OutputStream for a resource.
For Eg:
var storage:Storage=Storage
{
source: "test.txt"
}
var res:Resource=storage.resource;
var input=res.openInputStream();
When you want to do a more intensive file operation you can do so,by using a
FileInputStream,this way
var fileop=new FileInputStream(res.name);
Reading and Writing XML using Persistence API's:
We need to create a storage for using persistence,so the first step is,
var filewr = Storage {
source: "data.xml"
}
The most important part of the making data persistence is we need to write the data with persistence and
then only we can read it with persistence. So this program will create a storage if it is not present and
then add data to it,
In order to get Data from an XML file,we need to use PullParser API,it generally
extracts the data from the XML file. So we group this into a function loadData()
function loadData() {
var input = filewr.resource.openInputStream();
def parser = PullParser {
documentType: PullParser.XML
input: input
onEvent: parseCallback
};
try {
parser.parse();
printPlayers();
} catch (npe : java.lang.NullPointerException) {
initStorage();
}
}
In this function parse() method instantiates the PullParser and call the event of the
parser,and it will throw a NullPointerException() if the file doesn't exist. Hence if the exception is caught it
creates a storage,
function initStorage() {
println("Initializing Storage...");
var out = filewr.resource.openOutputStream(true);
var dummyRoot = "<root></root>";
out.write(dummyRoot.getBytes());
out.close();
}
Thus the XML file is created with the persistence API.
Sample Code:
Player.Fx
package storage;
public class Player {
public var id : String;
public var name : String;
public var team : String;
public function toXML() : String {
" <player>\\n"
" <id>{id}</id>\\n"
" <name>{name}</name>\\n"
" <team>{team}</team>\\n"
" <batting>\\n"
" <average></average>\\n"
" <form></form>\\n"
" <strikerate></strikerate>\\n"
" </batting>\\n"
" </player>";
}
}
Main.Fx
package storage;
import javafx.data.pull.Event;
import javafx.data.pull.PullParser;
import javafx.io.Storage;
var filewr = Storage {
source: "data.xml"
}
var players : Player[];
var player : Player;
function initStorage() {
println("Initializing Storage...");
var out = filewr.resource.openOutputStream(true);
var dummyRoot = "<root></root>";
out.write(dummyRoot.getBytes());
out.close();
}
function loadData() {
var input = filewr.resource.openInputStream();
def parser = PullParser {
documentType: PullParser.XML
input: input
onEvent: parseCallback
};
try {
parser.parse();
printPlayers();
} catch (npe : java.lang.NullPointerException) {
initStorage();
}
}
function storeData() {
var out = filewr.resource.openOutputStream(true);
out.write("<root>\\n".getBytes());
for(player in players) {
out.write(player.toXML().getBytes());
}
out.write("</root>".getBytes());
}
function parseCallback(e : Event) {
if(e.type == PullParser.TEXT) {
if(e.level == 1) {
if(e.qname.name == "player") {
// Add player to list of players
if(player != null) {
insert player into players;
}
player = Player { };
}
} else if(e.level == 2) {
if(e.qname.name == "id") {
player.id = e.text;
} else if(e.qname.name == "name") {
player.name = e.text;
} else if(e.qname.name == "team") {
player.team = e.text;
}
}
} else if(e.type == PullParser.END_DOCUMENT) {
// Add player to list of players
if(player != null) {
insert player into players;
}
}
}
function addPlayer(id : String, name : String, team : String) {
insert Player {
id: id
name: name
team: team
} into players;
}
function printPlayers() {
println("<root>");
for(player in players) {
println(player.toXML());
}
println("</root>");
}
function run() {
loadData();
addPlayer("1", "M S Dhoni", "Chennai Batsman");
storeData();
}
Thus we will be able to store data and retrieve it using persistence API's. The life span of
the storage file will be till the storage is cleared.
Comments:

Hi,

From JavaFX, is there a way to store data in third party OO DB like db4o?

Thanks

Posted by hp on June 30, 2009 at 08:46 AM IST #

Hi,
This feature is available for only file structures,like .txt and you can do it for XML files not for OO DB.You can expect it from upcoming versions.

Posted by Dwarakanath on July 04, 2009 at 12:56 PM IST #

Hi Dwarakanath.

I´m confused about the location where persistence writes the files. I did a writer code and it runned fine. Then I did a search on my filesystem and I did not find the file my code was supposed to create. After this, I wrote a reader code and it found the file and read the information correctly...

Do You have any additional information about it ?

Best regards,
Cláudio.

Posted by Cláudio on July 12, 2009 at 01:19 PM IST #

Claudio,
It is stored in cache,whose path is actually defined by java.It is not stored inside your application folder.

May I know why you need this?

Posted by Dwarak on July 12, 2009 at 03:48 PM IST #

Nothing special, Dwarak.
After the execution of my writer code I looked for the file in the disk just to see if my code was ok. ;-)
Thanks,
Cláudio.

Posted by Cláudio on July 15, 2009 at 02:03 AM IST #

I want to allow the user to install custom game packs, so knowing where resources are located is important. Is there no standard way to know where the CommonAppData folder is?

Posted by Justin on September 18, 2009 at 11:31 AM IST #

finally, a complete sample of java fx read/write xml. Thank you :)

Posted by Simina on September 22, 2009 at 07:25 AM IST #

Where ever it is stored,you can access it using the storage and resource class.There by you don't need to give exact location.
But I am not able to understand your requirement,can you site a example?

@Simina,
Thanks.

Posted by Dwarak on December 11, 2009 at 05:01 AM IST #

is there a way to get the list of files in a directory using javafx.

Posted by Ranjith on March 08, 2010 at 07:43 AM IST #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Dwarakanath J

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today