0

I have an XML file, which contains rules for code analyzer (to search vulnerabilities). So, it has very different rules like parameter, parameter count, any parameter type, const value and etc for method call (to detect specific calls), rules to detect some imports, inheritance and so on. But how to store this inside my program? I found two ways:

  1. Parse xml while scanning (to internal representation)
  2. Create classes for each element: parameter, parameter value, number of parameters

Is there a real life example of implementing this thing? Or you may just say the best/common way to do this

ivan
  • 287
  • 3
  • 14
  • Feel free to ask something and comment, I will alter the question – ivan Jun 02 '15 at 15:01
  • What would be the reason to keep it in the code? In this case you can't move between environments dev/test/prod - you will need to change code. Keep config in xml file in the same repository and use it as resource. – Alex Jun 02 '15 at 15:16

3 Answers3

0

Keeping in XML file will probably be the easiest method

GregH
  • 5,125
  • 8
  • 55
  • 109
  • But it will require me to write more duplicate code. Just imagine: different rules for different tasks. I also want to add Conditions which will contain logical operators – ivan Jun 02 '15 at 15:22
  • XML is the most common way I know of to store configs. The conditions will most likely be handled in java code. You may have to write more duplicate code but following a common methodology you are not killing how maintainable your code is – GregH Jun 02 '15 at 15:39
  • You are right, but I was planning to make a kind of AST (tokens are my final rules which are not NOT, OR, AND logical operators), isn't that so good? – ivan Jun 02 '15 at 16:09
  • can you not read from the xml file and create your AST off of that? Then you can reuse any applicable trees to reduce duplicated code – GregH Jun 02 '15 at 16:13
  • DId this help? Any more questions? If yes, I'd love to try to answer them if not please accept – GregH Jun 02 '15 at 16:35
  • Yes. Having AST means I will have internal representation, this is not the same with parsing XML – ivan Jun 02 '15 at 16:55
0

If you like to keep config in your code - use code to do so. Here is good example of simple initialization:

Map<String, Integer> config = new HashMap<String, Integer>()
{{
     put("One", 1);
     put("Two", 2);
     put("Three", 3);
}};
Community
  • 1
  • 1
Alex
  • 4,457
  • 2
  • 20
  • 59
  • But what is that? How HashMap can help me with storing configs? – ivan Jun 02 '15 at 15:24
  • You didn't provide any example. I assumed it's simple key=value pairs. If it is not then xml is the only option. And I'll quote myself " Keep config in xml file in the same repository and use it as resource" – Alex Jun 02 '15 at 17:57
0

To this cases i love to use properties files

.properties is a file extension for files mainly used in Java related technologies to store the configurable parameters of an application. They can also be used for storing strings for Internationalization and localization; these are known as Property Resource Bundles.

Suppose you have a file in your package path.to.your with a properties file file.properties you can include them in you jar and recover the file with getClass().getResourceAsStream("/path/to/your/file.properties") then you can load the bundle, change the params, add new keys, etc, more info with MKYong - Java Properties file examples

rekiem87
  • 1,565
  • 18
  • 33