use of com.helger.phive.api.execute.IValidationExecutor in project peppol-practical by phax.
the class PageSecureSchematronTools method fillContent.
@Override
protected void fillContent(@Nonnull final WebPageExecutionContext aWPEC) {
final Locale aDisplayLocale = aWPEC.getDisplayLocale();
final HCNodeList aNodeList = aWPEC.getNodeList();
{
final BootstrapButtonToolbar aToolbar = new BootstrapButtonToolbar(aWPEC);
final BootstrapForm aForm = aToolbar.addAndReturnChild(new BootstrapForm(aWPEC).setFormType(EBootstrapFormType.INLINE));
aForm.addFormGroup(new BootstrapFormGroup().setCtrl(new ExtValidationKeySelect(new RequestField(FIELD_VESID), aDisplayLocale)));
aForm.addFormGroup(new BootstrapFormGroup().setCtrl(new ActionSelect(new RequestField(CPageParam.PARAM_ACTION))));
aForm.addFormGroup(new BootstrapFormGroup().setCtrl(new HCCheckBox(new RequestFieldBoolean(FIELD_STYLE_OUTPUT, DEFAULT_STYLE_OUTPUT)), new HCTextNode(" format?")));
aForm.addFormGroup(new BootstrapFormGroup().setCtrl(new BootstrapSubmitButton().addChild("Run")));
aNodeList.addChild(aToolbar);
}
final String sVESID = aWPEC.params().getAsString(FIELD_VESID);
final VESID aVESID = VESID.parseIDOrNull(sVESID);
final boolean bStyleOutput = aWPEC.params().isCheckBoxChecked(FIELD_STYLE_OUTPUT, DEFAULT_STYLE_OUTPUT);
final IValidationExecutorSet<IValidationSourceXML> aVES = ExtValidationKeyRegistry.getFromIDOrNull(aVESID);
if (aVES != null) {
if (aWPEC.hasAction(ACTION_SHOW_PREPROCESSED_SCHEMA)) {
LOGGER.info("Showing preprocessed Schematron of " + aVESID.getAsSingleID());
final MapBasedNamespaceContext aNSCtx = new MapBasedNamespaceContext();
aNSCtx.addDefaultNamespaceURI(CSchematron.NAMESPACE_SCHEMATRON);
aNSCtx.addMapping("xsl", "http://www.w3.org/1999/XSL/Transform");
aNSCtx.addMapping("svrl", CSVRL.SVRL_NAMESPACE_URI);
final IXMLWriterSettings XWS = new XMLWriterSettings().setIndent(EXMLSerializeIndent.INDENT_AND_ALIGN).setNamespaceContext(aNSCtx);
aNodeList.addChild(info("Showing preprocessed version of " + aVESID.getAsSingleID()));
final BootstrapTabBox aTabBox = new BootstrapTabBox();
for (final IValidationExecutor<?> aVE : aVES.getAllExecutors()) {
final IReadableResource aRes = aVE.getValidationArtefact().getRuleResource();
final IValidationType aType = aVE.getValidationArtefact().getValidationArtefactType();
if (aType == EValidationType.SCHEMATRON_PURE || aType == EValidationType.SCHEMATRON_SCH) {
IHCNode aTabContent;
try {
// Read Schematron
final PSSchema aSchema = new PSReader(aRes, null, null).readSchema();
final IPSQueryBinding aQueryBinding = PSQueryBindingRegistry.getQueryBindingOfNameOrThrow(aSchema.getQueryBinding());
final PSPreprocessor aPreprocessor = PSPreprocessor.createPreprocessorWithoutInformationLoss(aQueryBinding);
// Pre-process
final PSSchema aPreprocessedSchema = aPreprocessor.getAsPreprocessedSchema(aSchema);
if (aPreprocessedSchema == null)
throw new SchematronPreprocessException("Failed to preprocess schema " + aSchema + " with query binding " + aQueryBinding);
// Convert to XML string
final String sXML = MicroWriter.getNodeAsString(aPreprocessedSchema.getAsMicroElement(), XWS);
IHCNode aCode;
if (bStyleOutput) {
final HCPrismJS aPrism = new HCPrismJS(EPrismLanguage.MARKUP).addPlugin(new PrismPluginLineNumbers()).addChild(sXML);
aCode = aPrism;
} else
aCode = pre(sXML);
final CollectingPSErrorHandler aErrHdl = new CollectingPSErrorHandler();
aPreprocessedSchema.validateCompletely(aErrHdl);
if (aErrHdl.isEmpty()) {
aTabContent = aCode;
} else {
final HCUL aUL = new HCUL();
aErrHdl.getAllErrors().forEach(x -> aUL.addItem(x.getErrorText(aDisplayLocale)));
aTabContent = new HCNodeList().addChild(error("Errors in the Schematron:").addChild(aUL)).addChild(aCode);
}
} catch (final Exception ex) {
aTabContent = error("Error parsing Schematron: " + ex.getMessage());
}
aTabBox.addTab("t" + aTabBox.getTabCount(), FilenameHelper.getBaseName(aRes.getPath()), aTabContent);
} else if (aType == EValidationType.SCHEMATRON_XSLT) {
final IHCNode aTabContent = info("This is already XSLT");
aTabBox.addTab("t" + aTabBox.getTabCount(), FilenameHelper.getBaseName(aRes.getPath()), aTabContent);
}
}
if (aTabBox.hasNoTabs())
aNodeList.addChild(warn("No Schematron artefacts found"));
else
aNodeList.addChild(aTabBox);
} else if (aWPEC.hasAction(ACTION_SHOW_XSLT)) {
LOGGER.info("Showing XSLT version of " + aVESID.getAsSingleID());
final MapBasedNamespaceContext aNSCtx = new MapBasedNamespaceContext();
aNSCtx.addDefaultNamespaceURI(CSchematron.NAMESPACE_SCHEMATRON);
aNSCtx.addMapping("xsl", "http://www.w3.org/1999/XSL/Transform");
aNSCtx.addMapping("svrl", CSVRL.SVRL_NAMESPACE_URI);
final IXMLWriterSettings XWS = new XMLWriterSettings().setIndent(EXMLSerializeIndent.INDENT_AND_ALIGN).setNamespaceContext(aNSCtx);
aNodeList.addChild(info("Showing XSLT version of " + aVESID.getAsSingleID()));
final BootstrapTabBox aTabBox = new BootstrapTabBox();
for (final IValidationExecutor<IValidationSourceXML> aVE : aVES.getAllExecutors()) {
final IReadableResource aRes = aVE.getValidationArtefact().getRuleResource();
final IValidationType aType = aVE.getValidationArtefact().getValidationArtefactType();
if (aType == EValidationType.SCHEMATRON_PURE || aType == EValidationType.SCHEMATRON_SCH) {
IHCNode aTabContent;
try {
// Read Schematron
final SchematronResourceSCH aSch = new SchematronResourceSCH(aRes);
if (!aSch.isValidSchematron())
throw new SchematronPreprocessException("Invalid Schematron!");
final Document aXSLT = aSch.getXSLTProvider().getXSLTDocument();
if (aXSLT == null)
throw new SchematronPreprocessException("Failed to convert to XSLT!");
// Convert to XML string
final String sXML = XMLWriter.getNodeAsString(aXSLT, XWS);
// Highlight
if (bStyleOutput) {
final HCPrismJS aPrism = new HCPrismJS(EPrismLanguage.MARKUP).addPlugin(new PrismPluginLineNumbers()).addChild(sXML);
aTabContent = aPrism;
} else
aTabContent = pre(sXML);
} catch (final Exception ex) {
aTabContent = error("Error parsing Schematron: " + ex.getMessage());
}
aTabBox.addTab("t" + aTabBox.getTabCount(), FilenameHelper.getBaseName(aRes.getPath()), aTabContent);
} else if (aType == EValidationType.SCHEMATRON_XSLT) {
final IHCNode aTabContent = info("This is already XSLT");
aTabBox.addTab("t" + aTabBox.getTabCount(), FilenameHelper.getBaseName(aRes.getPath()), aTabContent);
}
}
if (aTabBox.hasNoTabs())
aNodeList.addChild(warn("No Schematron artefacts found"));
else
aNodeList.addChild(aTabBox);
} else {
// TODO other action when necessary
}
LOGGER.info("Done");
}
}
Aggregations