home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / VCafe / prosrc.bin / Name.java < prev    next >
Encoding:
Java Source  |  1998-03-18  |  10.3 KB  |  341 lines

  1. /*
  2.  * @(#)Name.java
  3.  *
  4.  * Copyright (c) 1997 Symantec Corporation. All Rights Reserved.
  5.  *
  6.  */
  7. /**
  8.  * <p>This class takes care of all the name related issues such as getting the
  9.  * table name, the number of rows......
  10.  * The naming convension is :
  11.  * TableName@Field1,field2%Number of rows
  12.  *
  13.  */
  14. package symantec.itools.db.beans.binding;
  15. public class Name extends Object{
  16.  
  17.     /**
  18.      * Separators in the name.
  19.      */
  20.     public final static String LinkSeparator="-";
  21.     public final static String TableSeparator="@";
  22.     public final static String ColumnSeparator=",";
  23.     public final static String SizeSeparator="%";
  24.     public final static String AliasSeparator="=";
  25.     public final static String MethodSeparator=";";
  26.     public final static String AllColumnsString="All";
  27.     public final static String AllRowsString="All";
  28.     public final static int    ColumnNotFoundField=-666;
  29.     public final static int    NumericColumnOffset=1;
  30.     public final static int    AllRows=-1;
  31.     /**
  32.      * Different parts of the name:
  33.      * FullName=TableName@FieldName
  34.      */
  35.     protected String FullName="";
  36.     protected String FieldName="";
  37.     protected String TableName="";
  38.     protected int NumberOfRows=1;
  39.     /**
  40.      * Default constructor
  41.      */
  42.     public Name()
  43.     {}
  44.     /**
  45.      * Creates a Name given its fullname.
  46.      * @param fullname. the full name.
  47.      */
  48.     public Name(String fullname)
  49.     {
  50.         this.setName(fullname);
  51.     }
  52.  
  53.     public Name(String tname,String fname,int rows)
  54.     {
  55.         setName(tname+TableSeparator+fname+SizeSeparator+rows);
  56.         setNumberOfRows(rows);
  57.     }
  58.  
  59.     public Name(String tname,String fname)
  60.     {
  61.         setName(tname+TableSeparator+fname+SizeSeparator+"1");
  62.         setNumberOfRows(1);
  63.     }
  64.     /**
  65.      * Sets the differnt sets all the parts of the name.
  66.      * @param fullname. the full name.
  67.      */
  68.     public void setName(String fullname)
  69.     {
  70.         FullName=fullname;
  71.         FieldName=getFieldName(fullname);
  72.         TableName=getTableName(fullname);
  73.         NumberOfRows=getNumberOfRows(fullname);
  74.     }
  75.  
  76.     /**
  77.      * Returns the current full name
  78.      * @return String.  the current full name
  79.      */
  80.     public String getFullName()
  81.     {
  82.         if(!TableName.equals("") && !FieldName.equals("")){
  83.             FullName=TableName+TableSeparator+FieldName+SizeSeparator+NumberOfRows;
  84.             return(FullName);
  85.         }
  86.         return("none");
  87.     }
  88.  
  89.     /**
  90.      * Returns the current table name
  91.      * @return String.  the current table name
  92.      */
  93.     public String getTableName()
  94.     {
  95.         return(TableName);
  96.     }
  97.  
  98.     public void setTableName(String tname)
  99.     {
  100.         TableName=tname;
  101.     }
  102.     /**
  103.      * Returns the current fields name
  104.      * @return String.  the current fields name
  105.      */
  106.     public String getFieldName()
  107.     {
  108.         return(FieldName);
  109.     }
  110.     public void setFieldName(String fname)
  111.     {
  112.         FieldName=fname;
  113.     }
  114.     /**
  115.      * Returns the number of rows defined in the name.
  116.      * @return int.     the number of rows defined in the name.
  117.      */
  118.     public int getNumberOfRows()
  119.     {
  120.         return NumberOfRows;
  121.     }
  122.     public void setNumberOfRows(int rows)
  123.     {
  124.         NumberOfRows=rows;
  125.     }
  126.  
  127.  
  128.     /**
  129.      * Returns the number of columns defined in the name.
  130.      * @return int.     the number of columns defined in the name.
  131.      */
  132.     public int getNumberOfCols()
  133.     {
  134.         return(getNumberOfCols(FullName));
  135.     }
  136.  
  137.     /**
  138.      * Returns the index of a column given its name.
  139.      * @param name.     the name.
  140.      * @return int.     the index of a column given its name.
  141.      */
  142.     public int getColumnFromName(String name)
  143.     {
  144.             if (name.indexOf(ColumnSeparator)>0)name=name.substring(0,name.indexOf(ColumnSeparator));
  145.             int numericColumn=0;
  146.             boolean isNumber=true;
  147.                 for(int a=0;a<name.length();a++)
  148.                 {
  149.                     char number=name.charAt(a);
  150.                     if(number>='0' && number<='9')
  151.                     {
  152.                         numericColumn=numericColumn*10+(number-'0');
  153.                     }
  154.                     else 
  155.                     {
  156.                         isNumber=false;
  157.                         break;
  158.                     }
  159.                 }
  160.                 if(numericColumn>=0 && numericColumn < getNumberOfCols()&& isNumber){
  161.                 return numericColumn-NumericColumnOffset;
  162.                 }
  163.         
  164.         else{
  165.  
  166.         String Names=FieldName+ColumnSeparator;
  167.         int position=0;
  168.         while(Names.length()>0){
  169.  
  170.             String actual=Names;
  171.             if(actual.startsWith(name+",")){
  172.                //System.out.println(name+"---"+actual);
  173.                 return(position);
  174.             }
  175.  
  176.             else{
  177.                 position++;
  178.                 Names=Names.substring(Names.indexOf(ColumnSeparator)+1,Names.length());
  179.             }
  180.         }
  181.         }
  182.         System.out.println("column "+name+" not found");
  183.         return(ColumnNotFoundField);
  184.     }
  185.  
  186.     /**
  187.      * Returns an array of indexes of the columns included in the name.
  188.      * @param name.     the full name.
  189.      * @return int[].   an array of indexes of the columns included in the name.
  190.      */
  191.     public int[] getColumnsFromName(String name)
  192.     {
  193.         int[] columns =new int[getNumberOfCols(name)];
  194.         String ActualName;
  195.         name=getFieldName(name)+ColumnSeparator;
  196.  
  197.         if(name.equals(AllColumnsString+ColumnSeparator)){
  198.             columns =new int[getNumberOfCols()-4];
  199.             for(int i=0;i<columns.length;i++){
  200.                 columns[i]=i;
  201.             }
  202.             return(columns);
  203.         }
  204.         for(int i=0;i<columns.length;i++){
  205.             ActualName=name.substring(0,name.indexOf(ColumnSeparator)+1);
  206.             name=name.substring(name.indexOf(ColumnSeparator)+1,name.length());
  207.             columns[i]=getColumnFromName(ActualName);
  208.  
  209.         }
  210.         return(columns);
  211.     }
  212.  
  213.     /**
  214.      * Returns the table name given the full name.
  215.      * @param name.     the full name.
  216.      * @return String.  the table name given the full name.
  217.      */
  218.     public static String getTableName(String name)
  219.     {
  220.         int delimiter=name.indexOf(TableSeparator);
  221.         if(delimiter>=0){
  222.             return(name.substring(0,delimiter));
  223.         }
  224.         return("");
  225.     }
  226.  
  227.     /**
  228.      * Returns the fields name given the full name.
  229.      * @param name.     the full name.
  230.      * @return String.  the fields name given the full name.
  231.      */
  232.     public static String getFieldName(String name)
  233.     {
  234.         int delimiter=name.indexOf(TableSeparator);
  235.         name=name.substring(delimiter+1,name.length());
  236.         int posOfPC=name.indexOf(SizeSeparator);
  237.         if(posOfPC>=0){
  238.             name=name.substring(0,posOfPC);
  239.         }
  240.         return(name);
  241.     }
  242.  
  243.     /**
  244.      * Returns the number of rows given the full name.
  245.      * @param name. the full name.
  246.      * @return int. the number of rows given the full name.
  247.      */
  248.     public static int getNumberOfRows(String name)
  249.     {
  250.         int posOfPC=name.indexOf(SizeSeparator);
  251.         if(posOfPC>=0){
  252.             name=name.substring(posOfPC+1,name.length());
  253.             if(name.equals(AllRowsString))return AllRows;
  254.             return(Integer.valueOf(name).intValue());
  255.         }
  256.         if(name.length()>0){
  257.             return(1);
  258.         }
  259.         else return(0);
  260.     }
  261.  
  262.     /**
  263.      * Returns the number of columns given the full name.
  264.      * @param name. the full name.
  265.      * @return int. the number of columns given the full name.
  266.      */
  267.     public static int getNumberOfCols(String name)
  268.     {
  269.         int NumberOfCols=1;
  270.         int posOfAT=name.indexOf(TableSeparator);
  271.         name=name.substring(posOfAT+1,name.length());
  272.         int posOfPC=name.indexOf(SizeSeparator);
  273.         if(posOfPC>=0){
  274.             name=name.substring(0,posOfPC);
  275.         }
  276.         if(name.length()>0){
  277.             for(int i=0;i<name.length();i++){
  278.                 if(name.charAt(i)==Name.ColumnSeparator.toCharArray()[0])NumberOfCols++;
  279.             }
  280.             return(NumberOfCols);
  281.         }
  282.         return(0);
  283.     }
  284.     /**
  285.      * Compares the fieds of the mediatorDs to those in hismane. It is used
  286.      * when the mediatorDS recives a request for a DataItem and wants to know
  287.      * if it can fullfill it.
  288.      * @param hisName
  289.      * @return boolean is it my field?
  290.      */
  291.     protected boolean isMyField(String testName)
  292.     {
  293.         if(Name.getTableName(testName).equals(getTableName()))
  294.         {
  295.             String testFieldName=Name.getFieldName(testName)+Name.ColumnSeparator;
  296.             String myFieldName=getFieldName()+Name.ColumnSeparator;
  297.             int testNumberOfCols=Name.getNumberOfCols(testName);
  298.             int myNumberOfCols=getNumberOfCols();
  299.             for (int i=0;i<testNumberOfCols;i++)
  300.             {
  301.                 String actualTestField=testFieldName.substring(0,testFieldName.indexOf(Name.ColumnSeparator)+1);
  302.                 if(actualTestField.equals(AllColumnsString+Name.ColumnSeparator))
  303.                 {
  304.                     return true;
  305.                 }
  306.  
  307.                 int testPosition=0;
  308.                 for(int j=0; j<myNumberOfCols;j++)
  309.                 {
  310.                     if((myFieldName.regionMatches(false,testPosition,actualTestField,0,actualTestField.length())))
  311.                     {
  312.                         return true;
  313.                     }
  314.                     else
  315.                     {
  316.                         testPosition=myFieldName.indexOf(Name.ColumnSeparator,testPosition+1)+1;
  317.                     }
  318.                 }
  319.                 int numCol=0;
  320.                 for(int a=0;a<actualTestField.length()-1;a++)
  321.                 {
  322.                     char number=actualTestField.charAt(a);
  323.                     if(number>='0' && number<='9')
  324.                     {
  325.                         numCol=numCol*10+(number-'0');
  326.                     }
  327.                     else return false;
  328.                 }
  329.                 if (numCol>0 && numCol<=myNumberOfCols)
  330.                 {
  331.                     return true;
  332.                 }
  333.  
  334.             }
  335.         }
  336.         return false;
  337.     }
  338.  
  339.  
  340. }
  341.