Custom Conversions
goal
A Conversion function processes the input from the table column to something that can be used by a mask
or randomize
Rule.
To write your own Custom Conversion function, simply implement the IConversion.java
interface and @Override
the Object convert(String input)
function.
Place your custom Java function inside your root package/conversions
folder in the genereated project.
Notes
- The column is always read as a String
- Use a conversion to change the type or manipulate the data entry
- The convert function always assumes a String input, and may return any other Object type
- When used for a randomize rules, it converts the string into a type supported for the
randomize
rule
Conversion Interface
public interface IConversion {
Object convert(String input) throws Exception;
}
Java Examples
- ParseDigits.java
- TabDelim1.java
public class ParseDigits implements IConversion {
public static final String LABEL = "ParseDigits - simply remove all non-digits";
@Override
public Object convert(String txt) {
if(txt==null)
return "0";
char[] chars = txt.toCharArray();
StringBuilder sb = new StringBuilder();
for (char c : chars) {
if(Character.isDigit(c))
sb.append(c);
}
return sb.length()==0?"0":sb.toString();
}
}
Tabdelim1 for file input-sources, containing multiple columns of data
- If input source text file contains multiple columns of data, a conversion may be created to assign the correct delimited value.
- E.g., a text file containing a City, State and Zip Code delimited by a tab can have an associated conversion file
- All columns can use the same input text file with similar conversion function and as long as it is using sequence and repeatable random it will pick the same line from text file.
public class TabDelim1 implements IConversion {
public static final String LABEL = "TabDelim1 - Pick from first file column, delimited by tab";
@Override
public Object convert(String input) throws Exception {
return input.split("\\t")[0];
}
}
ANO Usage Examples
- You must explicitly import Custom Conversion functions before the Tasks and Rules Section
- ParseDigits in ANO
- TabDelim1 in ANO
table Person
column text age
size 16
conversion example.anonymizer.conversions.ParseDigits
task MyTask {
update Person
mask age
format %s
column age
convert ParseDigits // Remove digits from column values
}
table Person
column text city
size 16
column text postalCode
size 16
conversion example.anonymizer.conversions.TabDelim1
conversion example.anonymizer.conversions.TabDelim2
task MyTask {
update Person
mask city
format %s
file src/main/resources/locations.txt
convert TabDelim1
mask postalCode
format %s
file src/main/resources/locations.txt
convert TabDelim2
}