1 19 20 package org.netbeans.modules.schema2beansdev.gen; 21 22 import java.io.*; 23 import java.util.*; 24 25 public class XMLWriter extends IndentingWriter { 26 protected String xmlVersion = "1.0"; 27 protected String encoding = "UTF-8"; 28 protected boolean header = true; 29 protected Stack tags; 30 31 public int HEADER_SECTION = 0; 32 public int BODY_SECTION = 1; 33 static final protected int defaultSectionCount = 2; 34 35 public XMLWriter() { 36 super(defaultSectionCount); 37 privateInit(); 38 } 39 40 public XMLWriter(boolean header) { 41 super(defaultSectionCount); 42 this.header = header; 43 privateInit(); 44 } 45 46 public XMLWriter(String encoding) { 47 super(defaultSectionCount); 48 this.encoding = encoding; 49 privateInit(); 50 } 51 52 public XMLWriter(String encoding, String xmlVersion) { 53 super(defaultSectionCount); 54 this.encoding = encoding; 55 this.xmlVersion = xmlVersion; 56 privateInit(); 57 } 58 59 65 public int insertSectionAfter(int sectionNum) { 66 insertAdditionalBuffers(sectionNum, 1); 67 if (sectionNum < HEADER_SECTION) ++HEADER_SECTION; 68 if (sectionNum < BODY_SECTION) ++BODY_SECTION; 69 return sectionNum + 1; 70 } 71 72 public void reset() { 73 super.reset(); 74 privateInit(); 75 } 76 77 private void privateInit() { 78 tags = new Stack(); 79 try { 80 if (header) { 81 select(HEADER_SECTION); 82 write("<?xml version='"); 83 write(xmlVersion); 84 write("' encoding='"); 85 write(encoding); 86 write("' ?>\n"); 87 } 88 select(BODY_SECTION); 89 } catch (IOException e) { 90 throw new RuntimeException (e); 92 } 93 } 94 95 public void startTag(String tag) throws IOException { 96 startTag(null, tag, null, true); 97 } 98 99 public void startTag(String tag, boolean finish) throws IOException { 100 startTag(null, tag, null, finish); 101 } 102 103 public void startTag(String namespace, String tag) 104 throws IOException { 105 startTag(namespace, tag, null, true); 106 } 107 108 public void startTag(String namespace, String tag, boolean finish) 109 throws IOException { 110 startTag(namespace, tag, null, finish); 111 } 112 113 public void startTag(String namespace, String tag, String attributeString) 114 throws IOException { 115 startTag(namespace, tag, attributeString, true); 116 } 117 118 124 public void startTag(String namespace, String tag, String attributeString, 125 boolean finish) throws IOException { 126 String fullTag; 127 if (namespace != null) 128 fullTag = namespace+":"+tag; 129 else 130 fullTag = tag; 131 tags.push(fullTag); 132 write("<"); 133 write(fullTag); 134 if (attributeString != null) { 135 if (!attributeString.startsWith(" ")) 136 write(" "); 137 write(attributeString); 138 } 139 if (finish) 140 finishStartTag(); 141 else 142 setFirst(" "); 143 indentRight(); 144 } 145 146 150 public void finishStartTag() throws IOException { 151 write(">"); 152 } 153 154 158 public void finishStartTag(boolean children, boolean useCr) throws IOException { 159 if (!children) { 160 write("/"); 161 indentLeft(); 162 tags.pop(); 163 } 164 finishStartTag(); 165 if (useCr) 166 cr(); 167 } 168 169 public void endTag() throws IOException { 170 endTag(true); 171 } 172 173 public void endTag(boolean useCr) throws IOException { 174 indentLeft(); 175 String fullTag = (String ) tags.pop(); 176 write("</"); 177 write(fullTag); 178 write(">"); 179 if (useCr) 180 cr(); 181 } 182 } 183 | Popular Tags |