home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / TRIAL / JBUILDER / JSAMPLES.Z / StatementTokenizer.java < prev    next >
Encoding:
Java Source  |  1998-05-08  |  5.2 KB  |  197 lines

  1. /*
  2.  * Copyright (c) 1997-1998 Borland International, Inc. All Rights Reserved.
  3.  * 
  4.  * This SOURCE CODE FILE, which has been provided by Borland as part
  5.  * of a Borland product for use ONLY by licensed users of the product,
  6.  * includes CONFIDENTIAL and PROPRIETARY information of Borland.  
  7.  *
  8.  * USE OF THIS SOFTWARE IS GOVERNED BY THE TERMS AND CONDITIONS 
  9.  * OF THE LICENSE STATEMENT AND LIMITED WARRANTY FURNISHED WITH
  10.  * THE PRODUCT.
  11.  *
  12.  * IN PARTICULAR, YOU WILL INDEMNIFY AND HOLD BORLAND, ITS RELATED
  13.  * COMPANIES AND ITS SUPPLIERS, HARMLESS FROM AND AGAINST ANY CLAIMS
  14.  * OR LIABILITIES ARISING OUT OF THE USE, REPRODUCTION, OR DISTRIBUTION
  15.  * OF YOUR PROGRAMS, INCLUDING ANY CLAIMS OR LIABILITIES ARISING OUT OF
  16.  * OR RESULTING FROM THE USE, MODIFICATION, OR DISTRIBUTION OF PROGRAMS
  17.  * OR FILES CREATED FROM, BASED ON, AND/OR DERIVED FROM THIS SOURCE
  18.  * CODE FILE.
  19.  */
  20.  
  21. //Title:        Stored Procedure Sample
  22. //Version:      2.0
  23. //Copyright:    Copyright (c) 1998
  24. //Company:      Borland Int'l
  25. //Description:  Class can break an sql string up into sql statements.
  26. //              Feel free to copy/modify.
  27. //Special case: Oracle doesn't accept \r characters, and require a ';' after
  28. //              a function/procedure definition, but cannot handle a ';' after
  29. //              a table definition.
  30.  
  31.  
  32. package borland.samples.tutorial.dataset.storedprocedure;
  33.  
  34. public class StatementTokenizer {
  35.  
  36.   public StatementTokenizer(String stream, boolean acceptCR, boolean keepSemiColonForBeginEnd) {
  37.     this.keepSemiColonForBeginEnd = keepSemiColonForBeginEnd;
  38.     buffer = new StringBuffer(stream);
  39.     if (!acceptCR)
  40.       removeCR(stream);
  41.     index  = 0;
  42.     last   = stream.length();
  43.   }
  44.  
  45.   private void removeCR(String stream) {
  46.     int index = stream.indexOf((int)'\r');
  47.     while (index >= 0) {
  48.       buffer.setCharAt(index,' ');
  49.       index = stream.indexOf((int)'\r',index+1);
  50.     }
  51.   }
  52.           
  53.   private void skipComment() {
  54.     char ch = buffer.charAt(index);
  55.     if (ch == '/') {
  56.       index++;
  57.       while (index < last) {
  58.         ch = buffer.charAt(index++);
  59.         if (ch == '\r' || ch == '\n')
  60.           break;
  61.       }
  62.     } else if (ch == '*') {
  63.       index++;
  64.       int stop = last - 1;
  65.       while (index < stop) {
  66.         if (buffer.charAt(index++) == '*' && buffer.charAt(index) == '/')
  67.           break;
  68.       }
  69.       if (index < stop)
  70.         index++;   
  71.     }
  72.   }
  73.     
  74.   private void skipSpacing(boolean allowComment) {
  75.     while (index < last) {
  76.       char ch = buffer.charAt(index++);
  77.       switch (ch) {
  78.         case '\r':
  79.         case ' ':
  80.         case '\t':
  81.         case '\n':
  82.           continue;
  83.           
  84.         case '/':
  85.           if (allowComment) {
  86.             if (index < last)
  87.               skipComment();
  88.             continue;
  89.           }
  90.           
  91.         default:
  92.           index--;
  93.           return;
  94.       }
  95.     }
  96.   }
  97.   
  98.   private boolean isWhiteSpace(char ch) {
  99.     switch (ch) {
  100.       case ' ':
  101.       case '\t':
  102.       case '\r':
  103.       case '\n':
  104.         return true;
  105.       default:
  106.         return false;
  107.     }
  108.   }
  109.   
  110.   private boolean skipIdentifier(String identifier) {
  111.     char ch;
  112.     if (index > 0) {
  113.       ch = buffer.charAt(index-1);
  114.       if (!isWhiteSpace(ch))
  115.         return false;
  116.     }
  117.     int end = index + identifier.length();
  118.     if (end > last)
  119.       return false;
  120.       
  121.     String part = new String(buffer).substring(index,end);
  122.     if (!identifier.equalsIgnoreCase(part))
  123.       return false;
  124.  
  125.     if (end < last) {
  126.       ch = buffer.charAt(end);
  127.       if (ch != ';' && !isWhiteSpace(ch))
  128.         return false;
  129.     }
  130.       
  131.     index = end-1;
  132.     return true;
  133.   }
  134.   
  135.   public String nextToken() {
  136.     skipSpacing(true);
  137.     int start = index;
  138.     int begin_level = 0;
  139.     boolean is_creating = false;
  140.     boolean was_creating = false;
  141.                     
  142.     while (index < last) {
  143.       char ch = buffer.charAt(index);
  144.       if (ch == ';' && begin_level == 0)
  145.         break;
  146.       switch (ch) {
  147.         case 'b':
  148.         case 'B':
  149.           if (skipIdentifier("begin")) {
  150.             if (is_creating)
  151.               is_creating = false;
  152.             else
  153.               begin_level++;
  154.           }
  155.           break;
  156.             
  157.         case 'c':
  158.         case 'C':
  159.           if (skipIdentifier("create")) {
  160.             index++;
  161.             skipSpacing(false);
  162.             if (skipIdentifier("procedure") || skipIdentifier("function")) {
  163.               begin_level++;
  164.               index++;
  165.               is_creating = true;
  166.               was_creating = true;
  167.             }  
  168.             index--;
  169.           }
  170.           break;
  171.             
  172.         case 'e':
  173.         case 'E':
  174.           if (skipIdentifier("end"))
  175.             begin_level--;
  176.           break;
  177.  
  178.         default:
  179.       }         
  180.       index++;
  181.     }
  182.     if (start == last)
  183.       return null;
  184.     String result;
  185.     int end = keepSemiColonForBeginEnd && was_creating ? index + 1 : index; 
  186.     result = new String(buffer).substring(start,end);
  187.     if (index < last)
  188.       index++;
  189.     return result;
  190.   }
  191.  
  192.   private StringBuffer buffer;
  193.   private int          index;
  194.   private int          last;
  195.   private boolean      keepSemiColonForBeginEnd;
  196. }
  197.