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;
  
A utility class for XML data.

Author(s):
Christophe Lauret
Version:
2.0, 24 March 2004
 
 public final class XMLUtils {

  
Prevents creation of instances.
 
   private XMLUtils() {
   }

  
Replaces 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;"
  • Empty strings or null return respectively "" and null.

    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.

    This method does not replaces " (by &quot;) which is an invalid character in attribute values.

    Parameters:
    s The String to be parsed
    Returns:
    a valid string or empty if s is null or empty.
    See also:
    escapeAttr(java.lang.String)
 
   public static String escape(String s) {
   }

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

these characters are:

  • '&amp' by the ampersand entity "&amp"
  • '<' by the entity "&lt;"
  • ''' by the entity "&apos;"
  • '"' by the entity "&quot;"
  • Empty strings or null return respectively "" and null.

    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.

    Parameters:
    s The String to be parsed
    Returns:
    a valid string or empty if s is null or empty.
 
   public static String escapeAttr(String s) {
   }

  
Return a valid element name from the given string.

Letters are put to lower case and other characters are replaced by hyphens. If the first character is not a letter it is replaced by 'x'.

Parameters:
name The candidate element name
Returns:
A valid element name
 
   public static String toElementName(String name) {
     if (name == nullreturn null;
     char[] elementAsChars = name.toCharArray();
     if (!Character.isLetter(elementAsChars[0])) {
       elementAsChars[0] = 'x';
     } else {
       elementAsChars[0] = Character.toLowerCase(elementAsChars[0]);
    }
    for (int i = 1; i < elementAsChars.lengthi++) {
      if (!Character.isLetter(elementAsChars[i])) {
        elementAsChars[i] = '-';
      } else {
        elementAsChars[i] = Character.toLowerCase(elementAsChars[i]);
      }
    }
    return new String(elementAsChars);
  }
New to GrepCode? Check out our FAQ X