/* * (C) Copyright IBM Corp. 2004. All rights reserved. * * US Government Users Restricted Rights Use, duplication or * disclosure restricted by GSA ADP Schedule Contract with IBM Corp. * * The program is provided "as is" without any warranty express or * implied, including the warranty of non-infringement and the implied * warranties of merchantibility and fitness for a particular purpose. * IBM will not be liable for any damages suffered by you as a result * of using the Program. In no event will IBM be liable for any * special, indirect or consequential damages or lost profits even if * IBM has been advised of the possibility of their occurrence. IBM * will not be liable for any third party claims against you. */ import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; import org.xml.sax.helpers.DefaultHandler; import org.xml.sax.helpers.LocatorImpl; /** * A sample SAX application. This code illustrates how to kill * the SAX parser when you've found what you needed from your * document. In this case, we kill the parser after the fourth * element is found. */ public class SaxKiller extends DefaultHandler { int lineCount = 0; boolean inFourthLine = false; StringBuffer fourthLine = new StringBuffer(); boolean notSuccessful = true; public boolean isNotSuccessful() { return notSuccessful; } public void parseURI(String uri) throws Exception { SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser sp = spf.newSAXParser(); sp.parse(uri, this); } /** Start element. */ public void startElement(String namespaceURI, String localName, String rawName, Attributes attrs) { if (rawName.equals("line") && ++lineCount == 4) inFourthLine = true; } // startElement(String,AttributeList) /** Characters. */ public void characters(char ch[], int start, int length) { if (inFourthLine) fourthLine.append(new String(ch, start, length)); } // characters(char[],int,int); /** End element. */ public void endElement(String namespaceURI, String localName, String rawName) throws SAXException { if (rawName.equals("line") && inFourthLine) { System.out.println("\nThe text of the fourth line is: \n"); System.out.println("\t" + fourthLine); notSuccessful = false; SAXParseException spe = new SAXParseException("Found the fourth , " + "so we killed the parser!", new LocatorImpl()); fatalError(spe); } } // endElement(String) // // ErrorHandler methods // /** Warning. */ public void warning(SAXParseException ex) { System.err.println("\n\n[Warning] "+ getLocationString(ex)+": "+ ex.getMessage()); } /** Error. */ public void error(SAXParseException ex) { System.err.println("\n\n[Error] "+ getLocationString(ex)+": "+ ex.getMessage()); } /** Fatal error. */ public void fatalError(SAXParseException ex) throws SAXParseException { throw ex; } /** Returns a string of the location. */ private String getLocationString(SAXParseException ex) { StringBuffer str = new StringBuffer(); String systemId = ex.getSystemId(); if (systemId != null) { int index = systemId.lastIndexOf('/'); if (index != -1) systemId = systemId.substring(index + 1); str.append(systemId); } str.append(':'); str.append(ex.getLineNumber()); str.append(':'); str.append(ex.getColumnNumber()); return str.toString(); } // getLocationString(SAXParseException):String /** Main program entry point. */ public static void main(String argv[]) { if (argv.length == 0 || (argv.length == 1 && argv[0].equals("-help"))) { System.out.println("\nUsage: java SaxKiller uri"); System.out.println(" where uri is the URI of your XML document."); System.out.println(" Sample: java SaxKiller sonnet.xml"); System.out.println("\nIllustrates how to kill a SAX parser by " + "throwing an exception."); System.exit(1); } SaxKiller s1 = new SaxKiller(); try { s1.parseURI(argv[0]); } // We're expecting an exception, so we ignore anything that happens... catch (Exception e) { if (s1.isNotSuccessful()) System.err.println(e); } } // main(String[]) }