Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to parse XML if format in below manner? #11

Closed
zszazi opened this issue Feb 13, 2020 · 7 comments
Closed

How to parse XML if format in below manner? #11

zszazi opened this issue Feb 13, 2020 · 7 comments

Comments

@zszazi
Copy link

zszazi commented Feb 13, 2020

@mfatihercik

Consider I have my xml file in below format (its a partial file)

<package name="com/example/configure">
		<class name="com/example/configure/AutoConfigure"
			sourcefilename="AutoConfigure.java">
			<method name="tryItOut;" desc="()V" line="8">
				<counter type="INSTRUCTION" missed="3" covered="0" />
		        </method>
			<method name="sampleController"
				desc="()Lcom/example/controller/SampleController;" line="13">
				<counter type="INSTRUCTION" missed="4" covered="23" />
			</method>
                 </class>
</package>
<package name = "something>
                <class name = 
                       ...

I have to loop through multiple packages which can have multiple class names and which in-turn can have multiple methods inside the class

using DSM how should I capture the package name , class name and method name only if covered !=0
and filter the XML document and convert it to JSON output format

PS:I am new to java and the examples I saw on your website have data inserted between <class>"ClassName"</class> but not as <class name = "ClassName">...</class>

@mfatihercik
Copy link
Owner

mfatihercik commented Feb 13, 2020 via email

@zszazi
Copy link
Author

zszazi commented Feb 13, 2020

Thanks for the quick response

Something like the below one

{
  "Name":"JSON output"
  "Methods": [
    {
      "Id": 9375, #some random Id
      "Package": "com/example/controller",
      "Class": "com/example/controller/SampleController",
      "Method": "codeCoverageMethod",
    },
    {
      "Id": 8420,
      "Package": "com/example/controller",
      "Class": "com/example/SampleController",
      "Method": "callingThisMethod",
    }
  ]
}

But as far as the example XML given in previous post so only the method sampleController should be included and tryItOut should be excluded (because covered is 0 ) in the resultant json

So as per above XML the generated output should be

{
  "Name":"JSON output"
  "Methods": [
    {
      "Id": 9375, #some random Id
      "Package": "com/example/controller",
      "Class": "com/example/controller/SampleController",
      "Method": "sampleController",
    }
 ]
}

@mfatihercik
Copy link
Owner

mfatihercik commented Feb 13, 2020

you should first define DSM mapping yaml as like your JSONoutput.
mapping file will be more complicated because you reads most of data from attribute :)

result:
  path: /
  type: object
  fields:
    Name:
        default: 
          value: JSON Outut
          atStart: true  
        
    Methods:
      type: array # methods are array
      path: package/class/method  # loop over method tag
      filter: self.data.covered!=0      
      fields:
          Id:
            default: $new("java.util.Random").nextInt(10000)        
          Name:
             path: name
             xml:
               attribute: true
          Package:
             path: name
             parentPath: /package  # start from root
             xml:
                attribute: true
          Class:
             path: name
             parentPath: /package/class  start from root
             xml:
                attribute: true
          covered:
            path: covered  # name of the attribute
            parentPath: counter  # path which has attribute
            xml:
              attribute: true

Here is the code to convert your xml to json.
Currently DSM not support to exclude fields from serialization.
But DSM use jackson library to serialize json so you can configure ObjectMapper class to exclude fields.

public static void main(String[] args) throws JsonProcessingException
   {
       DSM dsm = new DSMBuilder(Main.class.getClassLoader().getResourceAsStream("test.yaml")).setType(TYPE.XML)
                       .create();

       Object result = dsm.toObject(Main.class.getClassLoader().getResourceAsStream("test.xml"));

       // convert to json
ObjectMapper objectMapper = dsm.getObjectMapper(); 
       String json = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(result);
       System.out.println(json);

   }

Here is the result

{
  "Name" : "JSON Outut",
  "Methods" : [ {
    "Package" : "com/example/configure",
    "Name" : "sampleController",
    "covered" : "23",
    "Id" : 6036
  } ]
}

@zszazi
Copy link
Author

zszazi commented Feb 13, 2020

Awesome
Would keep you updated on the progress

DSM project is very great !
Many should adopt this

@mfatihercik
Copy link
Owner

mfatihercik commented Feb 19, 2020

@zszazi can't see last comment you write. I received via email but it is not listed here.

@zszazi
Copy link
Author

zszazi commented Feb 19, 2020

@mfatihercik REPOST
I had 2 questions

  1. If i have to find only the covered in the INSTRUCTION level in the below XML
            <method name="CGLIB$sampleController$0"
                    desc="()Lcom/jd/jacoco/controller/SampleController;">
                <counter type="INSTRUCTION" missed="0" covered="3" />
                <counter type="COMPLEXITY" missed="0" covered="1" />
                <counter type="METHOD" missed="0" covered="1" />
            </method>

But when I do so with using DSM the covered output is from the METHOD type instead of INSTRUCTION type (ie I get covered as 1 instead of getting 3)

  1. Second question How can I apply Regex on the string while doing the filter (Is it possible to write this as a regex expression inside the DSM config yaml file?

PS:I am seeing major improvements in efficiency while using DSM

@mfatihercik
Copy link
Owner

  1. because multiple 'counter' tag exist inside 'method' tag last one will override previous ones
    if you want to only 'covered' of 'INSTRUCTION' counter you need to filter counter, too
          type:
            path: type  # name of the attribute
            parentPath: counter  # path which has attribute
            xml:
              attribute: true
          covered:
            path: covered  
            filter: self.data.type=="INSTRUCTION"
            parentPath: counter  # path which has attribute
            xml:
              attribute: true
  1. DSM use JEXL, Groovy or Javascript to resolve expressions. You can change scripting language
    DSMBuilder(new File("path/toFile")).setScriptingLang("javascript")
    Default is JEXL because it caches expression and run faster.
    so you can write any JEXL expression in filter field unless you return boolean.
Methods:
      type: array # methods are array
      path: package/class/method  # loop over method tag
      filter: self.data.covered!=0 and self.data.Name.matches(".*Controller.*")    
                                                       # Name is a String so we can use any method exist on String class.
      fields:
          Id:
            default: $new("java.util.Random").nextInt(10000)        
          Name:
             path: name
             xml:
               attribute: true
          Package:
             path: name             
             parentPath: /package  # start from root
             xml:
                attribute: true
          Class:
             path: name
             parentPath: /package/class  start from root
             xml:
                attribute: true
          type:
            path: type  # name of the attribute
            parentPath: counter  # path which has attribute
            xml:
              attribute: true
          covered:
            path: covered  
            filter: self.data.type=="INSTRUCTION"
            parentPath: counter  # path which has attribute
            xml:
              attribute: true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants