Search in sources :

Example 1 with IValidationExecutor

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");
    }
}
Also used : Locale(java.util.Locale) PrismPluginLineNumbers(com.helger.photon.uictrls.prism.PrismPluginLineNumbers) HCNodeList(com.helger.html.hc.impl.HCNodeList) VESID(com.helger.phive.api.executorset.VESID) RequestFieldBoolean(com.helger.photon.core.form.RequestFieldBoolean) IReadableResource(com.helger.commons.io.resource.IReadableResource) BootstrapTabBox(com.helger.photon.bootstrap4.nav.BootstrapTabBox) PSReader(com.helger.schematron.pure.exchange.PSReader) Document(org.w3c.dom.Document) PSSchema(com.helger.schematron.pure.model.PSSchema) MapBasedNamespaceContext(com.helger.xml.namespace.MapBasedNamespaceContext) IValidationType(com.helger.phive.api.IValidationType) SchematronPreprocessException(com.helger.schematron.pure.preprocess.SchematronPreprocessException) BootstrapButtonToolbar(com.helger.photon.bootstrap4.buttongroup.BootstrapButtonToolbar) RequestField(com.helger.photon.core.form.RequestField) CollectingPSErrorHandler(com.helger.schematron.pure.errorhandler.CollectingPSErrorHandler) ExtValidationKeySelect(com.helger.peppol.phive.ExtValidationKeySelect) IXMLWriterSettings(com.helger.xml.serialize.write.IXMLWriterSettings) XMLWriterSettings(com.helger.xml.serialize.write.XMLWriterSettings) IXMLWriterSettings(com.helger.xml.serialize.write.IXMLWriterSettings) IValidationExecutor(com.helger.phive.api.execute.IValidationExecutor) HCPrismJS(com.helger.photon.uictrls.prism.HCPrismJS) IValidationSourceXML(com.helger.phive.engine.source.IValidationSourceXML) PSPreprocessor(com.helger.schematron.pure.preprocess.PSPreprocessor) SchematronPreprocessException(com.helger.schematron.pure.preprocess.SchematronPreprocessException) BootstrapForm(com.helger.photon.bootstrap4.form.BootstrapForm) HCUL(com.helger.html.hc.html.grouping.HCUL) SchematronResourceSCH(com.helger.schematron.sch.SchematronResourceSCH) IPSQueryBinding(com.helger.schematron.pure.binding.IPSQueryBinding) HCCheckBox(com.helger.html.hc.html.forms.HCCheckBox) BootstrapSubmitButton(com.helger.photon.bootstrap4.button.BootstrapSubmitButton) BootstrapFormGroup(com.helger.photon.bootstrap4.form.BootstrapFormGroup) HCTextNode(com.helger.html.hc.impl.HCTextNode) IHCNode(com.helger.html.hc.IHCNode)

Aggregations

IReadableResource (com.helger.commons.io.resource.IReadableResource)1 IHCNode (com.helger.html.hc.IHCNode)1 HCCheckBox (com.helger.html.hc.html.forms.HCCheckBox)1 HCUL (com.helger.html.hc.html.grouping.HCUL)1 HCNodeList (com.helger.html.hc.impl.HCNodeList)1 HCTextNode (com.helger.html.hc.impl.HCTextNode)1 ExtValidationKeySelect (com.helger.peppol.phive.ExtValidationKeySelect)1 IValidationType (com.helger.phive.api.IValidationType)1 IValidationExecutor (com.helger.phive.api.execute.IValidationExecutor)1 VESID (com.helger.phive.api.executorset.VESID)1 IValidationSourceXML (com.helger.phive.engine.source.IValidationSourceXML)1 BootstrapSubmitButton (com.helger.photon.bootstrap4.button.BootstrapSubmitButton)1 BootstrapButtonToolbar (com.helger.photon.bootstrap4.buttongroup.BootstrapButtonToolbar)1 BootstrapForm (com.helger.photon.bootstrap4.form.BootstrapForm)1 BootstrapFormGroup (com.helger.photon.bootstrap4.form.BootstrapFormGroup)1 BootstrapTabBox (com.helger.photon.bootstrap4.nav.BootstrapTabBox)1 RequestField (com.helger.photon.core.form.RequestField)1 RequestFieldBoolean (com.helger.photon.core.form.RequestFieldBoolean)1 HCPrismJS (com.helger.photon.uictrls.prism.HCPrismJS)1 PrismPluginLineNumbers (com.helger.photon.uictrls.prism.PrismPluginLineNumbers)1