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.xml.esc;
  
 import java.io.Writer;

A utility class for escaping XML data using the UTF-8 encoding.

Author(s):
Christophe Lauret
Version:
12 May 2005
 
 public final class XMLEscapeWriterASCII extends XMLEscapeWriterBase implements XMLEscapeWriter {

  
The encoding used for this instance.
 
   private static final String ENCODING = "ASCII";

  
Creates a new XML escape writer using the utf-8 encoding.

Parameters:
writer The writer to wrap.
Throws:
java.lang.NullPointerException if the writer is null.
 
   public XMLEscapeWriterASCII(Writer writerthrows NullPointerException {
     super(writer);
   }

  
Replaces '<', '&', '"' and '\'' as well an any character that is not part of the standard unicode range.
 Char ::= #x9 | #xA | #xD |
          [#x20-#xD7FF] |
          [#xE000-#xFFFD] |
          [#x10000-#x10FFFF]
 
 
   public void writeAttValue(char[] chint offint lenthrows IOException {
     // process the rest
     char c = ' ';
     for (int i = offi < off+leni++) {
       c = ch[i];
       if      (c == '<') {
         super..write("&lt;");
       } else if (c == '>') {
         super..write("&gt;");
       } else if (c == '&') {
         super..write("&amp;");
       } else if (c == '"') {
         super..write("&quot;");
       } else if (c == '\'') {
         super..write("&apos;");
       } else if (c  > 255) {
         super..write("&#"+(int)c+";");
       } else if (c == '\n' || c == '\r' || c == '\t') {
         super..write(c);
       } else if (c < 32) {
         doNothing();
       } else if (c >= 127 && c < 160) {
         doNothing();
       } else { super..write(c); }
     }
   }

  
Replace characters which are invalid in element values, by the corresponding entity in a given String.

these characters are:

  • '&amp' by the ampersand entity "&amp"
  • '<' by the entity "&lt;"
  • Note: this function assumes that there are no entities in the given String. If there are existing entities, then the ampersand character will be escaped by the ampersand entity.

 
   public void writeText(char cthrows IOException {
     // process the rest
     if      (c == '<') {
       super..write("&lt;");
     } else if (c == '>') {
       super..write("&gt;");
    } else if (c == '&') {
      super..write("&amp;");
    } else if (c == '"') {
      super..write("&quot;");
    } else if (c == '\'') {
      super..write("&apos;");
    } else if (c  > 255) {
      super..write("&#"+(int)c+";");
    } else if (c == '\n' || c == '\r' || c == '\t') {
      super..write(c);
    } else if (c < 32) {
      doNothing();
    } else if (c >= 127 && c < 160) {
      doNothing();
    } else {
      super..write(c);
    }
  }

  
Does nothing.

This method exists so that we can explicitly say that we should do nothing in certain conditions.

  private static void doNothing() {
    return;
  }
New to GrepCode? Check out our FAQ X