001 // Copyright 2006, 2008 The Apache Software Foundation
002 //
003 // Licensed under the Apache License, Version 2.0 (the "License");
004 // you may not use this file except in compliance with the License.
005 // You may obtain a copy of the License at
006 //
007 // http://www.apache.org/licenses/LICENSE-2.0
008 //
009 // Unless required by applicable law or agreed to in writing, software
010 // distributed under the License is distributed on an "AS IS" BASIS,
011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012 // See the License for the specific language governing permissions and
013 // limitations under the License.
014
015 package org.apache.tapestry5.internal.structure;
016
017 import org.apache.tapestry5.Binding;
018 import org.apache.tapestry5.MarkupWriter;
019 import org.apache.tapestry5.ioc.services.TypeCoercer;
020 import org.apache.tapestry5.runtime.RenderCommand;
021 import org.apache.tapestry5.runtime.RenderQueue;
022
023 public class ExpansionPageElement implements RenderCommand
024 {
025 private final Binding binding;
026
027 private final boolean invariant;
028
029 private final TypeCoercer coercer;
030
031 private boolean cached;
032
033 private String cachedValue;
034
035 public ExpansionPageElement(Binding binding, TypeCoercer coercer)
036 {
037 this.binding = binding;
038 this.coercer = coercer;
039
040 invariant = this.binding.isInvariant();
041 }
042
043 public void render(MarkupWriter writer, RenderQueue queue)
044 {
045 String value = cached ? cachedValue : coercer.coerce(binding.get(), String.class);
046
047 if (invariant && !cached)
048 {
049 cachedValue = value;
050 cached = true;
051 }
052
053 writer.write(value);
054 }
055
056 @Override
057 public String toString()
058 {
059 return String.format("Expansion[%s]", binding.toString());
060 }
061 }