A helper class which builds a SchematronSchema instance object
from a DOM source
| Method from org.apache.cocoon.validation.schematron.SchematronFactory Detail: |
protected void bindAsserts(Rule rule,
String pathPrefix,
JXPathContext jxpContext) {
// ensure that mandatory elements which are not found
// will result in Exception
jxpContext.setLenient(false);
// schema reports
int elementCount = ((Integer) jxpContext.getValue ( "count(" + pathPrefix + "/assert)", Integer.class )).intValue();
logger.debug( "\nNumber of asserts: " + elementCount);
for (int i = 1; i < = elementCount; i++)
{
logger.debug( "Assert# : " + i);
Assert anAssert = new Assert();
String assertPrefix = pathPrefix + "/assert[" + i + "]";
String test = (String) jxpContext.getValue ( assertPrefix + "/@test", String.class );
anAssert.setTest( test );
logger.debug( "Assert test : " + anAssert.getTest());
// since diagnostics is a non-mandatory element
// we will try to get its value in a lenient mode
jxpContext.setLenient(true);
String diagnostics = (String) jxpContext.getValue ( assertPrefix + "/@diagnostics", String.class );
anAssert.setDiagnostics( diagnostics );
logger.debug( "Assert diagnostics : " + anAssert.getDiagnostics());
jxpContext.setLenient(false);
String message = (String) jxpContext.getValue ( assertPrefix, String.class );
anAssert.setMessage( message );
logger.debug( "Assert message : " + anAssert.getMessage());
rule.addAssert( anAssert );
}
}
populates the assert elements for a rule
from the dom tree |
protected void bindPatterns(SchematronSchema schema,
JXPathContext jxpContext) {
// ensure that mandatory elements which are not found
// will result in Exception
jxpContext.setLenient(false);
// schema patterns
int ptCount = ((Integer) jxpContext.getValue ( "count(/schema/pattern)", Integer.class )).intValue();
logger.debug( "\nNumber of patterns: " + ptCount);
for (int i = 1; i < = ptCount; i++)
{
logger.debug( "Pattern# : " + i);
Pattern pattern = new Pattern();
String ptprefix = "/schema/pattern[" + i + "]";
String name = (String) jxpContext.getValue ( ptprefix + "/@name", String.class );
pattern.setName( name );
logger.debug( "Pattern name : " + pattern.getName());
String id = (String) jxpContext.getValue ( ptprefix + "/@id", String.class );
pattern.setId( id );
logger.debug( "Pattern id : " + pattern.getId() );
bindRules( pattern, ptprefix, jxpContext );
schema.addPattern( pattern );
}
}
populates the patterns elements from the dom tree |
protected void bindPhaseActivePatterns(Phase phase,
String pathPrefix,
JXPathContext jxpContext) {
// ensure that mandatory elements which are not found
// will result in Exception
jxpContext.setLenient(false);
// phase active patterns
int elementCount = ((Integer) jxpContext.getValue ( "count(" + pathPrefix + "/active)", Integer.class )).intValue();
logger.debug( "Number of active patterns: " + elementCount);
for (int i = 1; i < = elementCount; i++)
{
logger.debug( "active pattern # : " + i);
ActivePattern activePattern = new ActivePattern();
String assertPrefix = pathPrefix + "/active[" + i + "]";
String pt = (String) jxpContext.getValue ( assertPrefix + "/@pattern", String.class );
activePattern.setPattern( pt );
logger.debug( "Phase active pattern : " + activePattern.getPattern());
phase.addActive( activePattern );
}
}
|
protected void bindPhases(SchematronSchema schema,
JXPathContext jxpContext) {
// ensure that mandatory elements which are not found
// will result in Exception
jxpContext.setLenient(false);
// schema phases
int phaseCount = ((Integer) jxpContext.getValue ( "count(/schema/phase)", Integer.class )).intValue();
logger.debug( "\nNumber of phases: " + phaseCount);
for (int i = 1; i < = phaseCount; i++)
{
logger.debug( "phase# : " + i);
Phase phase = new Phase();
String phprefix = "/schema/phase[" + i + "]";
String id = (String) jxpContext.getValue ( phprefix + "/@id", String.class );
phase.setId( id );
logger.debug( "phase id : " + phase.getId());
bindPhaseActivePatterns( phase, phprefix, jxpContext );
schema.addPhase( phase );
}
}
populates the phases elements from the dom tree |
protected void bindRerports(Rule rule,
String pathPrefix,
JXPathContext jxpContext) {
// ensure that mandatory elements which are not found
// will result in Exception
jxpContext.setLenient(false);
// schema reports
int elementCount = ((Integer) jxpContext.getValue ( "count(" + pathPrefix + "/report)", Integer.class )).intValue();
logger.debug( "\nNumber of reports: " + elementCount);
for (int i = 1; i < = elementCount; i++)
{
logger.debug( "Report# : " + i);
Report report = new Report();
String assertPrefix = pathPrefix + "/report[" + i + "]";
String test = (String) jxpContext.getValue ( assertPrefix + "/@test", String.class );
report.setTest( test );
logger.debug( "Report test : " + report.getTest());
// since diagnostics is a non-mandatory element
// we will try to get its value in a lenient mode
jxpContext.setLenient(true);
String diagnostics = (String) jxpContext.getValue ( assertPrefix + "/@diagnostics", String.class );
report.setDiagnostics( diagnostics );
logger.debug( "Report diagnostics : " + report.getDiagnostics());
jxpContext.setLenient(false);
String message = (String) jxpContext.getValue ( assertPrefix, String.class );
report.setMessage( message );
logger.debug( "Report message : " + report.getMessage());
rule.addReport( report );
}
}
populates the assert elements for a rule
from the dom tree |
protected void bindRules(Pattern pattern,
String pathPrefix,
JXPathContext jxpContext) {
// ensure that mandatory elements which are not found
// will result in Exception
jxpContext.setLenient(false);
// schema rules
int ruleCount = ((Integer) jxpContext.getValue ( "count(" + pathPrefix + "/rule)", Integer.class )).intValue();
logger.debug( "\nNumber of rules: " + ruleCount);
for (int i = 1; i < = ruleCount; i++)
{
logger.debug( "Rule# : " + i);
Rule rule = new Rule();
String rulePrefix = pathPrefix + "/rule[" + i + "]";
String context = (String) jxpContext.getValue ( rulePrefix + "/@context", String.class );
rule.setContext( context );
logger.debug( "Rule context : " + rule.getContext());
bindAsserts( rule, rulePrefix, jxpContext );
pattern.addRule( rule );
}
}
populates the rules elements for a pattern
from the dom tree |
protected SchematronSchema buildSchema(Document doc) {
SchematronSchema schema = new SchematronSchema();
boolean errors = false;
doc.getNamespaceURI ();
doc.getPrefix ();
// Initialize the JXPath context
Element root = doc.createElement ( "root" );
Element schemaElement = doc.getDocumentElement ();
schemaPrefix_ = schemaElement.getPrefix ();
root.appendChild ( schemaElement );
JXPathContext jxpContext = JXPathContext.newContext ( root );
jxpContext.setLenient(false);
// Bind sch:schema element
// schema title
String title = (String) jxpContext.getValue ( "/schema/title", String.class );
schema.setTitle( title );
logger.debug( "Schema title: " + schema.getTitle());
bindPatterns( schema, jxpContext );
bindPhases( schema, jxpContext );
return schema;
}
Build Schematron schema object from a DOM document |
public Schema compileSchema(InputSource schemaSrc) throws InstantiationException {
SchematronSchema schema = null;
try {
// load Schema file into a DOM document
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance ();
DocumentBuilder dbld = dbf.newDocumentBuilder ();
Document document = dbld.parse( schemaSrc );
schema = buildSchema( document );
} catch (Exception e) {
logger.error("!!! Failed loading Schematron schema", e);
throw new CascadingRuntimeException(" !!! Failed loading Schematron schema", e);
}
return schema;
}
Builds a new Schema instance from
the given XML InputSource |
protected String fixns(String path) {
// Ironicly, at the time I am writing this
// JDK 1.4 is offering String.replaceAll(regex, str)
// I don't use it however for backward compatibility
StringBuffer strbuf = new StringBuffer( path );
int i = 0;
int j = 0;
String dprefix = defaultSchemaPrefix_ + ":";
int dplen = dprefix.length();
while ( ( j = path.indexOf ( dprefix, i ) ) >= 0 )
{
strbuf.append ( path.substring ( i, j ) );
strbuf.append ( schemaPrefix_ );
strbuf.append ( ':" );
i = j + dplen;
}
strbuf.append( path.substring ( i ) );
return strbuf.toString ();
}
|
protected Logger setupLogger() {
//
// Constructors
//
Logger logger = Hierarchy.getDefaultHierarchy().getLoggerFor("XmlForm");
logger.setPriority( Priority.ERROR );
return logger;
}
|