Debian Codegraph @

src/dr/app/beauti/util/XMLWriter.java

raw symbols SCIP
1 /*
2 * XMLWriter.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.app.beauti.util;
27
28 import dr.util.Attribute;
29
30 import java.io.Writer;
31 import java.util.List;
32
33 /**
34 * @author Alexei Drummond
35 * @version $Id: XMLWriter.java,v 1.3 2005/06/27 21:18:40 rambaut Exp $
36 */
37 public class XMLWriter extends java.io.PrintWriter {
38
39 int level = 0;
40
41 public XMLWriter(Writer writer) {
42 super(writer);
43 }
44
45 public void increaseLevel() {
46 level += 1;
47 }
48
49 public void decreaseLevel() {
50 level -= 1;
51 }
52
53 public void writeComment(String... comments) {
54 writeComment(80, comments);
55 }
56
57 public void writeComment(int length, String... comments) {
58 writeBlankLine();
59
60 for (String comment : comments) {
61 StringBuffer buffer = new StringBuffer("<!-- ");
62 buffer.append(comment);
63 for (int i = buffer.length(); i < (length - 3); i++) {
64 buffer.append(' ');
65 }
66 buffer.append("-->");
67 writeText(buffer.toString());
68 }
69 }
70
71 public void writeOpenTag(String tagname) {
72 writeText("<" + tagname + ">");
73 increaseLevel();
74 }
75
76 public void writeOpenTag(String tagname, Attribute attribute) {
77 writeTag(tagname, new Attribute[]{attribute}, false);
78 }
79
80 public void writeOpenTag(String tagname, Attribute[] attributes) {
81 writeTag(tagname, attributes, false);
82 }
83
84 public void writeOpenTag(String tagname, List<Attribute> attributes) {
85 writeTag(tagname, attributes.toArray(new Attribute[attributes.size()]), false);
86 }
87
88 public void writeTag(String tagname, Attribute attribute, boolean close) {
89 writeTag(tagname, new Attribute[]{attribute}, close);
90 }
91
92 public void writeTag(String tagname, Attribute[] attributes, boolean close) {
93 StringBuffer buffer = new StringBuffer("<");
94 buffer.append(tagname);
95 for (Attribute attribute : attributes) {
96 if (attribute != null) {
97 buffer.append(' ');
98 buffer.append(attribute.getAttributeName());
99 buffer.append("=\"");
100 buffer.append(attribute.getAttributeValue());
101 buffer.append("\"");
102 }
103 }
104 if (close) {
105 buffer.append("/");
106 }
107 buffer.append(">");
108 writeText(buffer.toString());
109 if (!close) {
110 increaseLevel();
111 }
112 }
113
114 public void writeTag(String tagname, Attribute[] attributes, String content, boolean close) {
115 StringBuffer buffer = new StringBuffer("<");
116 buffer.append(tagname);
117 for (Attribute attribute : attributes) {
118 if (attribute != null) {
119 buffer.append(' ');
120 buffer.append(attribute.getAttributeName());
121 buffer.append("=\"");
122 buffer.append(attribute.getAttributeValue());
123 buffer.append("\"");
124 }
125 }
126 if (content != null) {
127 buffer.append(">");
128 buffer.append(content);
129 if (close) {
130 buffer.append("</");
131 buffer.append(tagname);
132 //buffer.append("/");
133 }
134 } else if (close) {
135 buffer.append("/");
136 }
137 buffer.append(">");
138 writeText(buffer.toString());
139 if (!close) {
140 increaseLevel();
141 }
142 }
143
144 public void writeCloseTag(String tagname) {
145 decreaseLevel();
146 writeText("</" + tagname + ">");
147 }
148
149 public void writeBlankLine() {
150 println();
151 flush();
152 }
153
154 public void writeText(String string) {
155 for (int i = 0; i < level; i++) {
156 write('\t');
157 }
158 println(string);
159
160 // wrapping lines at a fixed length can only do bad things...
161 // int subLength = 2000;
162 //
163 // for (int l = 0; l < string.length(); l += subLength) {
164 // int e = l + subLength;
165 // if (e > string.length()) e = string.length();
166 // for (int i = 0; i < level; i++) {
167 // write('\t');
168 // }
169 // println(string.substring(l, e));
170 // flush();
171 // }
172
173 }
174
175 public void writeIDref(String tagName, String paramName) {
176 writeTag(tagName, new Attribute[]{new Attribute.Default<String>(dr.xml.XMLParser.IDREF, paramName)}, true);
177 }
178 }
179