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;
018
019import org.apache.wicket.Component;
020import org.apache.wicket.markup.parser.IXmlPullParser.HttpTagType;
021import org.apache.wicket.markup.parser.XmlTag;
022import org.apache.wicket.markup.parser.XmlTag.TagType;
023import org.apache.wicket.request.Response;
024
025
026/**
027 * 
028 * @author Juergen Donnerstag
029 */
030public class HtmlSpecialTag extends MarkupElement
031{
032        /** The underlying xml tag */
033        protected final XmlTag xmlTag;
034
035        /** Boolean flags. See above */
036        private int flags = 0;
037
038        private final HttpTagType httpTagType;
039
040        /**
041         * Construct.
042         * 
043         * @param tag
044         *            The underlying xml tag
045         * @param httpTagType
046         */
047        public HtmlSpecialTag(final XmlTag tag, final HttpTagType httpTagType)
048        {
049                xmlTag = tag.makeImmutable();
050                this.httpTagType = httpTagType;
051        }
052
053        /**
054         * THIS METHOD IS NOT PART OF THE WICKET PUBLIC API. DO NOT USE IT!
055         * 
056         * @param flag
057         *            The flag to set
058         * @param set
059         *            True to turn the flag on, false to turn it off
060         */
061        public final void setFlag(final int flag, final boolean set)
062        {
063                if (set)
064                {
065                        flags |= flag;
066                }
067                else
068                {
069                        flags &= ~flag;
070                }
071        }
072
073        /**
074         * THIS METHOD IS NOT PART OF THE WICKET PUBLIC API. DO NOT USE IT!
075         * 
076         * @param flag
077         *            The flag to test
078         * @return True if the flag is set
079         */
080        public final boolean getFlag(final int flag)
081        {
082                return (flags & flag) != 0;
083        }
084
085        /**
086         * Gets the length of the tag in characters.
087         * 
088         * @return The tag's length
089         */
090        public final int getLength()
091        {
092                return xmlTag.getLength();
093        }
094
095        /**
096         * @see org.apache.wicket.markup.parser.XmlTag#getPos()
097         * @return Tag location (index in input string)
098         */
099        public final int getPos()
100        {
101                return xmlTag.getPos();
102        }
103
104        /**
105         * @return the tag type (OPEN, CLOSE or OPEN_CLOSE).
106         */
107        public final TagType getType()
108        {
109                return xmlTag.getType();
110        }
111
112        /**
113         * @see org.apache.wicket.markup.parser.XmlTag#isClose()
114         * @return True if this tag is a close tag
115         */
116        public final boolean isClose()
117        {
118                return xmlTag.isClose();
119        }
120
121        /**
122         * @see org.apache.wicket.markup.parser.XmlTag#isOpen()
123         * @return True if this tag is an open tag
124         */
125        public final boolean isOpen()
126        {
127                return xmlTag.isOpen();
128        }
129
130        /**
131         * @see org.apache.wicket.markup.parser.XmlTag#isOpenClose()
132         * @return True if this tag is an open and a close tag
133         */
134        public final boolean isOpenClose()
135        {
136                return xmlTag.isOpenClose();
137        }
138
139        /**
140         * Copies all internal properties from this tag to <code>dest</code>. This is basically cloning
141         * without instance creation.
142         * 
143         * @param dest
144         *            tag whose properties will be set
145         */
146        void copyPropertiesTo(final HtmlSpecialTag dest)
147        {
148                dest.flags = flags;
149        }
150
151        @Override
152        public CharSequence toCharSequence()
153        {
154                return xmlTag.toCharSequence();
155        }
156
157        /**
158         * Converts this object to a string representation.
159         * 
160         * @return String version of this object
161         */
162        @Override
163        public final String toString()
164        {
165                return "" + httpTagType + ": '" + xmlTag.toString() + "'";
166        }
167
168        /**
169         * Write the tag to the response
170         * 
171         * @param response
172         *            The response to write to
173         * @param stripWicketAttributes
174         *            if true, wicket:id are removed from output
175         * @param namespace
176         *            Wicket's namespace to use
177         */
178        public final void writeOutput(final Response response, final boolean stripWicketAttributes,
179                final String namespace)
180        {
181                response.write(toString());
182        }
183
184        /**
185         * Converts this object to a string representation including useful information for debugging
186         * 
187         * @return String version of this object
188         */
189        @Override
190        public final String toUserDebugString()
191        {
192                return xmlTag.toUserDebugString();
193        }
194
195        /**
196         * @return Returns the underlying xml tag.
197         */
198        public final XmlTag getXmlTag()
199        {
200                return xmlTag;
201        }
202
203        @Override
204        public boolean equalTo(final MarkupElement element)
205        {
206                if (element instanceof HtmlSpecialTag)
207                {
208                        final HtmlSpecialTag that = (HtmlSpecialTag)element;
209                        return getXmlTag().equalTo(that.getXmlTag());
210                }
211                return false;
212        }
213
214        /**
215         * For subclasses to override. Gets called just before a Component gets rendered. It is
216         * guaranteed that the markupStream is set on the Component and determineVisibility is not yet
217         * called.
218         * 
219         * @param component
220         *            The component that is about to be rendered
221         * @param markupStream
222         *            The current amrkup stream
223         */
224        public void onBeforeRender(final Component component, final MarkupStream markupStream)
225        {
226        }
227
228        /**
229         * Gets httpTagType.
230         * 
231         * @return httpTagType
232         */
233        public final HttpTagType getHttpTagType()
234        {
235                return httpTagType;
236        }
237}