<commit_before>/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2012, Red Hat, Inc., and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

package org.jboss.as.controller.transform;

import static org.jboss.as.controller.ControllerMessages.MESSAGES;
import org.jboss.as.controller.ExpressionResolver;
import org.jboss.as.controller.OperationContext;
import org.jboss.as.controller.OperationFailedException;
import org.jboss.as.controller.PathAddress;
import org.jboss.as.controller.PathElement;
import org.jboss.as.controller.ProcessType;
import org.jboss.as.controller.RunningMode;
import org.jboss.as.controller.registry.ImmutableManagementResourceRegistration;
import org.jboss.as.controller.registry.Resource;
import org.jboss.dmr.ModelNode;

import java.util.Iterator;
import java.util.Set;

/**
* @author <NAME>
*/
class ResourceTransformationContextImpl implements ResourceTransformationContext {

    private final Resource root;
    private final PathAddress current;
    private final OriginalModel originalModel;

    static ResourceTransformationContext create(final OperationContext context, final TransformationTarget target) {
        final Resource root = Resource.Factory.create();
        final Resource original = context.readResourceFromRoot(PathAddress.EMPTY_ADDRESS, true);
        final ImmutableManagementResourceRegistration registration = context.getRootResourceRegistration().getSubModel(PathAddress.EMPTY_ADDRESS);
        final ExpressionResolver expressionResolver = TransformerExpressionResolver.create(context, target.getTargetType());
        final OriginalModel originalModel = new OriginalModel(original, context.getRunningMode(), context.getProcessType(), target, registration, expressionResolver);
        return new ResourceTransformationContextImpl(root, PathAddress.EMPTY_ADDRESS, originalModel);
    }

    static ResourceTransformationContext create(TransformationTarget target, Resource model, ImmutableManagementResourceRegistration registration, ExpressionResolver resolver, RunningMode runningMode, ProcessType type) {
        final Resource root = Resource.Factory.create();
        final OriginalModel originalModel = new OriginalModel(model,  runningMode, type, target, registration,resolver);
        return new ResourceTransformationContextImpl(root, PathAddress.EMPTY_ADDRESS, originalModel);
    }

    ResourceTransformationContextImpl(Resource root, PathAddress address, final OriginalModel originalModel) {
        this.root = root;
        this.current = address;
        this.originalModel = originalModel;
    }

    public Resource createResource(final PathAddress element) {
        final PathAddress absoluteAddress = this.current.append(element);
        final Resource resource = Resource.Factory.create();
        addTransformedRecursiveResourceFromRoot(absoluteAddress, resource);
        return resource;
    }

    public Resource createResource(final PathAddress element, Resource copy) {
        final PathAddress absoluteAddress = this.current.append(element);
        final Resource resource = Resource.Factory.create();
        resource.writeModel(copy.getModel());
        addTransformedRecursiveResourceFromRoot(absoluteAddress, resource);
        return resource;
    }

    @Override
    public ResourceTransformationContext addTransformedResource(PathAddress address, Resource toAdd) {
        final PathAddress absoluteAddress = this.current.append(address);
        return addTransformedResourceFromRoot(absoluteAddress, toAdd);
    }

    @Override
    public ResourceTransformationContext addTransformedResourceFromRoot(PathAddress absoluteAddress, Resource toAdd) {
        // Only keep the mode, drop all children
        final Resource copy = Resource.Factory.create();
        if(toAdd != null) {
            copy.writeModel(toAdd.getModel());
        }
        return addTransformedRecursiveResourceFromRoot(absoluteAddress, copy);
    }

    @Override
    public void addTransformedRecursiveResource(PathAddress relativeAddress, Resource resource) {
        final PathAddress absoluteAddress = this.current.append(relativeAddress);
        addTransformedRecursiveResourceFromRoot(absoluteAddress, resource);
    }

    public ResourceTransformationContext addTransformedRecursiveResourceFromRoot(final PathAddress absoluteAddress, final Resource toAdd) {
        Resource model = this.root;
        final Iterator<PathElement> i = absoluteAddress.iterator();
        while (i.hasNext()) {
            final PathElement element = i.next();
            if (element.isMultiTarget()) {
                throw MESSAGES.cannotWriteTo("*");
            }
            if (! i.hasNext()) {
                if(model.hasChild(element)) {
                    throw MESSAGES.duplicateResourceAddress(absoluteAddress);
                } else {
                    model.registerChild(element, toAdd);
                    model = toAdd;
                }
            } else {
                model = model.getChild(element);
                if (model == null) {
                    PathAddress ancestor = PathAddress.EMPTY_ADDRESS;
                    for (PathElement pe : absoluteAddress) {
                        ancestor = ancestor.append(pe);
                        if (element.equals(pe)) {
                            break;
                        }
                    }
                    throw MESSAGES.resourceNotFound(ancestor, absoluteAddress);
                }
            }
        }
        return new ResourceTransformationContextImpl(root, absoluteAddress, originalModel);
    }

    @Override
    public ResourceTransformer resolveTransformer(PathAddress address) {
        final ResourceTransformer transformer = originalModel.target.resolveTransformer(address);
        if(transformer == null) {
            final ImmutableManagementResourceRegistration childReg = originalModel.getRegistration(address);
            if(childReg == null) {
                return ResourceTransformer.DISCARD;
            }
            if(childReg.isRemote() || childReg.isRuntimeOnly()) {
                return ResourceTransformer.DISCARD;
            }
            return ResourceTransformer.DEFAULT;
        }
        return transformer;
    }

    @Override
    public void processChildren(final Resource resource) throws OperationFailedException {
        final Set<String> types = resource.getChildTypes();<commit_msg>Do not ignore transformation of the root model<commit_after>/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2012, Red Hat, Inc., and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

package org.jboss.as.controller.transform;

import static org.jboss.as.controller.ControllerMessages.MESSAGES;

import java.util.Iterator;
import java.util.Set;

import org.jboss.as.controller.ExpressionResolver;
import org.jboss.as.controller.OperationContext;
import org.jboss.as.controller.OperationFailedException;
import org.jboss.as.controller.PathAddress;
import org.jboss.as.controller.PathElement;
import org.jboss.as.controller.ProcessType;
import org.jboss.as.controller.RunningMode;
import org.jboss.as.controller.registry.ImmutableManagementResourceRegistration;
import org.jboss.as.controller.registry.Resource;
import org.jboss.dmr.ModelNode;

/**
* @author <NAME>
*/
class ResourceTransformationContextImpl implements ResourceTransformationContext {

    private final Resource root;
    private final PathAddress current;
    private final OriginalModel originalModel;

    static ResourceTransformationContext create(final OperationContext context, final TransformationTarget target) {
        final Resource root = Resource.Factory.create();
        final Resource original = context.readResourceFromRoot(PathAddress.EMPTY_ADDRESS, true);
        final ImmutableManagementResourceRegistration registration = context.getRootResourceRegistration().getSubModel(PathAddress.EMPTY_ADDRESS);
        final ExpressionResolver expressionResolver = TransformerExpressionResolver.create(context, target.getTargetType());
        final OriginalModel originalModel = new OriginalModel(original, context.getRunningMode(), context.getProcessType(), target, registration, expressionResolver);
        return new ResourceTransformationContextImpl(root, PathAddress.EMPTY_ADDRESS, originalModel);
    }

    static ResourceTransformationContext create(TransformationTarget target, Resource model, ImmutableManagementResourceRegistration registration, ExpressionResolver resolver, RunningMode runningMode, ProcessType type) {
        final Resource root = Resource.Factory.create();
        final OriginalModel originalModel = new OriginalModel(model,  runningMode, type, target, registration,resolver);
        return new ResourceTransformationContextImpl(root, PathAddress.EMPTY_ADDRESS, originalModel);
    }

    ResourceTransformationContextImpl(Resource root, PathAddress address, final OriginalModel originalModel) {
        this.root = root;
        this.current = address;
        this.originalModel = originalModel;
    }

    public Resource createResource(final PathAddress element) {
        final PathAddress absoluteAddress = this.current.append(element);
        final Resource resource = Resource.Factory.create();
        addTransformedRecursiveResourceFromRoot(absoluteAddress, resource);
        return resource;
    }

    public Resource createResource(final PathAddress element, Resource copy) {
        final PathAddress absoluteAddress = this.current.append(element);
        final Resource resource = Resource.Factory.create();
        resource.writeModel(copy.getModel());
        addTransformedRecursiveResourceFromRoot(absoluteAddress, resource);
        return resource;
    }

    @Override
    public ResourceTransformationContext addTransformedResource(PathAddress address, Resource toAdd) {
        final PathAddress absoluteAddress = this.current.append(address);
        return addTransformedResourceFromRoot(absoluteAddress, toAdd);
    }

    @Override
    public ResourceTransformationContext addTransformedResourceFromRoot(PathAddress absoluteAddress, Resource toAdd) {
        // Only keep the mode, drop all children
        final Resource copy = Resource.Factory.create();
        if(toAdd != null) {
            copy.writeModel(toAdd.getModel());
        }
        return addTransformedRecursiveResourceFromRoot(absoluteAddress, copy);
    }

    @Override
    public void addTransformedRecursiveResource(PathAddress relativeAddress, Resource resource) {
        final PathAddress absoluteAddress = this.current.append(relativeAddress);
        addTransformedRecursiveResourceFromRoot(absoluteAddress, resource);
    }

    public ResourceTransformationContext addTransformedRecursiveResourceFromRoot(final PathAddress absoluteAddress, final Resource toAdd) {
        Resource model = this.root;
        if (absoluteAddress.size() > 0) {
            final Iterator<PathElement> i = absoluteAddress.iterator();
            while (i.hasNext()) {
                final PathElement element = i.next();
                if (element.isMultiTarget()) {
                    throw MESSAGES.cannotWriteTo("*");
                }
                if (! i.hasNext()) {
                    if(model.hasChild(element)) {
                        throw MESSAGES.duplicateResourceAddress(absoluteAddress);
                    } else {
                        model.registerChild(element, toAdd);
                        model = toAdd;
                    }
                } else {
                    model = model.getChild(element);
                    if (model == null) {
                        PathAddress ancestor = PathAddress.EMPTY_ADDRESS;
                        for (PathElement pe : absoluteAddress) {
                            ancestor = ancestor.append(pe);
                            if (element.equals(pe)) {
                                break;
                            }
                        }
                        throw MESSAGES.resourceNotFound(ancestor, absoluteAddress);
                    }
                }
            }
        } else {
            //If this was the root address, replace the resource model
            model.writeModel(toAdd.getModel());
        }
        return new ResourceTransformationContextImpl(root, absoluteAddress, originalModel);
    }

    @Override
    public ResourceTransformer resolveTransformer(PathAddress address) {
        final ResourceTransformer transformer = originalModel.target.resolveTransformer(address);
        if(transformer == null) {
            final ImmutableManagementResourceRegistration childReg = originalModel.getRegistration(address);
            if(childReg == null) {
                return ResourceTransformer.DISCARD;
            }
            if(childReg.isRemote() || childReg.isRuntimeOnly()) {
                return ResourceTransformer.DISCARD;
            }
            return ResourceTransformer.DEFAULT;
        }
        return transformer;
    }

    @Override
    public void processChildren(final Resource resource) throws OperationFailedException {
        final Set<String> types = resource.getChildTypes();
{'fraction_non_alphanumeric': 0.07269076305220884, 'fraction_numerical': 0.0023427041499330657, 'mean_word_length': 3.088943623426382, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5836817', 'n_tokens_mistral': 3509, 'n_tokens_neox': 3383, 'n_words': 975}