From b74ea3d7b3fc4bdb1f99c9a1d19f966e866bc9c6 Mon Sep 17 00:00:00 2001 From: Jay Jay Billings Date: Thu, 29 Oct 2015 02:44:57 +0000 Subject: [PATCH] Added support for space-based delimited text. 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 --- .../ice/io/csv/test/SpaceReaderTester.java | 160 ++++++++++++++++++ org.eclipse.ice.io/plugin.xml | 48 ++++++ .../src/org/eclipse/ice/io/csv/CSVReader.java | 116 +------------ .../eclipse/ice/io/csv/DelimitedReader.java | 152 +++++++++++++++++ .../ice/io/csv/SpaceDelimitedReader.java | 37 ++++ .../ice/reflectivity/ReflectivityModel.java | 5 +- 6 files changed, 407 insertions(+), 111 deletions(-) create mode 100644 org.eclipse.ice.io.test/src/org/eclipse/ice/io/csv/test/SpaceReaderTester.java create mode 100644 org.eclipse.ice.io/src/org/eclipse/ice/io/csv/DelimitedReader.java create mode 100644 org.eclipse.ice.io/src/org/eclipse/ice/io/csv/SpaceDelimitedReader.java diff --git a/org.eclipse.ice.io.test/src/org/eclipse/ice/io/csv/test/SpaceReaderTester.java b/org.eclipse.ice.io.test/src/org/eclipse/ice/io/csv/test/SpaceReaderTester.java new file mode 100644 index 000000000..95cba53aa --- /dev/null +++ b/org.eclipse.ice.io.test/src/org/eclipse/ice/io/csv/test/SpaceReaderTester.java @@ -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 lines = (ListComponent) 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()); + } + +} diff --git a/org.eclipse.ice.io/plugin.xml b/org.eclipse.ice.io/plugin.xml index 007124e51..ffca74f2b 100644 --- a/org.eclipse.ice.io/plugin.xml +++ b/org.eclipse.ice.io/plugin.xml @@ -3,5 +3,53 @@ + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/org.eclipse.ice.io/src/org/eclipse/ice/io/csv/CSVReader.java b/org.eclipse.ice.io/src/org/eclipse/ice/io/csv/CSVReader.java index 59924dc5e..e74da91d4 100644 --- a/org.eclipse.ice.io/src/org/eclipse/ice/io/csv/CSVReader.java +++ b/org.eclipse.ice.io/src/org/eclipse/ice/io/csv/CSVReader.java @@ -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 @@ -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 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(); - 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 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"; - } - } diff --git a/org.eclipse.ice.io/src/org/eclipse/ice/io/csv/DelimitedReader.java b/org.eclipse.ice.io/src/org/eclipse/ice/io/csv/DelimitedReader.java new file mode 100644 index 000000000..9e355bdd2 --- /dev/null +++ b/org.eclipse.ice.io/src/org/eclipse/ice/io/csv/DelimitedReader.java @@ -0,0 +1,152 @@ +/******************************************************************************* + * 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; + +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 base class for text + * files with a delimiter. It can read any well-formed delimited file. It stores + * its results in a ListComponent on the Form returned from read(). + * Each String [] in the ListComponent is a line of the file, split and trimmed + * but uncast. Clients must know the concrete type to which they want to cast. + * + * Comments are ignored and begin with the "#" character. + * + * The delimiter must be set by subclasses during construction. It is a " " by + * default. Likewise, the type name must be specified too. + * + * @author Jay Jay Billings + * + */ +public class DelimitedReader implements IReader { + + /** + * The delimiter. + */ + protected String delimiter = " "; + + /** + * The type of reader. + */ + protected String type; + + /** + * Logger for handling event messages and other information. + */ + private static final Logger logger = LoggerFactory + .getLogger(DelimitedReader.class); + + /** + * The lines of text read from the last file. + */ + ListComponent lines; + + /* + * (non-Javadoc) + * + * @see + * org.eclipse.ice.io.serializable.IReader#read(org.eclipse.core.resources + * .IFile) + */ + @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(); + 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(delimiter); + // 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 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; + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.ice.io.serializable.IReader#getReaderType() + */ + @Override + public String getReaderType() { + return type; + } + +} diff --git a/org.eclipse.ice.io/src/org/eclipse/ice/io/csv/SpaceDelimitedReader.java b/org.eclipse.ice.io/src/org/eclipse/ice/io/csv/SpaceDelimitedReader.java new file mode 100644 index 000000000..31602cf45 --- /dev/null +++ b/org.eclipse.ice.io/src/org/eclipse/ice/io/csv/SpaceDelimitedReader.java @@ -0,0 +1,37 @@ +/******************************************************************************* + * 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; + +/** + * This class implements the IReader interface to provide a reader for space + * delimited text files. It can read any well-formed space delimited text file. + * It stores its results in a ListComponent on the Form returned from + * read(). Each String [] in the ListComponent is a line of the file, split and + * trimmed but uncast. Clients must know the concrete type to which they want to + * cast. + * + * Comments are ignored and begin with the "#" character. + * + * @author Jay Jay Billings + * + */ +public class SpaceDelimitedReader extends DelimitedReader { + + /** + * The constructor + */ + public SpaceDelimitedReader() { + // Set the file type. The delimiter is already a space by default. + type = "space-delimited"; + } + +} diff --git a/org.eclipse.ice.reflectivity/src/org/eclipse/ice/reflectivity/ReflectivityModel.java b/org.eclipse.ice.reflectivity/src/org/eclipse/ice/reflectivity/ReflectivityModel.java index f133105ff..486e5fc1a 100644 --- a/org.eclipse.ice.reflectivity/src/org/eclipse/ice/reflectivity/ReflectivityModel.java +++ b/org.eclipse.ice.reflectivity/src/org/eclipse/ice/reflectivity/ReflectivityModel.java @@ -32,6 +32,8 @@ import org.eclipse.ice.datastructures.form.ResourceComponent; import org.eclipse.ice.datastructures.resource.VizResource; import org.eclipse.ice.io.csv.CSVReader; +import org.eclipse.ice.io.csv.SpaceDelimitedReader; +import org.eclipse.ice.io.serializable.IOService; import org.eclipse.ice.item.model.Model; import org.eclipse.ice.materials.IMaterialsDatabase; import org.eclipse.ice.materials.MaterialWritableTableFormat; @@ -200,7 +202,8 @@ public FormStatus process(String actionName) { IFile userDataFile = project.getFile(fileName); // Get the reader and read in the values. - Form dataForm = new CSVReader().read(userDataFile); + Form dataForm = getIOService().getReader("space-delimited") + .read(userDataFile); ListComponent userData = (ListComponent) dataForm .getComponent(1);