Generate code from properties file - java

Coming from C# background, I'm used to creating a resource file Resources.resx in Visual Studio. This generates a class in the background and I can access resoures by Resources.ResourceName, which has some benefits
it provides auto completion ("Intellisense")
it generates a compiler error if a resource is renamed (instead of a runtime error)
I can use "Find usages" to check where the same resource is used again
AFAIK, the equivalent to a RESX file in Java is a Properties file. I can access a resource using ResourceBundle.getBundle("filename").getString("resourcename");. As you can see, the filename and the resource name are strings and
I don't have auto completion
I get runtime errors instead of compiler errors
I need to do a generic text search to find other usages which is not very reliable
I strongly doubt that I'm the first person who wants all the additional benefits of generated code, so
How can I generate classes from properties files in Eclipse and use it like in C#?

you can use jlibs library. see tutorial Internationalization made easier. BTW I am the owner of this project;
this library provides compile time safety and uses annotations at compile time to generate java code

Civilian framework contains a generator tool to manage resource bundles and generate a class which contains constants for all entries in the resource bundle.
The steps to use it are the following:
You maintain a Excel file, first column contains the resource key, next columns translations in the target languages. Such a Excel can easily be given to translators.
Whenever you have changed this Excel file you run the ResBundleCompiler (a simple command-line tool). The compiler reads the excel file and generates a ResourceBundle properties file for every language and a class which defines constants for every resource key
In your application you use the constants instead of a literal strings to refer to resource keys, giving all the advantages you described.

What you need to use is an XML format and then use JAXB in JDK to create strongly typed object out of it. Specifically look at javax.xml.bind.JAXB
To see an example of how to generate classes directly from XML Schema read this: http://www.javaworld.com/article/2071784/enterprise-java/java-xml-mapping-made-easy-with-jaxb-2-0.html
The JAXB binding compiler lets you generate Java classes from a given XML schema. The JAXB binding compiler transforms an XML schema into a collection of Java classes that match the structure described in the XML schema. These classes are annotated with special JAXB annotations, which provide the runtime framework with the mappings it needs to process the corresponding XML documents.
The binding runtime framework provides an efficient and easy-to-use mechanism for unmarshalling (or reading) and marshalling (or writing) XML documents. It lets you transform an XML document into a hierarchy of Java objects (unmarshalling) or, inversely, transform a Java object hierarchy into XML format (marshalling).

Related

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.

How to generate XMI file for the java source code?

I'm writing a program in java language which takes the java source code as an input and the output is xmi file which contains the class diagram of the source code.
I have researched for xmi structure but I was not able to find an adequate resource which explain the structure and order of tags.
First question is how can I found out what is the order of the tags and definition of each tag? for instance, what is the meaning of "isActive = false" etc?
Secondly, is there any suggestion of libraries or anything in java language which can help me to finish this project?
I think the tag order doesn't matter in XMI. In general, XMI just defines how to map MOF to XML. So basically you build the M2 model (=UML) from Java and and then map this to XML with XMI if I understand this correctly (and I think the double indirection is why you don't find good resources for the mapping from UML to XMI directly).
For many people, examples may be more accessible than the chain of transformation rules defined by the various OMG standards. So I'd just draw a minimal UML example diagram in my favorite UML tool for the export aspect I am interested in and look at the exported XMI. Then add features as needed...
P.S.: You may want to use the Java reflection classes (package java.lang.reflect) instead of parsing the the Java source yourself to generate XMI (if you don't need to preserve method argument names).

Programatic generation of xml from a xsd that uses other xsds

I have a xsd that in turn uses/imports a set of xsds.
I would like to programtically generate sample xml from the xsd. The xml must contain all elements and attributes populated with example data based on data type.
How can I do this using eclipse api classes?
Also are there any other tools that accomplish this task and can be evoked in a java program or a batch file?
Any pointers to examples/documentation/api is highly appreciated.
Thanks in advance.
if I am reading your question correctly, I believe what you are trying to do is programmatically generate (i.e. using Java) XML documents based on an XML Schema Document (which may in turn import other supporting XSD's).
You may wish to have a look at Oracle/Sun's JAXB (Java Architecture for Xml Binding) which you can find more info about here:
http://jaxb.java.net/
JAXB works with J2SE-SDK and/or IDEs - such as Netbeans or Eclipse, and permits you to unmarshall (read XML documents into mapped Java Objects) or marshall (write Java objects as XML documents) as required. Standard mappings (known as binding declarations) are provided based on valid XML Schema provided to JAXB. You can also provide binding declarations through custom annotations directly within your XML Schema files or by using external JAXB declarations.
Another alternative (similar to JAXB) is Apache's XML-Beans.
Hope this helps!

Generate xsd schema from java source

Given a few hundred java source files how can I generate an xsd schema that describes their class hierarchy, attributes and methods?
Most of the resources I've seen describe how to convert java to xml given the schema but how is that schema created? I did find Simple but that requires me to alter the source which I am not allowed to do.
Apache axis provides the java2wsdl tool. Sure, you didn't ask for a wsdl but this tool should generate schemas (as needed) too.
So it's worth trying: implement some dummy interfaces with methods, that just use the types from your 'hundreds' of files, generate a wsdl and delete everything but the schemas.
XSD schemas in themselves are recipies allowing a program to say whether a given XML document "conforms"/"does not conform" to the schema.
For this to make sense for Java classes, you have to define a conversion scheme from Java source to XML, and that XML format can then have an XSD, which may or may not be easily derivable programatically.
What Java -> XML mapping do you use?

Can I use XText for a DSL involving an XML file type?

I have defined a small DSL that is mostly written in the form of different types of XML files in conjuction with some property files. This works very well but I wish to create an Eclipse Editor to make editing these files easier for beginners (I already have a working parser).
The main XML file can reference some items from the .properties files and vice-versa. THe main xml file can also reference other XML files. Certain options should only be available in the main xml file based on the contents of the .properties files and based on some osgi plugins that can be added to the DSL project (the syntax is dynamic depending on context). The structure of the language is fixed but the options available in each attribute or the choice of attributes themselves changes depending on metadata contained in plugin .jar files.
Questions:
Does XText support dynamic syntax (validation changes depending on external factors)?
Does XText support XML files / .properties files?
Thank you very much for your help in advance.
Please read the Xtext documentation.
Does XText support dynamic syntax (validation changes depending on external factors)?
Xtext automatically validates the syntax using the grammar definition. Additionally, you can write arbitrary validations in Java or Check.
Does XText support XML files / .properties files?
If you define a grammar for them, of course. ;) Neither of them are too difficult.
I don't completely understand what you are after, but if you have an XSD, have you considering turning that into an Ecore model, and then building a Xtext grammar for that? Or if you are starting from scratch, build your DSL Grammar first, then export the ecore as XSD, and use helpers like I've just posted in my http://blog2.vorburger.ch/2012/07/xtext-xml.html ? HTH.
If you don't have to have XML / XSD, and what you are after in essence really is more having a readable (say JSON-like, but strongly typed?) "generic" DSL for data structures constrained by some sort of "meta model" (say expressed in XCore, which is a DSL for an EMF Ecore model, which you can import from a XSD..), then maybe my EMF Simple Object Notation ESON (ex-EFactory) at https://github.com/vorburger/efactory is of interest to you?

Categories