summaryrefslogtreecommitdiffstats
path: root/Master/Agile Software Development/TestApp/src/Parser/HtmlParser.java
blob: 96ad2b25bb60b557af0ee1a5855a83139c878645 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Parser;

import java.util.logging.Level;
import java.util.logging.Logger;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;
import testapp.data.Item;
import testapp.data.PageItem;
import java.util.logging.*;

/**
 *
 * @author 
 */
public class HtmlParser //extends NodeVisitor {
{

    public HtmlParser() {
    }

    public Item parse(String url) {
        try {
            PageItem rootNode = new PageItem(url,null);

            org.htmlparser.Parser parser = new org.htmlparser.Parser(rootNode.getUrl());
            NodeList nodeList = parser.parse(null);

            nodeList.visitAllNodesWith(new Visitor(rootNode));

            return rootNode;

        } catch (ParserException ex) {
            Logger.getLogger(HtmlParser.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }

    public Item parse(Item rootItem) {
        try {
            org.htmlparser.Parser parser = new org.htmlparser.Parser(rootItem.getUrl());
            NodeList nodeList = parser.parse(null);

            nodeList.visitAllNodesWith(new Visitor(rootItem));

            return rootItem;

        } catch (ParserException ex) {
            Logger.getLogger(HtmlParser.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }
}