All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.topologi.diffx.xml.esc.XMLEscapeWriterUTF8 Maven / Gradle / Ivy

Go to download

docx4j is a library which helps you to work with the Office Open XML file format as used in docx documents, pptx presentations, and xlsx spreadsheets.

There is a newer version: 6.1.2
Show newest version
/*
 * 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.IOException;
import java.io.Writer;

/**
 * A utility class for escaping XML data using the UTF-8 encoding.
 *
 * @author Christophe Lauret
 * 
 * @version 14 May 2005
 */
public final class XMLEscapeWriterUTF8 extends XMLEscapeWriterBase implements XMLEscapeWriter {

  /**
   * The encoding used for this instance.
   */
  private static final String ENCODING = "utf-8";

  /**
   * Creates a new XML escape writer using the utf-8 encoding.
   * 
   * @param writer The writer to wrap.
   * 
   * @throws NullPointerException if the writer is null.
   */
  public XMLEscapeWriterUTF8(Writer writer) throws NullPointerException {
    super(writer, ENCODING);
  }

  /**
   * 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]
   * 
* * {@inheritDoc} */ public void writeAttValue(char[] ch, int off, int len) throws IOException { // process the rest char c = ' '; for (int i = off; i < off+len; i++) { c = ch[i]; if (c == '<') { super.w.write("<"); } else if (c == '>') { super.w.write(">"); } else if (c == '&') { super.w.write("&"); } else if (c == '"') { super.w.write("""); } else if (c == '\'') { super.w.write("'"); } else if (c > 255) { super.w.write("&#"+(int)c+";"); } else if (c == '\n' || c == '\r' || c == '\t') { super.w.write(c); } else if (c < 32) { doNothing(); } else if (c >= 127 && c < 160) { doNothing(); } else { super.w.write(c); } } } /** * Replace characters which are invalid in element values, * by the corresponding entity in a given String. * *

these characters are:
*

    *
  • '&' 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. * * {@inheritDoc} */ public void writeText(char c) throws IOException { // process the rest if (c == '<') { super.w.write("<"); } else if (c == '>') { super.w.write(">"); } else if (c == '&') { super.w.write("&"); } else if (c == '"') { super.w.write("""); } else if (c == '\'') { super.w.write("'"); } else if (c > 255) { super.w.write("&#"+(int)c+";"); } else if (c == '\n' || c == '\r' || c == '\t') { super.w.write(c); } else if (c < 32) { doNothing(); } else if (c >= 127 && c < 160) { doNothing(); } else { super.w.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() { } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy