Skip to content

Commit

Permalink
feat: add command line option for creating JSON grammars
Browse files Browse the repository at this point in the history
  • Loading branch information
danielpeintner committed Apr 1, 2019
1 parent a0b2576 commit d964770
Showing 1 changed file with 52 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*
*/

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -71,6 +72,8 @@

public class Grammars2JSON {

public static final PrintStream ps = System.out;

public static final boolean STATS_ON = true;

protected int statsCountTransitions = 0;
Expand Down Expand Up @@ -653,47 +656,55 @@ protected void printBuiltInDatatype(Writer w, int ind, Datatype dt,
}

public static void main(String[] args) throws Exception {
String xsd = null;
String grsOut = null;

// schema-for-json.xsd, see
// https://www.w3.org/TR/exi-for-json/schema-for-json.xsd
// xsd = "../exificient.js/grammars/schema-for-json.xsd";
// xsd = "../exificient.js/grammars/exi4json.xsd";
// notebook
// xsd = "../exificient.js/grammars/notebook.xsd";
// xsd = "../exificient.js/test/data/xml/any0.xsd";
xsd = "../exificient.js/test/data/xml/basic_rdf_query_v02.xsd";
// xsd =
// "D:\\Projects\\EXI\\EXIficient\\exificient.js\\test\\data\\xml\\unsignedInteger.xsd";
// xsd =
// "D:\\Projects\\EXI\\EXIficient\\exificient.js\\test\\data\\xml\\test1.xsd";

grsOut = xsd + ".grs";

XSDGrammarsBuilder grammarBuilder = XSDGrammarsBuilder.newInstance();

grammarBuilder.loadGrammars(xsd);

SchemaInformedGrammars grammarIn = grammarBuilder.toGrammars();

Grammars2JSON g2j = new Grammars2JSON();

/*
* Encode
*/
FileOutputStream fos = new FileOutputStream(grsOut);
g2j.toGrammarsJSON(grammarIn, fos);
fos.close();

/*
* STATS
*/
if (STATS_ON) {
System.out.println("Transitions: " + g2j.statsCountTransitions);
System.out.println("States: " + g2j.statsCountStates);
}
if (args == null || args.length == 0) {
// show help
ps.println("#########################################################################");
ps.println("### EXIficient - Grammars2JSON ###");
ps.println("### Command-Shell Options ###");
ps.println("#########################################################################");
ps.println(" " + "<xsd-input-file>");
} else {
String xsd = args[0];
String grsOut = null;

// schema-for-json.xsd, see
// https://www.w3.org/TR/exi-for-json/schema-for-json.xsd
// xsd = "../exificient.js/grammars/schema-for-json.xsd";
// xsd = "../exificient.js/grammars/exi4json.xsd";
// notebook
// xsd = "../exificient.js/grammars/notebook.xsd";
// xsd = "../exificient.js/test/data/xml/any0.xsd";
// xsd = "../exificient.js/test/data/xml/basic_rdf_query_v02.xsd";
// xsd =
// "D:\\Projects\\EXI\\EXIficient\\exificient.js\\test\\data\\xml\\unsignedInteger.xsd";
// xsd =
// "D:\\Projects\\EXI\\EXIficient\\exificient.js\\test\\data\\xml\\test1.xsd";

grsOut = xsd + ".grs";

XSDGrammarsBuilder grammarBuilder = XSDGrammarsBuilder.newInstance();

grammarBuilder.loadGrammars(xsd);

SchemaInformedGrammars grammarIn = grammarBuilder.toGrammars();

Grammars2JSON g2j = new Grammars2JSON();

/*
* Encode
*/
FileOutputStream fos = new FileOutputStream(grsOut);
g2j.toGrammarsJSON(grammarIn, fos);
fos.close();

/*
* STATS
*/
if (STATS_ON) {
System.out.println("Transitions: " + g2j.statsCountTransitions);
System.out.println("States: " + g2j.statsCountStates);
}
}
}

}

0 comments on commit d964770

Please sign in to comment.