private byte[] replaceInXml(CmsFile cmsFile) throws Exception {

        Exception e = null;
        CmsXmlContent xmlContent = CmsXmlContentFactory.unmarshal(getCms(), cmsFile);
        Pattern pattern = Pattern.compile(m_settings.getSearchpattern());
        // loop over the locales of the content
        boolean modified = false;
        boolean matched = false;
        String requestedLocale = m_settings.getLocale();
        for (Locale locale : xmlContent.getLocales()) {
            if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(requestedLocale)
                && !locale.toString().equalsIgnoreCase(requestedLocale)) {
                // does not match the requested locale, skip it
                continue;
            }
            // loop over the available element paths of the current content locale
            List<String> paths = xmlContent.getNames(locale);
            for (String xpath : paths) {
                // try to get the value extraction for the current element path
                I_CmsXmlContentValue value = xmlContent.getValue(xpath, locale);
                if (value.isSimpleType()) {
                    try {
                        String currPath = value.getPath();
                        if (CmsStringUtil.isEmptyOrWhitespaceOnly(m_settings.getXpath())
                            || currPath.equals(m_settings.getXpath())
                            || (CmsXmlUtils.removeXpath(currPath).equals(m_settings.getXpath()))) {
                            // xpath match
                            String oldVal = value.getStringValue(getCms());
                            Matcher matcher = pattern.matcher(oldVal);
                            matcher = Pattern.compile(m_settings.getSearchpattern()).matcher(oldVal);
                            if (matcher.find()) {
                                matched = true;
                                m_matchedResources.add(cmsFile);
                                if (m_replace) {
                                    String newVal = matcher.replaceAll(m_settings.getReplacepattern());
                                    if (!oldVal.equals(newVal)) {
                                        value.setStringValue(getCms(), newVal);
                                        modified = true;
                                    }
                                }
                            }
                        }
                    } catch (Exception ex) {
                        // log and go on
                        LOG.error(ex.getMessage(), ex);
                        e = ex;
                    }
                }
            }
        }
        if (e != null) {
            throw e;
        }
        if (matched) {
            getReport().println(Messages.get().container(Messages.RPT_SOURCESEARCH_MATCHED_0), I_CmsReport.FORMAT_OK);
        } else {
            getReport().println(
                Messages.get().container(Messages.RPT_SOURCESEARCH_NOT_MATCHED_0),
                I_CmsReport.FORMAT_NOTE);
        }
        if (modified) {
            return xmlContent.marshal();
        }
        return null;
    }
private byte [ ] replaceInXml ( CmsFile cmsFile ) throws Exception { Exception e = null ; CmsXmlContent xmlContent = CmsXmlContentFactory . unmarshal ( getCms ( ) , cmsFile ) ; Pattern pattern = Pattern . compile ( m_settings . getSearchpattern ( ) ) ; boolean modified = false ; boolean matched = false ; String requestedLocale = m_settings . getLocale ( ) ; for ( Locale locale : xmlContent . getLocales ( ) ) { if ( CmsStringUtil . isNotEmptyOrWhitespaceOnly ( requestedLocale ) && ! locale . toString ( ) . equalsIgnoreCase ( requestedLocale ) ) { continue ; } List < String > paths = xmlContent . getNames ( locale ) ; for ( String xpath : paths ) { I_CmsXmlContentValue value = xmlContent . getValue ( xpath , locale ) ; if ( value . isSimpleType ( ) ) { try { String currPath = value . getPath ( ) ; if ( CmsStringUtil . isEmptyOrWhitespaceOnly ( m_settings . getXpath ( ) ) || currPath . equals ( m_settings . getXpath ( ) ) || ( CmsXmlUtils . removeXpath ( currPath ) . equals ( m_settings . getXpath ( ) ) ) ) { String oldVal = value . getStringValue ( getCms ( ) ) ; Matcher matcher = pattern . matcher ( oldVal ) ; matcher = Pattern . compile ( m_settings . getSearchpattern ( ) ) . matcher ( oldVal ) ; if ( matcher . find ( ) ) { matched = true ; m_matchedResources . add ( cmsFile ) ; if ( m_replace ) { String newVal = matcher . replaceAll ( m_settings . getReplacepattern ( ) ) ; if ( ! oldVal . equals ( newVal ) ) { value . setStringValue ( getCms ( ) , newVal ) ; modified = true ; } } } } } catch ( Exception ex ) { LOG . error ( ex . getMessage ( ) , ex ) ; e = ex ; } } } } if ( e != null ) { throw e ; } if ( matched ) { getReport ( ) . println ( Messages . get ( ) . container ( Messages . RPT_SOURCESEARCH_MATCHED_0 ) , I_CmsReport . FORMAT_OK ) ; } else { getReport ( ) . println ( Messages . get ( ) . container ( Messages . RPT_SOURCESEARCH_NOT_MATCHED_0 ) , I_CmsReport . FORMAT_NOTE ) ; } if ( modified ) { return xmlContent . marshal ( ) ; } return null ; }
Performs a replacement for XML contents.<p>

@param cmsFile the file to operate on

@return the marshaled content
@throws Exception if something goes wrong