Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * This file is part of the DiffX library.
   *
   * For licensing information please see the file license.txt included in the release.
   * A copy of this licence can also be found at
   *   http://www.opensource.org/licenses/artistic-license-2.0.php
   */
  package com.topologi.diffx.format;
  
 import java.io.Writer;
 
A simple XML formatter that writes strictly what it is given.

This formatter will write the events exactly in the order in which they are given, in other words, there is no way to prevent this class from writing malformed XML. On other hand, the SmartXMLFormatter will close XML elements automatically, therefore rectifying a lot of the errors that lead to malformed XML.

Author(s):
Christophe Lauret
Version:
3 April 2005
 
 public final class StrictXMLFormatter implements XMLDiffXFormatter {
 
   // class attributes ---------------------------------------------------------------------------
 
  
Thw output goes here.
 
   private final PrintWriter xml;

  
The open 'del' tag, that is the element start tag that we put before test is being deleted.
 
   private String openDel = "<del>";

  
The close 'del' tag, that is the element end tag that we put after test is being deleted.
 
   private String closeDel = "</del>";

  
The open 'ins' tag, that is the element start tag that we put before test is being inserted.
 
   private String openIns = "<ins>";

  
The close 'ins' tag, that is the element end tag that we put after test is being inserted.
 
   private String closeIns = "</ins>";

  
The DiffX configuration to use
 
   private DiffXConfig config = new DiffXConfig();
 
   // state variables ----------------------------------------------------------------------------
 
  
Set to false once the prefix mapping has been declared.
   xmlns:dfx="http://www.allette.com.au/diffex"
 
 
   private transient boolean declareNamespace = true;

  
Set to true to indicate that there is an open 'ins' tag.
 
   private transient boolean isInserting = false;

  
Set to true to indicate that there is an open 'del' tag.
 
   private transient boolean isDeleting = false;

  
Set to true to indicate that there is an open element tag that does not have its right angle bracket.
 
   private transient boolean isElementNude = false;
 
 
  // constructors -------------------------------------------------------------------------------

  
Creates a new formatter on the standard output.
  public StrictXMLFormatter() {
    this. = new PrintWriter(.);
    init();
  }

  
Creates a new formatter using the specified writer.

Parameters:
w The writer to use.
  public StrictXMLFormatter(Writer w) {
    this. = new PrintWriter(w);
    init();
  }
  // methods ------------------------------------------------------------------------------------

  
Writes the XML declaration.
  private void init() {
    this..println("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
  }
  public void format(DiffXEvent ethrows IOException {
    // an element to open
    if (e instanceof OpenElementEvent) {
      if (this.) {
        denudeElement();
      }
      // close any ins / del tag
      if (this.) {
        closeIns();
      }
      if (this.) {
        closeDel();
      }
      OpenElementEvent oee = (OpenElementEvent)e;
      this..print('<'+oee.getName());
      if (this.) {
        this..print(" xmlns:dfx=\""+.+"\"");
        this. = false;
      }
      this. = true;
      // an element to close
    } else if (e instanceof CloseElementEvent) {
      if (this.) {
        denudeElement();
      }
      // close any ins / del tag
      if (this.) {
        closeIns();
      }
      if (this.) {
        closeDel();
      }
      this..print(e.toXML());
      // an attribute
    } else if (e instanceof AttributeEvent) {
      if (this.) {
        this..print(e.toXML());
      } else
        throw new IllegalStateException("Cannot write an attribute once the element is closed");
      // this is text
    } else {
      // close any ins / del tag
      if (this.) {
        denudeElement();
      }
      if (this.) {
        closeIns();
      }
      if (this.) {
        closeDel();
      }
      // a character sequence
      if (e instanceof WordEvent || e instanceof SpaceEvent) {
        this..print(e.toXML());
        // a single character
      } else if (e instanceof CharEvent) {
        this..print(((CharEvent)e).);
      }
    }
    this..flush();
  }
  public void insert(DiffXEvent ethrows IOException {
    // insert element
    if (e instanceof OpenElementEvent) {
      if (this.) {
        denudeElement();
      }
      if (this.) {
        closeDel();
      }
      OpenElementEvent oee = (OpenElementEvent)e;
      this..print('<'+oee.getName());
      if (this.) {
        this..print(" xmlns:dfx=\"http://www.allette.com.au/diffex\"");
      }
      this..print(" dfx:insert=\"true\"");
      this. = true;
      // an element to close
    } else if (e instanceof CloseElementEvent) {
      if (this.) {
        denudeElement();
      }
      if (this.) {
        closeDel();
      }
      this..print("</");
      this..print(((CloseElementEvent)e).getName());
      this..print('>');
    } else if (e instanceof AttributeEvent) {
      if (this.) {
        this..print(" ");
        this..print(((AttributeEvent)e).getName());
        this..print("=\"");
        this..print(((AttributeEvent)e).getValue());
        this..print('"');
      } else {
        throw new IllegalStateException("Cannot insert an attribute once the element is closed");
      }
    } else {
      // close any del tag
      if (this.) {
        denudeElement();
      }
      if (this.) {
        closeDel();
      }
      // a word
      if (e instanceof WordEvent) {
        if (!this.) {
          openIns();
        }
        this..print(e.toXML());
        // a white space
      } else if (e instanceof SpaceEvent) {
        this..print(e.toXML());
        // wrap the char in a <ins> element
      } else if (e instanceof CharEvent) {
        this..print(((CharEvent)e).);
      }
    }
    this..flush();
  }
  public void delete(DiffXEvent ethrows IOExceptionIllegalStateException {
    // we ignore delete attributes
    if (this.) {
      denudeElement();
    }
    if (this.) {
      closeIns();
    }
    // delete an element
    if (e instanceof OpenElementEvent) {
      OpenElementEvent oee = (OpenElementEvent)e;
      this..print('<'+oee.getName());
      if (this.) {
        this..print(" xmlns:dfx=\"http://www.allette.com.au/diffex\"");
      }
      this..print(" dfx:delete=\"true\"");
      this..print('>');
      // an element to close
    } else if (e instanceof CloseElementEvent) {
      this..print("</");
      this..print(((CloseElementEvent)e).getName());
      this..print('>');
      // text
    } else {
      // a word
      if (e instanceof WordEvent) {
        if (!this.) {
          openDel();
        }
        this..print(e.toXML());
        // a white space
      } else if (e instanceof SpaceEvent) {
        this..print(e.toXML());
        // wrap the char in a <ins> element
      } else if (e instanceof CharEvent) {
        this..print(((CharEvent)e).);
      }
    }
    this..flush();
  }

  
Sets the open and end tags for inserted text.

The default values are "<ins:>" and "</ins:>" respectively.

Parameters:
start The open tag for inserts.
end The close tag for inserts.
Throws:
java.lang.NullPointerException If any of the tags is null.
  public void setInsertTags(String startString endthrows NullPointerException {
    if (start == null)
      throw new NullPointerException("The start element for inserted text must have a value");
    if (end == null)
      throw new NullPointerException("The start element for inserted text must have a value");
    this. = start;
    this. = end;
  }

  
Sets the open and end tags for deleted text.

The default values are "<del:>" and "</del:>" respectively.

Parameters:
start The open tag for deletions.
end The close tag for deletions.
Throws:
java.lang.NullPointerException If any of the tags is null.
  public void setDeleteTags(String startString endthrows NullPointerException {
    if (start == null)
      throw new NullPointerException("The start element for deleted text must have a value");
    if (end == null)
      throw new NullPointerException("The start element for deleted text must have a value");
    this. = start;
    this. = end;
  }
  public void setConfig(DiffXConfig config) {
    this. = config;
  }
  public void setWriteXMLDeclaration(boolean show) {
  }

  
Adds the prefix mapping to this class.

Parameters:
mapping The prefix mapping to add.
  public void declarePrefixMapping(PrefixMapping mapping) {
    for (Enumeration<Stringuris = mapping.getURIs(); uris.hasMoreElements();) {
      String uri = uris.nextElement();
      mapping.getPrefix(uri);
      // TODO: does nothing !!!
    }
  }
  // private helpers ----------------------------------------------------------------------------

  
Opens the 'ins' element, and update the state flags.
  private void openIns() {
    this..print(this.);
    this. = true;
  }

  
Opens the 'del' element, and update the state flags.
  private void openDel() {
    this..print(this.);
    this. = true;
  }

  
Closes the 'ins' element, and update the state flags.
  private void closeIns() {
    this..print(this.);
    this. = false;
  }

  
Closes the 'del' element, and update the state flags.
  private void closeDel() {
    this..print(this.);
    this. = false;
  }

  
Closes the 'del' element, and update the state flags.
  private void denudeElement() {
    this..print(">");
    this. = false;
  }
New to GrepCode? Check out our FAQ X