Skip to content

Commit

Permalink
Make trim() robust, i.e. able to handle null values.
Browse files Browse the repository at this point in the history
  • Loading branch information
prasser committed Sep 1, 2016
1 parent 2d94696 commit cc2ac9d
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.deidentifier.arx.DataType;
import org.deidentifier.arx.gui.resources.Resources;
import org.deidentifier.arx.gui.view.SWTUtil;
import org.deidentifier.arx.io.IOUtil;
import org.deidentifier.arx.io.ImportColumnJDBC;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ColumnLabelProvider;
Expand Down Expand Up @@ -289,7 +290,7 @@ private void readColumns() {

while (rs.next()) {
ImportColumnJDBC column = new ImportColumnJDBC(i++,
rs.getString("COLUMN_NAME").trim(), //$NON-NLS-1$
IOUtil.trim(rs.getString("COLUMN_NAME")), //$NON-NLS-1$
DataType.STRING);
columns.add(new ImportWizardModelColumn(column));
}
Expand Down Expand Up @@ -428,7 +429,7 @@ protected void readPreview() {
while (rs.next()) {
String[] previewRow = new String[rs.getMetaData().getColumnCount()];
for (int j = 0; j < previewRow.length; j++) {
previewRow[j] = rs.getString(j + 1).trim();
previewRow[j] = IOUtil.trim(rs.getString(j + 1));
}
previewData.add(previewRow);
}
Expand Down
9 changes: 5 additions & 4 deletions src/main/org/deidentifier/arx/AttributeType.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.deidentifier.arx.io.CSVDataOutput;
import org.deidentifier.arx.io.CSVHierarchyInput;
import org.deidentifier.arx.io.CSVSyntax;
import org.deidentifier.arx.io.IOUtil;

/**
* Represents an attribute type.
Expand Down Expand Up @@ -137,15 +138,15 @@ private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IO
if (array != null) {
for (int row = 0; row < array.length; row++) {
if (array[row] != null && array[row].length > 0) {
array[row][0] = array[row][0].trim();
array[row][0] = IOUtil.trim(array[row][0]);
}
}
}
// Trim list
if (hierarchy != null) {
for (int row = 0; row < hierarchy.size(); row++) {
if (hierarchy.get(row) != null && hierarchy.get(row).length > 0) {
hierarchy.get(row)[0] = hierarchy.get(row)[0].trim();
hierarchy.get(row)[0] = IOUtil.trim(hierarchy.get(row)[0]);
}
}
}
Expand Down Expand Up @@ -201,7 +202,7 @@ private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IO
if (hierarchy != null) {
for (int row = 0; row < hierarchy.length; row++) {
if (hierarchy[row] != null && hierarchy[row].length > 0) {
hierarchy[row][0] = hierarchy[row][0].trim();
hierarchy[row][0] = IOUtil.trim(hierarchy[row][0]);
}
}
}
Expand Down Expand Up @@ -276,7 +277,7 @@ private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IO
if (hierarchy != null) {
for (int row = 0; row < hierarchy.length; row++) {
if (hierarchy[row] != null && hierarchy[row].length > 0) {
hierarchy[row][0] = hierarchy[row][0].trim();
hierarchy[row][0] = IOUtil.trim(hierarchy[row][0]);
}
}
}
Expand Down
33 changes: 33 additions & 0 deletions src/main/org/deidentifier/arx/io/IOUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* ARX: Powerful Data Anonymization
* Copyright 2012 - 2016 Fabian Prasser, Florian Kohlmayer and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.deidentifier.arx.io;

/**
* Utility for I/O
* @author Fabian Prasser, Florian Kohlmayer
*/
public class IOUtil {

/**
* Trims a given string. Can handle <code>null</code>.
* @param input
* @return
*/
public static String trim(String input) {
return input == null ? null : input.trim();
}
}
11 changes: 4 additions & 7 deletions src/main/org/deidentifier/arx/io/ImportAdapterExcel.java
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public String[] next() {
for (int i = 0; i < indexes.length; i++) {

row.getCell(indexes[i]).setCellType(Cell.CELL_TYPE_STRING);
result[i] = row.getCell(indexes[i]).getStringCellValue().trim();
result[i] = IOUtil.trim(row.getCell(indexes[i]).getStringCellValue());

if (!dataTypes[i].isValid(result[i])) {
if (config.columns.get(i).isCleansing()) {
Expand Down Expand Up @@ -252,18 +252,15 @@ private String[] createHeader() {

ImportColumn column = columns.get(i);

row.getCell(((ImportColumnExcel) column).getIndex())
.setCellType(Cell.CELL_TYPE_STRING);
String name = row.getCell(((ImportColumnExcel) column).getIndex())
.getStringCellValue().trim();
row.getCell(((ImportColumnExcel) column).getIndex()).setCellType(Cell.CELL_TYPE_STRING);
String name = IOUtil.trim(row.getCell(((ImportColumnExcel) column).getIndex()).getStringCellValue());

if (config.getContainsHeader() && !name.equals("")) {
/* Assign name of file itself */
header[i] = name;
} else {
/* Nothing defined in header (or empty), build name manually */
header[i] = "Column #" +
((ImportColumnExcel) column).getIndex();
header[i] = "Column #" + ((ImportColumnExcel) column).getIndex();
}

if (column.getAliasName() != null) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/org/deidentifier/arx/io/ImportAdapterJDBC.java
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public String[] next() {
String[] result = new String[indexes.length];
for (int i = 0; i < indexes.length; i++) {

result[i] = resultSet.getString(indexes[i]).trim();
result[i] = IOUtil.trim(resultSet.getString(indexes[i]));
if (!dataTypes[i].isValid(result[i])) {
if (config.columns.get(i).isCleansing()) {
result[i] = DataType.NULL_VALUE;
Expand Down Expand Up @@ -264,7 +264,7 @@ private String[] createHeader() {
/* Assign name from JDBC metadata */
try {
/* +1 offset, because counting in JDBC starts at 1 */
header[i] = resultSet.getMetaData().getColumnName(((ImportColumnJDBC) column).getIndex() + 1).trim();
header[i] = IOUtil.trim(resultSet.getMetaData().getColumnName(((ImportColumnJDBC) column).getIndex() + 1));
} catch (SQLException e) {
throw new IllegalArgumentException("Index for column '" + ((ImportColumnJDBC) column).getIndex() + "' couldn't be found");
}
Expand Down

0 comments on commit cc2ac9d

Please sign in to comment.