开发者

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.

  • Are there any free Java libraries that can assist in parsing custom scripting(formatting?) languages?
  • Since I already have the XML, would it be a better approach to just 'convert' this with a search & replace algorithm?

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 th开发者_StackOverflow社区e output, asking them to implement a different parser is not an option.


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


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");


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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜