Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package nu.validator.checker;
  
  
  import java.util.List;
Static utilities for working with (X)HTML5 attribute values.

Author(s):
hsivonen
Version:
$Id$
 
 public final class AttributeUtil {

    
An empty string array instance.
 
     private final static String[] EMPTY_STRING_ARRAY = {};

    
The pattern for extracting an integer by skipping white space and ignoring trailing garbage.
 
     private static Pattern INTEGER_PATTERN = Pattern.compile("^[ \t\n\r]*(-?[0-9]+)");

    
Private constructor to prevent instantiation.
 
     private AttributeUtil() {
         super();
     }

    
Returns the integer represented by attrVal or Integer.MIN_VALUE on error.

Parameters:
attrVal a string representing an integer attribute value (can be null)
Returns:
the integer represented by attrVal or Integer.MIN_VALUE on error
 
     public static int parseInteger(String attrVal) {
         if (attrVal == null) {
             return .;
         }
         Matcher m = .matcher(attrVal);
         if (!m.matches()) {
             return .;
         }
         try {
             return Integer.parseInt(m.group(1));
         } catch (NumberFormatException e) {
             return .;
         }
     }

    
Returns the non-negative integer represented by attrVal or -1 on error.

Parameters:
attrVal a string representing a non-negative integer attribute value (can be null)
Returns:
the integer represented by attrVal or -1 on error
 
     public static int parseNonNegativeInteger(String attrVal) {
         int rv = parseInteger(attrVal);
         if (rv < 0) {
             return -1;
         } else {
             return rv;
         }
     }

    
Returns the positive integer represented by attrVal or -1 on error.

Parameters:
attrVal a string representing a positive integer attribute value (can be null)
Returns:
the integer represented by attrVal or -1 on error
 
     public static int parsePositiveInteger(String attrVal) {
         int rv = parseInteger(attrVal);
         if (rv < 1) {
             return -1;
         } else {
             return rv;
         }
     }

    
Splits the argument on white space.

Parameters:
value the attribute value
Returns:
a string array with zero or more strings none of which is the empty string and none of which contains white space characters.
    public static String[] split(String value) {
        if (value == null || "".equals(value)) {
            return ;
        }
        int len = value.length();
        List<Stringlist = new LinkedList<String>();
        boolean collectingSpace = true;
        int start = 0;
        for (int i = 0; i < leni++) {
            char c = value.charAt(i);
            if (c == ' ' || c == '\t' || c == '\n' || c == '\r') {
                if (!collectingSpace) {
                    list.add(value.substring(starti));
                    collectingSpace = true;
                }
            } else {
                if (collectingSpace) {
                    start = i;
                    collectingSpace = false;
                }
            }
        }
        if (start < len) {
            list.add(value.substring(startlen));
        }
        return list.toArray();
    }
    
    
Checks if string matches lowerCaseLiteral when ASCII-lowercased.

Parameters:
lowerCaseLiteral a lower-case literal
string potentially mixed-case string or null
Returns:
true if string matches lowerCaseLiteral when ASCII-lowercased
    public static boolean lowerCaseLiteralEqualsIgnoreAsciiCaseString(String lowerCaseLiteral,
            String string) {
        if (string == null) {
            return false;
        }
        if (lowerCaseLiteral.length() != string.length()) {
            return false;
        }
        for (int i = 0; i < lowerCaseLiteral.length(); i++) {
            char c0 = lowerCaseLiteral.charAt(i);
            char c1 = string.charAt(i);
            if (c1 >= 'A' && c1 <= 'Z') {
                c1 += 0x20;
            }
            if (c0 != c1) {
                return false;
            }
        }
        return true;
    }
New to GrepCode? Check out our FAQ X