001: /*
002: * $Id: WicketTagIdentifier.java 461655 2006-07-30 12:37:49Z jdonnerstag $
003: * $Revision: 461655 $ $Date: 2006-07-30 14:37:49 +0200 (Sun, 30 Jul 2006) $
004: *
005: * ==============================================================================
006: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
007: * use this file except in compliance with the License. You may obtain a copy of
008: * the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
014: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
015: * License for the specific language governing permissions and limitations under
016: * the License.
017: */
018: package wicket.markup.parser.filter;
019:
020: import java.text.ParseException;
021: import java.util.ArrayList;
022: import java.util.List;
023:
024: import wicket.markup.ComponentTag;
025: import wicket.markup.Markup;
026: import wicket.markup.MarkupElement;
027: import wicket.markup.WicketTag;
028: import wicket.markup.parser.AbstractMarkupFilter;
029: import wicket.markup.parser.XmlTag;
030:
031: /**
032: * This is a markup inline filter. It identifies xml tags which have a special
033: * meaning for Wicket. There are two type of tags which have a special meaning
034: * for Wicket.
035: * <p>
036: * <ul>
037: * <li>All tags with Wicket namespace, e.g. <wicket:remove></li>
038: * <li>All tags with an attribute like wicket:id="myLabel" </li>
039: * </ul>
040: *
041: * @author Juergen Donnerstag
042: */
043: public final class WicketTagIdentifier extends AbstractMarkupFilter {
044: /** List of well known wicket tag namses */
045: private static List wellKnownTagNames;
046:
047: /** The current markup needed to get the markups namespace */
048: private final Markup markup;
049:
050: /**
051: * Construct.
052: *
053: * @param markup
054: * The markup as known by now
055: */
056: public WicketTagIdentifier(final Markup markup) {
057: this .markup = markup;
058: }
059:
060: /**
061: * Get the next tag from the next MarkupFilter in the chain and search for
062: * Wicket specific tags.
063: * <p>
064: * Note: The xml parser - the next MarkupFilter in the chain - returns
065: * XmlTags which are a subclass of MarkupElement. The implementation of this
066: * filter will return either ComponentTags or ComponentWicketTags. Both are
067: * subclasses of MarkupElement as well and both maintain a reference to the
068: * XmlTag. But no XmlTag is returned.
069: *
070: * @see wicket.markup.parser.IMarkupFilter#nextTag()
071: * @return The next tag from markup to be processed. If null, no more tags
072: * are available
073: */
074: public MarkupElement nextTag() throws ParseException {
075: // Get the next tag from the markup.
076: // If null, no more tags are available
077: XmlTag xmlTag = (XmlTag) getParent().nextTag();
078: if (xmlTag == null) {
079: return xmlTag;
080: }
081:
082: final String namespace = this .markup.getWicketNamespace();
083:
084: // Identify tags with Wicket namespace
085: ComponentTag tag;
086: if (namespace.equalsIgnoreCase(xmlTag.getNamespace())) {
087: // It is <wicket:...>
088: tag = new WicketTag(xmlTag);
089:
090: // Make it a wicket component. Otherwise it would be RawMarkup
091: tag.setId("_" + tag.getName());
092:
093: if (wellKnownTagNames.contains(xmlTag.getName()) == false) {
094: throw new ParseException(
095: "Unkown tag name with Wicket namespace: '"
096: + xmlTag.getName()
097: + "'. Might be you haven't installed the appropriate resolver?",
098: tag.getPos());
099: }
100: } else {
101: // Everything else, except tags with Wicket namespace
102: tag = new ComponentTag(xmlTag);
103: }
104:
105: // If the form <tag wicket:id = "value"> is used
106: final String value = tag.getAttributes().getString(
107: namespace + ":id");
108: if (value != null) {
109: if (value.trim().length() == 0) {
110: throw new ParseException(
111: "The wicket:id attribute value must not be empty. May be unmatched quotes?!?",
112: tag.getPos());
113: }
114: // Make it a wicket component. Otherwise it would be RawMarkup
115: tag.setId(value);
116: }
117:
118: return tag;
119: }
120:
121: /**
122: * Register a new well known wicket tag name (e.g. panel)
123: *
124: * @param name
125: */
126: public final static void registerWellKnownTagName(final String name) {
127: if (wellKnownTagNames == null) {
128: wellKnownTagNames = new ArrayList();
129: }
130:
131: if (wellKnownTagNames.contains(name) == false) {
132: wellKnownTagNames.add(name);
133: }
134: }
135: }
|