Code example for DefaultHandler

0
	 * This method checks through the XML with a blank parser to determine 
	 * whether SAX parsing will fail due to an exception. 
	 */ 
	public static boolean isBadXml(InputStream inputStream) { 
		try { 
			readSAXInput(new DefaultHandler(), inputStream);
            return false; 
		} catch (SAXException | IOException e) {
            STATIC_LOGGER.warn("Trying to read XML returned the error " + e.getMessage(), e);
        } catch (RestIOException e) {
            STATIC_LOGGER.warn("Invalid XML. Rethrowing as RestInvalidFormatException");
            throw new RestInvalidScanFormatException(e, "Invalid scan format.");
		} finally { 
			closeInputStream(inputStream); 
		} 
 
		return true; 
	} 
	 
	/** 
	 * This method with one argument sets up the SAXParser and inputStream correctly 
	 * and executes the parsing. With two it adds a completion code and exception handling. 
	 */ 
	public static void readSAXInput(DefaultHandler handler, String completionCode, InputStream stream) {
		try { 
			readSAXInput(handler, stream);
		} catch (IOException e) {
            STATIC_LOGGER.error("Encountered IOException while trying to read the SAX input.");
            throw new RestIOException(e, "Encountered IOException while trying to read data. Can't continue.");
		} catch (SAXException e) {
			if (!e.getMessage().equals(completionCode)) {
                STATIC_LOGGER.error("Encountered SAXException while trying to read the SAX input.");
                throw new RestIOException(e, "Encountered SAXException while trying to read data. Can't continue.");
            } 
		} finally { 
			closeInputStream(stream); 
		} 
	} 
	 
	public static void closeInputStream(InputStream stream) { 
		if (stream != null) { 
			try { 
				stream.close(); 
			} catch (IOException ex) {
				STATIC_LOGGER.warn("Closing an input stream failed.", ex);
			} 
		} 
	} 
	 
	private static void readSAXInput(DefaultHandler handler, InputStream stream) throws SAXException, IOException {
		XMLReader xmlReader = XMLReaderFactory.createXMLReader();
		xmlReader.setContentHandler(handler);
		xmlReader.setErrorHandler(handler);
				 
		// Wrapping the inputStream in a BufferedInputStream allows us to mark and reset it 
		BufferedInputStream newStream = new BufferedInputStream(stream);
		 
		// UTF-8 contains 3 characters at the start of a file, which is a problem.