001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.wicket.markup.parser.filter; 018 019import java.text.ParseException; 020 021import org.apache.wicket.Component; 022import org.apache.wicket.MarkupContainer; 023import org.apache.wicket.WicketRuntimeException; 024import org.apache.wicket.behavior.Behavior; 025import org.apache.wicket.markup.ComponentTag; 026import org.apache.wicket.markup.MarkupElement; 027import org.apache.wicket.markup.MarkupResourceStream; 028import org.apache.wicket.markup.MarkupStream; 029import org.apache.wicket.markup.html.TransparentWebMarkupContainer; 030import org.apache.wicket.markup.html.WebComponent; 031import org.apache.wicket.markup.parser.AbstractMarkupFilter; 032import org.apache.wicket.markup.resolver.IComponentResolver; 033import org.apache.wicket.util.string.Strings; 034 035 036/** 037 * This is a markup inline filter and a component resolver. It identifies wicket:message attributes 038 * and adds an attribute modifier to the component tag that can localize 039 * wicket:message="attr-name:i18n-key,attr-name-2:i18n-key-2,..." expressions, replacing values of 040 * attributes specified by attr-name with a localizer lookup with key i18n-key. If an attribute 041 * being localized has a set value that value will be used as the default value for the localization 042 * lookup. This handler also resolves and localizes raw markup with wicket:message attribute. 043 * 044 * @author Juergen Donnerstag 045 * @author Igor Vaynberg 046 */ 047public final class WicketMessageTagHandler extends AbstractMarkupFilter 048 implements 049 IComponentResolver 050{ 051 /** */ 052 private static final long serialVersionUID = 1L; 053 054 /** 055 * The id automatically assigned to tags with wicket:message attribute but without id 056 */ 057 public final static String WICKET_MESSAGE_CONTAINER_ID = "_message_attr_"; 058 059 /** 060 * Constructor for the IComponentResolver role. 061 */ 062 public WicketMessageTagHandler() 063 { 064 this(null); 065 } 066 067 /** 068 * Constructor for the IMarkupFilter role. 069 */ 070 public WicketMessageTagHandler(final MarkupResourceStream markupResourceStream) 071 { 072 super(markupResourceStream); 073 } 074 075 @Override 076 protected final MarkupElement onComponentTag(ComponentTag tag) throws ParseException 077 { 078 if (tag.isClose()) 079 { 080 return tag; 081 } 082 083 final String wicketMessageAttribute = tag.getAttributes().getString( 084 getWicketMessageAttrName()); 085 086 if (Strings.isEmpty(wicketMessageAttribute) == false) 087 { 088 // check if this tag is raw markup 089 if (tag.getId() == null) 090 { 091 // if this is a raw tag we need to set the id to something so 092 // that wicket will not merge this as raw markup and instead 093 // pass it on to a resolver 094 tag.setId(getWicketMessageIdPrefix(null) + getRequestUniqueId()); 095 tag.setAutoComponentTag(true); 096 tag.setModified(true); 097 } 098 tag.addBehavior(new AttributeLocalizer(getWicketMessageAttrName())); 099 } 100 101 return tag; 102 } 103 104 /** 105 * Attribute localizing behavior. See the javadoc of {@link WicketMessageTagHandler} for 106 * details. 107 * 108 * @author Igor Vaynberg (ivaynberg) 109 */ 110 public static class AttributeLocalizer extends Behavior 111 { 112 private static final long serialVersionUID = 1L; 113 114 private final String wicketMessageAttrName; 115 116 public AttributeLocalizer(String wicketMessageAttrName) 117 { 118 this.wicketMessageAttrName = wicketMessageAttrName; 119 } 120 121 @Override 122 public void onComponentTag(final Component component, final ComponentTag tag) 123 { 124 String expr = tag.getAttributes().getString(wicketMessageAttrName); 125 if (!Strings.isEmpty(expr)) 126 { 127 expr = expr.trim(); 128 129 String[] attrsAndKeys = Strings.split(expr, ','); 130 131 for (String attrAndKey : attrsAndKeys) 132 { 133 int colon = attrAndKey.lastIndexOf(":"); 134 // make sure the attribute-key pair is valid 135 if (attrAndKey.length() < 3 || colon < 1 || colon > attrAndKey.length() - 2) 136 { 137 throw new WicketRuntimeException( 138 "wicket:message attribute contains an invalid value [[" + expr + 139 "]], must be of form (attr:key)+"); 140 } 141 142 String attr = attrAndKey.substring(0, colon); 143 String key = attrAndKey.substring(colon + 1); 144 145 // we need to call the proper getString() method based on 146 // whether or not we have a default value 147 final String value; 148 if (tag.getAttributes().containsKey(attr)) 149 { 150 value = component.getString(key, null, tag.getAttributes().getString(attr)); 151 } 152 else 153 { 154 value = component.getString(key); 155 } 156 tag.put(attr, value); 157 } 158 } 159 } 160 } 161 162 @Override 163 public Component resolve(MarkupContainer container, MarkupStream markupStream, ComponentTag tag) 164 { 165 // localize any raw markup that has wicket:message attrs 166 if ((tag != null) && (tag.getId().startsWith(getWicketMessageIdPrefix(markupStream)))) 167 { 168 Component wc; 169 String id = tag.getId(); 170 171 if (tag.isOpenClose()) 172 { 173 wc = new WebComponent(id); 174 } 175 else 176 { 177 wc = new TransparentWebMarkupContainer(id); 178 } 179 180 return wc; 181 } 182 return null; 183 } 184 185 private String getWicketMessageAttrName() 186 { 187 String wicketNamespace = getWicketNamespace(); 188 return wicketNamespace + ':' + "message"; 189 } 190 191 private String getWicketMessageIdPrefix(final MarkupStream markupStream) 192 { 193 return getWicketNamespace(markupStream) + WICKET_MESSAGE_CONTAINER_ID; 194 } 195}