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?
Related
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.
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).
Is there an XML serialization framework for Java that does not use reflection, but instead generates static serialization code (Java source) from XSD ?
I've never seen anything that does exactly what you are asking for: generating serialization code from XSD. However, if you're not stuck with an existing XSD schema, Modello may satisfy your requirements.
Modello is used by Maven for parsing pom.xml and settings.xml files. It reads a .mdo file (like this description of the Maven project model), and can generate a Java object model; an XML Schema (XSD) file; and serialisation/de-serialisation code. The serialisation/deserialisation code can use one of a number of XML parser APIs (e.g. JDOM, StAX, etc.). The XML parser API used by Maven itself is xpp3.
Modello can also generate code to convert one version of the model to another. It can generate HTML documentation about your XML format.
If you have an existing XSD, it might be too much work to use modello. But, if you're creating your own XML format, it could be worth starting with modello and generating the XSD.
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!
The .NET Framework exposes a rather useful set of classes as part of the System.Xml.Schema namespace.
Among other things, classes such as XmlSchema and XmlSchemaElement provide a useful API for defining/writing and parsing/reading/traversing XML schema files.
Does an equivalent library exist for Java?
The tools to accomplish this are there, but I don't think there's anything specific like that in Java. What I would do is run JAXB on the XML Schema schema (http://www.w3.org/2001/XMLSchema), which will generate Java classes corresponding to the XSD constructs. Then you can use those to read/write XML schemas, and validate against the XML Schema schema.