Uploaded image for project: 'yangtools'
  1. yangtools
  2. YANGTOOLS-28

New parser builder imports all yang files in directory

    XMLWordPrintable

Details

    • Bug
    • Status: Resolved
    • Resolution: Done
    • None
    • None
    • None
    • None
    • Operating System: Windows
      Platform: PC

    • 91

    Description

      The new builder
      public Set<org.opendaylight.yangtools.yang.model.api.Module> parseYangModels(File yangFile,File directory)
      loads all yang files in the directory, not just those needed to parse the file.
      This will not scale when we have large numbers of files.

      Below shows the key code I used to build the list (I have omitted the antlr related code, but happy to attach that if it helps).

      ===============================================================
      I'm not saying my code is any good, just showing it can be done.
      ===============================================================

      >>> So my call in main was.
      Set<Module> modules = parser.parseYangModels(getAllYangFiles(yangFileStr, dirStr));

      >>>and here are the methods

      // parse the imports in the main file
      public List<File> getAllYangFiles(final String fileName, final String dirStr) throws IOException {
      assert fileName != null;
      assert dirStr != null;

      imports = new HashSet<String>(127);
      List<File> files = new ArrayList<File>();
      files.add(new File(dirStr + File.separator + fileName + YANG_FILE_EXTENSION)); // add the main file

      getYangImports(fileName, dirStr); // recursively get the file imports

      Iterator<String> i = imports.iterator();

      while (i.hasNext())

      { files.add(new File(dirStr + File.separator + i.next()+ YANG_FILE_EXTENSION)); }

      return files;
      }

      // recursively parse the imported yang files
      public void getYangImports(final String fileName, final String dirStr) throws IOException {
      assert fileName != null;
      assert dirStr != null;

      Set<String> fileImports = getYangFileImports(fileName, dirStr);

      if (!fileImports.isEmpty()) { // have some imports !
      Iterator<String> i = fileImports.iterator();
      String importStr;

      while (i.hasNext()) {
      importStr = i.next();
      if (!imports.contains(importStr))

      { // a new file to add to the list imports.add(importStr); getYangImports(importStr, dirStr); // recursive goodness }

      }
      }
      }

      // get the import statements in each file
      public Set<String> getYangFileImports(final String fileName, final String dirStr) throws IOException

      { assert fileName != null; assert dirStr != null; //lexer splits input into tokens YangLexer lexer = new YangLexer(new ANTLRFileStream(dirStr + File.separator + fileName + YANG_FILE_EXTENSION)); // need to use subclass here (not Lexer) //parser generates abstract syntax tree YangParser parser = new YangParser(new CommonTokenStream(lexer)); // need to use subclass here (not Parser) parser.setBuildParseTree(true); ParseTree tree = parser.module_stmt(); YangListener listener = new YangListener(); ParseTreeWalker.DEFAULT.walk(listener, tree); return listener.getImports(); }

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            Unassigned Unassigned
            chrhartl@cisco.com Christopher Hartley
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: