001 /* 002 * Copyright (c) 2003, 2004 Taavi Hupponen 003 * 004 * Permission is hereby granted, free of charge, to any person obtaining a 005 * copy of this software and associated documentation files (the "Software"), 006 * to deal in the Software without restriction, including without limitation 007 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 008 * and/or sell copies of the Software, and to permit persons to whom the 009 * Software is furnished to do so, subject to the following conditions: 010 * 011 * The above copyright notice and this permission notice shall be included in 012 * all copies or substantial portions of the Software. 013 * 014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 015 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 016 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 017 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 018 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 019 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 020 * DEALINGS IN THE SOFTWARE. 021 */ 022 023 /* 024 * Created on 15.4.2004 025 * 026 * 2004-08-29 fixed namespaces -- hsivonen 027 */ 028 package fi.iki.hsivonen.xml; 029 030 import gnu.xml.pipeline.EventConsumer; 031 import gnu.xml.pipeline.EventFilter; 032 033 import org.xml.sax.Attributes; 034 import org.xml.sax.SAXException; 035 036 /** 037 * Grabs the contents of the title element to be returned later. 038 * 039 * @author taavi 040 * 041 * 042 */ 043 public class TitleGrabber extends EventFilter { 044 045 /** 046 * The XHTML namespace URI 047 */ 048 private final static String XHTML_NS = "http://www.w3.org/1999/xhtml"; 049 050 private StringBuilder title; 051 private boolean headOpen, titleOpen; 052 053 public TitleGrabber(EventConsumer next) { 054 super(next); 055 setContentHandler(this); 056 headOpen = false; 057 titleOpen = false; 058 title = new StringBuilder(); 059 } 060 061 /** 062 * 063 */ 064 public TitleGrabber() { 065 super(); 066 setContentHandler(this); 067 } 068 069 public String getTitle() { 070 return title.toString(); 071 } 072 073 public void endElement(String uri, String local, String qname) throws SAXException { 074 if ("head".equals(local) && XHTML_NS.equals(uri)) { 075 headOpen = false; 076 } else if ("title".equals(local) && XHTML_NS.equals(uri)) { 077 titleOpen = false; 078 } 079 080 super.endElement(uri, local, qname); 081 082 } 083 084 public void startElement(String uri, String local, String qname, Attributes attributes) throws SAXException { 085 if ("head".equals(local) && XHTML_NS.equals(uri)) { 086 headOpen = true; 087 } else if ("title".equals(local) && XHTML_NS.equals(uri)) { 088 titleOpen = true; 089 } 090 091 super.startElement(uri, local, qname, attributes); 092 093 } 094 095 public void characters(char[] arg0, int arg1, int arg2) throws SAXException { 096 if (headOpen && titleOpen) { 097 title.append(arg0, arg1, arg2); 098 } 099 super.characters(arg0, arg1, arg2); 100 } 101 102 }