0

I would like to fulfill the following task. I am not sure if it is possible to do so the way I planed but therefore my question:

I’d like to write a Java program to allow users to load XML or CSV files and to manipulate and use their content in some sort of scripting environment like Rhino or Beanshell. The scripting should look something like this:

var myData = new dataLoader(“some_file.csv”);
myData.load(); // should load the content
(while myData.next()) {
  If( myData.status == “pending”) {
    myData.value = myData.value + 1;
    myData.status = “updated”; //set new status
    myData.update(); //update dataset
  }
}

Some_file.csv:

“value”; “status”;

“1”; “pending”;

“21”; “pending”;

“341”; “pending”;

Is this possible (to dynamically provide methods/functions/variables based on the file’ content)? If so any idea what my class “dataLoader” should look like? I would also appreciate a piece of advice where to start looking. Thank you for your help, Stephanie

Stephanie
  • 101
  • 1
  • 6

2 Answers2

0

You probably want to create a class which will read all the lines of a file, using a while with the BufferedReader method readLine or through Guava, that has a function which does just this. As for XML, you need a DOM parser which will load all the file inside a tree.

Then, store the data in a LinkedList or an array and use an integer variable as an index. When the method next() is called it will increase the index and return false if it's equal to the size of the array, true otherwise. It will also set value and status to the new values.

Update() will of course update the value at the index position with the field values.

The problem is when to write the structure to the disk. You could do it at the last call to next(), if you are sure it will be called iteratively until the end, or create a method like flush() which will write the array to the file. Another way is to write in the update() method, which seems the one most fitting your case.

Just for curiosity, what are you doing?

Jacopofar
  • 3,407
  • 2
  • 19
  • 29
  • Hello Jackopo, thank you for your reply. At my university we have several scripts for evaluation purposes. My goal is to bring all these different scripts together in one Java application. The only thing all these methods have in common is that the measurement data is stored in a CVS or XML file. So reading the file is one problem, but thanks to your suggestions I have an idea now. The other problem is how to access the data. Since the structure of the files is different I sort of need a "dynamic variable builder"... Or am I missing something here? Thanks, – Stephanie Nov 28 '12 at 20:28
  • It depends on how much is different, and how can you determine which kind of structure is. You mean CSV files has different columns? That is, are there sometimes other data and sometimes the order of columns is different? – Jacopofar Nov 29 '12 at 09:17
  • Yes, most of the files have different columns. During the java compiling I don't know what these columns will be. Therefore I was thinking about Rhino to give the user a "just in time" coding method. – Stephanie Nov 29 '12 at 19:16
  • Seems to me a good idea, you could provide the user a reference to an helper class which makes the script easier to write. In this case you could use an array of values to represent the current line, so the column meaning is managed by the script, which will manipulate this array instead of a value and status explicitly provided. – Jacopofar Nov 29 '12 at 22:39
  • Hello again. Sorry for my late reply, but I am afraid I do not understand what you mean. Do you mean a helper class which gathers the information? Thank you for your time. – Stephanie Dec 02 '12 at 22:02
  • Yes, since you write one Java program and many scripts, is better to move inside the Java part as much code as possible. So if you create some method like `getField(int numColumn)` the get the field `numColumn` (for example using `line.split("\t")[numColumn]`) the script will be able to call it directly to extract fields from CSV lines. – Jacopofar Dec 03 '12 at 09:14
0

This would be a very fun project and I am pretty sure it can be done.

I use lightweight scripted objects in BeanShell for holding data as follows:

    myData(value, status) {
     return this;
    };

   listofitems = new java.util.List();
   for(entry : data) {
     listofitems.add(myData(entry.value, entry.status);
   }

With this in mind and assuming you parsed the line of the csv file and obtained the headers, you can build the scripted objected dynamically:

hdr1 = "value";
hdr2 = "status";
scriptedObjectStr = "myData(" + hdr1 + "," + hdr2 + ") { return this; }";

eval(scriptedObjectStr);


y = myData("Test", "Pending");
print(y.value); // Test
print(y.status); // Pending

Hopefully you can make something of this to accomplish what you are trying to do.

GaryMcM
  • 425
  • 3
  • 9