Feeds:
Posts
Comments

Archive for the ‘code snippet’ Category

Java Annotations provide Java developers full potential of doing things in more easy and nice way. I will not go explaining Java annotations pros, cons, and the definitions; you can find out here http://java.sun.com/docs/books/tutorial/java/javaOO/annotations.html

instead I will add some piece which I hardly found: how can I write my own Annotation class and make use of them?! here you go:

I will take a simple Beans that Document who wrote some Java class and who reviewed it.

Requirements

  1. Author, Data Type: String
  2. Reviewers, Data Type: Array of String

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)

public @interface Documentation {

String author();

String [] reviewers() default {};

}

what have we done? we created a file called Documentation.java that is an interface, and because it’s an annotation it’s written like this @interface

we have add two methods signatures:

  1. author: returns a String.
  2. reviewers: returns an array of string, that is an optional element to mention, so we provided a default value of empty string array (I know you noticed that 😉

a question: what does@Retention(RetentionPolicy.RUNTIME) do? well, it indicates to the JVM that this annotation is intended to be used at runtime, simply.

now, it’s time to introduce the famous Hello World class 😀

import java.lang.annotation.Annotation;

@Documentation(author=”Alex”, reviewers={“Divid”, “Osama”})

public class HelloWorld {

public static void main(String [] args){

System.out.println(“checking documentation for class HelloWorld.java”);

Annotation annotation = HelloWorld.class.getAnnotation(Documentation.class);

if (annotation == null) {

System.out.println(“No Documentation was found for class HelloWorld, exit”);

System.exit(0);

}

Documentation doc = (Documentation) annotation;

System.out.println(“author: ” + doc.author());

for (String reviewer : doc.reviewers()) {

System.out.println(“reviewer: ” + reviewer);

}

System.exit(0);

}

}

what have we done? we created a Java Class called HelloWorld, that uses @Documentation annotation.

we have extracted the informtion provided for this class in @Documentation annotation element.

Advertisements

Read Full Post »

lately I was working with open flash chart (OFC 1.x) to add some sexy statistics showings. it’s really cool tool and easy to work with.

the charts was set right in, and everything is good, when moved to HTTPS domain it crashed on IE (6 and 7).

after a few minutes googling I found the solution that you need to add two response headers for IE to show the chart.

in JSP

response.addHeader(“Cache-Control”,”cache, must-revalidate”);
response.addHeader(“Pragma”,”public”);

here is the forum I benefit from: http://forums.codecharge.com/posts.php?post_id=97771

Enjoy!

Read Full Post »

following is a JavaScript code snippet using Powerful JQuery library for dealing with select HTML tag (which is – by the way – pain to deal with core javascript language).

the html code

…..

<select id=”my-ml-list” name=”my-ml-list”>

<option value=”java”>XML</option>

<option value=”java”>HTML</option>

<option value=”java”>XHTML</option>

<option value=”java”>HAML</option>

<option value=”java”>YAML</option>

<option value=”java”>YASL</option>

</select>

…..

what can jquery do for you with this list, some is follow:

iterate on all elements

<script language=”javascript”>

$(“my-ml-list option”).each (

function () {

// this object is the current option HTML object.

alert(“current option value is: ” + this.value); // show message of it’s value.

}

);

</script>

Read Full Post »

I was working with struts validators and need to test a phone number to be digits and starts with a leading 00, I wanna share it here

^00+\d{9,17}$

below is a ruby code snippet:

phone_no=”009621111111″

if phone_no =~ /^00+\d{0,9}$/

puts “match”

else

puts “No match”

end

a quick explain is

^ start from the beginning of the string

+\d{9,17} should include digits with at least 9 digits and at most 17 digits (excluded the leading 00)

$ indicates the end of the string

well, that was not much, but I wish it save someone’s 10 minutes!

Cheers

Read Full Post »