001    /*
002     * Copyright 1999,2004 The Apache Software Foundation.
003     * 
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     * 
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    package org.apache.commons.feedparser.locate;
018    
019    import java.util.regex.Pattern;
020    
021    /**
022     * <p>
023     * A FeedReference is used within the RSS/Atom location facility to pass back
024     * metadata for feed discoveries.
025     *
026     * <p>
027     * Right now we pass back the URL to the feeds as the `resource' parameter.  The
028     * media type (application/rss+xml, application/atom+xml, etc) as the type param
029     * (which is optional).  We also pass back the `title' back if its specified
030     * within autodiscovery.  This will be null if another discovery method is used.
031     *
032     * <p> Its important to realize that the media type is only given if we're 100%
033     * certain of the value.  If we have to use probe discovery or link discovery it
034     * might not be possible to obtain the media type without antoher network
035     * request (via HTTP headers).
036     * 
037     * <p> Note that internally (within the ProbeLocator) we don't use absolute
038     * resource URLs but use relative ones and use the media type as the default
039     * media type.
040     * 
041     * @author <a href="mailto:burton@apache.org">Kevin A. Burton</a>
042     */
043    public class FeedReference {
044    
045        public static final String ATOM_MEDIA_TYPE = "application/atom+xml";
046        public static final String RSS_MEDIA_TYPE  = "application/rss+xml";
047        public static final String XML_MEDIA_TYPE  = "text/xml";
048    
049        public static int METHOD_AUTO_DISCOVERY  = 1;
050        public static int METHOD_PROBE_DISCOVERY = 2;
051        public static int METHOD_LINK_DISCOVERY  = 4;
052        
053        /**
054         * The network addressable resource forfor this feed.
055         */
056        public String resource = null;
057    
058        /**
059         * The media type of this feed.
060         */
061        public String type = null;
062    
063        /**
064         * The title of the reference.  Usually FOAF, RSS or Atom per auto-discovery
065         * link.
066         */
067        public String title = null;
068    
069        /**
070         * The method of discovery... 
071         */
072        public int method = 0;
073        
074        protected Pattern schemePattern = Pattern.compile("^[^:/]*:/.*$");
075        
076        public FeedReference( String resource, String type ) {
077            this.resource = resource;
078            this.type = type;
079        }
080    
081        public String toString() {
082            return resource;
083        }
084        
085        public boolean equals(Object obj) {
086            if (obj == null || (obj instanceof FeedReference) == false)
087                return false;
088            
089            FeedReference compareMe = (FeedReference)obj;
090            
091            if (resource.equals(compareMe.resource)) {
092                //ignore title and type when doing equality
093                return true;
094            }
095            
096            return false;
097        }
098        
099        /** Determines if the resource given by this FeedReference is relative.
100         *  For example, the resource could be '/atom.xml', which is relative.
101         *  It could also be 
102         *  "http://rss.groups.yahoo.com/group/talkinaboutarchitecture/rss".
103         */
104        public boolean isRelative() {
105            if (resource == null)
106                return true;
107            
108            // look for a scheme:/
109            return !schemePattern.matcher(resource).matches();
110            
111        }
112        
113    }