Sam
Sam

Reputation: 3659

Simplest way to allow users to specify output format

I have written an application which outputs data as XML. However, it would be nice to allow the user to completely customize the output format so they can more easily integrate it into their applications.

What would be the best way to approach this problem? My initial thoughts are to define a grammar and write a parser from the ground up.

I should specify here that 'users' are other programmers so defining a simple language would be fine, and that the output is potentially recursive (imagine outputting the contents of a directory to XML).

Just looking for general advice in this area before I set off down the wrong track.

EDIT: To clarify... My situation is a bit unique. The application outputs coordinates and other data to be loaded into a game engine. Everybody seems to use a different, completely custom format in their own engine. Most people do not want to implement a JSON parser and would rather use what they already have working. In other words, it is in the interests of my users to have full control over the output, asking them to implement a different parser is not an option.

Upvotes: 0

Views: 96

Answers (3)

nsfyn55
nsfyn55

Reputation: 15363

Have you considered just using a templating engine like Velocity or FreeMarker.

Upvotes: 1

dmcnelis
dmcnelis

Reputation: 2923

If you are planning to provide this as an API to multiple parties, I would advise against allowing over-customization, it will add unnecessary complexity to your product and provide just one more place for bugs to be introduced.

Second, it will increase the complexity of your documentation and as a side affect likely cause your documentation to fall out of sync with the api in general.

The biggest thing I would suggest considering, in terms of making your stream easier to digest, is making the output available in JSON format, which just about every modern language has good support for (I use Gson for Java, myself).

Upvotes: 0

user425367
user425367

Reputation:

I would have created a result bean as a POJO.

Then I would have different classes working on the result bean. That way you can easily extend with new formats if needed.

E.g

Result result = logic.getResult();
XMLOutputter.output(result, "myXMLFile.xml");
Format1Outputter.output(result, "myFormat1File.fo1");
Format2Outputter.output(result, "myFormat2File.fo2");

Upvotes: 0

Related Questions