Document parse ( Object input , Element parserConfig , PrintWriter logger ) throws Exception { jlogger . finer ( "Received XML resource of type " + input . getClass ( ) . getName ( ) ) ; ArrayList < Object > schemas = new ArrayList < Object > ( ) ; ArrayList < Object > dtds = new ArrayList < Object > ( ) ; schemas . addAll ( this . schemaList ) ; dtds . addAll ( this . dtdList ) ; loadSchemaLists ( parserConfig , schemas , dtds ) ; Document resultDoc = null ; ErrorHandlerImpl errHandler = new ErrorHandlerImpl ( "Parsing" , logger ) ; if ( input instanceof InputStream ) { DocumentBuilderFactory dbf = nonValidatingDBF ; DocumentBuilder db = dbf . newDocumentBuilder ( ) ; db . setErrorHandler ( errHandler ) ; try ( InputStream xmlInput = ( InputStream ) input ) { resultDoc = db . parse ( xmlInput ) ; } catch ( Exception e ) { jlogger . log ( Level . INFO , "Error parsing InputStream" , e ) ; } } else if ( input instanceof Document ) { resultDoc = ( Document ) input ; } else { throw new IllegalArgumentException ( "XML input must be an InputStream or a Document object." ) ; } if ( null == resultDoc ) { throw new RuntimeException ( "Failed to parse input: " + input . getClass ( ) . getName ( ) ) ; } errHandler . setRole ( "Validation" ) ; if ( null == resultDoc . getDoctype ( ) && dtds . isEmpty ( ) ) { validateAgainstXMLSchemaList ( resultDoc , schemas , errHandler ) ; } else { validateAgainstDTDList ( resultDoc , dtds , errHandler ) ; } int error_count = errHandler . getErrorCount ( ) ; int warning_count = errHandler . getWarningCount ( ) ; if ( error_count > 0 || warning_count > 0 ) { String msg = "" ; if ( error_count > 0 ) { msg += error_count + " validation error" + ( error_count == 1 ? "" : "s" ) ; if ( warning_count > 0 ) msg += " and " ; } if ( warning_count > 0 ) { msg += warning_count + " warning" + ( warning_count == 1 ? "" : "s" ) ; } msg += " detected." ; logger . println ( msg ) ; } if ( error_count > 0 ) { String s = ( null != parserConfig ) ? parserConfig . getAttribute ( "ignoreErrors" ) : "false" ; if ( s . length ( ) == 0 || Boolean . parseBoolean ( s ) == false ) { resultDoc = null ; } } if ( warning_count > 0 ) { String s = ( null != parserConfig ) ? parserConfig . getAttribute ( "ignoreWarnings" ) : "true" ; if ( s . length ( ) > 0 && Boolean . parseBoolean ( s ) == false ) { resultDoc = null ; } } return resultDoc ; }
Parses and validates an XML resource using the given schema references .