Tuesday, December 23, 2008

Xalan, Java, XSLTC

What is difference in xalan XSLT processor and and SUN XSLTC compiler ?
They are completely different products. They are both XSLT processors, but they do the same job in very different ways: Xalan (like nearly all other XSLT processors) is in effect a stylesheet interpreter while XSLTC is a stylesheet compiler: it generates Java bytecodes which can be executed directly by the Java VM.

IBM's JDK includes XSLT4J which is based on Xalan. IBM JDK 1.5 it is based on Xalan Java 2.6.0. So there should be no issue moving from Xalan to the IBM JDK.

SUN's JDK 1.5 contains only XSLTC which does not have all of the features of the Xalan interpretive processor. The main differences are in extension support. XSLTC does not support the following extensions:
  • Dynamic EXSLT extensions
  • NodeInfo extension functions
  • SQL library extension
  • PipeDocument extension
  • Evaluate extension
  • Tokenize extension
  • JavaScript extensions
In addition to the above :
  • It does not support all of the XSLT type to Java type conversions that the interpreter supports. You may be able to work around this by adding an explicit cast (e.g. call to the boolean(), number(), etc. function).
  • It does not work well if you return a Boolean object instead of a boolean from a Java extension, or a Double object instead of a double, etc. You can work around this by returning the primitive types instead of objects.
  • It does not provide a default object when the method being called is not static and no object is provided in the extension call. You can work around this by providing the object in the extension call.
It only supports the abbreviated syntax for Java extension calls (see Using the abbreviated syntax for extensions implemented in Java)

So, when you use SUN's JDK 1.5 or later for Transformation on an XSL like this






You will get the following error:

ERROR: 'The first argument to the non-static Java function 'completeTaskOnExit' is not a valid object reference.'
FATAL ERROR: 'Could not compile stylesheet'
Exception in thread "main" javax.xml.transform.TransformerConfigurationException: Could not compile stylesheet
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.newTemplates(Unknown Source)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.newTransformer(Unknown Source)
at Transform.main(Transform.java:29)


If you want to continue to use the Xalan interpreter with SUN's JDK you can use the endorsed standards override mechanism - Create the "endorsed" directory in "...\jre\lib\endorsed" and copy the Xalan 2.7 files into it.

If you are using Xalan 2.8, then extracting "xalan.jar" from 'Xerces-J-tools.2.8.1.zip' and copying it into the 'endorsed' directory solves the problem of not finding 'org.apache.xalan.processor.TransformerFactoryImpl'.

OR you can also force the SUN JDK to use Xalan by setting the System property :

System.setProperty("javax.xml.transform.TransformerFactory", "org.apache.xalan.processor.TransformerFactoryImpl");

References:
Ad xslt-sample (not working on Java 1.5 & 1.6) ...
what is difference in xalan XSLT processor and and SUN XSLTC compiler
jdk1.5 and Xalan.jar differences?

Thursday, December 11, 2008

Argument with a stupid

Never argue with a stupid person. First they'll drag you down to their level, then they will beat you with experience.
Courtesy : Teja's Gtalk status message

Monday, December 1, 2008

Exception Details in JavaScript


function displayException(e,method)
{
if(method != null)
{
alert('Error occured in ' + method);
}
if(e instanceof EvalError)
{
alert('Eval function is used in an incorrect manner. Error name: ' + e.name + '. Error message: ' + e.message);
}
else if(e instanceof RangeError)
{
alert('A numeric variable exceeds its allowed range. Error name: ' + e.name + '. Error message: ' + e.message);
}
else if(e instanceof ReferenceError)
{
alert('An invalid reference is used. Error name: ' + e.name + '. Error message: ' + e.message);
}
else if(e instanceof SyntaxError)
{
alert('Syntax error occured while parsing JavaScript code. Error name: ' + e.name + '. Error message: ' + e.message);
}
else if(e instanceof TypeError)
{
alert('The type of a variable is not as expected. Error name: ' + e.name + '. Error message: ' + e.message);
}
else if(e instanceof URIError)
{
alert('encodeURI() or decodeURI() functions are used in an incorrect manner. Error name: ' + e.name + '. Error message: ' + e.message);
}
else
{
alert('An unspecified error occurred!. Error name: ' + e.name + '. Error message: ' + e.message);
}
}

Friday, November 28, 2008

Java code to execute a command etc

Java code to execute a command and enter input to that command and view output of that command.


import java.io.*;

public class CommandInput {
public static void main(String args[]) throws Exception{
String cmd = "java HelloWorld";
String passwd = "P@ssw0rd";
Runtime rt = Runtime.getRuntime();
Process p = rt.exec(cmd);
(new ConsolePrint(p.getInputStream())).start();
(new ConsolePrint(p.getErrorStream())).start();
BufferedWriter commandWriter = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
commandWriter.write(passwd);
commandWriter.close();
}
}

class ConsolePrint extends Thread{
InputStream is ;
ConsolePrint(InputStream is){
this.is = is;
}
public void run(){
BufferedReader br = new BufferedReader (new InputStreamReader(is));
String line = "";
try{
while ((line=br.readLine())!=null) {
System.out.println(line);
}
}catch(IOException ioe){
System.out.println(ioe.getMessage());
}
}
}

Friday, October 24, 2008

DIV shrink-to-fit width

You need to give the DIV shrink-to-fit width, which you can get by
giving it display: inline-block, display: table, display: table-cell or
float: left. float: left is probably the most widely supported.

JSP KeepGenerated in Weblogic 9 and 10

By default Weblogic deletes the java files generated during the transation phase of a jsp. It just retains the .class files. These java files are sometimes very useful in debugging a jsp.

It can be achieved by modifying the weblogic.xml in the following way :

<jsp-descriptor>
<precompile>false</precompile>
<precompile-continue>false</precompile-continue>
<keepgenerated>true</keepgenerated>
<verbose>true</verbose>
<working-dir>c:/temp/bea</working-dir>
</jsp-descriptor>

If this does not work, then login to Weblogic Admin Console. Go to Deployments. Select your application. If it is an enterprise application you may have to choose the sub-module. Go to Configuration. Apply Lock and Edit. Check the Keepgenerated flag and save the changes.

You may have to restart the server !!!

Sunday, October 5, 2008

GDuration & Duration manipulation

There is a lot of difference between the Java Bean generated for xsd:duration using Apache xbean.jar and Weblogic weblogic ant target jwsc/wsdlc.

While the Apache translates it to org.apache.xmlbeans.GDuration, Weblogic translates it to java.lang.String. Since a String can be anything, it is recommended to use the toString() method in Java XML datatype API javax.xml.datatype.Duration to have consistent behaviour.

So, if there is a requirement to have both the xml beans, then create javax.xml.datatype.Duration using any of the methods present in javax.xml.datatype.DatatypeFactory and then convert it to the required formats.


Duration duration = DatatypeFactory.newInstance().newDuration(durationInMillis);
//for Apache
GDuration gDuration = new GDuration(1, duration.getYears(), duration.getMonths(), duration.getDays(),duration.getHours(),duration.getMinutes(),duration.getSeconds(), null);
//for Weblogic
String s = duration.toString();

Notice that the sign of the duration(positive or negative) is hard-coded as 1(positive). The getSign() method in javax.xml.datatype.Duration returns -1,0,+1. But the passing 0 to the GDuration(int,int,int,int,int,int,int,java.math.BigDecimal) constructor throws an IllegaArgumentException. Also, javax.xml.datatype.Duration does not have a method to return fraction part of the duration unlike the getFraction() method in org.apache.xmlbeans.GDuration.

To get the total number of milliseconds from javax.xml.datatype.Duration, we just need to call either getTimeInMillis(java.util.Calendar)
or getTimeInMillis(java.util.Date). However there is no equivalent API in org.apache.xmlbeans.GDuration.

To get the total number of milliseconds from org.apache.xmlbeans.GDuration, we need to do the following :


GDuration gDuration = new GDuration(durationString);
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(0);
GDate base = new GDate(cal);
GDate d = base.add(gDuration);
long durationInMillis = d.getDate().getTime();

Friday, September 26, 2008

ScrollTop in IE

IE6 and DTD 4.01 requires you to use document.documentElement in stead of document.body, so u will need somthing like the below:

if (document.documentElement && !document.documentElement.scrollTop)
// IE6 +4.01 but no scrolling going on
else if (document.documentElement && document.documentElement.scrollTop)
// IE6 +4.01 and user has scrolled
else if (document.body && document.body.scrollTop)
// IE5 or DTD 3.2

Taken from this post

It was used in a very important fix in our code.

Monday, July 21, 2008

Remove duplicates from a List


Set set = new HashSet();
set.addAll(list);

// avoid overhead :D
if(set.size() < list.size()) {
list.clear();
list.addAll(set);
}

Friday, July 11, 2008

Create New Eclipse Workspace - With All Your Old Settings

If you want to create a new eclipse workspace and preserve all your settings,
simply copy the .metadata/.plugins/org.eclipse.core.runtime/.settings directory into your new workspace directory.
Here is a detaliled article Create New Eclipse Workspace - With All Your Old Settings

Tuesday, July 8, 2008

Celebrity

A celebrity is someone who works hard all his/her life to become known and then wears dark glasses to avoid being recognised

Ideal Marriage

Adam and Eve had an ideal marriage. He didn't have to hear about all the men she could have married, and she didn't have to hear about the way his mother cooked.

Courtesy : Teja

Thursday, July 3, 2008

Yesterday, Today and Tomorrow

Yesterday today was tomorrow yet tomorrow today will be yesterday

Courtesy : Teja

Wednesday, July 2, 2008

Find the path of class being loaded


java.net.URL codeBase = (Class).getClass().getProtectionDomain().getCodeSource().getLocation();
System.out.println(codeBase.getPath());

Change Environment Variable from command line

This says how to change environment variable from command line.
Permanent changes to Environment variables

Gist of the article :

1. Install Windows XP Service Pack 2 Support Tools
2. Use setx.exe

Monday, June 30, 2008

Search Engines

Taken from Netspeak The Hindu

www.powerset.com
www.qwika.com
www.imedix.com
www.revolution-health.com
www.twingly.com
www.safemanuals.com
www.hitlinkz.com
www.twing.com
www.pdf-search-engine.com

Thursday, March 13, 2008

“Open with Notepad” for all file types

In order to add an “Open with Notepad” (or the editor/whatever of your choice) context menu entry under Windows98/2000 for all file types, you’ve got to modify the registry like this:
In the registry key “HKEY_CLASSES_ROOT\*\” find the key “shell” or create it if it’s not there yet.

In “shell” create a new key and call it “openwnotepad” or alike. Change its “Default”-string’s value to “Open with Notepad”. This text will be your context menu text. If no default value is set then "openwnotepad" will appear in the context menu.

In “openwnotepad” create a new key, which you have to call “command”. Change its “Default”-string’s value to “C:\Program Files\notepad.exe" "%1″. Instead of notepad.exe you can point to the editor of your choice.

Warning and disclaimer: Meddling with the windows registry can cause a serious mess-up of your system. Doing so and/or using the information provided here is done at your own risk entirely. Don’t blame me.