package com.m20.absolute.schematron; import java.io.File; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; import net.sourceforge.nrl.parser.ast.constraints.IConstraint; import net.sourceforge.nrl.parser.ast.constraints.IConstraintRuleDeclaration; import net.sourceforge.nrl.parser.model.IModelElement; import net.sourceforge.nrl.parser.model.IPackage; import net.sourceforge.nrl.parser.model.xsd.XSDModelLoader; import org.concordion.integration.junit3.ConcordionTestCase; import org.eclipse.emf.ecore.resource.Resource; import org.eclipse.xsd.util.XSDResourceFactoryImpl; import com.m20.absolute.schematron.ast.Rule; import com.m20.absolute.schematron.ast.SchematronSerialiser; import com.m20.absolute.schematron.ast.xpath.PathExpr; import com.m20.absolute.schematron.ast.xpath.XPathSerialiser; /** * The class implements the Concordion JUnit test class. It loads the model * against which the NRL to Schematron test cases are built and it specifies * possible contexts. The contexts are placed in a HashMap which maps the NRL * contexts to an array of contexts expressed in XPath for Schematron rules. * This class makes use of the NRLSchematronGenerator.html file to translate the * specified NRL to Schematron test-cases. * * @author Adriana Alexandru */ public class NRLSchematronGeneratorTest extends ConcordionTestCase { class Result { public String xpath; public String schematron; } private static IPackage model = null; private Map> pathMap; public void setUp() throws Exception { if (model == null) { Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put( "xsd", new XSDResourceFactoryImpl()); XSDModelLoader loader = new XSDModelLoader(); model = loader.load(new File("models/test-model.xsd")); for (String warning : loader.getWarnings()) { System.err.println(warning); } } /* Populate the map of XML paths */ pathMap = new HashMap>(); pathMap.put( model.getElementByName("Amount", true), Arrays.asList(new String[] { "//trade/leg/pay", "//trade/leg/receive", "//trade/leg/netCashFlow", "//trade/totalValue" })); pathMap.put(model.getElementByName("Trade", true), Arrays.asList(new String[] { "//trade" })); } public Result translateToXPath(String nrlConstraint) throws Exception { IConstraint constraint = TestUtilities.parseConstraint(nrlConstraint, model); PathExpr pathExpr = new NRLSchematronGenerator().translate(constraint); Result result = new Result(); result.xpath = XPathSerialiser.serialise(pathExpr); return result; } public Result translateToSchematron(String nrlRule) throws Exception { IConstraintRuleDeclaration rule = TestUtilities.parseRule(nrlRule, model); Rule schematronRule = new NRLSchematronGenerator().translate(rule, pathMap); Result result = new Result(); result.schematron = SchematronSerialiser.serialise(schematronRule); return result; } }