Validating files having tree based structures - java

I am looking for a validator to validate tree structure based configuration files.
e.g.
a.student.name joe
a.student.class arts
Can you suggest any ideas on validating such config. So far I have searched and I could find validator for xml files only.

Unfortunately, schema validation for configuration files is rare outside of XML. The only other option I am aware of is Config4J (which I wrote).
If you visit the website, then you should scroll down to the bottom of the main web page to access the complete set of manuals (available in both PDF and HTML versions). I recommend you have a look at the following parts of the manuals to get an overview of Config4J and decide if it satisfies your needs for validation.
Chapters 2 and 3 of the "Getting Started Guide" provide an overview of the configuration syntax and the API. In particular, Section 3.10 provides a quick example of the schema language.
Chapter 9 of the "Getting Started Guide" provides a complete definition of the schema language.
Chapter 3 of the "Java API Guide" discusses the API for writing your own schema types to extend the functionality of the schema language.
Update: I discovered from the answer by kiran.kumar M that the Java and Ruby implementations of YAML have a schema validator called Kwalify.
Update: There is now a schema language for JSON.

Try www.yaml.org . Yaml supports tree structures.
Here is the list of few parsers,
jYaml, SnakeYaml , YamlBeans
Yaml is a file format and supports complex hirarchial structures. Most of the structural validations can be performed automatically by the parsers listed above. You may need addtional code to validate your business needs.
Also few online validators are also available
see :
http://yaml-online-parser.appspot.com/
http://instantyaml.appspot.com/
Also see https://stackoverflow.com/questions/450399/which-java-yaml-library-should-i-use
Validation https://stackoverflow.com/questions/287346/yaml-validation

If the structure is the same as a Java properties file, you can read properties from it. Then, you need to decide what you mean by "validate". If applicable, you probably have an easy way to solve your problem.

Without seeing any form of sample.
If you want to validate the structure of something, and semantics is not an issue you could use lex/yacc (read flex/bison).
Depending on the problem one could venture out and use ox after that. Basically starting to write a mini-compiler.

Related

Multiple language support in Spring project without using multiple message_language.properties files

I want to add different languages support in my Spring Web-MVC application without adding message_language.properties file for each language.
But I found message_language.properties file solution everywhere.
I searched deeply but I haven't got any solution for it.
please suggest me any solution....
Its not clear what are the reasons why you search for a different solution. Perhaps not what you want, but many projects that require dynamic language addition use database backed resource bundles (technically speaking its the solution without the properties file, but essentially the approach is the same).
If this is what you want you can check out the blog http://www.webreference.com/programming/Globalize-Web-Applications15_Java_ResourceBundles/index.html.
The following stackoverflow could be helpful as well
Database backed i18n for java web-app

Use Spring-Data-Rest to return response as XML instead of JSON

Spring-data-rest is currently RC1 (heading for GA July 16), but the documentation is still a little sketchy. So far all the example code I find shows responses defaulting to JSON, but I need XML, and ideally either XML or JSON based on ACCEPT header. I found one source in some comments in a DZone link that indicate XML is going to be supported. But that was posted during the M2 release, before RC1. I don't see anything in the Issues under the project either.
So does anybody know how to make either RC1 (or SNAPSHOT) produce XML instead of or in addition to JSON.
I wish there was an easy answer to producing XML in Spring Data REST, but I haven't found one yet. We defaulted to using JSON because we figured that's a super easy and lightweight "protocol" for transmitting objects and will work good enough for most cases.
There are a couple of problems with XML that we haven't found reasonable answers for:
How do I represent an object in XML? Do I use the property name as the element name or do I use a standard element name and put the property name in an attribute?
Where do I identify the type of the property (whether it's complex or simple, a Long, a BigInteger or what have you)?
Do I dispense with all the custom mapping information and just use JAXB or Spring OXM?
What do I do about links? Do I use the Atom namespace link element?
If I'm using Atom already, then why not use an Atom representation for everything?
Since answering these questions will necessarily involve more community input than we've had yet since the project is so new, I was thinking we'd wait until a post-1.0 release to add XML support. Priorities could obviously change if there's enough momentum in that direction, but I just don't see it happening in the next week.
There is some machinery you can override (this is just a Spring MVC application, after all) to produce any kind of a response you want. If you use the latest snapshot and override the RepositoryRestMvcConfiguration.contentNegotiatingViewResolver() method, you can provide an entirely different representation of the DTO coming out of the exporter. This is referred to in the wiki. You'll have to check the source code of JsonView to get the necessary details on what the DTO looks like internally, but it would be relatively easy to replace JsonView with MyOwnXmlView.
NOTE: This will only work for the current version of the REST exporter. The GA version will have different machinery to render output. I'll be providing hooks for doing whatever types of output rendering one wants to do, though, so you should be able to override output rendering by setting a property on the configuration. If you create a View subclass for this version, it will likely only be a matter of changing it to an HttpMessageConverter for the GA version.
Well with latest Spring IO Platform we can achieve this and with IO Platform we
dont need to worry about version also.
Have posted how to achieve in another stack overflow link as below :
Spring Boot REST with XML Support

what is yaml file in java and use of it?

i saw some site like this http://jyaml.sourceforge.net/ for yaml in java.
but i can't to use of that.
how can i use form yaml files?
if is it possible to use it in javafx 2.0?
thanks.
What is YAML
You should see the Wikipedia page for YAML at least. The official YAML website defines it as
[...] a human friendly data serialization
standard for all programming languages.
Use with Java
It depends on what you want to use it for - the most common use (I'd imagine, since I haven't used it myself) would be for storing application configuration, as an alternative to XML or JSON. Essentially, you'll have a simple text file that contains data in a structured format as defined by the YAML spec. Here is an article that discusses the use of YAML with Java.
To avoid reinventing the wheel, you should make use of a library that performs the serialization and deserialization for you i.e. it can read from and write to the text file and parse the data in it and hand it over to your application in an easier to use object form. The business logic, of course, must be written by you. There are several Java libraries that are available and this question on SO talks about which one to use and why: https://stackoverflow.com/questions/450399/which-java-yaml-library-should-i-use.
Yaml is a file format*, and jYaml is a Java library for working with that file format.
So you may use it to read or write information into this format.
How can i use form yaml files?
You write one, and use it with this library.
If is it possible to use it in javafx 2.0?
Can you use this library in JavaFX 2.0? If you can then yes. :)
* See comment

java annotations: library to override annotations with xml files

Java has annotations and that is good. However, some developers feel that it is best to annotate code with metadata using xml files - others prefer annotations but would use metadata to override annotations in source code.
I am writing a Java framework that uses annotations. The question is: is there a standard way to define and parse metadata from xml files. I think this is something every framework that uses annotations could benefit from but I can seem to find something like this on the Internet.
Must I roll my own xml parsing/validation or has someone already done something like this?
There is not a standard way, but here are some Java frameworks who does it:
JPA - check ejb-3_0-fr-spec-persistence.pdf
Spring Framework
TestNG - as written above, though I think it focuses to much on the annotation side rather than the actual configuration he tries to achieve
Seam Framework
I wrote the Annox library which does exactly what you need. With Annox you can read arbitrary annotations from XML.
It's not exactly what you want, but the backport175 project has an implementation of annotations for Java versions before Java 5.
It has some of the functionality you search in that it will read both its own style implementations and "real" annotations if they are present. Maybe this can be used as a starting point to build a more general framework.
Use JAXB http://java.sun.com/developer/technicalArticles/WebServices/jaxb/
You would write the xsd for your metadata file, generate JAXB classes that can help you parse the xml files.

Java configuration framework [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I'm in the process of weeding out all hardcoded values in a Java library and was wondering what framework would be the best (in terms of zero- or close-to-zero configuration) to handle run-time configuration? I would prefer XML-based configuration files, but it's not essential.
Please do only reply if you have practical experience with a framework. I'm not looking for examples, but experience...
Apache Commons Configuration works great. It supports having the configuration stored in a wide range of formats on the backend including properties, XML, JNDI, and more. It is easy to use and to extend. To get the most flexibility out of it use a factory to get the configuration and just use the Configuration interface after that.
Two feature of Commons Configuration that differentiate it over a straight Properties file is that it support automatic conversion to common types (int, float, String arrays) and it supports property substitution:
server.host=myHost
server.url=http://${server.host}/somePath
If your hardcoded values are just simple key-value pairs, you should look at java.util.Properties. It's a lot simpler than xml, easier to use, and mind-numbingly trivial to implement.
If you are working with Java and the data you are storing or retrieving from disk is modeled as a key value pair (which it sounds like it is in your case), then I really can't imagine a better solution.
I have used properties files for simple configuration of small packages in a bigger project, and as a more global configuration for a whole project, and I have never had problems with it.
Of course this has the huge benefit of not requiring any 3rd party libraries to utilize.
Here are various options:
java.util.Properties
java.util.prefs.Preferences (since Java 5)
Commons Configuration
jConfig
JFig
Carbon's Configuration Service
You might want to read Comparison of Commons Configuration With JFig and JConfig and Configuring your Applications using JFig for some feedback from various users.
Personally, I've used jConfig and it was a good experience.
Commons Configuration
We're using this. Properties files alone are much easier to handle, but if you need to represent more complex data commons configuration can do this and read your properties files as well.
If you aren't doing anything complicated I'd stick to properites files.
If you want to do something advanced (and typesafe), you might want to take a look at this: http://www.ibm.com/developerworks/java/library/j-configint/index.html
the Intelligent Parameter Utilization Tool (InPUT, page) allows to externalize almost any (hard coded) decision as a parameter into an XML based configuration file. It has been initiated in early 2012 as a response to the perceived deficiencies in existing configuration tools with respect to generality, and separation of concerns.
InPUT is probably more powerful than most use cases require, as it allows for the programming language independent formulation of experimental data (input - output), with features such as the definition of complex descriptor to class mappings, or randomized configuration spawning and validation based on predefined value ranges (for test and research, e.g. Monte Carlo simulations). You can define parameters with sub parameters, relative restrictions on parameter values (numerical param a > param b) etc. .
Its still in beta, but rather stable, I use it for my research, for the configuration and documentation of experiments, and for teaching purposes. Once it is available for other languages (C++ adapter in the pipe), other researchers/practitioners can reuse the descriptors running their implementations of the same algorithms in C++ (using the code mapping concept). That way, experimental results can be validated/programs can be migrated more easily. The documentation is still in working process, but a couple of examples are available on the page. InPUT is open source software.
For those interested, the Conceptual Research Paper.
I tend to use java.util.Properties (or similar classes in other languages and frameworks) wrapped in an application-specific configuration class most of the time, but I am very interested in alternatives or variations on this. Especially since things can become a bit tricky if graphical configuration dialogs or multiple views on the configuration data is involved.
Unfortunately I don't have any experience with specific libraries for Java (except with the ones I have written myself), but any pointers would be appreciated.
Update
OK. That wasn't entirely true, three is the Spring Java Configuration Project.
I wrote about this a couple of weeks ago and came to the conclusion that XML is one of the most widely used notations.
Is it the best? I don't think so, I really like JSON, but the tooling is still not up to XML so I guess we have to wait and see.
You can try YamlBeans. This way you write whatever classes you want to hold your config data, then you can automatically write and read them to and from YAML.
YAML is a human readable data format. It has more expressive power than java.util.Properties. You can have lists, maps, anchors, typed data, etc.
Please take a look at this URL:
http://issues.apache.org/jira/browse/CONFIGURATION-394
The Configuration framework which we're looking for it is something on top of Apache Commons Configuration and must support Concurrency Issues, JMX issues and most of stores(e.g .properties file, .xml files or PreferencesAPI).
What weblogic team provides on 'Administration Console' is intersting which through it you can have transactional(atomic) updates on configurations so that are registered listeners be notified.
The Apache guys insist that this project is out of scopes of Commons Configuration, maybe!
I've attached a simple configuration framework, take look please.
I just posted a brief bit of code about using Spring's ClassPathResource as an alternative to IoC. ClassPathResource permits you to place property files anywhere on the classpath (e.g., all in one place, or as peers to the code they configure. My example just uses java.util.Properties, so you can use the plaintext "name=value" style or its XML format.
Properties files a very simple, if you need something more functional, you could format some of your configuration files as Java classes. These can be placed in a different package/module and can be pre-compiled or loaded at runtime with a library like BeanShell.
Note: In the simplest case (pre-compiled) you don't need any additional libraries.
Regarding the suggestions to use java.util.Properties - starting in jdk 1.5, the Preferences API (java.util.prefs) appears to be the preferred alternative to using the Properties API.
Reasons: increased scalability, back-end neutrality, ect.
You could have a look at newly announced tools4j-config whose mission statement is to allow you to easily handle configuration at runtime.

Categories

Resources