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.html.form; 018 019import java.text.ParseException; 020 021import org.apache.wicket.markup.ComponentTag; 022import org.apache.wicket.markup.MarkupElement; 023import org.apache.wicket.markup.MarkupResourceStream; 024import org.apache.wicket.markup.WicketTag; 025import org.apache.wicket.markup.parser.AbstractMarkupFilter; 026import org.apache.wicket.util.string.Strings; 027 028/** 029 * Markup filter that identifies tags with the {@code wicket:for} attribute. See 030 * {@link AutoLabelResolver} for details. 031 * 032 * @author igor 033 */ 034public class AutoLabelTagHandler extends AbstractMarkupFilter 035{ 036 public AutoLabelTagHandler(MarkupResourceStream resourceStream) 037 { 038 super(resourceStream); 039 } 040 041 @Override 042 protected MarkupElement onComponentTag(final ComponentTag tag) throws ParseException 043 { 044 if (tag == null || tag.isClose() || tag instanceof WicketTag) 045 { 046 return tag; 047 } 048 049 String related = tag.getAttribute(getWicketNamespace() + AutoLabelResolver.WICKET_FOR); 050 if (related == null) 051 { 052 return tag; 053 } 054 055 related = related.trim(); 056 if (Strings.isEmpty(related)) 057 { 058 throw new ParseException("Tag contains an empty wicket:for attribute", tag.getPos()); 059 } 060 061 if (!"label".equalsIgnoreCase(tag.getName())) 062 { 063 throw new ParseException("Attribute wicket:for can only be attached to <label> tag", 064 tag.getPos()); 065 } 066 067 if (tag.getId() != null) 068 { 069 throw new ParseException( 070 "Attribute wicket:for cannot be used in conjunction with wicket:id", tag.getPos()); 071 } 072 073 tag.setId(AutoLabelResolver.LABEL_ATTR + getRequestUniqueId()); 074 tag.setModified(true); 075 tag.setAutoComponentTag(true); 076 return tag; 077 } 078}