java dyn parser - java

Im a novice java-programer, whos trying to create a small java app.
In the program im working on, I want to load the configurations from different ini-files.
The basic idea is, that I would have a library containing all the config files, the parser should read all of them and make configurations named after their filenames.
The parser should be created to work dynamicly, so it can read different types of configs.
example
House.ini
-> type0
-> id name height witdh length price_based_on_dimensions
-> id1 name1 height witdh length price_based_on_dimensions
These data should be saved to a config object named config.house. The tricky part is that a different config file, can have its type = type0 but with a different number of attributtes.
I realise that there is no simply solution to this, but any help and or guides to create a dynamic parser is welcome

I'm not really clear on the output you want to produce, but a Java INI parsing library might be a good place to start. For that, you should use ini4j.

Other then ini4j (which is a really good library) I personally prefer to use xml config files. To me they are easier to use and allow for easier configuration

Related

Syntax-Highlighting in Intellij - Custom Language

I am currently developing an Intellij-Plugin in Java for a custom language.
Is there any way, that I can easily and through my code register a file type and keywords for Syntax-Highlighting?
You can go into file > settings > editor > filetypes
Create new file name it however you like; say javascript custom
fill in the keyword you want highlighted by group (1-4)
** note to have it work it you will have to essentially recreate the entire list (which isn't to difficult via internet search for a keyword list)
Once your happy with the list simply add file name patterns on that file with a wild card *.js (this is the part that will use your custom file over the provided ** easy to go back/edit/delete your own file)
Here's an example I found online CSS Keyword list example

Efficient java library for text templating?

I've got a simple string coming in from a UI component as The device id is %{test}. Assume %{test} is a dynamic variable and the values for it are being assigned from the backend code. The final string should look like:
The device id is some text
----------------------------^ should be replaced with %{test} and appended to the whole string
I've read a bit and tried out some of the libraries which were pointed out here, such as Velocity and FreeMarker. But I'm quite unaware in terms of efficiency and performance on using those libraries.
Hope I could get some insights on this since I'm pretty new to this. Any help could be appreciated.
I suggest you to take a look at Arco Template Engine: It compiles the template in compile-time, producing a .java (or .class) file. And so, at run-time, the expansion is done very fast.
The templates should be coded in JSP format. Thus, all variables references must be written ${variable} (not %{variable}).
The only thing to take in account is that templates must be staticly generated (in order to be processed at compile-time).
(Read the FAQ and the examples).

Java JSON editor / visualizer

I wrote an application that takes a JSON file as configuration. Up to this point, I wrote the JSON config by hand. However, now I want to allow other users who are not familiar with JSON format to make their own configurations. There's only 3 types of objects that the configuration needs to store, but the user should be allowed to add multiple copies of these objects. I want to write a configuration application where the user can press a button such as "Add Type A" and an object of type A is populated with default values and visualized so that the user can select properties and edit them. I know how to write an application that does this, but I feel like I'm re-inventing the wheel. Does anyone know of an open-source Java library that I can use inside my config application to handle visualization and editing a JSON file? If I'm approaching this from completely the wrong direction, please let me know.
I'm tempted to say "don't fear to reinvent the wheel".
But the fact is that there a bunch solutions available :
How do I convert an object to JSON representation (and vice-versa)
Java representation of JSON Object
Javascript to Java using JSON
among them, GSON looks fine.
For my personal experience, Yaml turned out to be an appropriate solution.

Java - Writing, reading and modifying strings with parameters

So, I'm doing a project and now I have a question, so I would like your help :)
First, I already know how to write and read a .txt file, but I want something more than just x.hasNext().
I want to know how could write, read and modify a .txt file like .ini does. What? Simple (I think):
First, write a file like this:
[client1]
name=Bill Gates
nick=Uncle Bill
number=123456789
[client2]
name=Steve Jobs
nick=Stevie
number=987654321
And so many other parameters, just like above, but when I'm wanting to read a specific one (like name or nick of a certain "client") I can do it with easy (Ok, I know it will not be easy, but I think you understood :D)
So, if you already know what I want to learn, please, teach me :) If you don't, please explain me what you didn't understood :D
Thanks in advance for every helping
The format you describe is for a Windows .ini file, back from Windows 3.x days:
http://en.wikipedia.org/wiki/INI_file
Perhaps the closest thing to a "standard format" in Java is a "properties" file; typically in the format "name=value":
http://docs.oracle.com/javase/6/docs/api/java/util/Properties.html
If you were to write your own program and invent your own initialization file format, would not use an .ini file. Instead, I would recommend:
1) simple properties file (if possible)
... otherwise ...
2) an XML file (if you need multi-level, structured data)
However, if you want to read and write existing .ini files for an existing application, I would either:
1) write my own .ini parser (it isn't difficult)
... or ...
2) Download and run a library likke ini4j:
http://ini4j.sourceforge.net/
'Hope that helps!
The commons-configuration project from apache supports INI file formats:
HierarchicalINIConfiguration config = new HierarchicalINIConfiguration(file);
Set<String> sectionNames = config.getSections();
SubnodeConfiguration section = config.configurationAt(sectionName);
String value = section.getString(keyName);
Check out the javadocs, it's pretty easy to use.

Best way to save data in a Java application?

I'm trying to find the best way to save the state of a simple application.
From a DB point-of-view there are 4/5 tables with date fields and relationships off course.
Because the app is simple, and I want the user to have the option of moving the data around (usb pen, dropbox, etc), I wanted to put all data in a single file.
What is the best way/lib to do this?
XML usually is the best format for this (readability & openness), but I haven't found any great lib for this without doing SAX/DOM.
If you want to use XML, take a look at XStream for simple serialization of Java objects into XML. Here is "Two minute tutorial".
If you want something simple, standard Java Properties format can be also a way to store/load some small data.
consider using plain JAXB annotations that come with the JDK:
#XmlRootElement
private class Foo {
#XmlAttribute
private String text = "bar";
}
here's a blog-post of mine that gives more details on this simple usage of JAXB (it also mentiones a more "classy" JAXB-based approach -- in case you need better control over your XML schema, e.g. to guarantee backwards compatibility)
2 other options you might consider -
Hsqldb is a small sql db written in
java. More relevant for your
purposes, it can be configured to
simply write to a csv file as it's
data store, so you could conceivably
use it's text output as a portable
datastore and still use sql, if
that's what you prefer.
A second option might be to write the
datastore directly to a serialized
file either directly or through a
library like prevayler. Very good
performance and simple to implement,
cons are the fragility and opacity of
the format.
But if the data is small enough, xml is probably much less bother.
If you don't need to provide semantic meaning to your data then XML is probably a wrong choice. I would recommend using the fat-free alternative JSON, which is much more naturally built for data structures.

Categories

Resources