use of com.helger.schematron.pure.model.IPSElement in project ph-schematron by phax.
the class Issue30Test method testOfSchematronPH.
@Test
public void testOfSchematronPH() throws Exception {
final SchematronResourcePure aResPure = SchematronResourcePure.fromFile("src/test/resources/issues/github30/ph-test.sch");
aResPure.setEntityResolver(DefaultEntityResolver.createOnDemand(aResPure.getResource()));
final IPSErrorHandler aErrorHandler = new IPSErrorHandler() {
@Override
public void warn(final IReadableResource aRes, final IPSElement aSourceElement, final String sMessage) {
s_aLogger.info(sMessage);
}
@Override
public void error(final IReadableResource aRes, final IPSElement aSourceElement, final String sMessage, final Throwable t) {
s_aLogger.info(sMessage);
}
@Override
public void error(final IPSElement aSourceElement, final String sMessage) {
s_aLogger.info(sMessage);
}
};
aResPure.setErrorHandler(aErrorHandler);
aResPure.validateCompletely();
assertFalse(aResPure.isValidSchematron());
}
use of com.helger.schematron.pure.model.IPSElement in project ph-schematron by phax.
the class PSPreprocessor method _resolveRuleContent.
/**
* Resolve all <extends> elements. This method calls itself recursively
* until all extends elements are resolved.
*
* @param aRuleContent
* A list consisting of {@link PSAssertReport} and {@link PSExtends}
* objects. Never <code>null</code>.
* @param aLookup
* The rule lookup object
* @throws SchematronPreprocessException
* If the base rule of an extends object could not be resolved.
*/
private void _resolveRuleContent(@Nonnull final ICommonsList<IPSElement> aRuleContent, @Nonnull final PreprocessorLookup aLookup, @Nonnull final PreprocessorIDPool aIDPool, @Nullable final ICommonsMap<String, String> aParamValueMap, @Nonnull final PSRule aTargetRule) throws SchematronPreprocessException {
for (final IPSElement aElement : aRuleContent) {
if (aElement instanceof PSAssertReport) {
final PSAssertReport aAssertReport = (PSAssertReport) aElement;
aTargetRule.addAssertReport(_getPreprocessedAssert(aAssertReport, aIDPool, aParamValueMap));
} else {
final PSExtends aExtends = (PSExtends) aElement;
final String sRuleID = aExtends.getRule();
final PSRule aBaseRule = aLookup.getAbstractRuleOfID(sRuleID);
if (aBaseRule == null)
throw new SchematronPreprocessException("Failed to resolve rule ID '" + sRuleID + "' in extends statement. Available rules are: " + aLookup.getAllAbstractRuleIDs());
// Recursively resolve the extends of the base rule
_resolveRuleContent(aBaseRule.getAllContentElements(), aLookup, aIDPool, aParamValueMap, aTargetRule);
// Copy all lets
for (final PSLet aBaseLet : aBaseRule.getAllLets()) aTargetRule.addLet(aBaseLet.getClone());
}
}
}
use of com.helger.schematron.pure.model.IPSElement in project ph-schematron by phax.
the class PSPreprocessor method _getPreprocessedPhase.
@Nonnull
private static PSPhase _getPreprocessedPhase(@Nonnull final PSPhase aPhase, @Nonnull final PreprocessorIDPool aIDPool) throws SchematronPreprocessException {
final PSPhase ret = new PSPhase();
ret.setID(aIDPool.getUniqueID(aPhase.getID()));
ret.setRich(aPhase.getRichClone());
if (aPhase.hasAnyInclude())
throw new SchematronPreprocessException("Cannot preprocess <phase> with an <include>");
for (final IPSElement aElement : aPhase.getAllContentElements()) {
if (aElement instanceof PSActive)
ret.addActive(((PSActive) aElement).getClone());
else if (aElement instanceof PSLet)
ret.addLet(((PSLet) aElement).getClone());
// ps are ignored
}
ret.addForeignElements(aPhase.getAllForeignElements());
ret.addForeignAttributes(aPhase.getAllForeignAttributes());
return ret;
}
use of com.helger.schematron.pure.model.IPSElement in project ph-schematron by phax.
the class PSPreprocessor method _getPreprocessedPattern.
@Nullable
private PSPattern _getPreprocessedPattern(@Nonnull final PSPattern aPattern, @Nonnull final PreprocessorLookup aLookup, @Nonnull final PreprocessorIDPool aIDPool) throws SchematronPreprocessException {
if (aPattern.isAbstract()) {
// Will be inlined
return null;
}
final PSPattern ret = new PSPattern();
// abstract always false
// is-a must be resolved
ret.setID(aIDPool.getUniqueID(aPattern.getID()));
ret.setRich(aPattern.getRichClone());
if (aPattern.hasAnyInclude())
throw new SchematronPreprocessException("Cannot preprocess <pattern> with an <include>");
if (m_bKeepTitles && aPattern.hasTitle())
ret.setTitle(aPattern.getTitle().getClone());
final String sIsA = aPattern.getIsA();
if (sIsA != null) {
final PSPattern aBasePattern = aLookup.getAbstractPatternOfID(sIsA);
if (aBasePattern == null)
throw new SchematronPreprocessException("Failed to resolve the pattern denoted by is-a='" + sIsA + "'");
if (!ret.hasID())
ret.setID(aIDPool.getUniqueID(aBasePattern.getID()));
if (!ret.hasRich())
ret.setRich(aBasePattern.getRichClone());
// get the string replacements
final ICommonsNavigableMap<String, String> aParamValueMap = m_aQueryBinding.getStringReplacementMap(aPattern.getAllParams());
for (final IPSElement aElement : aBasePattern.getAllContentElements()) {
if (aElement instanceof PSLet)
ret.addLet(((PSLet) aElement).getClone());
else if (aElement instanceof PSRule) {
final PSRule aMinifiedRule = _getPreprocessedRule((PSRule) aElement, aLookup, aIDPool, aParamValueMap);
if (aMinifiedRule != null)
ret.addRule(aMinifiedRule);
}
// params must have be resolved
// ps are ignored
}
} else {
for (final IPSElement aElement : aPattern.getAllContentElements()) {
if (aElement instanceof PSLet)
ret.addLet(((PSLet) aElement).getClone());
else if (aElement instanceof PSRule) {
final PSRule aMinifiedRule = _getPreprocessedRule((PSRule) aElement, aLookup, aIDPool, null);
if (aMinifiedRule != null)
ret.addRule(aMinifiedRule);
}
// params must be resolved
// ps are ignored
}
}
ret.addForeignElements(aPattern.getAllForeignElements());
ret.addForeignAttributes(aPattern.getAllForeignAttributes());
return ret;
}
use of com.helger.schematron.pure.model.IPSElement in project ph-schematron by phax.
the class PSXPathBoundSchema method _createBoundElements.
@Nullable
private ICommonsList<PSXPathBoundElement> _createBoundElements(@Nonnull final IPSHasMixedContent aMixedContent, @Nonnull final XPath aXPathContext, @Nonnull final IPSXPathVariables aVariables) {
final ICommonsList<PSXPathBoundElement> ret = new CommonsArrayList<>();
boolean bHasAnyError = false;
for (final Object aContentElement : aMixedContent.getAllContentElements()) {
if (aContentElement instanceof PSName) {
final PSName aName = (PSName) aContentElement;
if (aName.hasPath()) {
// Replace all variables
final String sPath = aVariables.getAppliedReplacement(aName.getPath());
try {
final XPathExpression aXpathExpression = _compileXPath(aXPathContext, sPath);
ret.add(new PSXPathBoundElement(aName, sPath, aXpathExpression));
} catch (final XPathExpressionException ex) {
error(aName, "Failed to compile XPath expression in <name>: '" + sPath + "'", ex.getCause() != null ? ex.getCause() : ex);
bHasAnyError = true;
}
} else {
// No XPath required
ret.add(new PSXPathBoundElement(aName));
}
} else if (aContentElement instanceof PSValueOf) {
final PSValueOf aValueOf = (PSValueOf) aContentElement;
// Replace variables
final String sSelect = aVariables.getAppliedReplacement(aValueOf.getSelect());
try {
final XPathExpression aXPathExpression = _compileXPath(aXPathContext, sSelect);
ret.add(new PSXPathBoundElement(aValueOf, sSelect, aXPathExpression));
} catch (final XPathExpressionException ex) {
error(aValueOf, "Failed to compile XPath expression in <value-of>: '" + sSelect + "'", ex);
bHasAnyError = true;
}
} else {
// No XPath compilation necessary
if (aContentElement instanceof String)
ret.add(new PSXPathBoundElement((String) aContentElement));
else
ret.add(new PSXPathBoundElement((IPSElement) aContentElement));
}
}
if (bHasAnyError)
return null;
return ret;
}
Aggregations