private void updateReferences()

in core/src/main/java/org/apache/sling/cms/core/internal/operations/UpdateReferencesPostOperation.java [56:89]


    private void updateReferences(SlingHttpServletRequest request, final List<Modification> changes) {
        final String find = request.getResource().getPath();
        final String destination = request.getParameter(SlingPostConstants.RP_DEST);
        if (log.isDebugEnabled()) {
            log.debug("Using destination: {}", CommonUtils.escapeLogMessage(destination));
        }
        ReferenceOperation ro = new ReferenceOperation(request.getResource()) {
            @Override
            public void doProcess(Resource resource, String matchingKey) {
                ModifiableValueMap properties = resource.adaptTo(ModifiableValueMap.class);
                log.trace("Updating references in property {}@{}", resource.getPath(), matchingKey);
                if (properties != null) {
                    if (properties.get(matchingKey) instanceof String) {
                        String value = properties.get(matchingKey, "").replace(find, destination);
                        properties.put(matchingKey, value);
                        log.trace("Updated value {}", value);
                    } else if (properties.get(matchingKey) instanceof String[]) {
                        String[] values = properties.get(matchingKey, new String[0]);
                        for (int i = 0; i < values.length; i++) {
                            values[i] = values[i].replace(find, destination);
                        }
                        properties.put(matchingKey, values);
                        if (log.isTraceEnabled()) {
                            log.trace("Updated values {}", Arrays.toString(values));
                        }
                    }
                } else {
                    log.warn("Unable to update references in {}, unable to edit", resource);
                }
                changes.add(Modification.onModified(resource.getPath()));
            }
        };
        ro.init();
    }