View Javadoc

1   /*
2    * Copyright 1999,2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.commons.feedparser.locate.blogservice;
18  
19  import java.net.*;
20  import java.util.regex.Matcher;
21  import java.util.regex.Pattern;
22  
23  import org.apache.commons.feedparser.FeedParserException;
24  import org.apache.commons.feedparser.locate.*;
25  
26  /**
27   * Models the Xanga blog service, encapsulating whether a given weblog
28   * is this type of service and where it usually keeps its feeds.
29   * 
30   * @author Brad Neuberg, bkn3@columbia.edu
31   */
32  public class Xanga extends BlogService {
33      
34      /**
35       * A regex to extract the user from a Xanga URL
36       */
37      private static Pattern xangaURLPattern = Pattern.compile(".*user=(\\w*)");
38          
39      /** Returns whether we can trust the results of this blog service's 
40       *  autodiscovery links.  For example, TextAmerica returns invalid 
41       *  autodiscovery results.
42       */
43      public boolean hasValidAutoDiscovery() {
44          return true;
45      }
46      
47      /** Returns whether we should follow HTTP redirects for this blog service.
48       *  Some services don't implement HTTP redirects correctly, while others,
49       *  like Xanga, require it.
50       */
51      public boolean followRedirects() {
52          return true;
53      }
54      
55      /** Determines if the weblog at the given resource and with the given
56       *  content is this blog service.
57       * @param resource A full URI to this resource, such as 
58       * "http://www.codinginparadise.org".
59       * @param content The full HTML content at the resource's URL.
60       * @throws FeedParserException Thrown if an error occurs while 
61       * determining the type of this weblog.
62       */
63      public boolean isThisService(String resource, String content)
64                                                  throws FeedParserException {
65          boolean results = false;
66          
67          results = containsDomain(resource, "xanga.com");
68          
69          return results;
70      }
71  
72      /**
73       * Returns an array of FeedReferences that contains information on the
74       * usual locations this blog service contains its feed.  The feeds should
75       * be ordered by quality, so that higher quality feeds come before lower
76       * quality ones (i.e. you would want to have an Atom FeedReference
77       * object come before an RSS 0.91 FeedReference object in this list).
78       * @param resource A URL to the given weblog that might be used to build
79       * up where feeds are usually located.
80       * @param content The full content of the resource URL, which might
81       * be useful to determine where feeds are usually located.  This can be
82       * null.
83       * @throws FeedParserException Thrown if an error occurs while trying
84       * to determine the usual locations of feeds for this service.
85       */
86      public FeedReference[] getFeedLocations(String resource,
87                                              String content)
88                                                  throws FeedParserException {
89          // Xanga feeds have to be handled specially since they put their
90          // feeds at the location: http://www.xanga.com/rss.aspx?user=username
91          String user = getXangaUser(resource);
92          FeedReference xangaLocations[] =
93              { new FeedReference("rss.aspx?user=" + user, 
94                                  FeedReference.RSS_MEDIA_TYPE) };
95          
96          return xangaLocations;
97      }
98      
99      /** Xanga's feed locations are dependent on the 'user' attribute in a
100      *  Xanga URI.  This method helps extract the user element from an 
101      *  existing URI, such as http://www.xanga.com/home.aspx?user=wdfphillz.
102      */
103     protected String getXangaUser(String resource) {
104         Matcher xangaMatcher = xangaURLPattern.matcher(resource);
105         xangaMatcher.matches();
106         
107         return xangaMatcher.group(1);
108     }
109 }