Code example for DefaultHandler

	 * 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 { 
		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 { 
	public static void closeInputStream(InputStream stream) { 
		if (stream != null) { 
			try { 
			} 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();
		// 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. 
Experience pair programming with AI  Get Codota for Java