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 java.util.Iterator;
020
021/**
022 * 
023 * @author Juergen Donnerstag
024 */
025public class MarkupIterator implements Iterator<MarkupElement>
026{
027        private final IMarkupFragment markup;
028
029        private int index = -1;
030
031        private boolean componentTagOnly;
032        private boolean wicketTagOnly;
033        private boolean openTagOnly;
034
035        /**
036         * Construct.
037         * 
038         * @param markup
039         */
040        public MarkupIterator(final IMarkupFragment markup)
041        {
042                if (markup == null)
043                {
044                        throw new NullPointerException("Parameter 'markup' must not be null");
045                }
046                this.markup = markup;
047        }
048
049        /**
050         * @see java.util.Iterator#hasNext()
051         */
052        @Override
053        public boolean hasNext()
054        {
055                for (index++; index < markup.size(); index++)
056                {
057                        MarkupElement elem = markup.get(index);
058                        if ((componentTagOnly && (elem instanceof ComponentTag)) ||
059                                (wicketTagOnly && (elem instanceof WicketTag)))
060                        {
061                                if (openTagOnly)
062                                {
063                                        ComponentTag tag = (ComponentTag)elem;
064                                        if (tag.isOpen())
065                                        {
066                                                return true;
067                                        }
068                                        else
069                                        {
070                                                continue;
071                                        }
072                                }
073                                return true;
074                        }
075                        return true;
076                }
077
078                return false;
079        }
080
081        /**
082         * @see java.util.Iterator#next()
083         */
084        @Override
085        public MarkupElement next()
086        {
087                return markup.get(index);
088        }
089
090        /**
091         * @return The next element assuming it is a ComponentTag or WicketTag
092         */
093        public ComponentTag nextTag()
094        {
095                return (ComponentTag)next();
096        }
097
098        /**
099         * @return The next element assuming it is a WicketTag
100         */
101        public WicketTag nextWicketTag()
102        {
103                return (WicketTag)next();
104        }
105
106        /**
107         * @see java.util.Iterator#remove()
108         */
109        @Override
110        public void remove()
111        {
112                throw new UnsupportedOperationException("You can not remove markup elements");
113        }
114
115        /**
116         * Ignore raw markup and iterate over component and wicket tags only.
117         * 
118         * @param componentTagOnly
119         */
120        public final void setComponentTagOnly(boolean componentTagOnly)
121        {
122                this.componentTagOnly = componentTagOnly;
123        }
124
125        /**
126         * Ignore raw markup and component tags, and iterate over WicketTags only
127         * 
128         * @param wicketTagOnly
129         */
130        public final void setWicketTagOnly(boolean wicketTagOnly)
131        {
132                this.wicketTagOnly = wicketTagOnly;
133        }
134
135        /**
136         * Ignore close tag. Iterate over open and open-close tags only
137         * 
138         * @param openTagOnly
139         */
140        public final void setOpenTagOnly(boolean openTagOnly)
141        {
142                this.openTagOnly = openTagOnly;
143        }
144}