1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.feedparser.locate;
18
19 import java.util.regex.Pattern;
20
21 import org.apache.log4j.Logger;
22
23
24
25
26
27
28 public class ResourceExpander {
29
30 private static Logger log = Logger.getLogger( ResourceExpander.class );
31
32
33
34 protected static Pattern schemePattern = Pattern.compile("^\\w*://.*");
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59 public static String expand( String resource, String link ) {
60
61 if ( link == null )
62 return null;
63
64
65 if ( !isValidScheme( link ) )
66 return link;
67
68
69 if ( isExpanded( link ) )
70 return link;
71
72
73
74
75
76
77
78
79
80
81
82 if ( link.startsWith( "//" ) ) {
83
84 return "http:" + link;
85
86 }
87
88
89 if ( link.startsWith( "/" ) ) {
90
91 link = getSite( resource ) + link;
92
93 return link;
94
95 } else if ( link.startsWith( "#" ) ) {
96
97 link = resource + link;
98
99 return link;
100
101 } else if ( link.startsWith( ".." ) ) {
102
103
104
105 String base = getBase( resource ) + "/";
106
107 while ( link.startsWith( ".." ) ) {
108
109
110 int begin = 2;
111 if ( link.length() > 2 && link.charAt( 2 ) == '/' )
112 begin = 3;
113
114 link = link.substring( begin, link.length() );
115
116
117
118 int end = base.length();
119
120 if ( base.endsWith( "/" ) )
121 --end;
122
123 base = base.substring( 0, base.lastIndexOf( "/", end - 1 ) );
124
125 }
126
127 link = base + "/" + link;
128
129 return link;
130
131 }
132
133
134
135
136 resource = getBase(resource);
137 if ( link.startsWith( "http://" ) == false ) {
138
139 link = resource + "/" + link;
140 log.debug("link="+link);
141
142 }
143
144 return link;
145
146 }
147
148
149
150
151
152
153 public static boolean isExpanded( String resource ) {
154 return (resource.startsWith( "http://" ) ||
155 resource.startsWith( "file://" ));
156 }
157
158
159
160
161
162
163 public static boolean isValidScheme( String resource ) {
164 if (hasScheme(resource) == false)
165 return true;
166
167
168
169 if ( resource.startsWith( "http:" ) )
170 return true;
171
172 if ( resource.startsWith( "file:" ) )
173 return true;
174
175 return false;
176
177 }
178
179
180
181
182
183 public static boolean hasScheme( String resource ) {
184 return schemePattern.matcher( resource ).matches();
185
186 }
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201 public static String getSite( String resource ) {
202
203 if ( resource.startsWith( "file:" ) ) {
204 return "file://";
205 }
206
207
208 int end = resource.indexOf( "/", 8 );
209
210 if ( end == -1 ) {
211
212 end = resource.length();
213
214 }
215
216 return resource.substring( 0, end );
217
218 }
219
220
221
222
223
224
225 public static String getDomain( String resource ) {
226
227 String site = getSite( resource );
228
229 int firstIndex = -1;
230 int indexCount = 0;
231
232 int index = site.length();
233
234 while ( (index = site.lastIndexOf( ".", index-1 )) != -1 ) {
235
236 ++indexCount;
237
238 if ( indexCount == 2 )
239 break;
240
241 }
242
243 int begin = 7;
244 if ( indexCount >= 2 )
245 begin = index + 1;
246
247 return site.substring( begin, site.length() );
248
249 }
250
251
252
253
254
255
256
257
258
259
260
261
262
263 public static String getBase( String resource ) {
264
265
266 if ( resource == null )
267 return null;
268
269 int begin = "http://".length() + 1;
270
271 int end = resource.lastIndexOf( "/" );
272
273 if ( end == -1 || end <= begin ) {
274
275
276 end = resource.length();
277
278 }
279 return resource.substring( 0, end );
280
281 }
282
283 public static void main( String[] args ) throws Exception {
284
285 System.out.println( expand( "http://peerfear.org/foo/bar/", "../../blog" ) );
286
287 System.out.println( expand( "http://peerfear.org/foo/bar/", "../../index.html" ) );
288
289 System.out.println( expand( "http://peerfear.org/blog/", ".." ) );
290
291 System.out.println( expand( "http://peerfear.org", "/blog" ) );
292 System.out.println( expand( "http://peerfear.org", "http://peerfear.org" ) );
293
294 System.out.println( expand( "http://peerfear.org", "blog" ) );
295 System.out.println( expand( "http://peerfear.org/blog", "foo/bar" ) );
296
297 System.out.println( expand( "file://projects/newsmonster/", "blog" ) );
298
299 System.out.println( expand( "file:/projects/ksa/src/java/ksa/test/TestFeedTask_WithRelativePath.rss"
300 , "/blog" ) );
301 }
302
303 }
304