Summary
Based on the template need to generate a code, Output needs to be a Java/DotNet code
Analyses
Using the yeoman (https://yeoman.io/generators/). Created a Java ->
Hello World code generator.
Which takes the string as input and map that in the sysout.
Code
class HelloWorld {
public static void main(String args[]){
System.out.println("Hello World");
}
}
Yeoman template code
Template folder contains --> <className>Class.java.ejs
Input Args--> <className> , <message>
Input value --> HelloWorld, Welcome
class <%= className%>Class {
public static void main(String args[]){
System.out.println(<%= "message"%> );
}
}
Output:
HelloWorldClass.java
Based on the inputs, Values will map in the template and finally. I will get className.java class.
Expected
For the template based code generator Found "yeoman" framework uses development language as node.js. Its pretty good.
Like this any other template based code generator framework available in Python or node or js?
You should take a look at code generators based on template files.
For example Telosys which is based on the Velocity template engine.
It is quite simple and easy to use. And above all it can generate any type of language.
See templates examples for Java :
https://github.com/telosys-templates-v3/java-domain-T300
you can use ANTLR 4 along with StringTemplate or Velocity template to achieve this.
ANTLR would take care of your parsing part and you can use the generated string from ANTLR in your template for substitution.
Related
i am using PorterStemmer in java to get the base form of a verb, But i found a problem with the verbs "goes" and "gambles". Instead of stemming it to "go" and "gamble", it stems them to "goe" and "gambl". Is there a better tool that can handle verbs that ends with -es and -ed to retrieve the base form of a verb? P.S JAWS with wordnet java does that too.
Here is my code:
public class verb
{
public static void main(String[] args)
{
PorterStemmer ps = new PorterStemmer();
ps.setCurrent("gambles");
ps.stem();
System.out.println(ps.getCurrent());
}
}
Here is the output in console:
gambl
Take a few minutes to read this tutorial of Stanford NLP group
https://nlp.stanford.edu/IR-book/html/htmledition/stemming-and-lemmatization-1.html
You can find the stemmer actually is not working as what you may think. It is crude so it not always gives you a complete base form of verbs with the ending chopped off. In your case, since you are caring about getting a complete base form of a verb, lemmatization seems better for you.
I have a Java application that is required to do a bunch of stuff to a PowerPoint file, one of which involves invoking an existing PowerPoint macro and passing parameters to it. Here are some sample code:
public static void main (String[] args) {
createPptFile("test");
/* Do stuff */
/* Call macro here */
/* Do more stuff */
}
Sub SaySomething(something)
MsgBox(something)
End Sub
The Java application first creates a new test.pptm file, then at some point later the application is supposed to call SaySomething(something) while passing a value to it. How can I do this? What libraries, if any, would I need? Please provide some sample code to show how this can be done.
The application is to be implemented using Java 1.8, and will run on Windows 10 and using Microsoft Office 2013. Apache POI is being used to edit PowerPoint files at present, though using it is not mandatory.
I want to add a sentence to all new classes that I create in java. By default Java entry point comes like this:
public static void main(String[] args) {
}
and I want to achive something like this:
public static void main(String[] args) {
Scanner key = new Scanner(System.in);
}
Is this possible? Thank you!
Eclipse Java EE IDE for Web Developers.
Version: Mars Release (4.5.0)
Build id: 20150621-1200
You can create/edit/ any code template you want
For Eclipse go to
Window
Preferences
Java
Editor
Templates
More here:
http://help.eclipse.org/mars/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Freference%2Fpreferences%2Fjava%2Feditor%2Fref-preferences-templates.htm
another here :
http://help.eclipse.org/mars/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2FgettingStarted%2Fqs-EditorTemplates.htm
In Eclipse it is possible to edit built-in code templates (the one used by Eclipse e.g. for new class files) and create your own named snippets (for fast access to often-reused templates).
Code Templates
Go to Window > Preferences > Java > Code Style > Code Templates, then to Code > New Java files. Adapt this or some other template to suit your needs.
Templates
Usually it's better though to add a smaller template via Window > Preferences > Java > Editor > Templates and use it wherever you need. Smaller bricks are more reusable then hardcoded class templates. Those are applied with <type-beginning-of-template-name + ctrl + space.
I have a code generator, which takes a syntax tree and converts it into a source file (text).
Basically, it traverses through all nodes of the tree, maps the node to text and appends the resulting texts to a StringBuilder.
Now I want the node to text mappers to be implemented using Xtend like this:
public class NodeXMapper
{
private XtendRunner xtendRunner = ...;
public String map(final NodeX aNode)
{
return xtendRunner.runScript("def String map(NodeX aNode) {
''' «aNode.fieldX» - «aNode.fieldY» '''
}", aNode);
}
}
xtendRunner.runScript(String aScript, final Object... aParams) is a method, which passes the parameters aParams to Xtend script aScript and returns the result.
How can I implement that method?
Update 1: Here I found this piece of code, which seems to run Xtend code in Java:
// setup
XtendFacade f = XtendFacade.create("my::path::MyExtensionFile");
// use
f.call("sayHello",new Object[]{"World"});
But I can't find XtendFacade class in the Type hiearchy view of Eclipse.
The interpreter you found was for the old Xtend1 language, which is not what you are looking for.
The new Xtend you are referring to is compiled, so there is no interpreter.
However, you could build an interpreted expression language using Xbase. See the documentation and Github for an example on how to do that. Then you could run the interpreter of your expression language from Java.
I've got a program that currently has a mass of code that I would like to design away. This code takes a number of text files and passes it through an interestingly written interpreter to produce a plain text file report that goes on to other systems. In theory this allows a non-programmer to be able to modify the report without having to understand the inner workings of Java and the interpreter. In practice, any minor change likely necessitates going into the interpreter and tweaking it (and the domain specific language isn't exactly friendly even to other programmers).
I would love to redesign this code. As a primarily web programmer the first thing that came to mind when thinking of "non-programmer being able to modify the report ..." I replaced report with web page and said to myself "ah ha! Jsp." This would give me a nice What You See Is Almost What You Get approach for people along with taglibs and java scriptlets (as undesirable as the later may be) rather than awkwardly written DSL statements.
While it is possible to use jspc to compile a jsp into java (another part of the application runs ejbs on a jboss server so jspc isn't too far away), the boilerplate code that it uses tries to hook up the output to the pagecontext from the servletcontext. It would involve tricking the code into thinking it was running inside a web container (not an impossibility, but a kluge) and then removing the headers.
Is there a different templateing approach (or library) for java that could be used to print to a text file? Every one that I've looked at so far appears to either be optimized for web or tightly coupled to a particular application server (and designed for web work).
So you need a slim down version of JSP.
See if this one (JSTP) works for you
http://jstp.sourceforge.net/manual.html
Give Apache Velocity a try. It is incredibly simple and does not assume it is running in the context of a web application.
This is totally subjective, but I would argue it's syntax is easier for a non-programmer to understand than JSP and tag libraries.
If you want to be a real tread setter in your company, you could create a Grails application to do it and use Groovy templating (maybe in combination with the Quartz plugin for scheduling), it might be a bit of a hard sell if there is alot of existing code to be replaced but I love it...
http://groovy.codehaus.org/Groovy+Templates
If you want the safe bet, then (the also excellent) Velocity has to be it:
http://velocity.apache.org/
Probably you want to check Rythm template engine, with good performance (2 to 3 times faster than velocity) and elegant syntax (.net Razor like) and designed specifically to Java programmer.
Template, generate a string of user names separated by "," from a list of users
#args List<User> users
#for (User user: users) {
#user.getName() #user_sep
}
Template: if-else demo
#args User user
#if (user.isAdmin()) {
<div id="admin-panel">...</div>
} else {
<div id="user-panel">...</div>
}
Invoke template using template file
// pass render args by name
Map<String, Object> renderArgs = ...
String s = Rythm.render("/path/to/my/template.txt", renderArgs);
// or pass render arguments by position
String s = Rythm.render("/path/to/my/template.txt", "arg1", 2, true, ...);
Invoke template using inline text
User user = ...;
String s = Rythm.render("#args User user;Hello #user.getName()", user);
Invoke template with String interpolation mode
User user = ...;
String s = Rythm.render("Hello #name", user.getName());
ToString mode
public class Address {
public String unitNo;
public String streetNo;
...
public String toString() {
return Rythm.toString("#_.unitNo #_.streetNo #_.street, #_.suburb, #_.state, #_.postCode", this);
}
}
Auto ToString mode (follow apache commons lang's reflectionToStringBuilder, but faster than it)
public class Address {
public String unitNo;
public String streetNo;
...
public String toString() {
return Rythm.toString(this);
}
}
Document could be found at http://www.playframework.org/modules/rythm. Full demo app running on GAE: http://play-rythm-demo.appspot.com.
Note, the demo and doc are created for play-rythm plugin for Play!Framework, but most of the content also apply to the pure rythm template engine.
Source code:
Rythm template engine: https://github.com/greenlaw110/rythm/
Play Rythm Plugin: https://github.com/greenlaw110/play-rythm