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