Configuration file with custom settings? - java

I'm having difficulty attempting to explain what I mean but here I go..
I am looking for a configuration parser that does the following..
Allows me to configure REQUIRED settings and if they are not set then the program should not start.
Allow users to define as many 'custom settings' as they need. So for example, say you want to add some sort of redirect to the configuration, I'd like it to look like this.
redirect-1: 100->200
And that would hopefully 'redirect' 100 to 200. I would obviously build this logic into my program, but is there a library that will read sequential settings like redirect-1, redirect-2, etc. until it reaches the EOF? Hopefully I'm making sense.

I think what you are looking for is a configuration file parser that also provides a "schema validation" engine. Within the syntax of the schema validation language, you would specify which configuration variables are required (and their types), so the validation engine could verify that those variables are present and have values that comply with their types (for example, "42" is a valid integer, but "hello, world" is not).
XML has several competing schema validation languages, including XML Schema and RELAX NG. If you look on Amazon, you can find books on those.
JSON also has its own schema validation language.
The only other configuration language with its own schema validation language I can think of Config4*, which is one that I developed. If you want to read more about Config4*, then I suggest you read Chapters 2 and 3 of the Config4* Getting Started Guide (PDF and HTML) to get an overview of its syntax and API. Then skip to Chapter 9 to find full details of its schema validation language. The ignore rules (discussed in Section 9.2.6) can be used to specify that the schema validation engine should ignore some configuration variables/scopes.

Related

Auto-generating frontend and backend validation for Angular/Java

I hope my question is not too broad.
Our application has an Angular frontend and a Java backend and
we need both frontend and backened validation. At the moment, we manually code
validators in Typescript and Java that basically do the same thing.
Since that is not only tedious but also error-prone, we would like to define once (maybe in a JSON file)
which input fields have which constraints and ideally use a piece of software that auto-generates
Java and Typescript code based on that definition.
My question is: are there any open-source/commercial software solutions that take care of this?
It seems like a common enough problem, but I haven't come across
a proper solution yet. JSON schema seemed promising and since we're using swagger
anyway, I'd hoped that swagger-codegen would automatically take the supported JSON-schema keywords as constraints into account,
but I haven't figured out how it works. When I generate Angular code, constraints such as 'minimum' and 'maximum' are completely ignored,
and when I generate Java SpringBoot code, they only show up as annotations.
This is exactly what JSON Schema is designed for. Open API (swagger) modifies JSON Schema for it's own purposes, which doesn't include validation.
JSON Schema has validator implementations in most major languages allowing you to use the same schema to do the same validation on the front end and the back end. http://json-schema.org/implementations.html
Unfortunately, you likely won't be able to use the same schemas you use for your Open API service definition. I'm not aware of any validator implementations that support Open API's flavor of JSON Schema. I understand that Open API is trying to close the gap between their definition and JSON Schema, but a gap still exists.

XSD for testdata generation

I'm trying to generate flat file for test data using JAVA. Flat file has own mapping document which describes all fields of each line.
I was suggested to use XSD for mapping and I did some research on XSD. As I understood that XSD is for only validation of XML. In this case I have to randomly generate XML file based on XSD and convert it to txt or other format. Because as an output I need flat file, not XML.
It seems like using XSD i'm adding extra steps in creating the file as first create XML, validate with XSD and convert it expected format.
What would be your recommendation in my situation for following the mapping document?
Thanks in advance.
I have seen your kind of setup before. The reasons may be different than your particular scenario, but nonetheless it made sense. One thing to consider has to do with the skills and tools people have available and so whatever makes the job done quick and well, goes.
You seem to describe an offset based, "flat" data structure. In my case, people used COBOL copybooks which are very good at describing this. IBM Rational Developer had a built-in wizard which allows the creation of Java Data Bindings from a COBOL copybook. This is to say that within a minute one gets a Java class which can create a record for your flat file in no time (it comes with all the logic required to do the padding, etc.)
To get the data generated, there are tools capable of generating XML files which cover all constraints defined by an XSD (e.g. alternate content i.e. xsd:choice, enumerated values, etc.) Now, assuming you have a proper XSD describing your logical model of your flat file, one can get 10s, 100s, even 100K XML generated from an XSD spec. It takes a click, plus the time spent by the tool to create those files.
Next, to get the XML files in your generated Java class, and so avoid going through XSLT or whatever (many shops don't have the skills) it may be as simple as writing Java mapping code between a JAXB generated class and the one created above, or if matching is possible, simply annotate the generated class to support JAXB unmarshalling. This last step may take longer to code, but it would be trivial code any Java developer would know how to do it.
This could possibly give you a view into why someone may have recommended Java and XSD for this task. XSD is a modeling language with built in support for constraints, which may prove helpful in generating test data through combinatorial techniques.

swagger codegen keyword additions

I have to maintain a project that uses Swagger to codegen the api interface. Looking at the model.mustache, I see this:
{{#models}}
{{#model}}{{#description}}
/**
* {{description}}
**/{{/description}}
and a bunch of other keywords like enum, defaultValue, vars, package, etc. Where do these come from? I've been scouring the internet trying to find swagger documentation on this but I cannot find anything. Lots of the links that refer to swagger info on github return 404 pages. In the end, what I want to be able to do is to have a property exist in my actual .json file that swagger is using to generate the interfaces and models to conditionally generate code. I saw that there is the
{{#property}}{{/property}}
{{^property}}{{/property}}
notation for if/else statements, but it doesn’t work on any property of my own .json. For example, in my json I have an id field. So if I wanted to do a contrived example
{{#id}}System.out.println(“test test”);{{/id}}
won’t work because swagger does not recognize the id tag. So my question is, can I add these tags?
there are lots of tools to help out. If you see broken links, please submit issues to the swagger-codegen project.
As of 2.1.x-M1, swagger-codegen has a very helpful "debug" mode which lets you list all the possible values for your environment. You can invoke it as follows:
java -DdebugModels -jar {path-to}/swagger-codegen-cli.jar generate {opts}
The supported debug flags are:
-DdebugModels: outputs the variables for each model discovered by codegen
-DdebugOperations: outputs the variables for each operation
-DdebugSupportingFiles: shows supporting file data (anything but models or operations)
-DdebugSwagger: shows the parsed swagger representation
I do see that these are missing from the README. Will make sure they get added.

Internationalizing java software

For my internship i've been asked to do some research on software internationalization and the current practices and solutions.
I've done some research and have come to no viable solution. My project manager has asked that I ask on stackoverflow,
What are the current practices that you guys at your job do in order to internationalize your Java software?
EDIT
The following is a summary of my research in case any other person is interested in my findings:
As the software is written in Java, RessourceBundles are obviously used. RessourceBundles provide good key value lookup with fallback to default values if no specific translation for the current locale exists. ResourceBundles are also not limited to translation of text but to internationalization of, well, resources. For example, color or images mean differente things for different cultures.
While all that is nice, just purely using Java PropertiesResourceBundles fails to provide metadata for the translator and fails to handle plural forms.
GNU Gettext takes an alternate approche to internationalization. Messages are written in source code in english and then extracted and stored into a file. The extraction program searches for function calls and extracts the parameters. For example, tr("Hello, World!") the command line utility xgettext would search for occurences of the function "tr" and extract all string literals.
Java implementations of gettext exist, such as:
https://code.google.com/p/gettext-commons/
https://github.com/jhorstmann/i18n
What gettext provides that ResourceBundles don't is plural handling and context for translations.
Have a read of this trail as it should answer most of your questions.
For web applications we use the standard facilities offered by JavaEE. That essentially means passing a message bundle into a JSF page and then using mark up that looks like this #{msg.hello} in the page. "msg" is the name of the message bundle and "hello" is the key that will be used to look up the translated string.
The translations are all held in properties files which have a standardized format and naming convention. The process works in much the same way for client applications although I don't feel it's quite as smooth
As I understand it professional translators have software that will load properties files and assist them in producing the translations. Adding comments to your properties files is useful so the translators have some context when translating.
In addition to other answers I would suggest using some technique/software that can analyze/check that all localization resources in your project are in sync.
That usually should be done during build time, so you can find/catch errors earlier.
One of such tools that I personally use and would recommend is i18n-maven-plugin
Hope this helps.

Beginner's guide to writing grammar

The application I am working inputs lot of data from file import and updates the database column accordingly. I need to come up with a custom Rule engine that would process all the input values based on validation and perform transformation of data accordingly. E.x.
One of the fields in our application is Product Name. So one of the rules we need to implement is to convert Product name from lower case to upper case, if the input value from the file is in lower case. Similarly, there are many text/mathematical transformations that need to be done. For these reasons, we need to come up with custom rule engine where we define the rules for each attribute, parse them and then apply the rules.
I do know that ANTLR is one of the parser generators around for Java. I am seeking advice on following queries:
1> General information on working of a parser generator and best practices for implementing grammar.
2> Since I need to design this rule engine completely, can anyone point me to a sample rule engine out there that I can refer to? right from UI to database design. I am using GWT for UI, Java for core logic and oracle for database
3> Are there any other parser generators around for Java
4> Though I do want to follow the path of defining my own grammar and using parser generator to build this rule engine, is there any other approach I should consider?
You might want to consider just using JbossRules (formerly Drools) which is a Java based rules engine. Alternatively, a scripting engine may be another way to implement your rules (e.g. Apache Rhino (Javascript in Java)).
Writing your own in this situation seems like overkill, but it may allow you to provide better security guarantees if end users are going to be creating the rules / scripts.
EDIT to address questions in comments:
I suggest using an existing rules engine (ala JbossRules/Drools) instead of writing your own parser and grammar (for the rule component). Take a look here for instance: Drools.
For specialized logic that rules may need to use (db access or computation libraries) you should write a single Java API used by your rules (so that rules are not deeply accessing your other code since that can lead to bugs if/when you refactor). This advice applies regardless of which rules engine you use (your own or an existing one).
I assume that you already have the data format of your data input files solved and that you are only looking for a solution to the rule format and rule parsing.
There is JavaCC, which is a Parser generator and there is groovy for evaluating rules. If you are going to use a script engine or not depends on the grammar. If the rules can't be expressed in javascript, java, python, etc, and you want to write them in a new language, well then you have to use a parser generator. But you can always do anything you want inside methods that you create and then call them from the rules. The rules will be evaluated by the script engine.

Categories