Search in sources :

Example 6 with CollectingSAXErrorHandler

use of com.helger.xml.sax.CollectingSAXErrorHandler in project phoss-directory by phax.

the class PageSecureIndexImport method fillContent.

@Override
protected void fillContent(final WebPageExecutionContext aWPEC) {
    final HCNodeList aNodeList = aWPEC.getNodeList();
    final Locale aDisplayLocale = aWPEC.getDisplayLocale();
    final IIdentifierFactory aIdentifierFactory = PDMetaManager.getIdentifierFactory();
    final FormErrorList aFormErrors = new FormErrorList();
    {
        final IPDBusinessCardProvider aBCProv = PDMetaManager.getBusinessCardProvider();
        if (aBCProv instanceof SMPBusinessCardProvider) {
            final SMPBusinessCardProvider aSMPBCProv = (SMPBusinessCardProvider) aBCProv;
            if (aSMPBCProv.isFixedSMP()) {
                aNodeList.addChild(info("Fixed SMP URI " + aSMPBCProv.getFixedSMPURI() + " is used."));
            } else {
                aNodeList.addChild(info("The following SMLs are crawled for entries: " + StringHelper.getImplodedMapped(", ", aSMPBCProv.getAllSMLsToUse(), ISMLInfo::getDisplayName)));
            }
        }
    }
    final boolean bIsFormSubmitted = aWPEC.hasAction(CPageParam.ACTION_PERFORM);
    if (bIsFormSubmitted) {
        final IFileItem aFile = aWPEC.params().getAsFileItem(FIELD_FILE);
        if (aFile == null || StringHelper.hasNoText(aFile.getName()))
            aFormErrors.addFieldError(FIELD_FILE, "No file was selected");
        if (aFormErrors.isEmpty()) {
            final HCNodeList aResultNL = new HCNodeList();
            final SAXReaderSettings aSettings = new SAXReaderSettings();
            aSettings.setFeatureValues(EXMLParserFeature.AVOID_DOS_SETTINGS);
            final CollectingSAXErrorHandler aErrorHandler = new CollectingSAXErrorHandler();
            aSettings.setErrorHandler(aErrorHandler);
            final ICommonsList<IParticipantIdentifier> aQueued = new CommonsArrayList<>();
            final ICommonsList<IParticipantIdentifier> aNotQueued = new CommonsArrayList<>();
            aSettings.setContentHandler(new DefaultHandler() {

                @Override
                public void startElement(final String sURI, final String sLocalName, final String sQName, final Attributes aAttributes) throws SAXException {
                    if (sQName.equals("participant")) {
                        final String sScheme = aAttributes.getValue("scheme");
                        final String sValue = aAttributes.getValue("value");
                        final IParticipantIdentifier aParticipantID = aIdentifierFactory.createParticipantIdentifier(sScheme, sValue);
                        if (aParticipantID != null) {
                            if (PDMetaManager.getIndexerMgr().queueWorkItem(aParticipantID, EIndexerWorkItemType.CREATE_UPDATE, "import-triggered", PDIndexerManager.HOST_LOCALHOST).isChanged()) {
                                aQueued.add(aParticipantID);
                            } else {
                                aNotQueued.add(aParticipantID);
                            }
                        } else
                            LOGGER.error("Failed to convert '" + sScheme + "' and '" + sValue + "' to a participant identifier");
                    }
                }
            });
            LOGGER.info("Importing participant IDs from '" + aFile.getNameSecure() + "'");
            final ESuccess eSuccess = SAXReader.readXMLSAX(new FileItemResource(aFile), aSettings);
            LOGGER.info("Finished reading XML file. Queued " + aQueued.size() + "; not queued: " + aNotQueued.size() + "; errors: " + aErrorHandler.getErrorList().size());
            // Some things may have been queued even in case of error
            if (aQueued.isNotEmpty()) {
                final HCUL aUL = new HCUL();
                for (final IParticipantIdentifier aPI : aQueued) aUL.addItem(aPI.getURIEncoded());
                aResultNL.addChild(success(div("The following identifiers were successfully queued for indexing:")).addChild(aUL));
            }
            if (aNotQueued.isNotEmpty()) {
                final HCUL aUL = new HCUL();
                for (final IParticipantIdentifier aPI : aNotQueued) aUL.addItem(aPI.getURIEncoded());
                aResultNL.addChild(warn(div("The following identifiers could not be queued (because they are already in the queue):")).addChild(aUL));
            }
            if (eSuccess.isFailure()) {
                final HCUL aUL = new HCUL();
                for (final IError aError : aErrorHandler.getErrorList()) {
                    final String sMsg = aError.getAsString(AppCommonUI.DEFAULT_LOCALE);
                    LOGGER.error("  " + sMsg);
                    aUL.addItem(sMsg);
                }
                aResultNL.addChild(error(div("Error parsing provided XML:")).addChild(aUL));
            }
            aWPEC.postRedirectGetInternal(aResultNL);
        }
    }
    final BootstrapForm aForm = aNodeList.addAndReturnChild(getUIHandler().createFormFileUploadSelf(aWPEC, bIsFormSubmitted));
    aForm.addFormGroup(new BootstrapFormGroup().setLabelMandatory("Import file").setCtrl(new BootstrapFileUpload(FIELD_FILE, aDisplayLocale)).setHelpText("Select a file that was created from a full XML export to index of all them manually.").setErrorList(aFormErrors.getListOfField(FIELD_FILE)));
    final BootstrapButtonToolbar aToolbar = aForm.addAndReturnChild(new BootstrapButtonToolbar(aWPEC));
    aToolbar.addHiddenField(CPageParam.PARAM_ACTION, CPageParam.ACTION_PERFORM);
    aToolbar.addSubmitButton("Import all", EDefaultIcon.YES);
}
Also used : Locale(java.util.Locale) HCNodeList(com.helger.html.hc.impl.HCNodeList) CollectingSAXErrorHandler(com.helger.xml.sax.CollectingSAXErrorHandler) ISMLInfo(com.helger.peppol.sml.ISMLInfo) FormErrorList(com.helger.photon.core.form.FormErrorList) Attributes(org.xml.sax.Attributes) SAXReaderSettings(com.helger.xml.serialize.read.SAXReaderSettings) SAXException(org.xml.sax.SAXException) BootstrapFileUpload(com.helger.photon.bootstrap4.uictrls.ext.BootstrapFileUpload) IFileItem(com.helger.web.fileupload.IFileItem) BootstrapButtonToolbar(com.helger.photon.bootstrap4.buttongroup.BootstrapButtonToolbar) IIdentifierFactory(com.helger.peppolid.factory.IIdentifierFactory) SMPBusinessCardProvider(com.helger.pd.indexer.businesscard.SMPBusinessCardProvider) ESuccess(com.helger.commons.state.ESuccess) IError(com.helger.commons.error.IError) FileItemResource(com.helger.web.fileupload.FileItemResource) DefaultHandler(org.xml.sax.helpers.DefaultHandler) BootstrapForm(com.helger.photon.bootstrap4.form.BootstrapForm) IPDBusinessCardProvider(com.helger.pd.indexer.businesscard.IPDBusinessCardProvider) HCUL(com.helger.html.hc.html.grouping.HCUL) BootstrapFormGroup(com.helger.photon.bootstrap4.form.BootstrapFormGroup) CommonsArrayList(com.helger.commons.collection.impl.CommonsArrayList) IParticipantIdentifier(com.helger.peppolid.IParticipantIdentifier)

Aggregations

CollectingSAXErrorHandler (com.helger.xml.sax.CollectingSAXErrorHandler)6 XMLSchemaCache (com.helger.xml.schema.XMLSchemaCache)4 Validator (javax.xml.validation.Validator)4 FileSystemResource (com.helger.commons.io.resource.FileSystemResource)3 Test (org.junit.Test)3 CommonsArrayList (com.helger.commons.collection.impl.CommonsArrayList)2 IError (com.helger.commons.error.IError)2 IParticipantIdentifier (com.helger.peppolid.IParticipantIdentifier)2 CommonsHashMap (com.helger.commons.collection.impl.CommonsHashMap)1 ICommonsList (com.helger.commons.collection.impl.ICommonsList)1 ICommonsMap (com.helger.commons.collection.impl.ICommonsMap)1 ClassPathResource (com.helger.commons.io.resource.ClassPathResource)1 ESuccess (com.helger.commons.state.ESuccess)1 HCUL (com.helger.html.hc.html.grouping.HCUL)1 HCNodeList (com.helger.html.hc.impl.HCNodeList)1 IJsonArray (com.helger.json.IJsonArray)1 IJsonObject (com.helger.json.IJsonObject)1 JsonArray (com.helger.json.JsonArray)1 JsonObject (com.helger.json.JsonObject)1 JsonWriterSettings (com.helger.json.serialize.JsonWriterSettings)1