1 28 29 package org.jibx.runtime.impl; 30 31 import java.io.IOException ; 32 import java.io.Writer ; 33 34 import org.jibx.runtime.ICharacterEscaper; 35 36 44 45 public class GenericXMLWriter extends XMLWriterBase 46 { 47 48 private Writer m_writer; 49 50 51 private ICharacterEscaper m_escaper; 52 53 54 private boolean m_indent; 55 56 57 private int m_indentBase; 58 59 60 private int m_indentPerLevel; 61 62 63 private char[] m_indentSequence; 64 65 73 74 public GenericXMLWriter(String [] uris) { 75 super(uris); 76 } 77 78 86 87 public void setOutput(Writer outw, ICharacterEscaper escaper) { 88 try { 89 close(); 90 } catch (IOException e) { } 91 m_writer = outw; 92 m_escaper = escaper; 93 reset(); 94 } 95 96 107 108 public void setIndentSpaces(int count, String newline, char indent) { 109 if (count >= 0) { 110 if (newline == null) { 111 newline = "\n"; 112 } 113 m_indent = true; 114 m_indentBase = newline.length(); 115 m_indentPerLevel = count; 116 int length = newline.length() + count * 10; 117 m_indentSequence = new char[length]; 118 for (int i = 0; i < length; i++) { 119 if (i < newline.length()) { 120 m_indentSequence[i] = newline.charAt(i); 121 } else { 122 m_indentSequence[i] = indent; 123 } 124 } 125 } else { 126 m_indent = false; 127 } 128 } 129 130 137 138 protected void writeMarkup(String text) throws IOException { 139 m_writer.write(text); 140 } 141 142 149 150 protected void writeMarkup(char chr) throws IOException { 151 m_writer.write(chr); 152 } 153 154 160 161 protected void defineNamespace(int index, String prefix) {} 162 163 168 169 protected void undefineNamespace(int index) {} 170 171 178 179 protected void writePrefix(int index) throws IOException { 180 try { 181 String text = m_prefixes[index]; 182 if (text.length() > 0) { 183 m_writer.write(text); 184 m_writer.write(':'); 185 } 186 } catch (NullPointerException ex) { 187 throw new IOException ("Namespace URI has not been declared."); 188 } 189 } 190 191 198 199 protected void writeAttributeText(String text) throws IOException { 200 m_escaper.writeAttribute(text, m_writer); 201 } 202 203 210 211 public void writeTextContent(String text) throws IOException { 212 m_escaper.writeContent(text, m_writer); 213 m_textSeen = m_contentSeen = true; 214 } 215 216 223 224 public void writeCData(String text) throws IOException { 225 m_escaper.writeCData(text, m_writer); 226 m_textSeen = m_contentSeen = true; 227 } 228 229 239 240 public void indent() throws IOException { 241 if (m_indent) { 242 int length = m_indentBase + m_nestingDepth * m_indentPerLevel; 243 if (length > m_indentSequence.length) { 244 int use = Math.max(length, 245 m_indentSequence.length*2 - m_indentBase); 246 char[] grow = new char[use]; 247 System.arraycopy(m_indentSequence, 0, grow, 0, 248 m_indentSequence.length); 249 for (int i = m_indentSequence.length; i < use; i++) { 250 grow[i] = grow[m_indentBase]; 251 } 252 m_indentSequence = grow; 253 } 254 m_writer.write(m_indentSequence, 0, length); 255 } 256 } 257 258 264 265 public void close() throws IOException { 266 if (m_writer != null) { 267 indent(); 268 m_writer.flush(); 269 m_writer.close(); 270 m_writer = null; 271 } 272 } 273 } | Popular Tags |