0

I am currently doing a project on yang parser. I have come across an open source tool called "jYang" which is a parser for Yang files in Java. I have downloaded the source files and understood its procedure but i'm not knowing how to input the Yang file into the parser. So far, I have gone through the following websites but I couldnt figure out the solution : https://hal.inria.fr/inria-00411261/file/yang.pdf http://jyang.gforge.inria.fr/JYang_Home_Page.html

Tunaki
  • 132,869
  • 46
  • 340
  • 423

1 Answers1

0

The following is an example in section 4.3.2 "Programmatic access" from the first link you provided, with my comments added for clarity.

public static void main(String[] args) throws Exception {
    // Create a FileInputStream using a string with the path to your yang file
    FileInputStream yangfile = new FileInputStream(args[0]);
    // Pass the yang file input stream to the parser by calling its static constructor
    new yang(yangfile);
    // Run the parser on the yang file and store the result in spec
    YANG_Specification spec = yang.Start();
    // Call spec's functions to access it
    spec.check();
}

Looking at the source code, jYang provides 4 different static constructors, of which you will want to use the 2nd or 3rd to specify your file's encoding:

yang(java.io.InputStream stream);
yang(java.io.InputStream stream, String encoding);
yang(java.io.Reader stream);
yang(yangTokenManager tm);

jYang's constructors will throw an error if you attempt to call any of them more than once. The developers forced the class to behave statically by using a static state variable that tracks if its constructors have already been called. If you need to retry parsing the file or start parsing a new yang file, you will need to use the ReInit function followed by the Start function.

// One of these 4
yang.ReInit(java.io.InputStream stream);
yang.ReInit(java.io.InputStream stream, String encoding);
yang.ReInit(java.io.Reader stream);
yang.ReInit(yangTokenManager tm);
// Followed by
YANG_Specification spec = yang.Start();