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;
018    
019    import java.util.Iterator;
020    import java.util.List;
021    
022    import org.jaxen.jdom.JDOMXPath;
023    import org.jdom.Element;
024    
025    /**
026     * Handles parsing Blogger.com changes.xml files.
027     *
028     * @author <a href="mailto:burton@apache.org">Kevin A. Burton (burtonator)</a>
029     * @version $Id: ChangesFeedParser.java 373614 2006-01-30 22:31:21Z mvdb $
030     */
031    public class ChangesFeedParser {
032    
033        /**
034         * Parse this feed.
035         *
036         * 
037         */
038        public static void parse( FeedParserListener listener,
039                                  org.jdom.Document doc ) throws FeedParserException {
040    
041            try {
042                    
043                FeedParserState state = new FeedParserState();
044    
045                FeedVersion v = new FeedVersion();
046                v.isChanges = true;
047                listener.onFeedVersion( v );
048    
049                listener.init();
050    
051                FeedDirectoryParserListener fdpl = (FeedDirectoryParserListener)listener;
052    
053                //this should be the root directory.
054                JDOMXPath xpath = new JDOMXPath( "/weblogUpdates/weblog" );
055                List list = xpath.selectNodes( doc );
056    
057                Iterator i = list.iterator();
058                while ( i.hasNext() ) {
059    
060                    Element child = (Element)i.next();
061                    onWeblog( fdpl, state, child );
062                    
063                }
064                
065                listener.finished();
066    
067            } catch ( Throwable t ) { throw new FeedParserException( t ); }
068    
069        }
070    
071        private static void onWeblog( FeedDirectoryParserListener listener,
072                                      FeedParserState state,
073                                      Element current ) throws Exception {
074            
075            String weblog = current.getAttributeValue( "url" );
076            String description = current.getAttributeValue( "name" );
077            String title = description;
078            String feed = null;
079    
080            if ( weblog == null )
081                weblog = feed;
082            
083            listener.onItem( state, title, weblog, description, feed );
084    
085        }
086    
087    }
088