home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-03-18 | 10.3 KB | 341 lines |
- /*
- * @(#)Name.java
- *
- * Copyright (c) 1997 Symantec Corporation. All Rights Reserved.
- *
- */
- /**
- * <p>This class takes care of all the name related issues such as getting the
- * table name, the number of rows......
- * The naming convension is :
- * TableName@Field1,field2%Number of rows
- *
- */
- package symantec.itools.db.beans.binding;
- public class Name extends Object{
-
- /**
- * Separators in the name.
- */
- public final static String LinkSeparator="-";
- public final static String TableSeparator="@";
- public final static String ColumnSeparator=",";
- public final static String SizeSeparator="%";
- public final static String AliasSeparator="=";
- public final static String MethodSeparator=";";
- public final static String AllColumnsString="All";
- public final static String AllRowsString="All";
- public final static int ColumnNotFoundField=-666;
- public final static int NumericColumnOffset=1;
- public final static int AllRows=-1;
- /**
- * Different parts of the name:
- * FullName=TableName@FieldName
- */
- protected String FullName="";
- protected String FieldName="";
- protected String TableName="";
- protected int NumberOfRows=1;
- /**
- * Default constructor
- */
- public Name()
- {}
- /**
- * Creates a Name given its fullname.
- * @param fullname. the full name.
- */
- public Name(String fullname)
- {
- this.setName(fullname);
- }
-
- public Name(String tname,String fname,int rows)
- {
- setName(tname+TableSeparator+fname+SizeSeparator+rows);
- setNumberOfRows(rows);
- }
-
- public Name(String tname,String fname)
- {
- setName(tname+TableSeparator+fname+SizeSeparator+"1");
- setNumberOfRows(1);
- }
- /**
- * Sets the differnt sets all the parts of the name.
- * @param fullname. the full name.
- */
- public void setName(String fullname)
- {
- FullName=fullname;
- FieldName=getFieldName(fullname);
- TableName=getTableName(fullname);
- NumberOfRows=getNumberOfRows(fullname);
- }
-
- /**
- * Returns the current full name
- * @return String. the current full name
- */
- public String getFullName()
- {
- if(!TableName.equals("") && !FieldName.equals("")){
- FullName=TableName+TableSeparator+FieldName+SizeSeparator+NumberOfRows;
- return(FullName);
- }
- return("none");
- }
-
- /**
- * Returns the current table name
- * @return String. the current table name
- */
- public String getTableName()
- {
- return(TableName);
- }
-
- public void setTableName(String tname)
- {
- TableName=tname;
- }
- /**
- * Returns the current fields name
- * @return String. the current fields name
- */
- public String getFieldName()
- {
- return(FieldName);
- }
- public void setFieldName(String fname)
- {
- FieldName=fname;
- }
- /**
- * Returns the number of rows defined in the name.
- * @return int. the number of rows defined in the name.
- */
- public int getNumberOfRows()
- {
- return NumberOfRows;
- }
- public void setNumberOfRows(int rows)
- {
- NumberOfRows=rows;
- }
-
-
- /**
- * Returns the number of columns defined in the name.
- * @return int. the number of columns defined in the name.
- */
- public int getNumberOfCols()
- {
- return(getNumberOfCols(FullName));
- }
-
- /**
- * Returns the index of a column given its name.
- * @param name. the name.
- * @return int. the index of a column given its name.
- */
- public int getColumnFromName(String name)
- {
- if (name.indexOf(ColumnSeparator)>0)name=name.substring(0,name.indexOf(ColumnSeparator));
- int numericColumn=0;
- boolean isNumber=true;
- for(int a=0;a<name.length();a++)
- {
- char number=name.charAt(a);
- if(number>='0' && number<='9')
- {
- numericColumn=numericColumn*10+(number-'0');
- }
- else
- {
- isNumber=false;
- break;
- }
- }
- if(numericColumn>=0 && numericColumn < getNumberOfCols()&& isNumber){
- return numericColumn-NumericColumnOffset;
- }
-
- else{
-
- String Names=FieldName+ColumnSeparator;
- int position=0;
- while(Names.length()>0){
-
- String actual=Names;
- if(actual.startsWith(name+",")){
- //System.out.println(name+"---"+actual);
- return(position);
- }
-
- else{
- position++;
- Names=Names.substring(Names.indexOf(ColumnSeparator)+1,Names.length());
- }
- }
- }
- System.out.println("column "+name+" not found");
- return(ColumnNotFoundField);
- }
-
- /**
- * Returns an array of indexes of the columns included in the name.
- * @param name. the full name.
- * @return int[]. an array of indexes of the columns included in the name.
- */
- public int[] getColumnsFromName(String name)
- {
- int[] columns =new int[getNumberOfCols(name)];
- String ActualName;
- name=getFieldName(name)+ColumnSeparator;
-
- if(name.equals(AllColumnsString+ColumnSeparator)){
- columns =new int[getNumberOfCols()-4];
- for(int i=0;i<columns.length;i++){
- columns[i]=i;
- }
- return(columns);
- }
- for(int i=0;i<columns.length;i++){
- ActualName=name.substring(0,name.indexOf(ColumnSeparator)+1);
- name=name.substring(name.indexOf(ColumnSeparator)+1,name.length());
- columns[i]=getColumnFromName(ActualName);
-
- }
- return(columns);
- }
-
- /**
- * Returns the table name given the full name.
- * @param name. the full name.
- * @return String. the table name given the full name.
- */
- public static String getTableName(String name)
- {
- int delimiter=name.indexOf(TableSeparator);
- if(delimiter>=0){
- return(name.substring(0,delimiter));
- }
- return("");
- }
-
- /**
- * Returns the fields name given the full name.
- * @param name. the full name.
- * @return String. the fields name given the full name.
- */
- public static String getFieldName(String name)
- {
- int delimiter=name.indexOf(TableSeparator);
- name=name.substring(delimiter+1,name.length());
- int posOfPC=name.indexOf(SizeSeparator);
- if(posOfPC>=0){
- name=name.substring(0,posOfPC);
- }
- return(name);
- }
-
- /**
- * Returns the number of rows given the full name.
- * @param name. the full name.
- * @return int. the number of rows given the full name.
- */
- public static int getNumberOfRows(String name)
- {
- int posOfPC=name.indexOf(SizeSeparator);
- if(posOfPC>=0){
- name=name.substring(posOfPC+1,name.length());
- if(name.equals(AllRowsString))return AllRows;
- return(Integer.valueOf(name).intValue());
- }
- if(name.length()>0){
- return(1);
- }
- else return(0);
- }
-
- /**
- * Returns the number of columns given the full name.
- * @param name. the full name.
- * @return int. the number of columns given the full name.
- */
- public static int getNumberOfCols(String name)
- {
- int NumberOfCols=1;
- int posOfAT=name.indexOf(TableSeparator);
- name=name.substring(posOfAT+1,name.length());
- int posOfPC=name.indexOf(SizeSeparator);
- if(posOfPC>=0){
- name=name.substring(0,posOfPC);
- }
- if(name.length()>0){
- for(int i=0;i<name.length();i++){
- if(name.charAt(i)==Name.ColumnSeparator.toCharArray()[0])NumberOfCols++;
- }
- return(NumberOfCols);
- }
- return(0);
- }
- /**
- * Compares the fieds of the mediatorDs to those in hismane. It is used
- * when the mediatorDS recives a request for a DataItem and wants to know
- * if it can fullfill it.
- * @param hisName
- * @return boolean is it my field?
- */
- protected boolean isMyField(String testName)
- {
- if(Name.getTableName(testName).equals(getTableName()))
- {
- String testFieldName=Name.getFieldName(testName)+Name.ColumnSeparator;
- String myFieldName=getFieldName()+Name.ColumnSeparator;
- int testNumberOfCols=Name.getNumberOfCols(testName);
- int myNumberOfCols=getNumberOfCols();
- for (int i=0;i<testNumberOfCols;i++)
- {
- String actualTestField=testFieldName.substring(0,testFieldName.indexOf(Name.ColumnSeparator)+1);
- if(actualTestField.equals(AllColumnsString+Name.ColumnSeparator))
- {
- return true;
- }
-
- int testPosition=0;
- for(int j=0; j<myNumberOfCols;j++)
- {
- if((myFieldName.regionMatches(false,testPosition,actualTestField,0,actualTestField.length())))
- {
- return true;
- }
- else
- {
- testPosition=myFieldName.indexOf(Name.ColumnSeparator,testPosition+1)+1;
- }
- }
- int numCol=0;
- for(int a=0;a<actualTestField.length()-1;a++)
- {
- char number=actualTestField.charAt(a);
- if(number>='0' && number<='9')
- {
- numCol=numCol*10+(number-'0');
- }
- else return false;
- }
- if (numCol>0 && numCol<=myNumberOfCols)
- {
- return true;
- }
-
- }
- }
- return false;
- }
-
-
- }
-