home *** CD-ROM | disk | FTP | other *** search
/ Online Today 2000 January / Onto0100.iso / pc / JAVA / MSJAVX86.EXE / xmldso.cab / XML4IE3.cab / com / ms / xml / util / XMLOutputStream.class (.txt) < prev   
Encoding:
Java Class File  |  1997-10-10  |  3.9 KB  |  220 lines

  1. package com.ms.xml.util;
  2.  
  3. import com.ms.xml.parser.DTD;
  4. import java.io.IOException;
  5. import java.io.OutputStream;
  6. import java.io.OutputStreamWriter;
  7.  
  8. public class XMLOutputStream extends OutputStream {
  9.    static final int OUTPUTSW = 1;
  10.    static final int UCS2 = 2;
  11.    static final int UCS2_BOM = 3;
  12.    static final int ASCII = 4;
  13.    public static int PRETTY;
  14.    public static int COMPACT = 1;
  15.    private OutputStream out;
  16.    private OutputStreamWriter outsw;
  17.    String newline;
  18.    boolean littleendian;
  19.    String encoding;
  20.    private int writeState;
  21.    int outputStyle;
  22.    boolean jdk11;
  23.    private int indent;
  24.    public DTD dtd;
  25.    public NameSpaceContext nameSpaceContext = new NameSpaceContext();
  26.    public boolean savingDTD;
  27.  
  28.    public void close() throws IOException {
  29.       if (this.outsw != null) {
  30.          this.outsw.close();
  31.       } else {
  32.          this.out.close();
  33.       }
  34.    }
  35.  
  36.    public void setEncoding(String var1, boolean var2, boolean var3) throws IOException {
  37.       this.outsw = null;
  38.       String var4 = var1;
  39.       if (var1.equalsIgnoreCase("UTF-8")) {
  40.          var4 = "UTF8";
  41.       } else if (var1.equalsIgnoreCase("Shift_JIS")) {
  42.          var4 = "SJIS";
  43.       } else if (var1.equalsIgnoreCase("ISO-8859-1")) {
  44.          var4 = "8859_1";
  45.       } else {
  46.          if (var1.equalsIgnoreCase("ISO-10646-UCS-4") || var1.equalsIgnoreCase("UCS-4")) {
  47.             throw new IOException("UCS-4 not yet supported");
  48.          }
  49.  
  50.          if (var1.equalsIgnoreCase("UCS-2")) {
  51.             if (var3) {
  52.                this.writeState = 3;
  53.             } else {
  54.                this.writeState = 2;
  55.             }
  56.  
  57.             this.encoding = "UCS-2";
  58.             if (var2) {
  59.                this.littleendian = true;
  60.                this.out = new ByteSwapOutputStream(this.out);
  61.             }
  62.  
  63.             return;
  64.          }
  65.  
  66.          this.writeState = 4;
  67.          this.encoding = "ASCII";
  68.       }
  69.  
  70.       if (var1.equalsIgnoreCase("ASCII")) {
  71.          this.outsw = null;
  72.       } else {
  73.          try {
  74.             if (!this.jdk11) {
  75.                throw new IOException("Writers not supported in JDK 1.0");
  76.             } else {
  77.                this.outsw = new OutputStreamWriter(this.out, var4);
  78.                this.writeState = 1;
  79.                this.encoding = var1;
  80.             }
  81.          } catch (IOException var5) {
  82.             throw new IOException(var4 + " is not supported by your Java Virtual Machine." + "  Try installing the latest VM from http://www.microsoft.com/java/download.htm");
  83.          }
  84.       }
  85.    }
  86.  
  87.    public void addIndent(int var1) {
  88.       this.indent += var1;
  89.    }
  90.  
  91.    public void writeIndent() throws IOException {
  92.       if (this.outputStyle == PRETTY) {
  93.          for(int var1 = 0; var1 < this.indent; ++var1) {
  94.             this.write(9);
  95.          }
  96.       }
  97.  
  98.    }
  99.  
  100.    public XMLOutputStream(OutputStream var1) {
  101.       String var2 = System.getProperty("java.version");
  102.       this.jdk11 = var2.equals("1.1");
  103.       this.outputStyle = PRETTY;
  104.       this.littleendian = false;
  105.       this.savingDTD = false;
  106.       this.out = var1;
  107.       this.newline = System.getProperty("line.separator");
  108.       this.indent = 0;
  109.  
  110.       try {
  111.          if (!this.jdk11) {
  112.             throw new IOException("Writers not supported in JDK 1.0");
  113.          } else {
  114.             this.outsw = new OutputStreamWriter(var1, "UTF8");
  115.             this.writeState = 1;
  116.             this.encoding = "UTF-8";
  117.          }
  118.       } catch (IOException var3) {
  119.          this.outsw = null;
  120.          this.writeState = 4;
  121.          this.encoding = "ASCII";
  122.       }
  123.    }
  124.  
  125.    public void writeQualifiedName(Name var1, Name var2) throws IOException {
  126.       if (!(var1 instanceof QualifiedName)) {
  127.          if (var2 == null) {
  128.             this.writeChars(var1.toString());
  129.          } else {
  130.             this.writeChars("::" + var1.toString());
  131.          }
  132.       } else {
  133.          QualifiedName var3 = (QualifiedName)var1;
  134.          Name var4 = var3.getNameSpace();
  135.          Name var5 = var4;
  136.          if (var4 != var2 && (var2 != null || var4 != this.dtd.getDocType())) {
  137.             if (!DTD.isReservedNameSpace(var4)) {
  138.                var5 = this.nameSpaceContext.findNameSpace(var4);
  139.                if (var5 == null) {
  140.                   var5 = this.dtd.findShortNameSpace(var4);
  141.                }
  142.             }
  143.  
  144.             this.writeChars(var5.toString() + "::" + var3.getName().toString());
  145.          } else {
  146.             this.writeChars(var3.getName().toString());
  147.          }
  148.       }
  149.    }
  150.  
  151.    public void setOutputStyle(int var1) {
  152.       this.outputStyle = var1;
  153.    }
  154.  
  155.    public int getOutputStyle() {
  156.       return this.outputStyle;
  157.    }
  158.  
  159.    public void write(int var1) throws IOException {
  160.       switch (this.writeState) {
  161.          case 1:
  162.             this.outsw.write(var1);
  163.             return;
  164.          case 2:
  165.             int var4 = var1 >> 8;
  166.             int var5 = var1 & 255;
  167.             this.out.write(var4);
  168.             this.out.write(var5);
  169.             return;
  170.          case 3:
  171.             this.writeState = 2;
  172.             this.out.write(254);
  173.             this.out.write(255);
  174.             int var2 = var1 >> 8;
  175.             int var3 = var1 & 255;
  176.             this.out.write(var2);
  177.             this.out.write(var3);
  178.             return;
  179.          case 4:
  180.          default:
  181.             this.out.write(var1);
  182.       }
  183.    }
  184.  
  185.    public void writeChars(String var1) throws IOException {
  186.       int var2 = var1.length();
  187.  
  188.       for(int var3 = 0; var3 < var2; ++var3) {
  189.          this.write(var1.charAt(var3));
  190.       }
  191.  
  192.    }
  193.  
  194.    public void flush() throws IOException {
  195.       if (this.outsw != null) {
  196.          this.outsw.flush();
  197.       } else {
  198.          this.out.flush();
  199.       }
  200.    }
  201.  
  202.    public void writeNewLine() throws IOException {
  203.       if (this.outputStyle == PRETTY) {
  204.          this.writeChars(this.newline);
  205.       }
  206.  
  207.    }
  208.  
  209.    public void writeQuotedString(String var1) throws IOException {
  210.       byte var2 = 34;
  211.       if (var1.indexOf(34) >= 0) {
  212.          var2 = 39;
  213.       }
  214.  
  215.       this.write(var2);
  216.       this.writeChars(var1);
  217.       this.write(var2);
  218.    }
  219. }
  220.