CFLib.org – Common Function Library Project

validateXMLFile(xmlUrl [, throwOnError] [, fileLocktimeout])

Last updated February 18, 2004
Download UDF

author

Massimo Foti                                      Massimo Foti

Version: 1 | Requires: ColdFusion MX | Library: DataManipulationLib

 
Rated 0 time(s). Average Rating: 0

Description:
Validate an XML file against a DTD.

Return Values:
Returns a boolean.

Example:

Please see the example at: http://www.massimocorner.com/demos/validateXMLFile.zip

Parameters:

Name Description Required
xmlUrl File location of the XML document. Yes
throwOnError Determines if the UDF should throw an error if the XML file doesn't validate. Defaults to false. No
fileLocktimeout Specifies how long CF should wait to obtain a lock on the file. Defaults to 5. No

Full UDF Source:

<!---
Validate an XML file against a DTD.

@param xmlUrl      File location of the XML document. (Required)
@param throwOnError      Determines if the UDF should throw an error if the XML file doesn't validate. Defaults to false. (Optional)
@param fileLocktimeout      Specifies how long CF should wait to obtain a lock on the file. Defaults to 5. (Optional)
@return Returns a boolean.
@author Massimo Foti (massimo@massimocorner.com)
@version 1, February 18, 2004
--->

<cffunction name="validateXMLFile" output="false" returntype="boolean" hint="Validate an XML file against a DTD">
    <cfargument name="xmlUrl" type="string" required="true" hint="XML document url">
    <cfargument name="throwerror" type="boolean" required="false" default="false" hint="Throw an exception if the document isn't valid">
    <cfargument name="fileLockTimeout" type="numeric" required="false" default="5" hint="Specifies the maximum amount of time, to wait to obtain a lock on the file">
    <cfset var isValid=true>
    <cfset var saxFactory="">
    <cfset var xmlReader="">
    <cfset var eHandler="">
    <!--- Better to be sure the file exist --->
    <cfif NOT FileExists(arguments.xmlUrl)>
        <cfthrow message="validateXMLFile: #arguments.xmlUrl# doesn't exist" type="validateXMLFile">
    </cfif>
    <cftry>
        <cfscript>
        //Call the SAX parser factory
        saxFactory = CreateObject("java","javax.xml.parsers.SAXParserFactory").newInstance();
        //Creates a SAX parser and get the XML Reader
        xmlReader = saxFactory.newSAXParser().getXMLReader();
        //Turn on validation
        xmlReader.setFeature("http://xml.org/sax/features/validation",true);
        //Create an error handler
        eHandler = CreateObject("
java","org.apache.xml.utils.DefaultErrorHandler").init();
        //Assign the error handler
        xmlReader.setErrorHandler(eHandler);
        </cfscript>
        <!--- Throw an exception in case any Java initialization failed --->
        <cfcatch type="
Object">
            <cfthrow message="
validateXMLFile: failed to initialize Java objects" type="validateXMLFile">
        </cfcatch>
    </cftry>
    <cftry>
        <!---
        Since we are reading the file, we better lock it.
        Safer thing to do is to use the file's url as name for the lock
         --->

        <cflock name="
#arguments.xmlUrl#" timeout="#arguments.fileLockTimeout#" throwontimeout="yes" type="readonly">
            <cfset xmlReader.parse(arguments.xmlUrl)>

        </cflock>
        <!--- Catch SAX's exception and set the flag --->
    <cfcatch type="
org.xml.sax.SAXParseException">
        <!--- The SAX parser failed to validate the document --->
        <cfset isValid=false>

        <cfif arguments.throwerror>
            <!--- Throw an exception with the error message if required    --->
            <cfthrow message="
validateXMLFile: Failed to validate the document, #cfcatch.Message#" type="validateXMLFile">
        </cfif>
    </cfcatch>
    </cftry>
    <!--- Return the boolean --->
    <cfreturn isValid>
</cffunction>

Search CFLib.org


Latest Additions

Shawn Porter Shawn Porter added
DeMoronize
2 hour(s) ago

Chris Carey Chris Carey added
readPropertiesFi...
1 day(s) ago

Randy Johnson Randy Johnson added
lastDayofWeek
3 day(s) ago

Frank Marion Frank Marion added
sitemapPing
7 day(s) ago

Top Rated

James Sleeman                                     QuickSort
Rated 5.0, 3 time(s)

Barney Boisvert indentXml
Rated 5.0, 3 time(s)

Nathan Dintenfass                                 queryColumnsToSt...
Rated 5.0, 3 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 3 time(s)

Created by Raymond Camden / Design by Justin Johnson