Thursday, July 19, 2012

XML schema and DTD validation using SAX API

SAX (Sequential Access Parser) is a XML parser, of which the java implementation is used to demonstrate here of how to validate a XML document using a DTD or a schema using this simple API.
This particular example uses the DefaultHandler class which is implemented mainly from the ContentHandler class among other classes. This serves as a concrete base in building our program as it's not required to implement the whole ContentHandler class in your program, but override the relevant methods to get the expected behavior.

Here's a very simple code snippet of a XMLSchema validator,

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.helpers.DefaultHandler;

public class SAXParserForXSD extends DefaultHandler {
    public static void main(String[] args) {        
        /*
         * Checking whether a filename is present
         * */
        if(args.length ==0){
            System.out.println("Enter a file name to be parsed.");
            System.exit(1);
        }
        else{
            /*
             * grab the filename
             * */
            String input = args[0];
            
            try{                
                SAXParser parser = null;
                SAXParserFactory factory = SAXParserFactory.newInstance();
                
                //enable namespace facility
                factory.setNamespaceAware(true);
                
                //set validating on
                factory.setValidating(true);
                parser = factory.newSAXParser();
                parser.setProperty(
                          "http://java.sun.com/xml/jaxp/properties/schemaLanguage",
                          "http://www.w3.org/2001/XMLSchema"
                         );
                System.out.println("Validation by XMLSchema.");
                
                SAXParserForXSD handler = new SAXParserForXSD();
                parser.parse(input, handler);
                
            }catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

Here the most interesting code changes from a parsing program is depicted in red.
First you have to tell the parser that this document has to be validated and as XMLSchema supports the usage of namespaces in XML, you have to specify that in your code.(factory.setNamespaceAware(true);)
The key distinction between a schema validator and a DTD validator is the parser property that I've set here. It allows the parser to know that Schema is to be used.

In a DTD validator, you'd have to comment out the line
                factory.setNamespaceAware(true); and you have to remove the setproperty line as it's not required for DTD validation. 

You can create a program to run both validation methods using another argument to select the type of validation required(dtd or xsd using arg[1]).

Sunday, April 15, 2012

Update Realtek Ethernet driver Ubuntu

If your ethernet connection is really slow that might be due to having old drivers. This is a guide to update your realtek ethernet card. But in the same way you can update anyother adapter driver if you find the driver from the provider of that particular ethernet card.

In the Ubuntu terminal run,

lspci | grep Realtek
 
and observe the result. If it shows that you do have a realtek ethernet card then go to the link here and download the latest update for your computer.

and go to the downloaded folder, Extract it. And in the terminal go to that downloaded folder and execute the following commands as shown in the image(other than the cd and ls)

Restart the computer, it should speed up the connection if that was the cause of the slow connection!!!!

Filesharing between Windows 7 and Ubuntu (Networking)

To network an Ubuntu machine for filesharing, you need to install samba packages. You can do that by typing these commands in your Ubuntu Terminal(type terminal inside the dashboard).

sudo apt-get install samba system-config-samba python-glade2

Then you need to add your account to the samba server. You can do that by typing

sudo smbpasswd username -a

and provide your password.

Then in the windows machine, go to the command prompt( type cmd in the start menu) and execute the following command.

net config workstation

and get the workstation domain. (ex : WORKGROUP)
Then in the Ubuntu PC type samba in the dashboard and that will prompt a new window.
Go to preferences -> Server Settings and in the workgroup textfield insert the workstation domain and hit "OK".

In the Windows PC type "manage advanced sharing settings" and turn on file and printer sharing.

Restart both computers and in the network section of both machines, it should show the other computer as well as itself.

IF THAT DOESN'T SHOW, then you need to edit the ip of the Windows PC.
you can do that by
 In your Ubuntu PC find out the IP address by typing ifconfig in the terminal. You should see something like this.
(if you know how ip works, you can go ahead and set the ip of the windows machine to be in the same network as the Ubuntu PC and Skip the next part and it should work)

     write down the inet address and the Mask under the eth0 section.

In the windows PC,
  • "Start" and then go to "Control Panel"
  • Left click on "Network and Internet" and then choose "Network and Sharing Center"
  • On the left side choose "Change adapter settings"
  • Right click on your network card and from the drop-down choose "Properties"
  • Under the "Networking" tab
  • Choose "Internet Protocol Version 4 (TCP/IPv4)" or "Internet Protocol Version 6 (TCP/IPv6)" and then click on "Properties"
  • In next window you have option between "Obtain an IP Address Automatically" and "Use the following IP Address". Choose the latter and,
    here type in the written down ipaddress +1 in the ip field and the subnet mask should be the same as the Mask you wrote down

    ex : ubuntu_ip : 192.168.73.129 (considering the above image, this is different on your PC)
    windows ip should be 192.168.73.130
    subnet mask = Mask = 255.255.255.0     
  • Finally click on "OK" and your IP address should be changed

    What this does is put the two PC's in the same network.Then you can use the ping command in the Ubuntu PC to see whether it works.
    ping windowsip
    ex : ping 192.168.73.130
Then share folders in the two machines and restart. Then It will work !!!!!

If the ethernet connection is really slow, you can follow this guide to update your ethernet drivers. This is written particularly towards Realtek drivers. But the abstract idea can be used to implement towards updating other drivers.


Sunday, February 12, 2012

Unit Testing with ANT

Here I'm going to talk about how to write the XML code for Unit testing with ANT,

 <target name="compile" description="Compile Classes">
        <javac srcdir="${src}" destdir="${target}"/>
 </target>

In the above target, it compiles the source files from the ${src} and store them in the ${target}

<path id="test.classpath">
        <pathelement path="${test}"/>
        <pathelement path="${libs}/JUnit.jar"/>
        <pathelement path="${target}"/>
</path>
Here there are certain type of paths that you should introduce to the junit tests in ANT to run.
  1. ${test} : where your test classes are stored
  2. ${libs}/JUnit.jar : where your jUnit.jar is stored
  3. ${target} : where your compiled classes are stored
  <target name="compile-test" depends="compile" description="Compile Test Classes">
            <javac srcdir="${test}" verbose="true">
            <classpath refid="test.classpath"/>
        </javac>
 </target>
This particular target compiles the test classes to make them ready to run. The refid (means reference ID) here points to the paths mentioned above

    <target name="test-run" depends="compile-test" description="Run Test Cases">       
        <junit printsummary="yes" haltonfailure="yes" showoutput="yes" >
            <classpath refid="test.classpath"/>
            <batchtest fork="yes" todir="${test}">
                <formatter type="xml"/>
                <fileset dir="${test}">
                    <include name="**/*Test*.java"/>
                </fileset>
            </batchtest>
        </junit>
    </target>
 This is the test-run target, meaning this is where the test is run. All you need to understand is when selecting the fileset, I've selected all the test classes but you can limit them here.
If you've read through this simple tutorial, you should get a good understanding of how unit testing works. You can dig into more by following the link below.

If you need more details on ANT :

Saturday, February 4, 2012

Bumblebee 3.0 for Ubuntu 12.10,12.04 and 11.10

The bumblebee project has successfully released an 'optimus replacement' for the linux platform. The latest bumblebee version was released on January 19,2012, Bumblebee 3.0 . It is very stable and enhances the performance of Ubuntu 11.10 thus allowing Unity 3d to be used.
it provides, automatic power management which also survives suspend and a greater battery time.
But as Bumblebee requires low level hardware access, you can't test this on a Virtual Machine

Installing Bumblebee

first, if you haven't already installed the restricted drivers for the Nvidia GPU, do,

sudo apt-get purge nvidia-current  // this will update the GPU drivers

Then open the terminal and add ppa Bumbleebe ”Tumbleweed”, then install

sudo add-apt-repository ppa:bumblebee/stable
sudo apt-get update 
sudo apt-get install bumblebee 
 
To allow you to use Bumblebee (replace $user with your username)
sudo usermod -a -G bumblebee $user
 
Now it's all done, restart the computer and then you can test it,
run
glxspheres

and run
optirun glxspheres

Then observe the difference. Now you can enjoy Unity 3d, in fact Ubuntu boots right into it when it was restarted.

Monday, January 16, 2012

Installing OpenCV 2.3 in Eclipse in Ubuntu 11.04( updated to 12.10)

I'm writing this particular post because I didn't find a convincing working easy to follow article of how to install OpenCV for Eclipse in Ubuntu 11.04 and up (till 12.10).

Installing OpenCV
  1. Install OpenCV libraries from Ubuntu Software Center. Install libcv, libcv-dev, libhighgui, libhighgui-dev (and Whatever other libraries of your favor.)
  2. You can check the installation by opening up a terminal and typing pkg-config --libs opencv and pkg-config --cflags opencv
    and it should result something like this if you have installed all the libraries,

    -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d -lopencv_objdetect -lopencv_contrib -lopencv_legacy -lopencv_flann 

    -I/usr/include/opencv 
Setting up Eclipse
  1. Create a new C++ project and fill in the details 
  2. Then go to Project>Properties>C/C++ Build>Settings> Tools Tab
  3. Under GCC C++ compliler > Directories add /usr/include/opencv


      4. Then under GCC C++ Linker > Libraries add these parameters


Congratulations !! Now you are good to go. Here's a test program to test your configurations..!!

 #include “highgui/highgui.hpp”
int main( int argc, char** argv ) {
   IplImage* img = cvLoadImage( argv[1] );
   cvNamedWindow( “Example1”, CV_WINDOW_AUTOSIZE );
   cvShowImage( “Example1”, img );
   cvWaitKey(0);
   cvReleaseImage( &img );
   cvDestroyWindow( “Example1” );
}
// add a image and rename the img parameter from "Example 1" to whatever your image file name is :)