home *** CD-ROM | disk | FTP | other *** search
/ Cre@te Online 2000 December / Cre@teOnline CD05.iso / MacSoft / XML ConsoleMax.sea / XML ConsoleMax / Required / xml4j.jar / com / ibm / xml / parser / ToXMLStringVisitor.class (.txt) < prev    next >
Encoding:
Java Class File  |  1999-08-30  |  6.3 KB  |  239 lines

  1. package com.ibm.xml.parser;
  2.  
  3. import java.io.Writer;
  4. import org.w3c.dom.CDATASection;
  5. import org.w3c.dom.Node;
  6.  
  7. public class ToXMLStringVisitor extends NOOPVisitor implements Visitor {
  8.    protected Writer writer;
  9.    protected String encoding;
  10.    protected boolean isPrintNonSpecifiedAttributes;
  11.  
  12.    public ToXMLStringVisitor(Writer var1, String var2) {
  13.       this.isPrintNonSpecifiedAttributes = true;
  14.       this.writer = var1;
  15.       this.encoding = var2;
  16.    }
  17.  
  18.    public ToXMLStringVisitor(Writer var1) {
  19.       this(var1, (String)null);
  20.    }
  21.  
  22.    public void setPrintNonSpecifiedAttributes(boolean var1) {
  23.       this.isPrintNonSpecifiedAttributes = var1;
  24.    }
  25.  
  26.    public boolean getPrintNonSpecifiedAttributes() {
  27.       return this.isPrintNonSpecifiedAttributes;
  28.    }
  29.  
  30.    public void visitDocumentPre(TXDocument var1) throws Exception {
  31.       if (var1.getVersion() != null) {
  32.          this.writer.write("<?xml version=\"" + var1.getVersion() + "\"");
  33.          if (var1.getEncoding() != null) {
  34.             this.writer.write(" encoding=\"" + var1.getEncoding() + "\"");
  35.          }
  36.  
  37.          if (var1.getStandalone() != null) {
  38.             this.writer.write(" standalone=\"" + var1.getStandalone() + "\"");
  39.          }
  40.  
  41.          this.writer.write("?>");
  42.       }
  43.  
  44.    }
  45.  
  46.    public void visitDocumentPost(TXDocument var1) throws Exception {
  47.       this.writer.flush();
  48.    }
  49.  
  50.    public void visitElementPre(TXElement var1) throws Exception {
  51.       this.writer.write("<" + var1.getTagName());
  52.       TXAttribute[] var2 = var1.getAttributeArray();
  53.  
  54.       for(int var3 = 0; var3 < var2.length; ++var3) {
  55.          try {
  56.             TXAttribute var4 = var2[var3];
  57.             if (this.isPrintNonSpecifiedAttributes || var4.getSpecified()) {
  58.                this.visitAttributePre(var4);
  59.             }
  60.          } catch (ToNextSiblingTraversalException var5) {
  61.          }
  62.       }
  63.  
  64.       if (((Parent)var1).hasChildNodes()) {
  65.          this.writer.write(">");
  66.       } else {
  67.          this.writer.write("/>");
  68.       }
  69.    }
  70.  
  71.    public void visitElementPost(TXElement var1) throws Exception {
  72.       if (((Parent)var1).hasChildNodes()) {
  73.          this.writer.write("</" + var1.getTagName() + ">");
  74.       }
  75.  
  76.    }
  77.  
  78.    public void visitAttributePre(TXAttribute var1) throws Exception {
  79.       this.writer.write(" ");
  80.       this.writer.write(var1.getName());
  81.       this.writer.write("=\"");
  82.  
  83.       for(Node var2 = ((Parent)var1).getFirstChild(); var2 != null; var2 = var2.getNextSibling()) {
  84.          short var3 = var2.getNodeType();
  85.          if (var3 == 3) {
  86.             this.writer.write(Util.backReference(var2.getNodeValue(), "<&\"", this.encoding));
  87.          } else if (var3 == 5) {
  88.             this.writer.write("&");
  89.             this.writer.write(var2.getNodeName());
  90.             this.writer.write(";");
  91.          }
  92.       }
  93.  
  94.       this.writer.write("\"");
  95.       throw new ToNextSiblingTraversalException();
  96.    }
  97.  
  98.    public void visitPIPre(TXPI var1) throws Exception {
  99.       this.writer.write("<?" + var1.getNodeName());
  100.       String var2 = var1.getData();
  101.       if (var1.getData().length() > 0 && !XMLChar.isSpace(var2.charAt(0))) {
  102.          this.writer.write(" ");
  103.       }
  104.  
  105.       this.writer.write(var1.getData() + "?>");
  106.    }
  107.  
  108.    public void visitCommentPre(TXComment var1) throws Exception {
  109.       this.writer.write("<!--" + ((TXCharacterData)var1).getData() + "-->");
  110.    }
  111.  
  112.    public void visitTextPre(TXText var1) throws Exception {
  113.       if (var1 instanceof CDATASection) {
  114.          this.writer.write("<![CDATA[" + ((TXCharacterData)var1).getData() + "]]>");
  115.       } else {
  116.          this.writer.write(Util.backReference(((TXCharacterData)var1).getData(), this.encoding));
  117.       }
  118.    }
  119.  
  120.    public void visitDTDPre(DTD var1) throws Exception {
  121.       this.writer.write("<!DOCTYPE " + var1.getName());
  122.       if (var1.getExternalID() != null) {
  123.          this.writer.write(" " + var1.getExternalID());
  124.       }
  125.  
  126.       if (var1.isPrintInternalDTD() && var1.getInternalSize() > 0) {
  127.          this.writer.write(" [");
  128.       } else {
  129.          throw new ToNextSiblingTraversalException();
  130.       }
  131.    }
  132.  
  133.    public void visitDTDPost(DTD var1) throws Exception {
  134.       if (var1.isPrintInternalDTD() && var1.getInternalSize() > 0) {
  135.          this.writer.write("]");
  136.       }
  137.  
  138.       this.writer.write(">");
  139.    }
  140.  
  141.    public void visitElementDeclPre(ElementDecl var1) throws Exception {
  142.       this.writer.write("<!ELEMENT " + var1.getName() + " " + var1.getXML4JContentModel() + ">");
  143.    }
  144.  
  145.    public void visitAttlistPre(Attlist var1) throws Exception {
  146.       this.writer.write("<!ATTLIST " + var1.getName());
  147.       int var2 = var1.size();
  148.       if (var2 <= 0) {
  149.          this.writer.write(" >");
  150.       } else if (var2 == 1) {
  151.          this.visitAttDefPre(var1.elementAt(0));
  152.          this.writer.write(62);
  153.       } else {
  154.          this.writer.write("\n");
  155.  
  156.          for(int var3 = 0; var3 < var2; ++var3) {
  157.             Util.printSpace(this.writer, 4);
  158.             this.visitAttDefPre(var1.elementAt(var3));
  159.             if (var3 == var2 - 1) {
  160.                this.writer.write(">");
  161.             } else {
  162.                this.writer.write("\n");
  163.             }
  164.          }
  165.  
  166.       }
  167.    }
  168.  
  169.    public void visitAttDefPre(AttDef var1) throws Exception {
  170.       this.writer.write(' ' + var1.getName() + ' ');
  171.       int var2 = var1.getDeclaredType();
  172.       if (var2 != 10) {
  173.          this.writer.write(AttDef.S_TYPESTR[var1.getDeclaredType()] + ' ');
  174.       }
  175.  
  176.       if (var2 == 9 || var2 == 10) {
  177.          this.writer.write('(' + var1.elementAt(0));
  178.  
  179.          for(int var3 = 1; var3 < var1.size(); ++var3) {
  180.             this.writer.write('|' + var1.elementAt(var3));
  181.          }
  182.  
  183.          this.writer.write(") ");
  184.       }
  185.  
  186.       int var4 = var1.getDefaultType();
  187.       if (var4 == 2) {
  188.          this.writer.write("#REQUIRED");
  189.       } else if (var4 == 3) {
  190.          this.writer.write("#IMPLIED");
  191.       } else if (var4 == 1) {
  192.          this.writer.write("#FIXED \"");
  193.          this.writer.write(var1.getDefaultStringValue() == null ? "(null)" : Util.backReferenceForEntity(var1.getDefaultStringValue(), this.encoding));
  194.          this.writer.write(34);
  195.       } else {
  196.          if (var4 == -1) {
  197.             this.writer.write(34);
  198.             this.writer.write(var1.getDefaultStringValue() == null ? "(null)" : Util.backReferenceForEntity(var1.getDefaultStringValue(), this.encoding));
  199.             this.writer.write(34);
  200.          }
  201.  
  202.       }
  203.    }
  204.  
  205.    public void visitEntityDeclPre(EntityDecl var1) throws Exception {
  206.       this.writer.write("<!ENTITY ");
  207.       if (var1.isParameter()) {
  208.          this.writer.write("% ");
  209.       }
  210.  
  211.       this.writer.write(var1.getNodeName() + " ");
  212.       if (var1.getValue() != null) {
  213.          this.writer.write("\"" + Util.backReferenceForEntity(var1.getValue(), this.encoding) + "\">");
  214.       } else {
  215.          this.writer.write(var1.getExternalID().toString());
  216.          if (var1.getNotationName() != null) {
  217.             this.writer.write(" NDATA " + var1.getNotationName());
  218.          }
  219.  
  220.          this.writer.write(">");
  221.       }
  222.  
  223.       throw new ToNextSiblingTraversalException();
  224.    }
  225.  
  226.    public void visitNotationPre(TXNotation var1) throws Exception {
  227.       this.writer.write("<!NOTATION " + var1.getNodeName() + " " + var1.getExternalID() + ">");
  228.    }
  229.  
  230.    public void visitGeneralReferencePre(GeneralReference var1) throws Exception {
  231.       this.writer.write('&' + var1.getName() + ';');
  232.       throw new ToNextSiblingTraversalException();
  233.    }
  234.  
  235.    public void visitPseudoNodePre(PseudoNode var1) throws Exception {
  236.       this.writer.write(((TXCharacterData)var1).getData());
  237.    }
  238. }
  239.