Friday, April 20, 2007

ebooks ©

E Books that are free online , i.e. damn free

2020 ok
Pub 001

Wednesday, April 18, 2007

PHP Parser in java .

Yup , it is a PHP Parser in java.
The last week, one guy shows up and tells me to make a PHP Parser using JAVA.
Fine this is it.

Note : This is free, damn free

Please name the path for the php executable before compiling the code.

/*
*FileName : PhpParser.java
*Date Author Details
*Mar 31 07 200401058\RAJA PRATHYUSH KUMAR THOTA Created
*
*PhpParser parses a PHP file using the PHP interpreter path given by the user ( Path of Executable ) and the input php file ( Ex. test.php ) and
*outputs a HTML page ( Ex. test.php.html )
*
*The file gets the PHP executable path from the Preferences file
*
*@param String Path of the PHP file
*@param String Path of the PHP Executable
*@return File Parsed HTML Page
*
*
*
* Testing Guidelines
*
* -----
*
* Specify the path for the php executable while declaration. ( Ex. C:\Php\Php.exe or /usr/bin/php )
* Provide Absolute Path for the php file in the MAIN method, ensure you are escaping special characteristics ( Ex. ParsePhp("C:\\PHP TEST\\test.php") )
*
*/
import java.io.*;

class PhpParser
{
/*
*Decalare Variables
*
*/
public String FileName;
public String Cmd;
public String PhpExePath ; // give the php executable path

/*
*Method for Printing
*
*@param String
*@return Void
*/
public void Print ( String str )
{
System.out.println(str);
}

/*
*Constructor
*
*/
public PhpParser(String fileName)
{
FileName = fileName ;
}

/*
*Executes the command and obtains HTML file as Output
*
*@param String FileName
*@return Creates HTML file
*
*
*/

public void ParsePhp() throws IOException
{

String[] command;
fileName = FileName ;

// Exract the Directory Name and the File Name
File Dir = new File(getDirName(fileName));
FileName = getFileName(fileName);

//Obtain the OS name
String OsName = System.getProperty("os.name");


// OS Specific assignments
if(OsName.substring(0,6).equals("Window"))
{
command = new String[6];
command[0] = "cmd";
command[1] = "/c";
command[2] = PhpExePath;
command[3] = FileName;
command[4] = ">";
command[5] = FileName+".html";
}
else
{
PhpExePath = "/usr/bin/php";
//code for unix comes here
command = new String[3];
command[0] = "/bin/bash";
command[1] = "-c";
command[2] = PhpExePath+" "+FileName+" > "+FileName+".html";
}

/*
Print(getDirName(fileName));
Print(getFileName(fileName));
*/

// create environment for execution

String[] envp= new String[1];
envp[0] = "";

// executing the command

Process p = Runtime.getRuntime().exec(command,envp,Dir);

}

/*
*
*Extracts Directory Name from the given absolute path of the file
*
*@param String file name
*@return String Directory Name
*
*/

public String getDirName(String fileName)
{

String DirName;

//obtain the system specifc file separator
String Separator = File.separator;

// get the last occurence of the file separator
int LastIndexSep = fileName.lastIndexOf(Separator);

//get the dirname from the substring after the last occurence of the file separator
DirName = fileName.substring(0,LastIndexSep);

return DirName;
}

/*
*
*Obtains FileName from the absolute path
*
*@param String Absolute Path of the file
*@return String File name
*
*/
public String getFileName(String fileName)
{
// obtain the system specific file separator
String Separator = File.separator;

//get file name
int LastIndexSep = fileName.lastIndexOf(Separator);
fileName = fileName.substring(LastIndexSep+1);
//Print(fileName);
return fileName;
}

/*
* Main for testing the module
*
*/

public static void main (String args[]) throws IOException
{

PhpParser parser = new PhpParser();
parser.ParsePhp(); // parse the php

}

}