Skip to content

Commit

Permalink
Fix: Allow Most chars for variable Values
Browse files Browse the repository at this point in the history
  • Loading branch information
tkruse committed Jun 27, 2018
1 parent 36d960e commit c8a1cb9
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 9 deletions.
26 changes: 25 additions & 1 deletion tcases-lib/src/main/java/org/cornutum/tcases/DefUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,31 @@ public static void assertIdentifier( String id) throws IllegalArgumentException
+ " is not a valid identifier");
}
}


/**
* Returns true if the given string is a valid identifier.
*/
public static boolean isVarValue( String val)
{
return val != null && varValueRegex_.matcher( val).matches();
}

/**
* Throws an exception if the given string is not a valid variable value.
*/
public static void assertVarValue( String val) throws IllegalArgumentException
{
if( !isVarValue( val))
{
throw
new IllegalArgumentException
( (val==null? "null" : ("\"" + String.valueOf( val) + "\""))
+ " is not a valid variable value");
}
}


/**
* Throws an exception if the given string is not a valid identifier path name.
*/
public static void assertPath( String pathName) throws IllegalArgumentException
Expand All @@ -77,5 +100,6 @@ public static void assertPropertyIdentifiers( Collection<String> properties) thr
}

private static final Pattern identifierRegex_ = Pattern.compile( "[\\w\\-]+");
private static final Pattern varValueRegex_ = Pattern.compile( "[^\\p{Cntrl}]*");
}

Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public String getVar()
*/
public void setValue( String valueName)
{
assertIdentifier( valueName);
assertVarValue( valueName);
value_ = valueName;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public VarValueDef( String name, Type type)
*/
public void setName( String name)
{
assertIdentifier( name);
assertVarValue( name);
name_ = name;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ public String requireAttribute( Attributes attributes, String attributeName) thr
return requireAttribute( attributes, attributeName, getAttribute( attributes, attributeName));
}

/**
* Returns the value of the given attribute. Throws a SAXException if the attribute is undefined or empty.
*/
public String requireNonBlankAttribute( Attributes attributes, String attributeName) throws SAXException
{
return requireAttribute( attributes, attributeName, StringUtils.trimToNull(getAttribute( attributes, attributeName)));
}

/**
* Returns the value of the given integer attribute or null if undefined. Throws a SAXException if the attribute is invalid.
*/
Expand All @@ -76,7 +84,7 @@ public Integer getInteger( Attributes attributes, String attributeName) throws S
*/
public Integer requireInteger( Attributes attributes, String attributeName) throws SAXException
{
return toInteger( attributeName, requireAttribute( attributes, attributeName));
return toInteger( attributeName, requireNonBlankAttribute( attributes, attributeName));
}

/**
Expand Down Expand Up @@ -126,7 +134,7 @@ public String getIdentifier( Attributes attributes, String attributeName) throws
*/
public String requireIdentifier( Attributes attributes, String attributeName) throws SAXException
{
return toIdentifier( attributeName, requireAttribute( attributes, attributeName));
return toIdentifier( attributeName, requireNonBlankAttribute( attributes, attributeName));
}

/**
Expand Down Expand Up @@ -164,7 +172,7 @@ public String getIdPath( Attributes attributes, String attributeName) throws SAX
*/
public String requireIdPath( Attributes attributes, String attributeName) throws SAXException
{
return toIdPath( attributeName, requireAttribute( attributes, attributeName));
return toIdPath( attributeName, requireNonBlankAttribute( attributes, attributeName));
}

/**
Expand Down Expand Up @@ -194,8 +202,7 @@ public String toIdPath( String attributeName, String attributeValue) throws SAXE
*/
public String getAttribute( Attributes attributes, String attributeName)
{
String value = attributes.getValue( attributeName);
return StringUtils.isBlank( value)? null : value;
return attributes.getValue( attributeName);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

package org.cornutum.tcases.util;

import org.apache.commons.lang3.StringEscapeUtils;

import java.io.OutputStream;
import java.io.Writer;

Expand Down Expand Up @@ -125,7 +127,7 @@ public void writeAttribute( String name, String value)
print( " ");
print( name);
print( "=\"");
print( value);
print( StringEscapeUtils.escapeXml10(value));
print( "\"");
}
}

0 comments on commit c8a1cb9

Please sign in to comment.