home *** CD-ROM | disk | FTP | other *** search
- package com.ibm.xml.parser;
-
- import java.io.Writer;
- import org.w3c.dom.CDATASection;
- import org.w3c.dom.Node;
-
- public class ToXMLStringVisitor extends NOOPVisitor implements Visitor {
- protected Writer writer;
- protected String encoding;
- protected boolean isPrintNonSpecifiedAttributes;
-
- public ToXMLStringVisitor(Writer var1, String var2) {
- this.isPrintNonSpecifiedAttributes = true;
- this.writer = var1;
- this.encoding = var2;
- }
-
- public ToXMLStringVisitor(Writer var1) {
- this(var1, (String)null);
- }
-
- public void setPrintNonSpecifiedAttributes(boolean var1) {
- this.isPrintNonSpecifiedAttributes = var1;
- }
-
- public boolean getPrintNonSpecifiedAttributes() {
- return this.isPrintNonSpecifiedAttributes;
- }
-
- public void visitDocumentPre(TXDocument var1) throws Exception {
- if (var1.getVersion() != null) {
- this.writer.write("<?xml version=\"" + var1.getVersion() + "\"");
- if (var1.getEncoding() != null) {
- this.writer.write(" encoding=\"" + var1.getEncoding() + "\"");
- }
-
- if (var1.getStandalone() != null) {
- this.writer.write(" standalone=\"" + var1.getStandalone() + "\"");
- }
-
- this.writer.write("?>");
- }
-
- }
-
- public void visitDocumentPost(TXDocument var1) throws Exception {
- this.writer.flush();
- }
-
- public void visitElementPre(TXElement var1) throws Exception {
- this.writer.write("<" + var1.getTagName());
- TXAttribute[] var2 = var1.getAttributeArray();
-
- for(int var3 = 0; var3 < var2.length; ++var3) {
- try {
- TXAttribute var4 = var2[var3];
- if (this.isPrintNonSpecifiedAttributes || var4.getSpecified()) {
- this.visitAttributePre(var4);
- }
- } catch (ToNextSiblingTraversalException var5) {
- }
- }
-
- if (((Parent)var1).hasChildNodes()) {
- this.writer.write(">");
- } else {
- this.writer.write("/>");
- }
- }
-
- public void visitElementPost(TXElement var1) throws Exception {
- if (((Parent)var1).hasChildNodes()) {
- this.writer.write("</" + var1.getTagName() + ">");
- }
-
- }
-
- public void visitAttributePre(TXAttribute var1) throws Exception {
- this.writer.write(" ");
- this.writer.write(var1.getName());
- this.writer.write("=\"");
-
- for(Node var2 = ((Parent)var1).getFirstChild(); var2 != null; var2 = var2.getNextSibling()) {
- short var3 = var2.getNodeType();
- if (var3 == 3) {
- this.writer.write(Util.backReference(var2.getNodeValue(), "<&\"", this.encoding));
- } else if (var3 == 5) {
- this.writer.write("&");
- this.writer.write(var2.getNodeName());
- this.writer.write(";");
- }
- }
-
- this.writer.write("\"");
- throw new ToNextSiblingTraversalException();
- }
-
- public void visitPIPre(TXPI var1) throws Exception {
- this.writer.write("<?" + var1.getNodeName());
- String var2 = var1.getData();
- if (var1.getData().length() > 0 && !XMLChar.isSpace(var2.charAt(0))) {
- this.writer.write(" ");
- }
-
- this.writer.write(var1.getData() + "?>");
- }
-
- public void visitCommentPre(TXComment var1) throws Exception {
- this.writer.write("<!--" + ((TXCharacterData)var1).getData() + "-->");
- }
-
- public void visitTextPre(TXText var1) throws Exception {
- if (var1 instanceof CDATASection) {
- this.writer.write("<![CDATA[" + ((TXCharacterData)var1).getData() + "]]>");
- } else {
- this.writer.write(Util.backReference(((TXCharacterData)var1).getData(), this.encoding));
- }
- }
-
- public void visitDTDPre(DTD var1) throws Exception {
- this.writer.write("<!DOCTYPE " + var1.getName());
- if (var1.getExternalID() != null) {
- this.writer.write(" " + var1.getExternalID());
- }
-
- if (var1.isPrintInternalDTD() && var1.getInternalSize() > 0) {
- this.writer.write(" [");
- } else {
- throw new ToNextSiblingTraversalException();
- }
- }
-
- public void visitDTDPost(DTD var1) throws Exception {
- if (var1.isPrintInternalDTD() && var1.getInternalSize() > 0) {
- this.writer.write("]");
- }
-
- this.writer.write(">");
- }
-
- public void visitElementDeclPre(ElementDecl var1) throws Exception {
- this.writer.write("<!ELEMENT " + var1.getName() + " " + var1.getXML4JContentModel() + ">");
- }
-
- public void visitAttlistPre(Attlist var1) throws Exception {
- this.writer.write("<!ATTLIST " + var1.getName());
- int var2 = var1.size();
- if (var2 <= 0) {
- this.writer.write(" >");
- } else if (var2 == 1) {
- this.visitAttDefPre(var1.elementAt(0));
- this.writer.write(62);
- } else {
- this.writer.write("\n");
-
- for(int var3 = 0; var3 < var2; ++var3) {
- Util.printSpace(this.writer, 4);
- this.visitAttDefPre(var1.elementAt(var3));
- if (var3 == var2 - 1) {
- this.writer.write(">");
- } else {
- this.writer.write("\n");
- }
- }
-
- }
- }
-
- public void visitAttDefPre(AttDef var1) throws Exception {
- this.writer.write(' ' + var1.getName() + ' ');
- int var2 = var1.getDeclaredType();
- if (var2 != 10) {
- this.writer.write(AttDef.S_TYPESTR[var1.getDeclaredType()] + ' ');
- }
-
- if (var2 == 9 || var2 == 10) {
- this.writer.write('(' + var1.elementAt(0));
-
- for(int var3 = 1; var3 < var1.size(); ++var3) {
- this.writer.write('|' + var1.elementAt(var3));
- }
-
- this.writer.write(") ");
- }
-
- int var4 = var1.getDefaultType();
- if (var4 == 2) {
- this.writer.write("#REQUIRED");
- } else if (var4 == 3) {
- this.writer.write("#IMPLIED");
- } else if (var4 == 1) {
- this.writer.write("#FIXED \"");
- this.writer.write(var1.getDefaultStringValue() == null ? "(null)" : Util.backReferenceForEntity(var1.getDefaultStringValue(), this.encoding));
- this.writer.write(34);
- } else {
- if (var4 == -1) {
- this.writer.write(34);
- this.writer.write(var1.getDefaultStringValue() == null ? "(null)" : Util.backReferenceForEntity(var1.getDefaultStringValue(), this.encoding));
- this.writer.write(34);
- }
-
- }
- }
-
- public void visitEntityDeclPre(EntityDecl var1) throws Exception {
- this.writer.write("<!ENTITY ");
- if (var1.isParameter()) {
- this.writer.write("% ");
- }
-
- this.writer.write(var1.getNodeName() + " ");
- if (var1.getValue() != null) {
- this.writer.write("\"" + Util.backReferenceForEntity(var1.getValue(), this.encoding) + "\">");
- } else {
- this.writer.write(var1.getExternalID().toString());
- if (var1.getNotationName() != null) {
- this.writer.write(" NDATA " + var1.getNotationName());
- }
-
- this.writer.write(">");
- }
-
- throw new ToNextSiblingTraversalException();
- }
-
- public void visitNotationPre(TXNotation var1) throws Exception {
- this.writer.write("<!NOTATION " + var1.getNodeName() + " " + var1.getExternalID() + ">");
- }
-
- public void visitGeneralReferencePre(GeneralReference var1) throws Exception {
- this.writer.write('&' + var1.getName() + ';');
- throw new ToNextSiblingTraversalException();
- }
-
- public void visitPseudoNodePre(PseudoNode var1) throws Exception {
- this.writer.write(((TXCharacterData)var1).getData());
- }
- }
-