#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    17
    Rep Power
    0

    Convert this PHP code to Java


    I need this code to be converted into Java.This file is named as XPathExpressioinHandler.php.I need this to be converted to XPathExpressionHandler.java
    Can someone help me out????

    ****************************************************
    <?php

    class XPathExpressionHandler {
    var $name;
    var $expression;
    var $nullable;
    var $cast;
    var $datatype;

    function XPathExpressionHandler($name, $expression, $cast, $nullable, $datatype) {
    $this->parentExpression = $parentExpression;
    $this->name = $name;
    $this->expression = $expression;
    $this->cast = $cast;
    $this->nullable = $nullable;
    $this->datatype = $datatype;
    }

    function createString () {
    $cString = "";
    //create string that looks like this:
    //'<name>' <datatype> <NULL/NOT NULL> default = <'' or '0'>,
    $cString = " `" . $this->name . "` " . $this->datatype . " ";
    if($this->nullable == true) { $cString .= "NULL "; }
    else { $cString .= "NOT NULL "; }
    if($this->cast == "number") { $cString .= "default '0' , "; }
    else if(strtolower($this->datatype) == "text") { $cString .= " , "; }
    else { $cString .= "default '' , "; }
    return $cString;
    }

    function getCast() { return $this->cast; }

    function getName() { return $this->name; }

    function getNullable() { return $this->nullable; }


    function extractData($doc, $baseExpression, $index = 1) {
    $expression = $baseExpression . "[" . $index . "]" . $this->expression;
    echo "Expr XPath: " . $expression;
    $xpath = xpath_new_context($doc);
    xpath_register_ns($xpath, "te", "http://www.teachengineering.org");
    $xpresult = xpath_eval($xpath, $expression);
    $nodeset = $xpresult->nodeset;
    $element = $nodeset[0];
    if($element == NULL) {
    echo " = \"\"<br><br>\n";
    return "";
    }
    $name = $element->node_name();
    $children = $element->child_nodes();
    $textnode = $children[0];
    $text = $textnode->node_value();
    echo " = \"" . $text . "\"<br><br>\n";
    $text = addslashes($text);
    return $text;
    }

    }

    ?>
    ****************************************************
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Location
    Bavaria, Germany
    Posts
    140
    Rep Power
    12
    I didn't do java in some time and since i didn't compile the code it won't work but its a start ;-) (its assuming you're using jdom as your XML library).

    Code:
    import org.jdom.*;
    import org.jdom.xpath.*;
    
    public class XPathExpressionHandler { 
    
      private String name; 
      private String expression; 
      private boolean nullable; 
      private String cast; 
      private String datatype; 
    
      public XPathExpressionHandler(String name, String expression, String cast, boolean nullable, String datatype) { 
        this.parentExpression = parentExpression; 
        this.name = name; 
        this.expression = expression; 
        this.cast = cast; 
        this.nullable = nullable; 
        this.datatype = datatype; 
      } 
    
      public String createString() { 
        String cString = ""; 
        
        //create string that looks like this: 
        //'<name>' <datatype> <NULL/NOT NULL> default = <'' or '0'>, 
        cString = " `" + this.name + "` " + this.datatype + " "; 
        if (this.nullable == true) { cString += "NULL "; } 
        else { cString .= "NOT NULL "; } 
        if(this.cast == "number") { cString += "default '0' , "; } 
        else if(this.datatype.toLower() == "text") { cString += " , "; } 
        else { cString += "default '' , "; } 
        return cString; 
      } 
    
      public String getCast() { return this.cast; } 
      
      public String getName() { return this.name; } 
      
      public bool getNullable() { return this.nullable; } 
    
    
      public String extractData(XmlDocument doc, String baseExpression) {
        return extractData(doc, baseExpression, 1);
      }
      
      public String extractData(Document doc, String baseExpression, int index) {
        String expression = baseExpression + "[" + index + "]" + this.expression; 
        System.out.println("Expr XPath: " + expression); 
        
        XPath xpath = new XPath(expression); 
        xpath.addNamespace("te", "http://www.teachengineering.org");     
        Object result = (Node)xpath.selectSingleNodes(doc);
        if (result == null) {
          System.out.println(" = \"\"<br><br>\n"); 
          return ""; 
        } 
        
        String name = null;
        String text = null;
        if (result instanceof Element) {
          name = ((Element)result).getName();
          text = ((Element)result).getText();
        } else if (result instanceof Attribute) {
          name = ((Attribute)result).getName();            
          text = ((Attribute)result).getValue();
        } else {
          System.out.println(" = \"\"<br><br>\n"); 
          return "";    
        }
            
        System.out.println(" = \"" . text . "\"<br><br>\n"); 
        
        text = text.replace("\\", "\\\\"); 
        text = text.replace("'", "\\'");
        text = text.replace("\"", "\\\");
        
        return text; 
      } 
    
    }
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    17
    Rep Power
    0
    Thank you.I'll try compiling and get back if I find it difficult
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    17
    Rep Power
    0

    Can you give a start for this class also


    need a start for this also.
    ***************************************************
    <?php

    require_once("xpathexpressionhandler.php");

    class XPathGroupHandler {
    //an array of node handlers, these are the child handlers
    var $groups;
    var $expressions;
    var $name;
    var $rootName;
    var $thisExpression;
    var $fullExpression;
    var $cast;
    var $database;

    function XPathGroupHandler($database, $name, $thisExpression, $parentName) {
    $this->groups = array();
    $this->expressions = array();
    $this->database = $database;
    $this->name = $name;
    $this->thisExpression = $thisExpression;
    $this->parentName = $parentName;
    $this->createExpressions();
    $this->dropTable();
    $this->createTable();
    $this->createGroups();
    }

    function createGroups() {
    $groupQuery = "SELECT name, expression FROM relation, types WHERE ( relation.component = types.name ) " .
    "AND ( types.cast = 'group' ) AND ( relation.group = '" . $this->name . "' )";
    echo "Group SQL: " . $groupQuery . "<br><br>\n";
    $groupResult = mysql_query($groupQuery, $this->database);
    while ($row = mysql_fetch_array($groupResult)) {
    $rName = $row['name'];
    $rExpression = $row['expression'];
    $this->groups[] = new XPathGroupHandler($this->database, $rName, $rExpression, $this->name);
    }
    }

    function createExpressions() {
    $expressionQuery = "SELECT name, expression, types.null as nullable, cast, datatype FROM relation, types WHERE ( relation.component = types.name ) " .
    "AND ( types.cast != 'group' ) AND ( relation.group = '" . $this->name . "' )";
    echo "Group SQL: " . $expressionQuery . "<br><br>\n";
    $expressionResult = mysql_query($expressionQuery, $this->database);
    while ($row = mysql_fetch_array($expressionResult)) {
    $rName = $row['name'];
    $rExpression = $row['expression'];
    if(strtolower($row['nullable']) == "yes") {
    $rNullable = true;
    }
    else {
    $rNullable = false;
    }
    $rCast = $row['cast'];
    $rDatatype = $row['datatype'];
    $this->expressions[] = new XPathExpressionHandler($rName, $rExpression, $rCast, $rNullable, $rDatatype);
    }
    }

    function dropTable() {
    $dropStatement = "DROP TABLE `" . $this->name . "`";
    echo "Root SQL: " . $dropStatement . "<br><br>\n";
    $result = mysql_query($dropStatement, $this->database);
    echo "ERROR: " . mysql_error() . "<br><br>";
    }

    function createTable() {
    $createStatement = "CREATE TABLE `" . $this->name . "` ( `id` int(10) unsigned NOT NULL default '0', `parentName` varchar(100) NOT NULL default '', " .
    "`parentID` int(10) unsigned NOT NULL default '0', `index` int(10) unsigned NOT NULL default '0', ";
    foreach($this->expressions as $expression) {
    $createStatement .= $expression->createString();
    }
    $createStatement .= "PRIMARY KEY ( `id` ) , UNIQUE `unique_id` ( `parentName` , `parentID` , `index` ) )";
    echo "Group SQL: " . $createStatement . "<br><br>\n";
    $result = mysql_query($createStatement, $this->database);
    echo "ERROR: " . mysql_error() . "<br><br>";
    }

    function extractData($doc, $baseExpression, $parentID) {
    $currentExpression = $baseExpression . $this->thisExpression;
    $countExpression = "count(" . $currentExpression . ")";
    echo "Group XPath: " . $countExpression;
    $xpath = xpath_new_context($doc);
    xpath_register_ns($xpath, "te", "http://www.teachengineering.org");
    $xpresult = xpath_eval($xpath, $countExpression);
    $groupCount = $xpresult->value;
    echo " = $groupCount<br><br>\n";
    for($loop = 1; $loop <= $groupCount; $loop++) {
    $countQuery = "SELECT MAX(`id`) FROM `" . $this->name . "`";
    echo "Group SQL: " . $countQuery . "<br><br>\n";
    $result = mysql_query($countQuery, $this->database);
    $row = mysql_fetch_array($result);
    $thisID = $row[0] + 1;
    $columns = "`id` , `parentName` , `parentID` , `index`";
    $values = "'" . $thisID . "' , '" . $this->parentName . "' , '" . $parentID . "' , '" . $loop . "'";
    foreach($this->expressions as $expression) {
    $exprValue = $expression->extractData($doc, $currentExpression, $loop);
    $exprNullable = $expression->getNullable();
    $exprName = $expression->getName();
    $exprCast = $expression->getCast();
    // echo "GROUP value: $exprValue ; nullable: $exprNullable ; name: $exprName ; cast: $exprCast ;<br>\n";
    if(($exprNullable == false) && ($exprValue == "")) {
    /* throw error */
    echo "** GROUP NULL ERROR **<br><br>\n";
    }
    if($exprCast == "string") {
    $exprValue = "'" . $exprValue . "'";
    }
    if($exprValue != "") {
    $columns .= " , `" . $exprName . "`";
    $values .= " , " . $exprValue;
    }
    }
    $insertStatement = "INSERT INTO `" . $this->name . "` ( " . $columns . " ) VALUES ( " . $values . " )";
    echo "Group SQL: " . $insertStatement . "<br><br>\n";
    $result = mysql_query($insertStatement, $this->database);
    echo "ERROR: " . mysql_error() . "<br><br>";
    echo "<blockquote>\n";
    foreach($this->groups as $group) {
    $nextExpression = $baseExpression . $this->thisExpression . "[" . $loop . "]";
    $group->extractData($doc, $nextExpression, $thisID);
    }
    echo "</blockquote>\n";
    }
    }

    }

    ?>
    *********************************************
  8. #5
  9. /(bb|[^b]{2})/

    Join Date
    Nov 2001
    Location
    Somewhere in the great unknown
    Posts
    5,163
    Rep Power
    792
    This should be moved to the Java forum.
  10. #6
  11. Moderator Emeritus
    Devshed Supreme Being (6500+ posts)

    Join Date
    Feb 2002
    Location
    Austin, TX
    Posts
    7,186
    Rep Power
    2265
    Originally posted by Onslaught
    This should be moved to the Java forum.
    Per Onslaught's request, moved from PHP to Java forum.
    DrGroove, Devshed Moderator | New to Devshed? Read the User Guide | Connect with me on LinkedIn
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    17
    Rep Power
    0
    Any ideas about the question?
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    17
    Rep Power
    0
    I need help on this particular function
    function extractData($doc, $baseExpression, $index = 1) {
    $expression = $baseExpression . "[" . $index . "]" . $this->expression;
    echo "Expr XPath: " . $expression;
    $xpath = xpath_new_context($doc);
    xpath_register_ns($xpath, "goo", "http://www.foo.org");
    $xpresult = xpath_eval($xpath, $expression);
    $nodeset = $xpresult->nodeset;
    $element = $nodeset[0];
    if($element == NULL) {
    echo " = \"\"<br><br>\n";
    return "";
    }
    $name = $element->node_name();
    $children = $element->child_nodes();
    $textnode = $children[0];
    $text = $textnode->node_value();
    echo " = \"" . $text . "\"<br><br>\n";
    $text = addslashes($text);
    return $text;
    }
  16. #9
  17. No Profile Picture
    Moderator =(8^(|)
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2002
    Location
    Sacramento, CA
    Posts
    1,710
    Rep Power
    14
    You're going to need an xpath library for java. I'll recommend dom4j since I've been very happy with it. http://dom4j.org/
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    17
    Rep Power
    0
    Its not the question of Dom4j but converting this PHP snippet to Java. Any help is appreciated.
  20. #11
  21. No Profile Picture
    Moderator =(8^(|)
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2002
    Location
    Sacramento, CA
    Posts
    1,710
    Rep Power
    14
    Yes, you're going to need a java library that understands xpath expressions in order to convert the code.
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    17
    Rep Power
    0
    I am currently using JDOM that would serve the same purpose. Any help in converting the code?
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    1
    Rep Power
    0

    Convert this PHP to java


    //@trigrams = ();
    while(<>){@words = split /\s/, $_;
    for($i=0 ; $i<$#words-1 ; $i++){
    $trigram = $words[$i] . $words[$i+1] . $words[$i+2];
    $found = -1;
    if(@trigrams){
    SEARCHtrigramINDEX:
    for($index = 0; $index <= $#trigrams; $index++){
    if($trigrams[$index] eq $trigram){
    $found = $index;
    last SEARCHtrigramINDEX;
    }
    }
    }
    if($found > -1){
    $trigramfrequency[$found]++;
    }
    else{
    push @trigrams , $trigram;
    $trigramfrequency[$#trigrams]++;
    }
    }
    } print "trigram frequencies in your text:\n";
    for($index = 0; $index <= @trigrams; $index++){
    print "$trigrams[$index] $trigramfrequency[$index]\n";
    }

    }

    }
    }
    }
  26. #14
  27. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0
    i recommend you to use or search on the internet..

IMN logo majestic logo threadwatch logo seochat tools logo