home *** CD-ROM | disk | FTP | other *** search
/ Online Today 2000 January / Onto0100.iso / pc / JAVA / MSJAVX86.EXE / xmldso.cab / com / ms / xml / util / XMLOutputStream.class (.txt) < prev    next >
Encoding:
Java Class File  |  1998-11-05  |  4.1 KB  |  249 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 DEFAULT = 0;
  14.    public static int PRETTY = 1;
  15.    public static int COMPACT = 2;
  16.    private OutputStream out;
  17.    private OutputStreamWriter outsw;
  18.    String newline;
  19.    boolean littleendian;
  20.    String encoding;
  21.    private int writeState;
  22.    int outputStyle;
  23.    boolean jdk11;
  24.    public boolean mixed;
  25.    private int indent;
  26.    public DTD dtd;
  27.    public NameSpaceContext nameSpaceContext = new NameSpaceContext();
  28.    public boolean savingDTD;
  29.  
  30.    public void close() throws IOException {
  31.       if (this.outsw != null) {
  32.          this.outsw.close();
  33.       } else {
  34.          this.out.close();
  35.       }
  36.    }
  37.  
  38.    public void setEncoding(String var1, boolean var2, boolean var3) throws IOException {
  39.       this.outsw = null;
  40.       String var4 = var1;
  41.       if (var1.equalsIgnoreCase("UTF-8")) {
  42.          var4 = "UTF8";
  43.       } else if (var1.equalsIgnoreCase("Shift_JIS")) {
  44.          var4 = "SJIS";
  45.       } else if (var1.equalsIgnoreCase("ISO-8859-1")) {
  46.          var4 = "8859_1";
  47.       } else {
  48.          if (var1.equalsIgnoreCase("ISO-10646-UCS-4") || var1.equalsIgnoreCase("UCS-4")) {
  49.             throw new IOException("UCS-4 not yet supported");
  50.          }
  51.  
  52.          if (var1.equalsIgnoreCase("UCS-2")) {
  53.             if (var3) {
  54.                this.writeState = 3;
  55.             } else {
  56.                this.writeState = 2;
  57.             }
  58.  
  59.             this.encoding = "UCS-2";
  60.             if (var2) {
  61.                this.littleendian = true;
  62.                this.out = new ByteSwapOutputStream(this.out);
  63.             }
  64.  
  65.             return;
  66.          }
  67.  
  68.          this.writeState = 4;
  69.          this.encoding = "ASCII";
  70.       }
  71.  
  72.       if (var1.equalsIgnoreCase("ASCII")) {
  73.          this.outsw = null;
  74.       } else {
  75.          try {
  76.             if (!this.jdk11) {
  77.                throw new IOException("Writers not supported in JDK 1.0");
  78.             } else {
  79.                this.outsw = new OutputStreamWriter(this.out, var4);
  80.                this.writeState = 1;
  81.                this.encoding = var1;
  82.             }
  83.          } catch (IOException var5) {
  84.             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");
  85.          }
  86.       }
  87.    }
  88.  
  89.    public XMLOutputStream(OutputStream var1) {
  90.       String var2 = System.getProperty("java.version");
  91.       this.jdk11 = var2.equals("1.1");
  92.       this.outputStyle = DEFAULT;
  93.       this.littleendian = false;
  94.       this.savingDTD = false;
  95.       this.mixed = false;
  96.       this.out = var1;
  97.       this.newline = System.getProperty("line.separator");
  98.       this.indent = 0;
  99.  
  100.       try {
  101.          if (!this.jdk11) {
  102.             throw new IOException("Writers not supported in JDK 1.0");
  103.          } else {
  104.             this.outsw = new OutputStreamWriter(var1, "UTF8");
  105.             this.writeState = 1;
  106.             this.encoding = "UTF-8";
  107.          }
  108.       } catch (IOException var3) {
  109.          this.outsw = null;
  110.          this.writeState = 4;
  111.          this.encoding = "ASCII";
  112.       }
  113.    }
  114.  
  115.    public void addIndent(int var1) {
  116.       this.indent += var1;
  117.    }
  118.  
  119.    public void writeIndent() throws IOException {
  120.       if (this.outputStyle == PRETTY && !this.mixed) {
  121.          for(int var1 = 0; var1 < this.indent; ++var1) {
  122.             this.write(9);
  123.          }
  124.       }
  125.  
  126.    }
  127.  
  128.    public void writeQualifiedName(Name var1, Atom var2) throws IOException {
  129.       Atom var3 = var1.getNameSpace();
  130.       Atom var4 = var3;
  131.       if (var3 == var2) {
  132.          this.writeChars(var1.getName());
  133.       } else if (var3 != null) {
  134.          if (this.dtd != null && !DTD.isReservedNameSpace(var3)) {
  135.             var4 = this.nameSpaceContext.findNameSpace(var3);
  136.             if (var4 == null) {
  137.                var4 = this.dtd.findShortNameSpace(var3);
  138.             }
  139.  
  140.             if (var4 == null) {
  141.                var4 = var3;
  142.             }
  143.          }
  144.  
  145.          this.writeChars(var4.toString() + ":" + var1.getName());
  146.       } else if (var2 == null) {
  147.          this.writeChars(var1.getName());
  148.       } else {
  149.          this.writeChars(":" + var1.getName());
  150.       }
  151.    }
  152.  
  153.    public void setOutputStyle(int var1) {
  154.       this.outputStyle = var1;
  155.    }
  156.  
  157.    public int getOutputStyle() {
  158.       return this.outputStyle;
  159.    }
  160.  
  161.    public void write(int var1) throws IOException {
  162.       switch (this.writeState) {
  163.          case 1:
  164.             this.outsw.write(var1);
  165.             return;
  166.          case 2:
  167.             int var4 = var1 >> 8;
  168.             int var5 = var1 & 255;
  169.             this.out.write(var4);
  170.             this.out.write(var5);
  171.             return;
  172.          case 3:
  173.             this.writeState = 2;
  174.             this.out.write(254);
  175.             this.out.write(255);
  176.             int var2 = var1 >> 8;
  177.             int var3 = var1 & 255;
  178.             this.out.write(var2);
  179.             this.out.write(var3);
  180.             return;
  181.          case 4:
  182.          default:
  183.             this.out.write(var1);
  184.       }
  185.    }
  186.  
  187.    public void writeChars(String var1) throws IOException {
  188.       int var2 = var1.length();
  189.  
  190.       for(int var3 = 0; var3 < var2; ++var3) {
  191.          char var4 = var1.charAt(var3);
  192.          if (var4 == '\n') {
  193.             int var5 = this.newline.length();
  194.  
  195.             for(int var6 = 0; var6 < var5; ++var6) {
  196.                this.write(this.newline.charAt(var6));
  197.             }
  198.          } else {
  199.             this.write(var4);
  200.          }
  201.       }
  202.  
  203.    }
  204.  
  205.    public void flush() throws IOException {
  206.       if (this.outsw != null) {
  207.          this.outsw.flush();
  208.       } else {
  209.          this.out.flush();
  210.       }
  211.    }
  212.  
  213.    public void writeNewLine() throws IOException {
  214.       if (this.outputStyle == PRETTY && !this.mixed) {
  215.          int var1 = this.newline.length();
  216.  
  217.          for(int var2 = 0; var2 < var1; ++var2) {
  218.             this.write(this.newline.charAt(var2));
  219.          }
  220.       }
  221.  
  222.    }
  223.  
  224.    public void writeQuotedString(String var1) throws IOException {
  225.       byte var2 = 34;
  226.       if (var1.indexOf(34) >= 0 && var1.indexOf(39) < 0) {
  227.          var2 = 39;
  228.       }
  229.  
  230.       this.write(var2);
  231.       int var3 = var1.length();
  232.  
  233.       for(int var4 = 0; var4 < var3; ++var4) {
  234.          char var5 = var1.charAt(var4);
  235.          if (var5 == var2) {
  236.             if (var2 == 34) {
  237.                this.writeChars(""");
  238.             } else {
  239.                this.writeChars("'");
  240.             }
  241.          } else {
  242.             this.write(var5);
  243.          }
  244.       }
  245.  
  246.       this.write(var2);
  247.    }
  248. }
  249.