KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > clif > protocol > http > lib > HttpUtil


1 /*
2 * CLIF is a Load Injection Framework
3 * Copyright (C) 2003 France Telecom R&D
4 * Copyright (C) 2003 INRIA
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * CLIF $Name: $
21 *
22 * Contact: clif@objectweb.org
23 *
24 * @authors: Julien Buret
25 * @authors: Nicolas Droze
26 */

27
28 package org.objectweb.clif.protocol.http.lib;
29
30 import java.net.URI JavaDoc;
31 import java.util.Vector JavaDoc;
32 import java.util.regex.Matcher JavaDoc;
33 import java.util.regex.Pattern JavaDoc;
34
35 /**
36  * Some useful method
37  */

38 public class HttpUtil {
39
40     private HtmlParser parser = null;
41     private Vector JavaDoc domainHtmlLinks = new Vector JavaDoc();
42     private Object JavaDoc[] links;
43     private String JavaDoc link;
44     private int i;
45
46     public String JavaDoc relativeToAbsolute(String JavaDoc relative, String JavaDoc url)
47         throws Exception JavaDoc {
48
49         URI JavaDoc base = new URI JavaDoc(url);
50         return base.resolve(new URI JavaDoc(relative)).toString();
51     }
52
53     public Object JavaDoc[] getLinks(String JavaDoc arg) throws Exception JavaDoc {
54
55         return HtmlParser.getInstance().getLinks(arg);
56     }
57
58     public Object JavaDoc[] getFields(String JavaDoc arg) throws Exception JavaDoc {
59
60         return HtmlParser.getInstance().getFields(arg);
61     }
62
63     public Object JavaDoc[] getTags(String JavaDoc[] arg) throws Exception JavaDoc {
64
65         return HtmlParser.getInstance().getTags(arg);
66     }
67
68     /**
69      * Return all the links contained in an Html body, with the conditions that:
70      * The links point to an Html document.
71      * The links point to another URL on the same domain.
72      * @param arg
73      * @param host
74      * @return
75      * @throws Exception
76      */

77     public Object JavaDoc[] getDomainHtmlLinks(String JavaDoc arg, String JavaDoc host)
78         throws Exception JavaDoc {
79
80         domainHtmlLinks = new Vector JavaDoc();
81         links = HtmlParser.getInstance().getLinks(arg);
82
83         for (i = 0; i < links.length; i++) {
84             link = (String JavaDoc) links[i];
85
86             if (((link.indexOf("http://") == -1) || (link.indexOf(host) != -1))
87                 && (link.endsWith(".html")
88                     || link.endsWith(".htm")
89                     || link.endsWith("/"))) {
90                 domainHtmlLinks.add(link);
91             }
92         }
93
94         return domainHtmlLinks.toArray();
95     }
96
97     public Object JavaDoc[] regExp(String JavaDoc exp, String JavaDoc input) {
98     Pattern JavaDoc p = Pattern.compile(exp);
99     Matcher JavaDoc m = p.matcher(input);
100
101     while (m.find()) {
102         System.out.println(m.group());
103     }
104
105     return null;
106     }
107
108 }
109
Popular Tags