View Javadoc

1   /*
2    * Copyright 2004-2006 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package com.isenshi.util;
17  
18  import java.util.Iterator;
19  import java.util.Stack;
20  
21  /***
22   * @author nishioka
23   */
24  public class XMLStringBuffer {
25  
26  	public static final String NEWLINE = "\n";
27  
28  	private static final String XML_VERSION = "1.0";
29  
30  	private StringBuffer buf_;
31  
32  	private Stack tagStack_;
33  
34  	private boolean isClosed_;
35  
36  	private boolean isNewline_ = true;
37  
38  	private boolean isTab_ = true;
39  
40  	public XMLStringBuffer() {
41  		buf_ = new StringBuffer();
42  		tagStack_ = new Stack();
43  		isClosed_ = true;
44  	}
45  
46  	/***
47  	 * @throws IllegalStateException
48  	 *             if called not in inital time of document
49  	 * @throws IllegalArgumentException
50  	 *             if called when standalone parameter is not "yes" or "no"
51  	 */
52  	public void appendXMLDeclaration(String encoding, String standalone) {
53  
54  		if (buf_.length() > 0)
55  			throw new IllegalStateException(
56  					"XML Declaration must appear at the start of document.");
57  
58  		// version declaration could not be ommitted
59  		buf_.append("<?xml version=\"" + XML_VERSION + "\"");
60  		if (encoding != null && !encoding.equals("")) {
61  			buf_.append(" encoding=\"" + encoding + "\"");
62  		}
63  
64  		if (standalone != null) {
65  			if (standalone.equalsIgnoreCase("yes")
66  					|| standalone.equalsIgnoreCase("no")) {
67  				buf_.append(" standalone=\"" + standalone + "\"");
68  			} else {
69  				throw new IllegalArgumentException(
70  						"Attribute \"standalone\" must be yes or no.");
71  			}
72  		}
73  		buf_.append("?>");
74  		appendNewLine();
75  	}
76  
77  	public void appendAttribute(String name, String value) {
78  		if (value != null) {
79  			buf_.append(" ");
80  			buf_.append(name);
81  			buf_.append("=\"");
82  			buf_.append(value);
83  			buf_.append("\"");
84  		}
85  	}
86  
87  	public void appendBody(String body) {
88  		if (body == null) {
89  			return;
90  		}
91  		assertBody();
92  		buf_.append(body);
93  		if (isNewline_)
94  			appendNewLine();
95  	}
96  
97  	public void appendStartTag(String tag) {
98  		assertBody();
99  
100 		buf_.append("<");
101 		buf_.append(tag);
102 		isClosed_ = false;
103 
104 		doTagPush(tag);
105 	}
106 
107 	public void endTag() {
108 		String tag = doTagPop();
109 
110 		if (isClosed_) {
111 			if (isTab_)
112 				doTab();
113 			buf_.append("</");
114 			buf_.append(tag);
115 			closeTag(true);
116 		} else {
117 			closeTag(false);
118 		}
119 	}
120 
121 	public void endAllTags() {
122 		Iterator itr = tagStack_.iterator();
123 		while (itr.hasNext()) {
124 			endTag();
125 		}
126 	}
127 
128 	private void assertBody() {
129 		if (!isClosed_) {
130 			closeTag(true);
131 		}
132 		if (isTab_)
133 			doTab();
134 	}
135 
136 	protected void doTab() {
137 		for (int i = 0; i < tagStack_.size(); i++) {
138 			buf_.append("\t");
139 		}
140 	}
141 
142 	private void doTagPush(String tag) {
143 		tagStack_.push(tag);
144 	}
145 
146 	private String doTagPop() {
147 		if(tagStack_.size()>0){
148 			return (String) tagStack_.pop();
149 		}else{
150 			return "nulldesu";
151 		}
152 	}
153 
154 	public void closeTag() {
155 		closeTag(true);
156 	}
157 
158 	private void closeTag(boolean onlyClose) {
159 		if (!onlyClose) {
160 			buf_.append("/");
161 		}
162 		isClosed_ = true;
163 		buf_.append(">");
164 		if (isNewline_)
165 			buf_.append(NEWLINE);
166 	}
167 
168 	public String toString() {
169 		return buf_.toString();
170 	}
171 
172 	public void setNewline(boolean isNewline) {
173 		this.isNewline_ = isNewline;
174 	}
175 
176 	public void setTab(boolean isTab) {
177 		this.isTab_ = isTab;
178 	}
179 
180 	public int nextIndex() {
181 		int idx = buf_.length();
182 		return (isClosed_) ? idx : idx + 1;
183 	}
184 
185 	public String cut(int start, int end) {
186 		String s = buf_.substring(start, end);
187 		buf_.delete(start, end);
188 		return s;
189 	}
190 
191 	public boolean isTagEmpty() {
192 		return tagStack_.empty();
193 	}
194 
195 	public void appendBodyLine(String body) {
196 		boolean tmpTab = isTab_;
197 		boolean tmpNewLine = isNewline_;
198 		
199 		setTab(false);
200 		setNewline(false);
201 		
202 		assertBody();
203 		appendNewLine();
204 		appendBody(body);
205 		appendNewLine();
206 		
207 		setTab(tmpTab);
208 		setNewline(tmpNewLine);
209 	}
210 	
211 	public void appendNewLine(){
212 		buf_.append(NEWLINE);
213 	}
214 }