I would like to store all the gherkin feature files created by a user on the front end as GherkinDocuments on the back end using the gherkin parser. Once saved, I would also like to be able to display the raw gherkin document on the front end. I have read through the documentation and cannot find anything built-in that converts the GherkinDocument back to a raw text. The toString() method is also not overloaded to print out. Is there a way to convert a GherkinDocument object to raw text within the gherkin parser?
I want to be able to keep as much of the original formatting as possible. Normally I would just write my own utility to perform this, however the structure of the GherkinDocument object renders it tedious. I would prefer to use existing capabilities if they exist.
I talked to Aslak, Cucumber developer, on the cucumber help gitter. He told me:
Hi #tramstheman have you considered storing it as text instead of serialising the GherkinDocument AST? It is very quick to parse that text back into an AST when you need to.
There isn't currently a renderer/prettifier that will turn an AST back to source as #mattwynne suggested. The tests don't do roundtrips, they just perform approval testing on various outputs (parser tokens, ASTs as JSON, pickles as JSON)
What I have done instead is extended the GherkinDocument object and set it to store the raw text inside it, as similarly suggested by Aslak.
What about reading the feature files as is and display them? They are available in your test class path. Move them to your production class path and they will be possible to read from any class, test or production. This will allow you to open a stream for each file and display it without any modification.
Related
I have the following csv file (In production the number of records can range from 20k-100k and has many fields )
id,firstname,lastname,email,profession
100,Betta,Wandie,Betta.Wandie#gmail.com,developer
101,Janey,Firmin,Janey.Firmin#gmail.com,doctor
I need to convert this to json and do further processing.
CSV->JSON->PROCESS FURTHER
.I am able to convert it to JSON directly using the code given here
directly convert CSV file to JSON file using the Jackson library
But i want do validations for json like if lastname has null value then ignore that record or id is missing then ignore that record.
How can i handle the validation?I am using Java 8 and spring boot latest version
I have done something similar by using JavaScript (Nashorn). Yes, that is nasty, but it works, and it is astonishingly fast!
Unfortunately, I do not have the source code at hand …
Why I did it that way had the same reasons as #chrylis-on strike implies in their comment: the validation is much easier if you have an object for each JSON record. But as I was lazy, and there was definitely no need for the Java object I had to create for this, I had the idea with the JavaScript script inside my Java program.
Basically, a JSON String is the source for a JavaScript program; you can assign it directly to a JavaScript variable and then you can access the records as array elements and the fields by their name. So your JavaScript code walks through the records and drops those that do not match your validation rules.
Now the Java part: the keyword here is JSR223; it is an API that allows you to execute scripts inside your Java environment. In your case, you have to provide the converted JSON in the context, then you start the script that writes the modified JSON back to the context.
If the converted JSON is too large, you can even use the same technique to check record by record; if you compile the script, it is nearly as fast as native Java.
You can even omit Jackson and let JavaScript do the conversion …
Sorry that I cannot provide code samples; I will try to get hold on them and add them if I get them.
I want to parse an HTML file and read its content, organize it and then use it in MATLAB
Since I have a background in JAVA and using Jsoup to parse the HTML files, I decided to go that way and parse the HTML file from JAVA and then send the results to MATLAB.
The problem is that my result will be an object, that I will create it, called "seizureList", and has the following entries : classification, onset, pattern, vigilance, and origin.
How I'm supposed to convert this object from JAVA to MATLAB?
A simple but working solution would be to write the result to a file from JAVA, And then read that file in MATLAB and parse it, but I want a more efficient way.
Note that I've gone through the other questions related to this, but they are only dealing with a string return or simple stuff, not a user defined object.
Any help is appreciated.
You can run Java directly from within Matlab. Then the resulting Java object would appear as a variable in the Matlab workspace, and you can access its public fields, call its methods, etc.
For more informaton, have a look at Call Java Libraries in the Matlab documentation.
I've already parsed javascript source using Rhino and reconstructed it successfully.
and when I call astroot.toSource(), it shows to me reconstructed source well.
but .toSource() method can't prints Comments.
using .toSource() method, all my javascript source's comments are disappear.
so, How can I get the full source including comments?
My goal is write AstRoot Object(contain source) to a new javascript file that including full comments.
I'm using Rhino 1.7R4
In general, this is difficult because comments can appear in the middle of any decl, state ment or expression. So how to represent that fact in the various AST objects? It could be done but is very messy for parser and the AST objects it creates.
If you restrict yourself to only allowing comments on statement boundaries there are some possible solutions.
One way would be to write your own javascript tokenizer and inspect the stream while reading the file. Then you would need to figure out how to track them. One hackish way would be to transform them into 'var somexXXxx = "comment";' and use a naming convention to transform them back after ast.toSource() call. That would map your comments into the AST node structure.
I am going to use a template engine in Java (probably FreeMarker). Now I wonder how to write a unit test for it.
I can prepare an expected output as a text file but I will have to change it manually whenever I change the template. I would like to parse the output using the template to get the model data and it with the original model. Can I do that?
Depends on what your template will output. Of course if it produced a well formed format like XML or JSON you could easily parse it so I'm left to assume it's probably HTML. You might be able to use JTidy to parse it into a DOM model that can be used from Java.
http://jtidy.sourceforge.net/
Another option might be to use good old fashion grep routine, then build primitives like assertContains, assertDoesNotContain, etc. I have a JSON library I write unit tests for and I took that route to just do old fashion grep, and it's worked well in that it catches bugs. I worried about parsing the JSON code directly because I'd be using the code I wrote in my tests to test that very code. Your case might be different so parsing to a DOM model might make more sense.
The big thing, to consider, is you're testing the view. So as things change visually you might have a very hard time keeping your tests up to date. The view gets changed a lot in most programs because your client, PM, person in control of what you're doing, changes their mind frequently.
I have to convert a .log file into a nice and pretty HTML file with tables. Right now I just want to get the HTML header down. My current method is to println to file every single line of the HTML file. for example
p.println("<html>");
p.println("<script>");
etc. there has to be a simpler way right?
How about using a JSP scriplet and JSTL?, you could create some custom object which holds all the important information and display it formatted using the Expression Language.
Printing raw HTML text as strings is probably the "easiest" (most straightforward) way to do what you're asking but it has its drawbacks (e.g. properly escaping the content text).
You could use the DOM (e.g. Document et al) interface provided by Java but that would hardly be "easy". Perhaps there are "DOM builder" type tools/libraries for Java that would simplify this task for you; I suggest looking at dom4j.
Look at this Java HTML Generator library (easy to use). It should make generating the actual HTML muuuch clearer. There are complications when creating HTML with Java Strings (what happens if you want to change something like a rowspan?) that can be avoided with this library. Especially when dealing with tables.
There are many templating engines available. Have a look at https://stackoverflow.com/questions/174204/suggestions-for-a-java-based-templating-engine
This way you can define a template in a txt file and have the java code fill in the variables.