Friday, 17 February 2012

Extract summary of web page

Extract summary of web page
This task use java and jsoup to extract summary of web page.
Extract summary of web page
  1. Create Summary class as following
  2. Call Summary.extract() method as following
Call Summary.extract() method
String link = "http://forums.digitalpoint.com";
Document doc = Jsoup.parse(new URL(link), 60000);
String sum = Summary.extract(doc);
logger.info(sum);
    
Summary class
public static class Summary {
     
    public static String extract(Document doc) {
        String tag = "";
        List<String> lines = new ArrayList<String>();
         
        for (int i = 0; i < doc.children().size(); i++) {
            summary(doc.child(i), lines);
        }

        for (int i = 0; i < lines.size(); i++) {
            if (tag.length() > 0) tag += "\r\n";
            tag += lines.get(i);
        }
         
        return tag;
    }
        
    private static void summary(Element ele, List<String> lines) {
        if (ele.children().size() == 0 || allowedChildren(ele)) {
            String[] tags = new String[] { "div", "p" };
            for (int i = 0; i < tags.length; i++) {
                if (ele.tagName().equalsIgnoreCase(tags[i])) {
                    String text = ele.text().trim();
                    if (text.length() > 0) {
                        if (text.endsWith(".") || text.endsWith("?")) {
                            lines.add(text);
                        }
                    }
                }
            }
        } else {
            for (int i = 0; i < ele.children().size(); i++) {
                summary(ele.child(i), lines);
            }
        }
    }
        
    private static boolean allowedChildren(Element ele) {
        String[] tags = new String[] { "a", "b", "i", "strong" };
        for (int i = 0; i < ele.children().size(); i++) {
            Element child = ele.child(i);
            boolean found = false;
            for (int j = 0; j < tags.length; j++) {
                if (tags[j].equalsIgnoreCase(child.tagName())) {
                    found = true;
                    break;
                }
            }
            if (!found) return false;
        }
        return true;
    }
     
}
    

  Protected by Copyscape Online Copyright Protection

Task tagged by [jsoup]

  1. Extract summary of web page
  2. Grab jobs from vWorker
  3. Grab jobs from Freelancer
  4. Grab jobs from oDesk
  5. Grab jobs from Stack Overflow Careers
  6. Extract Lotus Notes database icon
  7. Create javascript sandbox with jsoup support
  8. Grab vBulletin pages in logged-in mode
  9. Add JSON support to javascript sandbox
  10. Grab google search results
  11. Grab categories from Amazon aStores
  12. Grab products from Amazon aStores
  13. Grab categories from BestBuy
  14. Grab products from BestBuy
  15. Grab categories from Walmart
  16. Grab products from Walmart
  17. Grab categories from Newegg
  18. Grab products from Newegg
  19. Add Lucene support to javascript sandbox
  20. Add MySQL support to javascript sandbox
  21. Grab categories from HP Shopping
  22. Grab products from HP Shopping
  23. Grab video from YouTube
  24. Grab video from DailyMotion
  25. Grab book/journal from ScienceDirect
  26. Grab article from ScienceDirect
  27. Grab search results from Google
  28. Grab search results from Bing
  29. Grab search results from Yahoo

Parse simple math expression

Parse simple math expression
This task use java to parse simple math expression.
Parse simple math expression
  1. Create Formula class as following
  2. Call Formula.calculate() method as following
Call Formula.calculate() method
Map<String, Object> data = new HashMap<String, Object>();
data.put("a", 2);
data.put("b", 3);
String formula = "a - b * a";
Object result = Formula.calculate(data, formula);
if (result != null) {
    logger.info(result.toString());
} else {
    logger.info("NULL");
}
    
Formula class
public static class Formula {

    private Map<String, Object> data;
    private String formula;

    public Formula(Map<String, Object> data, String formula) {
        this.data = data;
        this.formula = formula;
    }
     
    public static Object calculate(Map<String, Object> data, String formula) {
        return new Formula(data, formula).calculate();
    }
     
    public Object calculate() {
        int pos = lastOperator(formula, new String[] { "+", "-" });
        if (pos < 0) {
            pos = lastOperator(formula, new String[] { "*", "/" });
            if (pos < 0) {
                return calculate(formula);
            } else {
                return calculate(formula.substring(0, pos).trim(), formula.charAt(pos) + "", formula.substring(pos + 1).trim());
            }
        } else {
            return calculate(formula.substring(0, pos).trim(), formula.charAt(pos) + "", formula.substring(pos + 1).trim());
        }
    }
     
    private Object calculate(String leftSide, String operator, String rightSide) {
        Object valLeft = null;
        Object valRight = null;
        int pos = lastOperator(leftSide, new String[] { "+", "-" });
        if (pos < 0) {
            pos = lastOperator(leftSide, new String[] { "*", "/" });
            if (pos < 0) {
                valLeft = calculate(leftSide);
            } else {
                valLeft = calculate(leftSide.substring(0, pos).trim(), leftSide.charAt(pos) + "", leftSide.substring(pos + 1).trim());
            }
        } else {
            valLeft = calculate(leftSide.substring(0, pos).trim(), leftSide.charAt(pos) + "", leftSide.substring(pos + 1).trim());
        }
        pos = lastOperator(rightSide, new String[] { "*", "/" });
        if (pos < 0) {
            valRight = calculate(rightSide);
        } else {
            valRight = calculate(rightSide.substring(0, pos).trim(), rightSide.charAt(pos) + "", rightSide.substring(pos + 1).trim());
        }
        return calculate(valLeft, operator, valRight);
    }
     
    private Object calculate(Object leftSide, String operator, Object rightSide) {
        Object tag = null;
        if (leftSide == null || rightSide == null) return null;
        if (isNumber(leftSide) && isNumber(rightSide)) {
            double valLeft = parseNumber(leftSide);
            double valRight = parseNumber(rightSide);
            tag = calculate(valLeft, operator, valRight);
        } else if (isString(leftSide) || isString(rightSide)) {
            if ("+".equals(operator)) {
                tag = leftSide.toString() + rightSide.toString();
            }
        }
        return tag;
    }
     
    private Object calculate(double leftSide, String operator, double rightSide) {
        if ("+".equals(operator)) {
            return leftSide + rightSide;
        } else if ("-".equals(operator)) {
            return leftSide - rightSide;
        } else if ("*".equals(operator)) {
            return leftSide * rightSide;
        } else if ("/".equals(operator) && rightSide != 0) {
            return leftSide / rightSide;
        } else {
            return null;
        }
    }
     
    private double parseNumber(Object src) {
        double tag = 0;
        try {
            tag = Double.parseDouble(src.toString());
        } catch (Exception e) {
            tag = 0;
        }
        return tag;
    }
     
    private boolean isString(Object src) {
        String kind = src.getClass().getName();
        if ("java.lang.String".equals(kind)) return true;
        return false;
    }
     
    private boolean isNumber(Object src) {
        if (src == null) return false;
        String[] types = new String[] { "java.lang.Character", "java.lang.Byte", "java.lang.Short", "java.lang.Integer", "java.lang.Long", "java.lang.Float", "java.lang.Double" };
        String kind = src.getClass().getName();
        for (int i = 0; i < types.length; i++) {
            if (types[i].equalsIgnoreCase(kind)) return true;
        }
        return false;
    }
     
    private Object calculate(String src) {
        if (data.containsKey(src)) {
            return data.get(src);
        } else {
            return null;
        }
    }
     
    private int lastOperator(String src, String[] operators) {
        int tag = -1;
        for (int i = 0; i < operators.length; i++) {
            int pos = src.lastIndexOf(operators[i]);
            if (pos < 0) continue;
            if (tag < 0) {
                tag = pos;
            } else {
                if (pos > tag) {
                    tag = pos;
                }
            }
        }
        return tag;
    }
     
}
    

  Protected by Copyscape Online Copyright Protection

Wednesday, 4 January 2012

Configure log4j for Jetty 8.0.4

Configure log4j for Jetty 8.0.4
This task configure log4j for Jetty 8.0.4
Configure log4j for Jetty 8.0.4
  1. Copy log4j-1.2.8.jar to JETTY_HOME/lib/ext folder
  2. Append JETTY_HOME/resources/log4j.properties with following code
  3. log4j.appender.R=org.apache.log4j.RollingFileAppender
    log4j.appender.R.File=logs/website.log
    log4j.appender.R.layout=org.apache.log4j.PatternLayout
    log4j.appender.R.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
    log4j.appender.R.MaxFileSize=200KB
    log4j.appender.R.MaxBackupIndex=10240
    log4j.rootLogger=WARN, R
    log4j.logger.website=DEBUG
          

  Protected by Copyscape Online Copyright Protection

Install and run Jetty 8.0.4

Install and run Jetty 8.0.4
This task install and run Jetty 8.0.4
Install and run Jetty 8.0.4
  1. Download Jetty 8.0.4
  2. Unzip to SRC folder
  3. Copy following folders and files to TAG folder
  4. + SRC/etc
    + SRC/lib
    + SRC/logs
    + SRC/resources
    + SRC/webapps
    + SRC/start.jar
    + SRC/start.ini
          
  5. Run following commands
  6. cd TAG
    java -jar start.jar
          

  Protected by Copyscape Online Copyright Protection

Task tagged by [log4j]

  1. Configure log4j for Jetty 8.0.4

Task tagged by [Jetty]

  1. Install and run Jetty 8.0.4
  2. Configure log4j for Jetty 8.0.4