Skip to content

Instantly share code, notes, and snippets.

@bherrmann7
Created January 23, 2010 20:48
Show Gist options
  • Save bherrmann7/284784 to your computer and use it in GitHub Desktop.
Save bherrmann7/284784 to your computer and use it in GitHub Desktop.
// validate a schema using relagNG
// 1. download jing from http://code.google.com/p/jing-trang/
// 2. copy the jing.jar into $GROOVY_HOME/lib
// 3. now copy/paste this into the groovy console and have fun!!!
import com.thaiopensource.util.PropertyMapBuilder;
import com.thaiopensource.validate.*
import com.thaiopensource.validate.rng.CompactSchemaReader;
import com.thaiopensource.xml.sax.ErrorHandlerImpl;
import org.xml.sax.InputSource;
def relaxngCompact = '''
element records
{
element car
{
attribute name { text },
attribute make { text },
attribute year { xsd:integer }
}
}
'''
def xmlDoc = '''
<records>
<car name='HSV Maloo' make='Holden' year='2006'/>
</records>'''
PropertyMapBuilder properties = new PropertyMapBuilder();
properties.put(ValidateProperty.ERROR_HANDLER, new ErrorHandlerImpl(System.out));
SchemaReader sr = CompactSchemaReader.getInstance();
def schemaStream = new InputSource(new ByteArrayInputStream(relaxngCompact.bytes))
ValidationDriver driver = new ValidationDriver(properties.toPropertyMap(), sr);
if (!driver.loadSchema(schemaStream)) {
println 'problem loading schema'
return
}
if (!driver.validate( new InputSource(new ByteArrayInputStream(xmlDoc.bytes))))
println 'problem validating'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment