private void repairXmlContents() throws CmsException {

        // set the resource filter to filter XML contents of the selected type
        CmsResourceFilter filter = CmsResourceFilter.IGNORE_EXPIRATION.addRequireType(m_settings.getResourceTypeId());
        String path = CmsResource.getFolderPath(m_settings.getVfsFolder());
        // get the list of resources to check
        List<CmsResource> resources = getCms().readResources(path, filter, m_settings.isIncludeSubFolders());

        // set the report counters
        int count = 0;
        int resSize = resources.size();

        // create an entity resolver to use
        CmsXmlEntityResolver resolver = new CmsXmlEntityResolver(getCms());

        // iterate the resources
        Iterator<CmsResource> i = resources.iterator();
        while (i.hasNext()) {

            count++;
            CmsResource res = i.next();

            // generate report output
            getReport().print(
                org.opencms.report.Messages.get().container(
                    org.opencms.report.Messages.RPT_SUCCESSION_2,
                    String.valueOf(count),
                    String.valueOf(resSize)),
                I_CmsReport.FORMAT_NOTE);
            getReport().print(Messages.get().container(Messages.RPT_PROCESSING_XMLCONTENT_0), I_CmsReport.FORMAT_NOTE);
            getReport().print(
                org.opencms.report.Messages.get().container(
                    org.opencms.report.Messages.RPT_ARGUMENT_1,
                    getCms().getSitePath(res)));
            getReport().print(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0));

            try {

                // get the file contents
                CmsFile file = getCms().readFile(res);
                // get the XML content
                CmsXmlContent xmlContent = CmsXmlContentFactory.unmarshal(getCms(), file);

                // check the XML structure
                boolean fixFile = m_settings.isForce();
                if (!fixFile) {
                    try {
                        xmlContent.validateXmlStructure(resolver);
                    } catch (CmsXmlException e) {
                        // XML structure is not valid, this file has to be fixed
                        fixFile = true;
                    }
                }
                if (fixFile) {

                    // check the lock state of the file to repair
                    CmsLock lock = getCms().getLock(res);
                    boolean isLocked = false;
                    boolean canWrite = false;
                    if (lock.isNullLock()) {
                        // file is not locked, lock it
                        getCms().lockResource(getCms().getSitePath(res));
                        isLocked = true;
                        canWrite = true;
                    } else if (lock.isOwnedBy(getCms().getRequestContext().getCurrentUser())) {
                        // file is locked by current user
                        canWrite = true;
                    }

                    if (canWrite) {
                        // enable "auto correction mode" - this is required or the XML structure will not be fully corrected
                        xmlContent.setAutoCorrectionEnabled(true);
                        // now correct the XML
                        xmlContent.correctXmlStructure(getCms());
                        file.setContents(xmlContent.marshal());
                        // write the corrected file
                        getCms().writeFile(file);
                    } else {
                        // no write operation possible
                        getReport().println(
                            Messages.get().container(Messages.RPT_XMLCONTENTREPAIR_NOTLOCKED_0),
                            I_CmsReport.FORMAT_NOTE);
                    }

                    if (isLocked) {
                        // unlock previously locked resource
                        getCms().unlockResource(getCms().getSitePath(res));
                    }

                    if (canWrite) {
                        // successfully repaired XML content, report it
                        getReport().println(
                            Messages.get().container(Messages.RPT_XMLCONTENTREPAIR_REPAIRED_0),
                            I_CmsReport.FORMAT_OK);
                    }

                } else {
                    // nothing to fix, skip this file
                    getReport().println(
                        Messages.get().container(Messages.RPT_XMLCONTENTREPAIR_SKIPFILE_0),
                        I_CmsReport.FORMAT_NOTE);
                }

            } catch (CmsException e) {
                // an error occurred, show exception on report output
                getReport().println(e);
            }
        }
    }
private void repairXmlContents ( ) throws CmsException { CmsResourceFilter filter = CmsResourceFilter . IGNORE_EXPIRATION . addRequireType ( m_settings . getResourceTypeId ( ) ) ; String path = CmsResource . getFolderPath ( m_settings . getVfsFolder ( ) ) ; List < CmsResource > resources = getCms ( ) . readResources ( path , filter , m_settings . isIncludeSubFolders ( ) ) ; int count = 0 ; int resSize = resources . size ( ) ; CmsXmlEntityResolver resolver = new CmsXmlEntityResolver ( getCms ( ) ) ; Iterator < CmsResource > i = resources . iterator ( ) ; while ( i . hasNext ( ) ) { count ++ ; CmsResource res = i . next ( ) ; getReport ( ) . print ( org . opencms . report . Messages . get ( ) . container ( org . opencms . report . Messages . RPT_SUCCESSION_2 , String . valueOf ( count ) , String . valueOf ( resSize ) ) , I_CmsReport . FORMAT_NOTE ) ; getReport ( ) . print ( Messages . get ( ) . container ( Messages . RPT_PROCESSING_XMLCONTENT_0 ) , I_CmsReport . FORMAT_NOTE ) ; getReport ( ) . print ( org . opencms . report . Messages . get ( ) . container ( org . opencms . report . Messages . RPT_ARGUMENT_1 , getCms ( ) . getSitePath ( res ) ) ) ; getReport ( ) . print ( org . opencms . report . Messages . get ( ) . container ( org . opencms . report . Messages . RPT_DOTS_0 ) ) ; try { CmsFile file = getCms ( ) . readFile ( res ) ; CmsXmlContent xmlContent = CmsXmlContentFactory . unmarshal ( getCms ( ) , file ) ; boolean fixFile = m_settings . isForce ( ) ; if ( ! fixFile ) { try { xmlContent . validateXmlStructure ( resolver ) ; } catch ( CmsXmlException e ) { fixFile = true ; } } if ( fixFile ) { CmsLock lock = getCms ( ) . getLock ( res ) ; boolean isLocked = false ; boolean canWrite = false ; if ( lock . isNullLock ( ) ) { getCms ( ) . lockResource ( getCms ( ) . getSitePath ( res ) ) ; isLocked = true ; canWrite = true ; } else if ( lock . isOwnedBy ( getCms ( ) . getRequestContext ( ) . getCurrentUser ( ) ) ) { canWrite = true ; } if ( canWrite ) { xmlContent . setAutoCorrectionEnabled ( true ) ; xmlContent . correctXmlStructure ( getCms ( ) ) ; file . setContents ( xmlContent . marshal ( ) ) ; getCms ( ) . writeFile ( file ) ; } else { getReport ( ) . println ( Messages . get ( ) . container ( Messages . RPT_XMLCONTENTREPAIR_NOTLOCKED_0 ) , I_CmsReport . FORMAT_NOTE ) ; } if ( isLocked ) { getCms ( ) . unlockResource ( getCms ( ) . getSitePath ( res ) ) ; } if ( canWrite ) { getReport ( ) . println ( Messages . get ( ) . container ( Messages . RPT_XMLCONTENTREPAIR_REPAIRED_0 ) , I_CmsReport . FORMAT_OK ) ; } } else { getReport ( ) . println ( Messages . get ( ) . container ( Messages . RPT_XMLCONTENTREPAIR_SKIPFILE_0 ) , I_CmsReport . FORMAT_NOTE ) ; } } catch ( CmsException e ) { getReport ( ) . println ( e ) ; } } }
Performs the correction of the XML content resources according to their XML schema definition.<p>

@throws CmsException if reading the list of resources to repair fails