Wednesday, March 6, 2013

Javascript to convert HTML entity code to Original Character and viceversa

 
The following javascript functions will be useful when working with special characters in HTML.
 
First function converts html entities to literal characters, second function does the reverse

// This Fucntion will decode an entity to its original character
// eg & will become '&' and '"' will become "
function entityToText(entityText) {
      var d = document.createElement("div");
      d.innerHTML = entityText;
      return d.firstChild.nodeValue;
}
 
// This Fucntion will encode a special character to its entity
// eg ‘&’ will become '&' and " will become '"'
function textToEntity(specialText) {
     var d = document.createElement("div");
     d.appendChild(document.createTextNode(specialText));
     return d.innerHTML;
}
 
 
alert(entityToText('&lt;&gt;&amp;&quot;')); // returns <>&"
alert(textToEntity('<&>')); // returns &lt;&amp;&gt;
 
 
.

To Print the stacktrace of the current method

Problem:

I want to print the call stack trace of the current method.

Solution:

final Throwable t = new Throwable();
t.printStackTrace();
 
 
 
.

Monday, July 13, 2009

Hide an html element while printing the web page.

We may encounter certain scenarios where we may have to hide some part of our web page from printing.
Eg: A printer icon shown in a print preview page. Or a price column in the shopping cart etc.

we can use css for hiding the html elements while printing.

1. Add the following css class to your stylesheet.

@media print {
.printHide {
display:none;
}
}


2. Add the style class, printHide for the elements which you want to hide.

The following eg code will hide the link "print" while printing the page.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<style>
@media print {
.printHide {
display:none;
}
}
</style>
</HEAD>

<BODY>
<a href="javascript:void(0);" onclick="window.print();"
class="printHide">Print This Page</a>

<p>
Text Text Text Text Text
Text Text Text Text Text
</p>

<p>
Text Text Text Text Text
Text Text Text Text Text
</p>

</BODY>
</HTML>

Wednesday, June 24, 2009

Insert Double Byte Characters to Database

We cannot insert the double byte characters (eg: Chinese, Russian, Japanese etc) into a database directly. This is not possible even if we configure the database to Unicode encoding.

All the editors we use to run the sql does not recognize the double byte characters. They support only ASCI characters. So if we try to run a query which contains a double byte character, say Chinese, then the editor will replace the characters with question marks (???) means it does not recognize the symbol.

For Eg: if we copy the following query to an sql editor,

insert into () values ('支付宝');

then the editor will convert it as

insert into () values ('???');


There is a solution for this issue.

We can use the sql function compose() and we can pass the unicode of the characters to the function.

Eg: insert into () values (compose(unistr('\652F\4ED8\5B9D')));

This will insert the double character to the db.

The characters can be converted to the unicode using the following site
http://rishida.net/scripts/uniview/conversion.php


.

Friday, June 12, 2009

How to generate class-path variable in Manifest file of a jar

The project has dependency on some external jar files. To create a build file in such a way that build.xml doesn’t need to be modified when a new jar file is added to the lib folder.

For that we need to generate Class-Path attribute in the MANIFEST.MF dynamically. The following code shows how to do that.


1. Define a path element say, classpath

<path id="classpath">
<fileset dir="${lib.dir}" includes="**/*.jar"/>
</path>

The above code will set the absolute path of all the jar files in the lib folder to the path element 'classpath'

2. Convert the path element values into relative path and store it in a string property, say jar.classpath

<pathconvert property="jar.classpath" refid="classpath" pathsep=" ">
<mapper>
<chainedmapper>
<flattenmapper/>
<globmapper from="*.jar" to="lib/*.jar"/>
</chainedmapper>
</mapper>
</pathconvert>


3. Use the property 'jar.classpath' to define the manifest file while creating the jar.

<target name="jar" depends="clean,compile">
<mkdir dir="${jar.dir}"/>
<mkdir dir="${jar.lib.dir}"/>
<!-- Copying the external libraries to build/jar/lib folder. -->
<copy todir="${jar.lib.dir}">
<fileset dir="${lib.dir}" excludes="**/*.svn"/>
</copy>
<jar destfile="${jar.dir}/${jar.file}" basedir="${classes.dir}" excludes="**/*.svn">
<manifest>
<attribute name="Class-Path" value=". ${jar.classpath}" />
</manifest>
</jar>
</target>

Note :- The lib directory with the external jar files should be in the same directory as that of the project jar file.

We can't put the external jar files inside the project jar files with the above method.

Tuesday, June 9, 2009

Reusability of Thread.

Can you resuse a Thread Object? Can you restart it by calling start() again?

No. Once a thread's run() method has completed, the thread can never be restarted. In fact, at that point the thread moves into a state - dead. In dead state, the thead has finished its run()method and can never be restarted. The Thread object might still be on the heap, as aliving object that you can call other methods on (if appropriate), but the Thread object has permanently lost its 'threadness'. In other words, there is no longer a seperate call stack, and the Thread object is no longer a thread. It's Just an object, at that point, like all other objects.

But there are design patterns for making a pool of threads that you can keep using to perform different jobs. But you don't do it by retsarting a dead thread.

How to Implement a scheduler in java

Use java.util.Timer and java.util.TimerTask to Implement a scheduler in java.

The following are all the Timer methods you can use to schedule repeated executions of tasks:

schedule(TimerTask task, long delay, long period)
schedule(TimerTask task, Date time, long period)
scheduleAtFixedRate(TimerTask task, long delay, long period)
scheduleAtFixedRate(TimerTask task, Date firstTime, long period)

When scheduling a task for repeated execution, use one of the schedule methods when smoothness is important and a scheduleAtFixedRate method when time synchronization is more important.

schedule method is appropriate for activities where it is more important to keep the frequency accurate in the short run than in the long run.

While in scheduleAtFixedRate, each execution is scheduled relative to the scheduled execution time of the initial execution. If an execution is delayed for any reason (such as garbage collection or other background activity), two or more executions will occur in rapid succession to "catch up."

Call Timer.cancel() to stop the Timer.

Example:

public class AnnoyingBeep {
Toolkit toolkit;
Timer timer;

public AnnoyingBeep() {
toolkit = Toolkit.getDefaultToolkit();
timer = new Timer();
timer.schedule(new RemindTask(),
0, //initial delay
1*1000); //subsequent rate
}

class RemindTask extends TimerTask {
int numWarningBeeps = 3;

public void run() {
if (numWarningBeeps > 0) {
toolkit.beep();
System.out.format("Beep!%n");
numWarningBeeps--;
} else {
toolkit.beep();
System.out.format("Time's up!%n");
//timer.cancel(); //Not necessary because
//we call System.exit
System.exit(0); //Stops the AWT thread
//(and everything else)
}
}
}
...
}

To Move a File from One directory to another in Java

We can use File.renameTo() method to move a file from one folder to another.

// File (or directory) to be moved
File file = new File("filename");

// Destination directory
File dir = new File("directoryname");

// Move file to new directory
boolean success = file.renameTo(new File(dir, file.getName()));

if (!success) {
// File was not successfully moved
}

Thursday, April 23, 2009

Install system support for multiple languages (Asian Languages)

Windows XP

This procedure applies only if you are running a language version of Microsoft Windows XP that doesn't match the language you want to type.

  1. On the Windows Start menu, point to Control Panel, and then click Date, Time, Language, and Regional Options.
  2. Click the Regional and Language Options icon, and then click the Languages tab.
  3. In the Supplemental languages support box, select the check boxes that correspond to the languages you want to use.

Windows 2000

This procedure applies only if you are running a language version of Microsoft Windows 2000 that doesn't match the language you want to type.

  1. On the Windows Start menu, point to Settings, and then click Control Panel.
  2. Double-click the Regional Options icon, and then click the General tab.
In the Language settings for the system box, select the check boxes next to the languages you want to use.

SQL for altering user schema to add a new tablespace

The following sql is to create a tablespace after creation of a user schema and then alter the user schema to take the new tablespace as default.
It also changes the system defined tablespace 'temp' as the temporary tablespace for the user.


create tablespace pfss datafile '/u12/oradata/wcrsec01/PFSS.dbf' size 100m autoextend on next 50m maxsize 1000m;

create tablespace pfss_idx datafile '/u12/oradata/wcrsec01/PFSS_IDX.dbf' size 100m autoextend on next 50m maxsize 1000m;

alter user pfss default tablespace pfss temporary tablespace temp;

Alter user pfss quota unlimited on pfss_idx;

Alter user pfss quota unlimited on pfss;