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: *
017: */
018:
019: package org.apache.jorphan.util;
020:
021: import org.apache.commons.collections.ArrayStack;
022:
023: // @see org.apache.jorphan.util.TestXMLBuffer for unit tests
024:
025: /**
026: * Provides XML string building methods.
027: * Not synchronised.
028: *
029: */
030: public class XMLBuffer {
031: private StringBuffer sb = new StringBuffer(); // the string so far
032:
033: private ArrayStack tags = new ArrayStack(); // opened tags
034:
035: public XMLBuffer() {
036:
037: }
038:
039: private void startTag(String t) {
040: sb.append("<");
041: sb.append(t);
042: sb.append(">");
043: }
044:
045: private void endTag(String t) {
046: sb.append("</");
047: sb.append(t);
048: sb.append(">");
049: }
050:
051: /**
052: * Open a tag; save on stack.
053: *
054: * @param tagname
055: * @return this
056: */
057: public XMLBuffer openTag(String tagname) {
058: tags.push(tagname);
059: startTag(tagname);
060: return this ;
061: }
062:
063: /**
064: * Close top tag from stack.
065: *
066: * @param tagname
067: *
068: * @return this
069: *
070: * @throws IllegalArgumentException if the tag names do not match
071: */
072: public XMLBuffer closeTag(String tagname) {
073: String tag = (String) tags.pop();
074: if (!tag.equals(tagname)) {
075: throw new IllegalArgumentException("Trying to close tag: "
076: + tagname + " ; should be " + tag);
077: }
078: endTag(tag);
079: return this ;
080: }
081:
082: /**
083: * Add a complete tag with content.
084: *
085: * @param tagname
086: * @param content
087: * @return this
088: */
089: public XMLBuffer tag(String tagname, String content) {
090: startTag(tagname);
091: sb.append(content);
092: endTag(tagname);
093: return this ;
094: }
095:
096: /**
097: * Add a complete tag with content.
098: *
099: * @param tagname
100: * @param content
101: * @return this
102: */
103: public XMLBuffer tag(String tagname, StringBuffer content) {
104: startTag(tagname);
105: sb.append(content);
106: endTag(tagname);
107: return this ;
108: }
109:
110: /**
111: * Convert the buffer to a string, closing any open tags
112: */
113: public String toString() {
114: while (!tags.isEmpty()) {
115: endTag((String) tags.pop());
116: }
117: return sb.toString();
118: }
119: }
|