0

For instance, I have such JSON:

{
  "extends": "core-range",
  "dependencies": [
    "paper-progress",
    "paper-input"
  ],
  "jsdoc": [
    {
      "description": "Fired when the slider's value changes.",
      "kind": "event",
      "name": "core-change",
      "longname": "event:core-change"
    },
    {
      "name": "snaps",
      "kind": "member",
      "longname": "snaps",
      "scope": "global"
    },
    {
      "name": "pin",
      "kind": "member",
      "longname": "pin",
      "scope": "global"
    },
    {
      "name": "disabled",
      "kind": "member",
      "longname": "disabled",
      "scope": "global"
    }
  ]
}

I need to generate such Java class:

import com.google.gwt.core.client.js.JsProperty;
import com.google.gwt.core.client.js.JsType;
import com.google.gwt.user.client.EventListener;

@JsType(prototype = "HTMLElement", isNative = true)
public interface PaperSlider extends HTMLElement , CoreRange {
  Class<?>[] dependencies = new Class<?>[]{PaperProgress.class, PaperInput.class};

  void addEventListener(String event, EventListener listener);

  @JsProperty PaperSlider snaps(boolean val);
  @JsProperty boolean snaps();

  @JsProperty PaperSlider pin(boolean val);
  @JsProperty boolean pin();

  @JsProperty PaperSlider disabled(boolean val);
  @JsProperty boolean disabled();
}

What's the best way to generate it? Probably it makes sense to use templates. The most difficult part is methods generation.

I didn't find any Node.js module which supports repetable templates.

BTW, I have many JSON files and I'm going to add this to my Gulp task for generation some source files.

1
  • Couldn't you just use a few mutually recursive functions to do that? Basically a kind of recursive descent parser. Commented Mar 15, 2015 at 0:28

1 Answer 1

3

There's a few ways to go about this. I would recommend doing it via grunt.

Install grunt globally:

npm install grunt-cli -g

Then install your local grunt (in your current directory):

npm install grunt

Create the template (class.tmpl):

import com.google.gwt.core.client.js.JsProperty;
import com.google.gwt.core.client.js.JsType;
import com.google.gwt.user.client.EventListener;

@JsType(prototype = "HTMLElement", isNative = true)
public interface PaperSlider extends HTMLElement , CoreRange {
  Class<<%= className %>>[] dependencies = new Class<<%= className %>>[]{PaperProgress.class, PaperInput.class};

  void addEventListener(String event, EventListener listener);

  <% _.forEach(methods,function(method){ %>
       <%= method.description && ("// " + method.description) %>
      @JsProperty PaperSlider <%= method.name %>(boolean val);
      @JsProperty boolean <%= method.name %>();
  <% }) %>

}

And finally create the Gruntfile.js:

// config is your JSON file
// Yes, you can require json files
var config = require("./config");

module.exports = function(grunt) {
    grunt.registerTask("makeClass",function() {
        var template = grunt.file.read("./class.tmpl");
        var fileData = grunt.template.process(template,{
            data: {
                methods: config.jsdoc,
                className: "JSClass"
            }
        });
        grunt.file.write("./class.java", fileData);
    })

    grunt.registerTask('default', ["makeClass"]);
};

Now simply run grunt.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for nice answer. I already use gulp in my project and I don't want to add another build tool.
I think, gulp-template should work in a similar way. But there are no documentation at all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.