An Excerpt from http://www.paulgraham.com..
The language to learn, if you want to get a good job, is a language that people don't learn merely to get a job.
Read the complete article..
Wednesday, December 22, 2010
Tuesday, December 21, 2010
SAXParseException: White spaces are required between publicId and systemId
An error often encountered by newbies building webapps using Spring3 ..
org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 1 in XML document from ServletContext resource [/WEB-INF/myservlet-servlet.xml] is invalid; nested exception is org.xml.sax.SAXParseException: White spaces are required between publicId and systemId.
This occurs due to improper sequence of entries in schemaLocation definition in spring configuration (say /WEB-INF/myservlet-servlet.xml).
sample of incorrect entries
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation=
"http://www.springframework.org/schema/beans
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/context
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
This will cause,
org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 1 in XML document from ServletContext resource [/WEB-INF/myservlet-servlet.xml] is invalid; nested exception is org.xml.sax.SAXParseException: White spaces are required between publicId and systemId.
Correct the order of entries so that Namespace and Schemalocation are defined alternately within the schemaLocation attribute.
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation=
"http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
DTD:
Defines xml document structure .
Saved as .dtd file and can be referenced in an xml document.
Here is a good tutorial on dtd.
Schema definition:
Successor to dtd.
Superior.
Is written as xml and saved in .xsd file.
More on schema can be found here.
org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 1 in XML document from ServletContext resource [/WEB-INF/myservlet-servlet.xml] is invalid; nested exception is org.xml.sax.SAXParseException: White spaces are required between publicId and systemId.
This occurs due to improper sequence of entries in schemaLocation definition in spring configuration (say /WEB-INF/myservlet-servlet.xml).
sample of incorrect entries
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation=
"http://www.springframework.org/schema/beans
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/context
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
This will cause,
org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 1 in XML document from ServletContext resource [/WEB-INF/myservlet-servlet.xml] is invalid; nested exception is org.xml.sax.SAXParseException: White spaces are required between publicId and systemId.
Correct the order of entries so that Namespace and Schemalocation are defined alternately within the schemaLocation attribute.
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation=
"http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
DTD:
Defines xml document structure .
Saved as .dtd file and can be referenced in an xml document.
Here is a good tutorial on dtd.
Schema definition:
Successor to dtd.
Superior.
Is written as xml and saved in .xsd file.
More on schema can be found here.
Friday, December 17, 2010
java support for tiff and other image formats
Java ImageIO supports most common image formats like jpeg,png,gif,bmp etc..Tiff format is not one them.This caused some inconvenience when I was working on a face recognition app that uses tiff images from the JAFFE image database (http://www.kasrl.org/jaffe.html).So I had to use plugins from jai-imageio .To check availability of image readers for various image formats,I modified the code in my previous post slightly.
...
public static void checkImage(String name) throws IOException{
ImageInputStream imgInStream = ImageIO.createImageInputStream(new FileInputStream(name));
Iterator readers = ImageIO.getImageReaders(imgInStream);
if (readers.hasNext()) {
ImageReader reader = readers.next();
System.out.println("At least one image reader exists for "+name);
System.out.println("image reader ="+reader);
}else{
System.out.println("No image reader exists for "+name);
}
}
public static void printAllSupportedImageFormats(){
String[] names = ImageIO.getWriterFormatNames();
System.out.println("supports "+names.length+" formats");
for ( String name: names ){
System.out.print( name +" ");
}
System.out.println();
}
public static void main(String[] args) throws IOException {
printAllSupportedImageFormats();
String img1 = "/home/sajan/Pictures/Bounty_Ship.jpg";
String img2 = "/home/sajan/Pictures/PGPHX82.GIF";
String img3 = "/home/sajan/Pictures/abe_natsumi.ppm";
String img4 = "/home/sajan/Pictures/abe_natsumi.pgm";
String img5 = "/home/sajan/images/JAFFEPRB/KLH158.tiff";
String[] imgs = new String[]{img1,img2,img3,img4,img5};
for (String img : imgs){
checkImage(img);
}
}
...
Using standard ImageIO,I got this result...
supports 12 formats
jpg BMP bmp JPG jpeg wbmp png JPEG PNG WBMP GIF gif
At least one image reader exists for /home/sajan/Pictures/Bounty_Ship.jpg
image reader =com.sun.imageio.plugins.jpeg.JPEGImageReader@1f42b49
At least one image reader exists for /home/sajan/Pictures/PGPHX82.GIF
image reader =com.sun.imageio.plugins.gif.GIFImageReader@87a5cc
No image reader exists for /home/sajan/Pictures/abe_natsumi.ppm
No image reader exists for /home/sajan/Pictures/abe_natsumi.pgm
No image reader exists for /home/sajan/images/JAFFEPRB/KLH158.tiff
After adding jai-imageio-1.1.jar to the classpath,the result was...
supports 24 formats
BMP raw JPEG2000 RAW jpeg tif WBMP jpeg2000 GIF TIF TIFF bmp jpg PNM JPG pnm wbmp png JPEG PNG jpeg 2000 gif JPEG 2000 tiff
at least one image reader exists for /home/sajan/Pictures/Bounty_Ship.jpg
image reader =com.sun.imageio.plugins.jpeg.JPEGImageReader@1adc30
at least one image reader exists for /home/sajan/Pictures/PGPHX82.GIF
image reader =com.sun.imageio.plugins.gif.GIFImageReader@1bc887b
at least one image reader exists for /home/sajan/Pictures/abe_natsumi.ppm
image reader =com.sun.media.imageioimpl.plugins.pnm.PNMImageReader@17ee8b8
at least one image reader exists for /home/sajan/Pictures/abe_natsumi.pgm
image reader =com.sun.media.imageioimpl.plugins.pnm.PNMImageReader@1995d80
at least one image reader exists for /home/sajan/images/JAFFEPRB/KLH158.tiff
image reader =com.sun.media.imageioimpl.plugins.tiff.TIFFImageReader@1df280b
The ImageReader implementations for PNMImageReader and TIFFImageReader above are from jai-imageio .
...
public static void checkImage(String name) throws IOException{
ImageInputStream imgInStream = ImageIO.createImageInputStream(new FileInputStream(name));
Iterator
if (readers.hasNext()) {
ImageReader reader = readers.next();
System.out.println("At least one image reader exists for "+name);
System.out.println("image reader ="+reader);
}else{
System.out.println("No image reader exists for "+name);
}
}
public static void printAllSupportedImageFormats(){
String[] names = ImageIO.getWriterFormatNames();
System.out.println("supports "+names.length+" formats");
for ( String name: names ){
System.out.print( name +" ");
}
System.out.println();
}
public static void main(String[] args) throws IOException {
printAllSupportedImageFormats();
String img1 = "/home/sajan/Pictures/Bounty_Ship.jpg";
String img2 = "/home/sajan/Pictures/PGPHX82.GIF";
String img3 = "/home/sajan/Pictures/abe_natsumi.ppm";
String img4 = "/home/sajan/Pictures/abe_natsumi.pgm";
String img5 = "/home/sajan/images/JAFFEPRB/KLH158.tiff";
String[] imgs = new String[]{img1,img2,img3,img4,img5};
for (String img : imgs){
checkImage(img);
}
}
...
supports 12 formats
jpg BMP bmp JPG jpeg wbmp png JPEG PNG WBMP GIF gif
At least one image reader exists for /home/sajan/Pictures/Bounty_Ship.jpg
image reader =com.sun.imageio.plugins.jpeg.JPEGImageReader@1f42b49
At least one image reader exists for /home/sajan/Pictures/PGPHX82.GIF
image reader =com.sun.imageio.plugins.gif.GIFImageReader@87a5cc
No image reader exists for /home/sajan/Pictures/abe_natsumi.ppm
No image reader exists for /home/sajan/Pictures/abe_natsumi.pgm
No image reader exists for /home/sajan/images/JAFFEPRB/KLH158.tiff
After adding jai-imageio-1.1.jar to the classpath,the result was...
supports 24 formats
BMP raw JPEG2000 RAW jpeg tif WBMP jpeg2000 GIF TIF TIFF bmp jpg PNM JPG pnm wbmp png JPEG PNG jpeg 2000 gif JPEG 2000 tiff
at least one image reader exists for /home/sajan/Pictures/Bounty_Ship.jpg
image reader =com.sun.imageio.plugins.jpeg.JPEGImageReader@1adc30
at least one image reader exists for /home/sajan/Pictures/PGPHX82.GIF
image reader =com.sun.imageio.plugins.gif.GIFImageReader@1bc887b
at least one image reader exists for /home/sajan/Pictures/abe_natsumi.ppm
image reader =com.sun.media.imageioimpl.plugins.pnm.PNMImageReader@17ee8b8
at least one image reader exists for /home/sajan/Pictures/abe_natsumi.pgm
image reader =com.sun.media.imageioimpl.plugins.pnm.PNMImageReader@1995d80
at least one image reader exists for /home/sajan/images/JAFFEPRB/KLH158.tiff
image reader =com.sun.media.imageioimpl.plugins.tiff.TIFFImageReader@1df280b
The ImageReader implementations for PNMImageReader and TIFFImageReader above are from jai-imageio .
Sunday, December 12, 2010
checking for Images
I needed to parse a directory and determine which files are images that can be displayed on a panel in my java application. I thought of checking the extensions of files and returning a boolean value of 'true' if they matched those of image files. Examining the folder, I found that somebody had renamed the files to include incorrect file extensions. 'Fake.jpg' was actually a text file. Some image files didn't have extensions at all. 'RealImage' was a .png file. So, checking for extensions wouldn't work.
Then I thought of taking a file and finding out whether the file data bytes started with magic numbers which identify a particular image format .For example ,a PNG file header starts with an 8 bytes signature of
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Iterator;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
public class Utils {
public static boolean isImageFile(String filename) throws IOException{
boolean isImage=false;
FileInputStream is = new FileInputStream(filename);;
ImageInputStream imginstream = ImageIO.createImageInputStream(is);
// get image readers who can read the image format
Iterator iter = ImageIO.getImageReaders(imginstream);
if (iter.hasNext()) {
isImage=true;
}
return isImage;
}
}
Then I thought of taking a file and finding out whether the file data bytes started with magic numbers which identify a particular image format .For example ,a PNG file header starts with an 8 bytes signature of
0x89504e470d0a1a0a
whereas a GIF89a file has a signature 0x474946383961.
Examining the bytes is the right way to find out the file format. But then ,I didn't want to put too many if then blocks to check every possible image file format that may happen to be in the directory. So I thought of using java's ImageIO class to check for valid images.import java.io.FileInputStream;
import java.io.IOException;
import java.util.Iterator;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
public class Utils {
public static boolean isImageFile(String filename) throws IOException{
boolean isImage=false;
FileInputStream is = new FileInputStream(filename);;
ImageInputStream imginstream = ImageIO.createImageInputStream(is);
// get image readers who can read the image format
Iterator
if (iter.hasNext()) {
isImage=true;
}
return isImage;
}
}
running directories and zip files in python
After coding an application in python,I wanted to distribute the source in a way any user can run the application without bothering to install it.Luckily ,in Python 2.6 ,a zip archive or directory can be executed by passing their names to python interpreter.
The section http://docs.python.org/release/2.6.5/whatsnew/2.6.html#other-language-changes mentions.
"Directories and zip archives containing a __main__.py file can now be executed directly by passing their name to the interpreter.The directory or zip archive is automatically inserted as the first entry in sys.path."
Suppose my application contains a package called 'mypackage' which has all the application code.I can create a front-end to the application and put it in a file called __main__.py
import mypackage
mypackage.myapplication_method()
1.executing the directory
I put this __main__.py file and mypackage into a folder mycode.Now the folder structure is say,
C:\python\mycode
-- mypackage
-- __main__.py
Now ,at the command prompt ,I can cd to C:\python and call python mycode to execute the application.
2.executing the zip archive
Put the __main__.py and mypackage in to a zip file ,say myzip.zip.Copy this zipfile to C:\python directory.At the command prompt ,cd to C:\python and run python myzip.zip to execute the application.
There is no need to set PYTHONPATH.
These two facilities make life easier for those who doesn't want to go to the trouble of creating/running setup scripts just to distribute/execute a demo application which may contain multiple packages.
The section http://docs.python.org/release/2.6.5/whatsnew/2.6.html#other-language-changes mentions.
"Directories and zip archives containing a __main__.py file can now be executed directly by passing their name to the interpreter.The directory or zip archive is automatically inserted as the first entry in sys.path."
Suppose my application contains a package called 'mypackage' which has all the application code.I can create a front-end to the application and put it in a file called __main__.py
import mypackage
mypackage.myapplication_method()
1.executing the directory
I put this __main__.py file and mypackage into a folder mycode.Now the folder structure is say,
C:\python\mycode
-- mypackage
-- __main__.py
Now ,at the command prompt ,I can cd to C:\python and call python mycode to execute the application.
2.executing the zip archive
Put the __main__.py and mypackage in to a zip file ,say myzip.zip.Copy this zipfile to C:\python directory.At the command prompt ,cd to C:\python and run python myzip.zip to execute the application.
There is no need to set PYTHONPATH.
These two facilities make life easier for those who doesn't want to go to the trouble of creating/running setup scripts just to distribute/execute a demo application which may contain multiple packages.
Subscribe to:
Posts (Atom)