/*
 * Copyright (C) 2013 salesforce.com, inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.auraframework.renderer;

import java.io.IOException;
import java.util.List;

import javax.inject.Inject;

import org.auraframework.annotations.Annotations.ServiceComponentRenderer;
import org.auraframework.def.ComponentDefRef;
import org.auraframework.def.Renderer;
import org.auraframework.instance.BaseComponent;
import org.auraframework.instance.Wrapper;
import org.auraframework.service.InstanceService;
import org.auraframework.service.RenderingService;
import org.auraframework.system.RenderContext;
import org.auraframework.throwable.quickfix.QuickFixException;

@ServiceComponentRenderer
public class ExpressionRenderer implements Renderer {

    @Inject
    private RenderingService renderingService;

    @Inject
    private InstanceService instanceService;

    @Override
    public void render(BaseComponent<?, ?> component, RenderContext rc) throws IOException, QuickFixException {
        Object value = component.getAttributes().getValue("value");

        if (value instanceof Wrapper) {
            value = ((Wrapper) value).unwrap();
        }

        if (value instanceof String) {
            String escaped = (String) value;
            // This amounts to a convoluted test for "is the direct container
            // of this expression a template?"  We need that test because we use
            // "bad" characters like script tags in our own expressions, but want to
            // deny them from users.
            boolean inTemplate = component.getAttributes().getValueProvider()
                    .getDescriptor().getDef().isTemplate();

            if (!inTemplate) {
                // We don't escape all the HTML characters, because quotes in particular
                // would cause problems.
                escaped = escaped.replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;");
            }
            rc.getCurrent().append(escaped);
        } else if (value instanceof List) {
            List<?> kids = (List<?>) value;
            for (Object kid : kids) {
                if (kid instanceof BaseComponent) {
                    this.renderingService.render((BaseComponent<?, ?>) kid, rc);
                } else if (kid instanceof ComponentDefRef) {
                    BaseComponent cmp = (BaseComponent) this.instanceService.getInstance((ComponentDefRef) kid,
                            component);
                    this.renderingService.render(cmp, rc);
                }
            }
        } else if (value != null) {
            rc.getCurrent().append(value.toString());
        }
    }
}

@@ -844,16 +844,118 @@
 DefRef;%0A
+import org.auraframework.def.DefDescriptor.DefType;%0Aimport org.auraframework.def.DefinitionReference;%0A
 import o
@@ -2919,27 +2919,181 @@
 eof 
-ComponentDefRef) %7B%0A
+DefinitionReference) %7B%0A                    DefinitionReference defRef = ((DefinitionReference) kid).get();%0A                    if (defRef.type() == DefType.COMPONENT) %7B%0A    
     
@@ -3199,13 +3199,20 @@
 ef) 
-kid,%0A
+defRef,%0A    
     
@@ -3259,32 +3259,36 @@
                 
+  
+  
 this.renderingSe
@@ -3306,24 +3306,46 @@
 r(cmp, rc);%0A
+                    %7D%0A
             

Fix ComponentDefRef rendering in ExpressionRenderer for DefRefDelegate support