1 19 package org.netbeans.lib.jmi.xmi; 20 21 import java.io.*; 22 import java.nio.charset.*; 23 import java.util.*; 24 import java.net.URL ; 25 import org.xml.sax.*; 26 import org.xml.sax.helpers.DefaultHandler ; 27 28 import org.netbeans.lib.jmi.util.DebugException; 29 30 public class DefaultWriter extends DefaultHandler { 31 32 private static final char QUOTE = '\''; 33 private static final String EOL = System.getProperty("line.separator"); 34 private static final String INDENT = " "; 35 private static final int INDENT_LENGTH = INDENT.length (); 36 40 private static final int MAX = 70; 41 42 private static final String XMI_VERSION = "1.2"; 43 private static final String EXPORTER_NAME = "Netbeans XMI Writer"; 44 private static final String EXPORTER_VERSION = "1.0"; 45 46 48 private OutputStreamWriter writer; 50 private Locator locator; 52 private String encoding = null; 54 55 private boolean hasContent = true; private boolean hasCharacters = false; 59 private String indentSpaces = ""; 61 private int charsCount = 0; 63 64 66 public DefaultWriter() { 67 } 68 69 public DefaultWriter(OutputStreamWriter writer, String encoding) { 70 this.writer = writer; 71 this.encoding = encoding; 72 } 73 74 public void init () throws SAXException { 75 hasContent = true; 76 hasCharacters = true; 77 indentSpaces = ""; 78 charsCount = 0; 79 80 if (writer == null) { 81 try { 82 OutputStream ostr = new URL (locator.getSystemId()).openConnection().getOutputStream(); 83 writer = new OutputStreamWriter (ostr); 84 } catch (IOException e) { 85 throw new SAXException (e); 86 } 87 } 88 String enc = (encoding != null) ? encoding : canonicalNameFor(writer.getEncoding()); 89 write ("<?xml version = '1.0' encoding = '" + enc + "' ?>"); 90 writeln (); 91 } 92 93 98 private static String canonicalNameFor(String encoding) { 99 try { 100 encoding = Charset.forName(encoding).name(); 101 } catch (IllegalCharsetNameException e) { 102 } catch (UnsupportedCharsetException e) { 103 } 104 return encoding; 105 } 106 107 109 public Writer getWriter () { 110 return writer; 111 } 112 113 118 private void write (String text) throws SAXException { 119 try { 120 writer.write (text); 121 } catch (IOException e) { 122 throw new SAXException (e); 123 } 124 } 125 126 129 private void writeln () throws SAXException { 130 try { 131 writer.write (EOL); 132 } catch (IOException e) { 133 throw new SAXException (e); 134 } 135 charsCount = 0; 136 } 137 138 145 private String replaceSpecialChars (String s, boolean replaceWhitechars) { 146 int length = s.length (); 147 char [] chars = new char [6 * length]; 148 int n = 0; 149 for (int x = 0; x < length; x++) { 150 char c = s.charAt (x); 151 switch (c) { 152 case '&': 153 chars [n] = '&'; n++; chars [n] = 'a'; n++; 154 chars [n] = 'm'; n++; chars [n] = 'p'; n++; 155 chars [n] = ';'; n++; 156 break; 157 case '\'': 158 chars [n] = '&'; n++; chars [n] = 'a'; n++; 159 chars [n] = 'p'; n++; chars [n] = 'o'; n++; 160 chars [n] = 's'; n++; chars [n] = ';'; n++; 161 break; 162 case '\"': 163 chars [n] = '&'; n++; chars [n] = 'q'; n++; 164 chars [n] = 'u'; n++; chars [n] = 'o'; n++; 165 chars [n] = 't'; n++; chars [n] = ';'; n++; 166 break; 167 case '<': 168 chars [n] = '&'; n++; chars [n] = 'l'; n++; 169 chars [n] = 't'; n++; chars [n] = ';'; n++; 170 break; 171 case '>': 172 chars [n] = '&'; n++; chars [n] = 'g'; n++; 173 chars [n] = 't'; n++; chars [n] = ';'; n++; 174 break; 175 default: 176 if (replaceWhitechars) { 177 switch (c) { 178 case '\n': 179 chars [n] = '&'; n++; chars [n] = '#'; n++; 180 chars [n] = '1'; n++; chars [n] = '0'; n++; 181 chars [n] = ';'; n++; 182 break; 183 case '\r': 184 chars [n] = '&'; n++; chars [n] = '#'; n++; 185 chars [n] = '1'; n++; chars [n] = '3'; n++; 186 chars [n] = ';'; n++; 187 break; 188 case '\t': 189 chars [n] = '&'; n++; chars [n] = '#'; n++; 190 chars [n] = '9'; n++; chars [n] = ';'; n++; 191 break; 192 default: 193 chars [n] = c; n++; 194 } 195 } else { 196 chars [n] = c; n++; 197 } 198 } } return new String (chars, 0, n); 201 } 202 203 208 private void startElement (String name, Attributes attrs) throws SAXException { 209 if (!hasContent && !hasCharacters) { 210 write (">"); 211 writeln (); 212 } 213 hasContent = false; 214 hasCharacters = false; 215 write (indentSpaces); 216 write ("<" + name); 217 charsCount += name.length () + 1; 218 indentSpaces = indentSpaces + INDENT; 219 for (int x = 0; x < attrs.getLength(); x++) { 220 addAttribute (attrs.getQName(x), attrs.getValue(x)); 221 } 222 } 223 224 229 private void endElement (String name) throws SAXException { 230 indentSpaces = indentSpaces.substring (0, indentSpaces.length () - INDENT_LENGTH); 231 if (hasContent) { 232 write (indentSpaces); 233 write ("</" + name + ">"); 234 } else if (hasCharacters) { 235 write ("</" + name + ">"); 236 } else 237 write ("/>"); 238 writeln (); 239 hasContent = true; 240 } 241 242 248 private void addAttribute (String name, String value) throws SAXException { 249 value = replaceSpecialChars (value, true); 250 if (charsCount > MAX) { 252 writeln (); 253 write (indentSpaces); 254 } else { 255 write (" "); 256 charsCount++; 257 } 258 write (name + " = " + QUOTE + value + QUOTE); 259 charsCount += name.length () + value.length () + 5; 260 } 261 262 269 private void characters (String text) throws SAXException { 270 text = replaceSpecialChars (text, false); 271 if (!hasContent) 272 write (">"); 273 write (text); 274 hasCharacters = true; 275 } 276 277 279 public void startDocument() throws org.xml.sax.SAXException { 280 init (); 281 } 282 283 public void endDocument() throws org.xml.sax.SAXException { 284 try { 285 writer.flush(); 286 writer.close(); 287 } catch (IOException e) { 288 throw new SAXException (e); 289 } 290 } 291 292 public void startElement(String uri, String sName, String qName, Attributes attributes) throws org.xml.sax.SAXException { 293 startElement (qName, attributes); 294 } 295 296 public void endElement(String uri, String sName, String qName) throws org.xml.sax.SAXException { 297 endElement (qName); 298 } 299 300 public void characters(char[] buf, int offset, int len) throws org.xml.sax.SAXException { 301 characters (new String (buf, offset, len)); 302 } 303 304 public void setDocumentLocator(Locator locator) { 305 this.locator = locator; 306 } 307 308 324 325 } 326 | Popular Tags |