| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 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 |
|
| 35 |
|
| 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 |
|
| 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 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 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 |
|