Saturday, 20 September 2008

C++ in plain english ( book recommendation )

Are you a C / C++ developer?

I was looking for something in my books today and decided to write this post for those who use C / C++ .
It’s about one particular book I own, probably my favorite...

Have you ever heard of a book “C++ in plain English”? Yes? No?
Well, if you ARE a C / C++ developer, than you definitely should get one!
I have one for several years, and I’m completely satisfied with it!



This is not a book that will help you learn C / C++ from the beginning.
It has a purpose like a dictionary: to help you find what some function, operator or class does and how to use it…

In the first part of the book you’ll find a complete overview of C++.
Complete syntax , operators , functions and classes of ANSI C++ are listed and explained in it.

The book itself helps you find what you need in two ways:

- By asking you what do you want to do , and than telling you which function , operator or class to use…
- By listing all functions , operators and classes with explanation what it does and when to use it…

The second part of the book is C++ in practice. Here you’ll find texts about OOP and all of it’s principles , and about many other things you use in C++ programming.

Great thing about all this is that entire book is indeed one big C++’s dictionary and a reminder , and because of that ( in my opinion ) professionals who work with C/C++ will be more satisfied with it than the beginners looking for a way to learn C/C++ .

I used it while working on several projects already, and every time I found an answer and a solution to a problem…

Tuesday, 16 September 2008

Parsing XML: PHP vs Java

Let's see how much time does PHP programmer spend to parse some XML file.
Here is our test XML file:


and this is how I would parse this document in PHP:


and the result would be:
 
Nice, don't you think?
And this is how I would parse the same file in Java:


Main.java:

package me.tcom.kdarko.blog;

import java.io.IOException;
import java.util.ArrayList;
import org.xml.sax.SAXException;

public class Main
{
   
    private ArrayList arraylist = null ;
   
    public static void main(String[] args)
    {
    try {
        Main m = new Main();
        m.arraylist = new EmployeesListParser("list_of_employees.xml").doParse();
        for(Employee e:m.arraylist)
        System.out.println("Employee: ID: " + e.getId() + " , " + e.getName() + "  " + e.getLast_name());       
        }
        catch (IOException ex)
        {
        System.out.println(ex.getMessage());
        }
        catch (SAXException ex)
        {
        System.out.println(ex.getMessage());
        }
        catch (Exception ex)
        {
        System.out.println(ex.getMessage());
        } 
    }

}
Employee.java:

package me.tcom.kdarko.blog;

public class Employee
{

    String id , name, last_name;

    public Employee() {
    }
  
    public Employee(String id, String name, String last_name) {
        this.id = id;
        this.name = name;
        this.last_name = last_name;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getLast_name() {
        return last_name;
    }

    public void setLast_name(String last_name) {
        this.last_name = last_name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
       
}
EmployeesListParser.java:

package me.tcom.kdarko.blog;

import java.io.IOException;
import java.util.ArrayList;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import com.sun.org.apache.xerces.internal.parsers.SAXParser;

public class EmployeesListParser implements ContentHandler
{
    private String XML_FILE_NAME;
    String temp = null;
    Employee employee = new Employee();
    StringBuffer sb=new StringBuffer();
    private ArrayList arraylist = new ArrayList();

    public EmployeesListParser(String XML_FILE_NAME) {
        this.XML_FILE_NAME = XML_FILE_NAME;
    }
   
    public ArrayList doParse() throws IOException, SAXException
    {
     XMLReader parser = new SAXParser();
     parser.setContentHandler(this);
     parser.parse(XML_FILE_NAME);
     return arraylist;  
    }

    public void startElement(String uri, String name, String qName, Attributes atts) throws SAXException
    {
         sb.setLength(0);
         if(name.equalsIgnoreCase("id"))
             temp="id";
         else if(name.equalsIgnoreCase("name"))
             temp="name";
         else if(name.equalsIgnoreCase("last_name"))
             temp = "last_name";
         else if(name.equalsIgnoreCase("employee"))
          employee = new Employee();
    }

    public void endElement(String uri, String name, String qName) throws SAXException
    {
    sb.setLength(0);
    if(name.equalsIgnoreCase("employee"))
    {
     arraylist.add(employee);
     employee=null;
    }
    }

    public void characters(char[] arg0, int arg1, int arg2) throws SAXException
    {
        sb.append(arg0,arg1,arg2);
        String s = sb.toString().trim();
        if(!s.equals(""))
        {
        if( temp.equalsIgnoreCase("id") )
        {
         employee.setId( s );
        }
        else if( temp.equalsIgnoreCase("name") )
        {
        employee.setName( s );
         }
         else if( temp.equalsIgnoreCase("last_name") )
         {
        employee.setLast_name(s);
         }
    }
    }

    public void setDocumentLocator(Locator locator) {
    }

    public void startDocument() throws SAXException {
    }

    public void endDocument() throws SAXException {
    }

    public void startPrefixMapping(String prefix, String uri) throws SAXException {
    }

    public void endPrefixMapping(String prefix) throws SAXException {
    }

    public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
    }

    public void processingInstruction(String target, String data) throws SAXException {
    }

    public void skippedEntity(String name) throws SAXException {
    }
   
}
 
and the result would be:



:-)
No comment...

Friday, 12 September 2008

Data block wizard problem: cannot select all available columns

I had a problem few days ago with oracle forms builder,  and unfortunately could not find answer on the net why is it happening…
I created a view and needed to make a data block on my canvas, but data block wizard refused to let me choose all available columns in that view:

Let’s start from the beginning.
Let’s say we have a table called test:

Let’s make some (stupid) view of it. Let’s say we need a view that looks like this:
Select id, first , second, first || second as "FIRST AND SECOND TOGETHER" from test

Lets call it test_view.
So, I wrote:
Create view test_view as Select id, first , second, first || second as "FIRST AND SECOND TOGETHER" from test
And I couldn’t use column "FIRST AND SECOND TOGETHER" !!!???

This was the first time this happened to me, and I was working with view in data block wizards before…
After a while I finally found out why this was happening.
It seems that oracle forms builder  won’t let you select column that was named as “” in create view statement.
It is totally legal to say:
 Create view test_view as
Select id, first , second, first || second as "FIRST AND SECOND TOGETHER" from test
And it will work just fine, but you won’t be able to use it in oracle forms builder.
So, at the end, I  was foced to name my column FIRST_AND_SECOND_TOGETHER ( one word!!!) :
CREATE OR REPLACE VIEW TEST_VIEW
AS
select id , first, second , first || second as FIRST_AND_SECOND_TOGETHER from test;
and it worked!!!
I hope this will help someone…