use of com.helger.schematron.pure.model.PSLet 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.PSLet in project ph-schematron by phax.
the class PSPreprocessor method getForcedPreprocessedSchema.
/**
* Convert the passed schema to a pre-processed schema independent if it is
* already minimal or not.
*
* @param aSchema
* The schema to be made minimal. May not be <code>null</code>
* @return A minimal copy of the schema. May be <code>null</code> if the
* original schema is not yet minimal and {@link #isKeepEmptySchema()}
* is set to <code>false</code>.
* @throws SchematronPreprocessException
* In case a preprocessing error occurs
*/
@Nullable
public PSSchema getForcedPreprocessedSchema(@Nonnull final PSSchema aSchema) throws SchematronPreprocessException {
ValueEnforcer.notNull(aSchema, "Schema");
final PreprocessorLookup aLookup = new PreprocessorLookup(aSchema);
final PreprocessorIDPool aIDPool = new PreprocessorIDPool();
final PSSchema ret = new PSSchema(aSchema.getResource());
ret.setID(aIDPool.getUniqueID(aSchema.getID()));
ret.setRich(aSchema.getRichClone());
ret.setSchemaVersion(aSchema.getSchemaVersion());
ret.setDefaultPhase(aSchema.getDefaultPhase());
ret.setQueryBinding(aSchema.getQueryBinding());
if (m_bKeepTitles && aSchema.hasTitle())
ret.setTitle(aSchema.getTitle().getClone());
if (aSchema.hasAnyInclude())
throw new SchematronPreprocessException("Cannot preprocess <schema> with an <include>");
for (final PSNS aNS : aSchema.getAllNSs()) ret.addNS(aNS.getClone());
// start ps are skipped
for (final PSLet aLet : aSchema.getAllLets()) ret.addLet(aLet.getClone());
for (final PSPhase aPhase : aSchema.getAllPhases()) ret.addPhase(_getPreprocessedPhase(aPhase, aIDPool));
for (final PSPattern aPattern : aSchema.getAllPatterns()) {
final PSPattern aMinifiedPattern = _getPreprocessedPattern(aPattern, aLookup, aIDPool);
if (aMinifiedPattern != null) {
// Pattern without rules?
if (aMinifiedPattern.getRuleCount() > 0 || m_bKeepEmptyPatterns)
ret.addPattern(aMinifiedPattern);
}
}
// Schema without patterns?
if (aSchema.getPatternCount() == 0 && !m_bKeepEmptySchema)
return null;
// end ps are skipped
if (m_bKeepDiagnostics && aSchema.hasDiagnostics())
ret.setDiagnostics(_getPreprocessedDiagnostics(aSchema.getDiagnostics()));
ret.addForeignElements(aSchema.getAllForeignElements());
ret.addForeignAttributes(aSchema.getAllForeignAttributes());
return ret;
}
use of com.helger.schematron.pure.model.PSLet 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.PSLet 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.PSLet in project ph-schematron by phax.
the class PSPreprocessor method _getPreprocessedRule.
@Nullable
private PSRule _getPreprocessedRule(@Nonnull final PSRule aRule, @Nonnull final PreprocessorLookup aLookup, @Nonnull final PreprocessorIDPool aIDPool, @Nullable final ICommonsMap<String, String> aParamValueMap) throws SchematronPreprocessException {
if (aRule.isAbstract()) {
// Will be inlined
return null;
}
final PSRule ret = new PSRule();
ret.setFlag(aRule.getFlag());
ret.setRich(aRule.getRichClone());
ret.setLinkable(aRule.getLinkableClone());
// abstract is always false
ret.setContext(m_aQueryBinding.getWithParamTextsReplaced(aRule.getContext(), aParamValueMap));
ret.setID(aIDPool.getUniqueID(aRule.getID()));
if (aRule.hasAnyInclude())
throw new SchematronPreprocessException("Cannot preprocess <rule> with an <include>");
for (final PSLet aLet : aRule.getAllLets()) ret.addLet(aLet.getClone());
_resolveRuleContent(aRule.getAllContentElements(), aLookup, aIDPool, aParamValueMap, ret);
ret.addForeignElements(aRule.getAllForeignElements());
ret.addForeignAttributes(aRule.getAllForeignAttributes());
return ret;
}
Aggregations