Skip to content

Commit

Permalink
Added support for space-based delimited text.
Browse files Browse the repository at this point in the history
Added a base class for delimited text. Added a space-based delimited
text file reader. Fixed the reflectivity model to use the IOService and
to use space-based delimiters.

Signed-off-by: Jay Jay Billings <billingsjj@ornl.gov>
  • Loading branch information
Jay Jay Billings committed Oct 29, 2015
1 parent 730b6e3 commit b74ea3d
Show file tree
Hide file tree
Showing 6 changed files with 407 additions and 111 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
/*******************************************************************************
* Copyright (c) 2013, 2014 UT-Battelle, LLC.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Initial API and implementation and/or initial documentation -
* Jay Jay Billings
*******************************************************************************/
package org.eclipse.ice.io.csv.test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.IOException;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.ice.datastructures.ICEObject.ListComponent;
import org.eclipse.ice.datastructures.form.Form;
import org.eclipse.ice.io.csv.CSVReader;
import org.eclipse.ice.io.csv.SpaceDelimitedReader;
import org.junit.BeforeClass;
import org.junit.Test;

/**
* Test class for {@link org.eclipse.ice.io.csv.CSVReader}.
*
* @author Jay Jay Billings
*
*/
public class SpaceReaderTester {

/**
* The space-delimited file that will be tested
*/
private static IFile testFile;

/**
* The reader that will be tested.
*/
private static SpaceDelimitedReader reader;

/**
* @throws java.lang.Exception
*/
@BeforeClass
public static void setUpBeforeClass() throws Exception {
// Get the file separator used on this system, which is different across
// OSes.
String separator = System.getProperty("file.separator");
// Create the path for the reflectivity file in the ICE tests directory
String userHome = System.getProperty("user.home");
IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
IProject project = null;
String projectName = "CSVLoaderTesterWorkspace";
String filename = "waveVector_space.csv";
IPath projectPath = new Path(userHome + separator + "ICETests"
+ separator + projectName + separator + ".project");

// Setup the project
try {
// Create the project description
IProjectDescription desc = ResourcesPlugin.getWorkspace()
.loadProjectDescription(projectPath);
// Get the project handle and create it
project = workspaceRoot.getProject(desc.getName());
project.create(desc, new NullProgressMonitor());
// Open the project if it is not already open
if (project.exists() && !project.isOpen()) {
project.open(new NullProgressMonitor());
}
// Refresh the workspace
project.refreshLocal(IResource.DEPTH_INFINITE,
new NullProgressMonitor());
// Create the IFile handle for the csv file
testFile = project.getFile(filename);
} catch (CoreException e) {
// Catch exception for creating the project
e.printStackTrace();
fail();
}

// Create the reader
reader = new SpaceDelimitedReader();

return;
}

/**
* Test method for
* {@link org.eclipse.ice.io.csv.CSVReader#read(org.eclipse.core.resources.IFile)}
* .
*
* @throws CoreException
* @throws IOException
*/
@Test
public void testRead() throws CoreException, IOException {

// Load the file
Form form = reader.read(testFile);

// Check the Form
assertTrue(form.getComponents().get(0) instanceof ListComponent);
ListComponent<String[]> lines = (ListComponent<String[]>) form
.getComponents().get(0);
assertTrue(lines.get(0) instanceof String[]);
assertEquals(402,lines.size());

// Check the first element of the list. The first line of the file is a
// comment, so it should be skipped and this should be data.
String [] line = lines.get(0);
assertEquals(4,line.length);
assertEquals("8.14174169E-03",line[0]);
assertEquals("0.78066729",line[1]);
assertEquals("0.08261301",line[2]);
assertEquals("0.000387",line[3]);

// Check the last element of the list
line = lines.get(lines.size()-1);
assertEquals(4,line.length);
assertEquals("4.40137332E-01",line[0]);
assertEquals("0.00000013",line[1]);
assertEquals("0.00000007",line[2]);
assertEquals("0.010323",line[3]);

return;
}

/**
* Test method for
* {@link org.eclipse.ice.io.csv.CSVReader#findAll(org.eclipse.core.resources.IFile, java.lang.String)}
* .
*/
// @Test
public void testFindAll() {
fail("Not yet implemented");
}

/**
* Test method for {@link org.eclipse.ice.io.csv.CSVReader#getReaderType()}.
*/
@Test
public void testGetReaderType() {
assertEquals("space-delimited", reader.getReaderType());
}

}
48 changes: 48 additions & 0 deletions org.eclipse.ice.io/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,53 @@
<plugin>
<extension-point id="reader" name="Reader" schema="schema/IReader.exsd"/>
<extension-point id="writer" name="Writer" schema="schema/IWriter.exsd"/>
<extension
id="org.eclipse.ice.io.spaceReader"
name="Space-delimited reader"
point="org.eclipse.ice.io.reader">
<implementation
class="org.eclipse.ice.io.csv.SpaceDelimitedReader">
</implementation>
</extension>
<extension
id="org.eclipse.ice.io.csvReader"
name="CSV Reader"
point="org.eclipse.ice.io.reader">
<implementation
class="org.eclipse.ice.io.csv.CSVReader">
</implementation>
</extension>
<extension
id="org.eclipse.ice.io.iniReader"
name="INI Reader"
point="org.eclipse.ice.io.reader">
<implementation
class="org.eclipse.ice.io.ini.INIReader">
</implementation>
</extension>
<extension
id="org.eclipse.ice.io.IPSReader"
name="IPS Reader"
point="org.eclipse.ice.io.reader">
<implementation
class="org.eclipse.ice.io.ips.IPSReader">
</implementation>
</extension>
<extension
id="org.eclipse.ice.io.INIWriter"
name="INI Writer"
point="org.eclipse.ice.io.writer">
<implementation
class="org.eclipse.ice.io.ini.INIWriter">
</implementation>
</extension>
<extension
id="org.eclipse.ice.io.IPSWriter"
name="IPS Writer"
point="org.eclipse.ice.io.writer">
<implementation
class="org.eclipse.ice.io.ips.IPSWriter">
</implementation>
</extension>

</plugin>
116 changes: 6 additions & 110 deletions org.eclipse.ice.io/src/org/eclipse/ice/io/csv/CSVReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,6 @@
*******************************************************************************/
package org.eclipse.ice.io.csv;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

import javax.naming.OperationNotSupportedException;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.ice.datastructures.ICEObject.ListComponent;
import org.eclipse.ice.datastructures.form.Entry;
import org.eclipse.ice.datastructures.form.Form;
import org.eclipse.ice.io.serializable.IReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* This class implements the IReader interface to provide a reader for CSV
* files. It can read any well-formed CSV file. It stores its results in a
Expand All @@ -39,102 +23,14 @@
* @author Jay Jay Billings
*
*/
public class CSVReader implements IReader {

/**
* Logger for handling event messages and other information.
*/
private static final Logger logger = LoggerFactory
.getLogger(CSVReader.class);
public class CSVReader extends DelimitedReader {

/**
* The lines of text read from the last file.
*/
ListComponent<String []> lines;

/*
* (non-Javadoc)
*
* @see
* org.eclipse.ice.io.serializable.IReader#read(org.eclipse.core.resources
* .IFile)
* The constructor
*/
@Override
public Form read(IFile file) {

// Configure the form
Form form = new Form();
form.setName(file.getName());
form.setDescription(file.getName());

// Configure the list
lines = new ListComponent<String[]>();
lines.setName(file.getName());
lines.setDescription(file.getName());

try {
// Grab the contents of the file
BufferedReader reader = new BufferedReader(new InputStreamReader(
file.getContents()));
String line = null;
while ((line = reader.readLine()) != null) {
// Skip lines that pure comments
if (!line.startsWith("#")) {
// Clip the line if it has a comment symbol in it to be
// everything before the symbol
if (line.contains("#")) {
int index = line.indexOf("#");
line = line.substring(0, index);
}
// Clean up any crap on the line
String[] lineArray = line.trim().split(",");
// And clean up any crap on each split piece
for (String element : lineArray) {
element = element.trim();
}
// Put the lines in the list
lines.add(lineArray);
}
}
form.addComponent(lines);
} catch (CoreException e) {
// Complain
logger.error(getClass().getName() + " Exception!",e);
} catch (IOException e) {
// TODO Auto-generated catch block
logger.error(getClass().getName() + " Exception!",e);
}

return form;
}

/*
* (non-Javadoc)
*
* @see
* org.eclipse.ice.io.serializable.IReader#findAll(org.eclipse.core.resources
* .IFile, java.lang.String)
*/
@Override
public ArrayList<Entry> findAll(IFile file, String regex) {
try {
throw new OperationNotSupportedException("CSVReader Error: "
+ "IReader.findAll() is not supported... yet.");
} catch (OperationNotSupportedException e) {
// TODO Auto-generated catch block
logger.error(getClass().getName() + " Exception!",e);
}
return null;
public CSVReader() {
// Set the delimiter and type
delimiter = ",";
type = "csv";
}

/*
* (non-Javadoc)
*
* @see org.eclipse.ice.io.serializable.IReader#getReaderType()
*/
@Override
public String getReaderType() {
return "csv";
}

}
Loading

0 comments on commit b74ea3d

Please sign in to comment.