KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > protocol > http > parser > URLCollection


1 // $Header: /home/cvs/jakarta-jmeter/src/protocol/http/org/apache/jmeter/protocol/http/parser/URLCollection.java,v 1.4 2004/03/30 18:08:09 sebb Exp $
2
/*
3  * Copyright 2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17 */

18
19 package org.apache.jmeter.protocol.http.parser;
20
21 import java.net.MalformedURLException JavaDoc;
22 import java.net.URL JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.Iterator JavaDoc;
25
26 /**
27  * Collection class designed for handling URLs
28  *
29  * Before a URL is added to the collection, it is wrapped in a URLString class.
30  * The iterator unwraps the URL before return.
31  *
32  * N.B.
33  * Designed for use by HTMLParser, so is not a full implementation
34  * - e.g. does not support remove()
35  *
36  * @version $Revision: 1.4 $ $Date: 2004/03/30 18:08:09 $
37  */

38 public class URLCollection
39 {
40     Collection JavaDoc coll;
41
42     // Inaccessible default constructor
43
private URLCollection(){}
44     
45     /**
46      * Creates a new URLCollection from an existing Collection
47      *
48      */

49     public URLCollection(Collection JavaDoc c)
50     {
51         coll = c;
52     }
53
54     /**
55      * Adds the URL to the Collection, first wrapping
56      * it in the URLString class
57      *
58      * @param u URL to add
59      * @return boolean condition returned by the add() method of the underlying collection
60      */

61     public boolean add(URL JavaDoc u)
62     {
63         return coll.add(new URLString(u));
64     }
65     
66     /*
67      * Adds the string to the Collection, first wrapping
68      * it in the URLString class
69      *
70      * @param s string to add
71      * @return boolean condition returned by the add() method of the underlying collection
72      */

73     private boolean add(String JavaDoc s)
74     {
75         return coll.add(new URLString(s));
76     }
77     
78     /**
79      * Convenience method for adding URLs to the collection
80      * If the url parameter is null or empty, nothing is done
81      *
82      * @param url String, may be null or empty
83      * @param baseUrl
84      * @return boolean condition returned by the add() method of the underlying collection
85      */

86     public boolean addURL(String JavaDoc url, URL JavaDoc baseUrl)
87     {
88         if (url == null || url.length() == 0) return false;
89         boolean b=false;
90         try
91         {
92             b=this.add(new URL JavaDoc(baseUrl, url));
93         }
94         catch(MalformedURLException JavaDoc mfue)
95         {
96             //TODO log a warning message?
97
b=this.add(url);// Add the string if cannot create the URL
98
}
99         return b;
100     }
101     
102     
103     public Iterator JavaDoc iterator()
104     {
105         return new UrlIterator(coll.iterator());
106     }
107
108     /*
109      * Private iterator used to unwrap the URL from the URLString class
110      *
111      */

112     private static class UrlIterator implements Iterator JavaDoc
113     {
114         Iterator JavaDoc iter;
115         
116         UrlIterator(Iterator JavaDoc i)
117         {
118             iter=i;
119         }
120     
121         public boolean hasNext()
122         {
123             return iter.hasNext();
124         }
125     
126         /*
127          * Unwraps the URLString class to return the URL
128          */

129         public Object JavaDoc next()
130         {
131             return ((URLString) iter.next()).getURL();
132         }
133     
134         public void remove()
135         {
136             throw new UnsupportedOperationException JavaDoc();
137         }
138     }
139 }
140
Popular Tags