1 /* 2 * Copyright (c) 2002-2017, Mairie de Paris 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright notice 10 * and the following disclaimer. 11 * 12 * 2. Redistributions in binary form must reproduce the above copyright notice 13 * and the following disclaimer in the documentation and/or other materials 14 * provided with the distribution. 15 * 16 * 3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its 17 * contributors may be used to endorse or promote products derived from 18 * this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE 24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 * 32 * License 1.0 33 */ 34 package fr.paris.lutece.plugins.googleapi.service; 35 36 import fr.paris.lutece.plugins.googleapi.business.Item; 37 38 import org.xml.sax.Attributes; 39 import org.xml.sax.SAXException; 40 import org.xml.sax.helpers.DefaultHandler; 41 42 import java.util.List; 43 import java.util.Stack; 44 45 46 /** 47 * Simple SAX event handler, which prints out the titles of all entries in the 48 * Atom response feed. 49 */ 50 public class FeedHandler extends DefaultHandler 51 { 52 protected List<Item> _listItems; 53 54 /** 55 * Stack containing the opening XML tags of the response. 56 */ 57 protected Stack<String> xmlTags = new Stack<String>( ); 58 59 /** 60 * True if we are inside of a data entry's title, false otherwise. 61 */ 62 protected boolean _bInsideEntryTitle; 63 protected boolean _bInsideEntryContent; 64 protected Item _item; 65 66 /** 67 * Receive notification of an opening XML tag: push the tag to 68 * <code>xmlTags</code>. If the tag is a title tag inside an entry tag, 69 * turn <code>insideEntryTitle</code> to <code>true</code>. 70 * @param listItems 71 */ 72 public void setItemList( List<Item> listItems ) 73 { 74 _listItems = listItems; 75 } 76 77 /** 78 * 79 * @param uri 80 * @param localName 81 * @param qName 82 * @param attributes 83 * @throws org.xml.sax.SAXException 84 */ 85 public void startElement( String uri, String localName, String qName, Attributes attributes ) 86 throws SAXException 87 { 88 if ( qName.equals( "title" ) && xmlTags.peek( ).equals( "entry" ) ) 89 { 90 _bInsideEntryTitle = true; 91 _item = new Item( ); 92 _listItems.add( _item ); 93 } 94 else if ( qName.equals( "content" ) && xmlTags.peek( ).equals( "entry" ) ) 95 { 96 _bInsideEntryContent = true; 97 } 98 else if ( qName.equals( "link" ) && xmlTags.peek( ).equals( "entry" ) ) 99 { 100 if ( attributes.getValue( "rel" ).equals( "alternate" ) ) 101 { 102 _item.setLink( attributes.getValue( "href" ) ); 103 } 104 } 105 else if ( qName.equals( "media:thumbnail" ) ) 106 { 107 _item.setThumbnailUrl( attributes.getValue( "url" ) ); 108 } 109 110 xmlTags.push( qName ); 111 } 112 113 /** 114 * Receive notification of a closing XML tag: remove the tag from teh stack. 115 * If we were inside of an entry's title, turn <code>insideEntryTitle</code> 116 * to <code>false</code>. 117 * @param uri 118 * @param localName 119 * @param qName 120 * @throws org.xml.sax.SAXException 121 */ 122 public void endElement( String uri, String localName, String qName ) 123 throws SAXException 124 { 125 // If a "title" element is closed, we start a new line, to prepare 126 // printing the new title. 127 xmlTags.pop( ); 128 129 if ( _bInsideEntryTitle ) 130 { 131 _bInsideEntryTitle = false; 132 } 133 else if ( _bInsideEntryContent ) 134 { 135 _bInsideEntryContent = false; 136 } 137 } 138 139 /** 140 * Callback method for receiving notification of character data inside an 141 * XML element. 142 * @param ch 143 * @param start 144 * @param length 145 * @throws org.xml.sax.SAXException 146 */ 147 public void characters( char[] ch, int start, int length ) 148 throws SAXException 149 { 150 // display the character data if the opening tag is "title" and its parent is 151 // "entry" 152 if ( _bInsideEntryTitle ) 153 { 154 _item.setTitle( new String( ch, start, length ) ); 155 } 156 else if ( _bInsideEntryContent ) 157 { 158 _item.setContent( new String( ch, start, length ) ); 159 } 160 } 161 }