⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tnsparse.java

📁 一个用java写的地震分析软件(无源码)-used to write a seismic analysis software (without source)
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
   }

   /*
    * private String getLastToken (String str)
    *    @param    str: String to test
    *    @return   String: Last token in the string
    *
    *    Tokenize a string and return last token
    */
   private String getLastToken (String str)
      throws Exception {

      String retstr = new String ("");
      if (str.length() > 0) {
         StringTokenizer st = new StringTokenizer(str);
         while (st.hasMoreTokens()) {
            retstr = st.nextToken();
         }
      }
      return retstr;
   }

   /*
    * private void throwException (String msg, boolean flush)
    *    @param   msg: Exception message
    *             flush: if true flush all strings and close any open Connections
    *
    *    Throws new Exception
    */
   private void throwException (String msg, boolean flush)
      throws Exception {

      if (flush)
         close ();
      throw new Exception (msg);
   }

   /*
    * === EXCLUSIVE FILE READERS AND PARSERS ===
    *
    * These private methods exclusively Check, Read and Parse the Tnsnames.ora file
    * and build the filestr (Continuous string of services names without any spaces,
    * CRLF, comments or any Non-Service name data). Also builds aliasstr (End-Of-Line
    * separated string that contains all service names. <ServiceName>.<DomainName>.
    *
    *
    *
    * private void tnsReadAndParseFile ()
    *    @param
    *    @return
    *
    *    Read and parse Tnsnames.ora file. Build the filestr, aliasstr to be used globally
    */
   private void tnsReadAndParseFile ()
      throws Exception {

      /* Check Tnsnames.ora file existense and readability */
      tnsCheckFileValid ();
      /* Read Tnsnames.ora file using FileReader. Check if file is valid format */
      tnsReadFile ();
      /* Remove all spaces and replace with null's "" */
      tnsRemoveAndReplace (spacerep, nullrep);
      /* Remove all comment lines */
      tnsRemoveComments ();
      /* Remove all multiple eol's with single eol */
      tnsRemoveAndReplace (eol, eol);
      /* Fix =(DESCRIPTION keyword */
      tnsFixDescKeyword ();
      /* Separate all Connection strings by strchar5 */
      tnsSeparateConnStrings ();
      /* Now remove all eol's with null's "". This will create one continuous string */
      tnsRemoveAndReplace (eol, nullrep);
      /* Remove any lines that are not part of connection strings */
      tnsRemoveNonDataLines ();
      /* Set Alias/Service name string */
      tnsSetAliasString ();
   }

   /*
    * private void tnsCheckFileValid ()
    *    @param
    *    @return
    *
    *    Check Tnsnames.ora file existense and readability
    */
   private void tnsCheckFileValid ()
      throws Exception {

      if (!tnsnamesora.isFile())
         throwException ("File '" + tnsnamesora.getAbsolutePath() + "' does not exist or is not a normal file",
 true);
      if (!tnsnamesora.canRead())
         throwException ("File '" + tnsnamesora.getAbsolutePath() + "' is not readable", true);
      if (tnsnamesora.length() == 0)
         throwException ("File '" + tnsnamesora.getAbsolutePath() + "' is empty", true);
   }

   /*
    * private void tnsReadFile ()
    *    @param
    *    @return
    *
    *    Read Tnsnames.ora file using FileReader. Check if file is of valid
    *    Tnsnames.ora format
    */
   private void tnsReadFile ()
      throws IOException, Exception {

      FileReader fr = new FileReader (tnsnamesora);
      long fsize = tnsnamesora.length();
      char[] cbuf = new char[(int) fsize];
      int offset = 0;
      int len = (int) fsize;
      fr.read(cbuf, offset, len);
      fr.close();
      String strtmp = new String (cbuf);
      filestr = "";
      /* Convert everything to UPPERCASE */
      filestr = strtmp.toUpperCase();
      /* Check file format */
      tnsCheckFileFormat ();
   }

   /*
    * private void tnsRemoveAndReplace (String remstr, String repstr)
    *    @param   remstr: String to be removed
    *             repstr: String to be replaced with
    *    @return
    *
    *    Remove and replace strings from filestr
    */
   private void tnsRemoveAndReplace (String remstr, String repstr)
      throws Exception {

      String strtmp = new String (filestr);
      filestr = "";
      if (strtmp.indexOf(remstr) != -1) {
         StringTokenizer st = new StringTokenizer(strtmp, remstr);
         while(st.hasMoreTokens()) {
            filestr = filestr + st.nextToken() + repstr;
         }
      }
      if (filestr.equalsIgnoreCase(""))
         filestr = strtmp;
      tnsCheckFileFormat ();
   }

   /*
    * private void tnsRemoveComments ()
    *    @param
    *    @return
    *
    *    Remove all comment lines from filestr marked by "#"
    */
   private void tnsRemoveComments ()
      throws Exception {

      String strtmp = new String (filestr);
      filestr = "";
      String tokenstr = new String ();
      if (strtmp.indexOf(commchar +"") != -1) {
         StringTokenizer st = new StringTokenizer(strtmp, eol);
         while(st.hasMoreTokens()) {
            tokenstr = st.nextToken().trim();
            if (tokenstr.charAt(0) != commchar)
               filestr = filestr + tokenstr + eol;
         }
      }
      if (filestr.equalsIgnoreCase(""))
         filestr = strtmp;
      tnsCheckFileFormat ();
   }

   /*
    * private void tnsFixDescKeyword ()
    *    @param
    *    @return
    *
    *    Fix =(DESCRIPTION keyword so that there are no spaces and eol's between '='
    *    and '(DESCRIPTION'
    */
   private void tnsFixDescKeyword ()
      throws Exception {

      String strtmp = new String (filestr);
      filestr = "";
      int i = 0;
      int j = 0;
      if (strtmp.indexOf("=" + eol + "(DESCRIPTION") != -1) {
         while (strtmp.indexOf("=" + eol + "(DESCRIPTION", i) != -1) {
            j = strtmp.indexOf("=" + eol + "(DESCRIPTION", i);
            filestr = filestr + strtmp.substring(i, j + 1);
            i = strtmp.indexOf("(", j);
            if (strtmp.indexOf("=" + eol + "(DESCRIPTION", i) == -1)
               filestr = filestr + strtmp.substring(i, strtmp.length());
         }
      }
      if (filestr.equalsIgnoreCase(""))
         filestr = strtmp;
      tnsCheckFileFormat ();
   }

   /*
    * private void tnsSeparateConnStrings ()
    *    @param
    *    @return
    *
    *    Separate all Connection strings using constant strchar5
    */
   private void tnsSeparateConnStrings ()
      throws Exception {

      String strtmp = new String (filestr);
      filestr = "";
      String strext = new String ();
      String tokstr = new String ();
      boolean bExit = false;
      int i = 0;
      int j = 0;
      if (strtmp.indexOf("=(DESCRIPTION") != -1) {
         while (true) {
            tokstr = getLastToken(strtmp.substring(i, strtmp.indexOf("=(DESCRIPTION", i)));
            i = strtmp.indexOf(tokstr, i);
            /* Jump 20 spaces to deal with =(DESCRIPTION_LIST=(DESCRIPTION */
            j = strtmp.indexOf("=(DESCRIPTION", strtmp.indexOf("=(DESCRIPTION", i) + 20);
            if (j == -1) {
               j = strtmp.length();
               bExit = true;
            }
            else {
               tokstr = getLastToken(strtmp.substring(i, j));
               j = strtmp.substring(0, j).lastIndexOf(tokstr);
            }
            strext = strtmp.substring(i, j);
            filestr = filestr + strext + strchar5;
            if (bExit)
               break;
            i = j;
         }
      }
      if (filestr.equalsIgnoreCase(""))
         filestr = strtmp;
      if (filestr.endsWith(strchar5))
         filestr = filestr.substring(0, filestr.length() - 1);
      tnsCheckFileFormat ();
   }

   /*
    * private void tnsRemoveNonDataLines ()
    *    @param
    *    @return
    *
    *    Remove all lines if any, that are not comments and not part of connection strings
    */
   private void tnsRemoveNonDataLines ()
      throws Exception {

      String strtmp = new String (filestr);
      filestr = "";
      String tokstr = new String ();
      boolean bOk = true;
      int i = 0;
      if (strtmp.indexOf("=(DESCRIPTION") != -1) {
         StringTokenizer st = new StringTokenizer (strtmp, strchar5);
         while (st.hasMoreTokens()) {
            tokstr = st.nextToken();
            i = 0;
            i = tokstr.lastIndexOf("(CONNECT_DATA=");
            if (i == -1)
               i = tokstr.length();
            else {
               if (tokstr.indexOf("(SOURCE_ROUTE=", i) != -1)
                  i = tokstr.indexOf("(SOURCE_ROUTE=", i);
               if (tokstr.indexOf(")", i) == -1)
                  i = tokstr.length();
               else {
                  i = tokstr.indexOf(")", i);
                  while (i < tokstr.length()) {
                     i = i + 1;
                     if ((i >= tokstr.length()) || (tokstr.charAt(i) != (char)41))
                        break;
                  }
               }
            }
            filestr = filestr + tokstr.substring(0, i) + strchar5;
         }
      }
      if (filestr.equalsIgnoreCase(""))
         filestr = strtmp;
      tnsCheckFileFormat ();
   }

   /*
    * private void tnsSetAliasString ()
    *    @param
    *    @return
    *
    *    Set End-Of-Line delimited aliasstr that contains all service names
    */
   private void tnsSetAliasString ()
      throws Exception {

      String strtmp = new String (aliasstr);
      aliasstr = "";
      String tokstr = new String ();
      StringTokenizer st = new StringTokenizer(filestr, strchar5);
      while(st.hasMoreTokens()) {
         tokstr = st.nextToken();
         if (tokstr.indexOf("=", 0) != -1)
            aliasstr = aliasstr + tokstr.substring(0, tokstr.indexOf("=", 0)) + eol;
      }
      if (aliasstr.endsWith(eol))
            aliasstr = aliasstr.substring(0, aliasstr.length() - 1);
   }

   /*
    * private void tnsCheckFileFormat ()
    *    @param
    *    @return
    *
    *    Check filestr for Tnsnames.ora file keywords. If missing, throw Exception
    */
   private void tnsCheckFileFormat ()
      throws Exception {

      if (filestr.length() > 0) {
         if ((filestr.indexOf("DESCRIPTION") == -1) ||
	 (filestr.indexOf("ADDRESS") == -1)  || (filestr.indexOf("CONNECT_DATA") == -1))
            throwException ("File '" + tnsnamesora.getAbsolutePath() +
	    "' is not an Oracle tnsnames.ora format file OR unable to parse", true);
      }
   }

       public static void main (String[] args)
          throws Exception {

//          TNSParse t = new TNSParse (args[0]);
          TNSParse t = new TNSParse ("c:/codespace/junk/tnsnames.ora");
          String s = new String (t.getAliasList());
          t.close();
          System.out.println (s);
       }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -