Project

General

Profile

« Previous | Next » 

Revision 942

Added by Jing Tao almost 23 years ago

Two metacat util methods were add. One is getDocIdFromString and the other is getVersionFromString. They output a docid and revision if a string like str1.str2.str3. were passed to them.

View differences:

src/edu/ucsb/nceas/metacat/MetaCatUtil.java
451 451
    return (str.toString());
452 452
  } 
453 453
  
454
  /** 
455
   * Utility method to get docid from a given string
456
   * @param string, the given string should be these two format:
457
   *              1) str1.str2  in this case docid= str1.str2
458
   *              2) str1.str2.str3, in this case docid =str1.str2
459
   * @param the sperator char
460
   */
461
  public static String getDocIdFromString(String str)
462
  {
463
    String docId = null;
464
    int dotNumber = 0;//count how many dots in given string
465
    int indexOfLastDot = 0;
466
    
467
    //assume that seperator is one charactor string
468
    char seperator=getOption("accNumSeparator").charAt(0);
469
     
470
    for (int i=0; i<str.length(); i++)
471
    {
472
      if ( str.charAt(i)==seperator)
473
      {
474
        dotNumber++;//count how many dots
475
        indexOfLastDot=i;//keep the last dot postion
476
      }
477
    }//for
478
    
479
    //The string formatt is wrong, because it has more than two or less than
480
    //one seperator
481
    if ( dotNumber>2 || dotNumber < 1)
482
    {
483
      docId=null;
484
    }
485
    else if (dotNumber == 2) //the case for str1.str2.str3
486
    {
487
       docId=str.substring(0, indexOfLastDot);
488
    }
489
    else if (dotNumber == 1) //the case for str1.str2
490
    {
491
      docId=str;
492
    }
493
    
494
    return docId;  
495
  }//getDocIdFromString
496

  
497
  /** 
498
   * Utility method to get version number from a given string
499
   * @param string, the given string should be these two format:
500
   *              1) str1.str2(no version)  version =-1;
501
   *              2) str1.str2.str3, in this case version = str3;
502
   *              3) other, vresion =-2
503
   * @param the sperator char
504
   */
505
  public static int getVersionFromString(String str)
506
                                throws NumberFormatException
507
  {
508
    int version=-1;
509
    String versionString=null;
510
    int dotNumber = 0;//count how many dots in given string
511
    int indexOfLastDot = 0;
512
    
513
    //assume that seperator is one charactor string
514
    char seperator=getOption("accNumSeparator").charAt(0);
515
    
516
    for (int i=0; i<str.length(); i++)
517
    {
518
      if ( str.charAt(i)==seperator)
519
      {
520
        dotNumber++;//count how many dots
521
        indexOfLastDot=i;//keep the last dot postion
522
      }
523
    }//for
524
    
525
    //The string formatt is wrong, because it has more than two or less than
526
    //one seperator
527
    if ( dotNumber>2 || dotNumber < 1)
528
    {
529
      version=-2;
530
    }
531
    else if (dotNumber == 2) //the case for str1.str2.str3
532
    {
533
       versionString=str.substring((indexOfLastDot+1), str.length());
534
       version=Integer.parseInt(versionString);
535
    }
536
    else if (dotNumber == 1) //the case for str1.str2
537
    {
538
      version=-1;
539
    }
540
    
541
    return version;  
542
  }//getVersionFromString
543
  
454 544
}

Also available in: Unified diff