001: /*
002: * $Id: MarkupFragment.java 460171 2006-04-08 20:40:18Z jdonnerstag $ $Revision: 460171 $
003: * $Date: 2006-04-08 22:40:18 +0200 (Sat, 08 Apr 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;
019:
020: import java.util.ArrayList;
021: import java.util.Collections;
022: import java.util.Iterator;
023: import java.util.List;
024:
025: import org.apache.commons.logging.Log;
026: import org.apache.commons.logging.LogFactory;
027:
028: import wicket.util.string.AppendingStringBuffer;
029:
030: /**
031: * A list of markup elements associated with a Markup. Might be all elements of
032: * a markup resource, might be just the elements associated with a specific tag.
033: *
034: * @see wicket.markup.Markup
035: * @see wicket.markup.MarkupElement
036: * @see wicket.markup.ComponentTag
037: * @see wicket.markup.RawMarkup
038: *
039: * @author Juergen Donnerstag
040: */
041: public class MarkupFragment {
042: private static final Log log = LogFactory
043: .getLog(MarkupFragment.class);
044:
045: /** Placeholder that indicates no markup */
046: public static final MarkupFragment NO_MARKUP_FRAGMENT = new MarkupFragment(
047: Markup.NO_MARKUP);
048:
049: /** The list of markup elements */
050: private/* final */List markupElements;
051:
052: /** The associate markup */
053: private final Markup markup;
054:
055: /**
056: * Constructor
057: *
058: * @param markup
059: * The associated Markup
060: */
061: MarkupFragment(final Markup markup) {
062: this .markup = markup;
063: this .markupElements = new ArrayList();
064: }
065:
066: /**
067: * @return String representation of markup list
068: */
069: public final String toString() {
070: final AppendingStringBuffer buf = new AppendingStringBuffer(400);
071: buf.append(this .markup.toString());
072: buf.append("\n");
073:
074: final Iterator iter = this .markupElements.iterator();
075: while (iter.hasNext()) {
076: buf.append(iter.next());
077: buf.append(",");
078: }
079:
080: return buf.toString();
081: }
082:
083: /**
084: * For Wicket it would be sufficient for this method to be package
085: * protected. However to allow wicket-bench easy access to the information
086: * ...
087: *
088: * @param index
089: * Index into markup list
090: * @return Markup element
091: */
092: public final MarkupElement get(final int index) {
093: return (MarkupElement) markupElements.get(index);
094: }
095:
096: /**
097: * Gets the associate markup
098: *
099: * @return The associated markup
100: */
101: public final Markup getMarkup() {
102: return this .markup;
103: }
104:
105: /**
106: * For Wicket it would be sufficient for this method to be package
107: * protected. However to allow wicket-bench easy access to the information
108: * ...
109: *
110: * @return Number of markup elements
111: */
112: public int size() {
113: return markupElements.size();
114: }
115:
116: /**
117: * Add a MarkupElement
118: *
119: * @param markupElement
120: */
121: final void addMarkupElement(final MarkupElement markupElement) {
122: this .markupElements.add(markupElement);
123: }
124:
125: /**
126: * Add a MarkupElement
127: *
128: * @param pos
129: * @param markupElement
130: */
131: final void addMarkupElement(final int pos,
132: final MarkupElement markupElement) {
133: this .markupElements.add(pos, markupElement);
134: }
135:
136: /**
137: * Make all tags immutable and the list of elements unmodifable.
138: */
139: final void makeImmutable() {
140: for (int i = 0; i < this .markupElements.size(); i++) {
141: MarkupElement elem = (MarkupElement) this .markupElements
142: .get(i);
143: if (elem instanceof ComponentTag) {
144: // Make the tag immutable
145: ((ComponentTag) elem).makeImmutable();
146: }
147: }
148:
149: this .markupElements = Collections
150: .unmodifiableList(this .markupElements);
151: }
152:
153: /**
154: * Reset the markup to its defaults, except for the wicket namespace which
155: * remains unchanged.
156: */
157: final void reset() {
158: this .markupElements = new ArrayList();
159: }
160:
161: /**
162: * Create an iterator for the markup elements
163: *
164: * @return Iterator
165: */
166: public final Iterator iterator() {
167: return iterator(0, null);
168: }
169:
170: /**
171: * Create an iterator for the tags being an istance of 'matchClass'
172: *
173: * @param startIndex
174: * The index to start with
175: * @param matchClass
176: * Iterate over elements matching the class
177: * @return Iterator
178: */
179: public final Iterator iterator(final int startIndex,
180: final Class matchClass) {
181: return new Iterator() {
182: int index = startIndex - 1;
183:
184: public boolean hasNext() {
185: while (++index < size()) {
186: MarkupElement element = get(index);
187: if ((matchClass == null)
188: || matchClass.isInstance(element)) {
189: return true;
190: }
191: }
192: return false;
193: }
194:
195: public Object next() {
196: return get(index);
197: }
198:
199: public void remove() {
200: markupElements.remove(index);
201: index -= 1;
202: }
203: };
204: }
205: }
|