001 // Copyright 2004, 2005 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.tapestry.services.impl;
016
017 import java.util.Iterator;
018 import java.util.Map;
019
020 import org.apache.hivemind.util.ToStringBuilder;
021 import org.apache.tapestry.IComponent;
022 import org.apache.tapestry.IMarkupWriter;
023 import org.apache.tapestry.IRender;
024 import org.apache.tapestry.IRequestCycle;
025 import org.apache.tapestry.parse.LocalizationToken;
026 import org.apache.tapestry.parse.TextToken;
027
028 /**
029 * A class used with invisible localizations. Constructed from a {@link TextToken}.
030 *
031 * @author Howard Lewis Ship
032 * @since 3.0
033 */
034
035 public class LocalizedStringRender implements IRender
036 {
037 private IComponent _component;
038
039 private String _key;
040
041 private Map _attributes;
042
043 private String _value;
044
045 private boolean _raw;
046
047 private String _tag;
048
049 public LocalizedStringRender(IComponent component, LocalizationToken token)
050 {
051 _component = component;
052 _key = token.getKey();
053 _raw = token.isRaw();
054 _tag = token.getTag();
055 _attributes = token.getAttributes();
056 }
057
058 public void render(IMarkupWriter writer, IRequestCycle cycle)
059 {
060 if (cycle.isRewinding())
061 return;
062
063 if (_attributes != null)
064 {
065 writer.begin(_tag == null ? "span" : _tag);
066
067 Iterator i = _attributes.entrySet().iterator();
068
069 while (i.hasNext())
070 {
071 Map.Entry entry = (Map.Entry) i.next();
072 String attributeName = (String) entry.getKey();
073 String attributeValue = (String) entry.getValue();
074
075 writer.attribute(attributeName, attributeValue);
076 }
077 }
078
079 if (_value == null)
080 _value = _component.getMessages().getMessage(_key);
081
082 writer.print(_value, _raw);
083
084 if (_attributes != null)
085 writer.end();
086 }
087
088 public String toString()
089 {
090 ToStringBuilder builder = new ToStringBuilder(this);
091
092 builder.append("component", _component);
093 builder.append("key", _key);
094 builder.append("raw", _raw);
095 builder.append("attributes", _attributes);
096
097 return builder.toString();
098 }
099
100 }