The simplest way to parse XML in Java
You can do a lot with XML, but often all you want to do is to read a simple file with some basic data in it. The options for doing this, SAX, DOM and JAXB are all relatively verbose and often off-putting. So here is a class that will make all this much much more simple.
Parsing a file such as:
1 2 3 4 5 6 7 8 9 10 11 12 | <config> <title>test</title> <version major="1" minor="2"/> <roles> <role name="admin"/> <role name="user"/> </roles> <users> <user name="joe" password="pass" role="admin"/> <user name="harry" password="secret" role="user"/> </users> </config> |
Can be achieved with the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | Xml config = new Xml("config.xml","config"); System.out.println("title: "+config.child("title").content()); Xml version = config.child("version"); System.out.println("version: "+version.integer("major")+"."+version.integer("minor")); for(Xml role:config.child("roles").children("role")) System.out.println("role: name: "+role.string("name")); for(Xml user:config.child("users").children("user")) { System.out.println( "user: name: "+user.string("name")+ ", password: "+user.string("password")+ ", role: "+user.string("role")); } |
As you can see, it's nice and simple and allows you to get to the information quickly and without any hassle. The API uses the DOM parser underneath, but attempts to make the data more easily available. All you need is the following [Click to download] class, which you can of course customise however you like.
Update:
I've just added another nice method to this class. Is called e4xEval, that is actualy a xpath eval but i made it use the flex e4x syntax. So, if you want to get all the role name you can do:
1 2 3 | Xml config = new Xml("config.xml","config"); String[] list = config.e4xEval("config.roles.role.@name"); // @ is attribute |
Cheeky little tips that will help against email harvesting
Prevent email address harvesting, image downloading, and image theft by using CSS Pseudo Elements in conjunction with the Content property. This is a pure HTML/CSS solution to the problems of email harvesting and image theft with one common method.
There is a little known feature of CSS called Pseudo Elements that allows for some special effects to be applied to elements. Using pseudo elements it is possible to add content before or after an element.
1 2 3 4 5 6 7 8 9 10 | <style type="text/css">
.demodiv{
display:inherit;
}
.demodiv:before{
content:"myemail@example.com";
}
</style>
<div class="demodiv"> </div> |
This will output: myemail@example.com
You can also use as content url('someimage.jpg'); to display images this way. A bit more detailed at amatoc.com
Another way to mask emails with css power:
1 2 3 4 5 6 | <style type="text/css">
span.test {
direction: rtl; unicode-bidi:bidi-override;
}
</style>
<span class="test">moc.tset@tset</span> |
The CSS reverses the direction of the text, so you write your email backwards, the CSS sorts it out so it displays fine on your website, but this way, ’spam bots’ can’t read your email!
Handy CSS Debug Snippet
Great little debuging snippet found while browsing. The code adds different coloured borders to the assets depending on its level. Leave commented out if not needed.
1 2 3 4 5 6 7 8 | * { outline: 2px dotted red } * * { outline: 2px dotted green } * * * { outline: 2px dotted orange } * * * * { outline: 2px dotted blue } * * * * * { outline: 1px solid red } * * * * * * { outline: 1px solid green } * * * * * * * { outline: 1px solid orange } * * * * * * * * { outline: 1px solid blue } |
How to identify at runtime if the swf is in debug or release mode/build
It may be interesting to know at runtime whether an application is running from a debug or release SWF file. It allows the application to automatically execute debug specific code when the file is a debug SWF file and ignore this code when it is published as a release file. Also, it should be nice to know if you have a debug flash player just for the same reasons. But for the last one, there is a function in the Capabilities class called isDebugger.
One solution would be to use conditional compilation. The other solution is in the following code (hack).
package org.adm.runtime { import flash.system.Capabilities; public class ModeCheck { /** * Returns true if the user is running the app on a Debug Flash Player. * Uses the Capabilities class **/ public static function isDebugPlayer() : Boolean { return Capabilities.isDebugger; } /** * Returns true if the swf is built in debug mode **/ public static function isDebugBuild() : Boolean { return new Error().getStackTrace().search(/:[0-9]+]$/m) > -1; } /** * Returns true if the swf is built in release mode **/ public static function isReleaseBuild() : Boolean { return !isDebugBuild(); } } }
This code simply searches for line numbers of errors in StackTrace result. Only StackTrace result of a debug SWF file contains line numbers. Then we know that we are running a debug or release SWF file.
This class is also available in the Components Pack at Runtime subpackage
Ho-To: Install Alchemy
For those of you who tried but didn't succed, I found some straigthforward instructions for this.
Setup Hell:
Install cygwin to "c:\cygwin" - make sure to check boxes to install Perl, "Archive" (ZIP), and Devel/gcc:g++ (3.4.4.3 presumably) (is Perl necessary?)
Copy the alchemy directory to: c:\cygwin\home\Lee
Copy the flex sdk bin dir from c:\program files\etc to "c:\flex" (to avoid paths with spaces in it!)
Run the alchemy config script once:
run cygwin cd alchemy ./config
Edit the textfile "C:\cygwin\home\Lee\alchemy\alchemy-setup" and uncomment and edit line 22 to: add "export ADL=c:\flex\adl.exe"
Edit .bashrc (in C:\cygwin\home\Lee)
echo "LEE PROFILE" export FLEX_HOME=~/flex export ALCHEMY_HOME=~/alchemy # "This should be added before your PATH is modified" !! source /home/Lee/alchemy/alchemy-setup PATH=$ALCHEMY_HOME/achacks:/home/Lee/flex:$PATH export PATH alc-on
Do this (just once, I think):
cd $ALCHEMY_HOME/bin ln -s llvm-stub llvm-stub.exe
Compiling a SW:
CD to the directory with the source
Always do "alc-on" before compiling, cuz it doesn't work without it even though i added it to the startup (dunno)
gcc stringecho.c -O3 -Wall -swc -o stringecho.swc -- should give you a swc.
Import the swc into your flex builder 4 project and make sure compiler targets SDK v4
Shortcuts
alc-home - takes you to the Alchemy install folder.
alc-on - puts Alchemy gcc toolchain replacements at the front of your path.
alc-off - restores original path.
alc-util - shows you various Alchemy-related environment vars
USEFUL TO KNOW
which gcc - tells you which gcc it will use (should be the one in the achacks dir)
ln - links shit
rm - deletes links as well as files
Ugh: Make sure to do "alc-on" and "alchemy-setup" even though you put it in the startup script
(?)
...
Objects:
AS3_Val
AS3_ArrayValue
Methods:
AS3_Release
AS3_LibInit
