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.markup.ComponentTag; 022import org.apache.wicket.markup.MarkupElement; 023import org.apache.wicket.markup.MarkupResourceStream; 024import org.apache.wicket.markup.WicketParseException; 025import org.apache.wicket.markup.WicketTag; 026import org.apache.wicket.markup.parser.AbstractMarkupFilter; 027 028 029/** 030 * This is a markup inline filter. It identifies preview regions useful for HTML designers to design 031 * the page. But they must be removed prior to sending the markup to the client. Preview regions are 032 * enclosed by <wicket:remove> tags. 033 * 034 * @author Juergen Donnerstag 035 */ 036public final class WicketRemoveTagHandler extends AbstractMarkupFilter 037{ 038 /** */ 039 public static final String REMOVE = "remove"; 040 041 /** 042 * Construct. 043 */ 044 public WicketRemoveTagHandler() 045 { 046 this(null); 047 } 048 049 public WicketRemoveTagHandler(MarkupResourceStream resourceStream) 050 { 051 super(resourceStream); 052 } 053 054 @Override 055 protected final MarkupElement onComponentTag(ComponentTag tag) throws ParseException 056 { 057 // If it is not a remove tag then we are finished 058 if (!(tag instanceof WicketTag) || !((WicketTag)tag).isRemoveTag()) 059 { 060 return tag; 061 } 062 063 // remove tag must not be open-close tags 064 if (tag.isOpenClose()) 065 { 066 throw new WicketParseException("Wicket remove tag must not be an open-close tag:", tag); 067 } 068 069 // fetch markup elements 070 MarkupElement markupElement; 071 while ((markupElement = getNextFilter().nextElement()) != null) 072 { 073 // skip non-component tags 074 if((markupElement instanceof ComponentTag) == false) 075 { 076 continue; 077 } 078 079 // find the corresponding close tag for the given tag 080 ComponentTag closeTag = (ComponentTag)markupElement; 081 082 // No Wicket component tags are allowed within the preview region. 083 // Wicket components will a component name assigned. 084 if (closeTag.getId() == null) 085 { 086 continue; 087 } 088 089 // The first Wicket component following the preview region open 090 // tag, must be it's corresponding close tag. 091 if (closeTag.closes(tag)) 092 { 093 // The tag (from open to close) should be ignored by 094 // MarkupParser and not be added to the Markup. 095 tag.setIgnore(true); 096 return tag; 097 } 098 099 throw new WicketParseException( 100 "Markup remove regions must not contain Wicket component tags:", closeTag); 101 } 102 103 throw new WicketParseException( 104 "Did not find close tag for markup remove region. Open tag:", tag); 105 } 106}