Package com.extentech.toolkit

Examples of com.extentech.toolkit.CompatibleVector


     // IF enclosed by parens, DO NOT split apart into operands:
    int funcLen= 1;
    if (enclosing) {
      returnStack.addAll(FormulaParser.getPtgsFromFormulaString(form, fmla, true));
    } else {    
        CompatibleVector cv= splitFunctionOperands(fmla);
        funcLen = cv.size();
      // loop through the operands to the function and recurse
      for (int y=0;y<cv.size();y++){
        String s = (String)cv.elementAt(y);    // flag as a complete expression
        returnStack.addAll(FormulaParser.getPtgsFromFormulaString(form, s, true));
      }
    }   
   
    // Handle PtgFuncVar-specifics such as number of parameters and add-in PtgNameX record
View Full Code Here


   *
   *
   */
  private static Stack parseFinalLevel(XLSRecord form, String fmla, boolean bIsComplete){
    Stack returnStack= new Stack();
    CompatibleVector parseThings = new CompatibleVector();
   
    // break it up into components first
    Vector elements = new Vector();
    elements = splitString(fmla, bIsComplete);
   
    WorkBook bk= form.getWorkBook()// nec. to determine if parsed element is a valid name handle name
   
    // convert each element into Ptg's
    // each element at this point should be a named operand, or an unidentified operator
    for (int x = 0;x<elements.size();x++){
      String val = (String)elements.elementAt(x);
      String name= convertString(val, bk);
       
      Ptg pthing = null;
      try{
        pthing = XLSRecordFactory.getPtgRecord(name);
        if (pthing==null && name.equals("PtgName"))
          // TODO: MUST evaluate which type of PtgName is correct: understand usage!
          if (form.getOpcode()==XLSConstants.FORMULA || form.getOpcode()==XLSConstants.ARRAY)
            pthing= new PtgName(0x43)// assume this token to be of type Value (i.e PtgNameV) instead of Reference (PtgNameR)
          else  // DV needs ref-type name 
            pthing= new PtgName(0x23)// PtgNameR   
      }catch(InvalidRecordException e){
          Logger.logInfo("parsing formula string.  Invalid Ptg: " + name + " error: " +e);
      }
      // if it is an operator we don't need to do anything with it!
      if(pthing !=null) {
          pthing.setParentRec(form);
         
        if (!pthing.getIsOperator()){
          if (pthing.getIsReference()){
            // createPtgRefFromString will handle any type of string reference
            // will return a PtgRefErr if cannot parse location
            pthing= PtgRef.createPtgRefFromString(val, form);        
          }else if (pthing instanceof PtgStr){
            PtgStr pstr = (PtgStr)pthing;
            val = StringTool.strip(val, '\"');
            pstr.setVal(val);
          }else if (pthing instanceof PtgNumber){
            PtgNumber pnum = (PtgNumber)pthing;
            if (val.indexOf("%")==-1)
              pnum.setVal((new Double(val).doubleValue()));
            else
              pnum.setVal(val);
          }else if (pthing instanceof PtgInt){
            PtgInt pint = (PtgInt)pthing;
            pint.setVal((Integer.valueOf(val).intValue()));
          }else if (pthing instanceof PtgBool){
            PtgBool pbool = (PtgBool)pthing;
            pbool.setVal(Boolean.valueOf(val).booleanValue());
          }else if(pthing instanceof PtgArray){
            PtgArray parr = (PtgArray)pthing;
            parr.setVal(val);
          }else if (pthing instanceof PtgName){ // SHOULD really return PtgErr("#NAME!") as it's a missing Name instead of adding a new name
            PtgName pname = (PtgName)pthing;           
            pname.setName(val);
          } else if (pthing instanceof PtgNameX) {
            PtgNameX pnameX= (PtgNameX) pthing;
            pnameX.setName(val);
          }else if (pthing instanceof PtgMissArg) {
            ((PtgMissArg)pthing).init(new byte[] {22});
          }else if (pthing instanceof PtgErr) {
            pthing= new PtgErr(PtgErr.convertStringToLookupByte(val));
          }
        }
        parseThings.add(pthing);
      } else {
        PtgMissArg pname= new PtgMissArg();
      }
     
    }
View Full Code Here

     *
     * One of the keys here is to not split on a comma from an internal function, for instance,
     * "IF((1<2),MOD(45,6),0) should not split between 45 & 6!  Note the badLocs vector that handles this.
     */
    private static CompatibleVector splitFunctionOperands(String formStr){
        CompatibleVector locs = new CompatibleVector();
      // if there are no commas then we don't have to do all of this...
        if (formStr.equals("")) return locs;  // KSC: Handle no parameters by returning a null vector

        // first handle quoted strings 20081111 KSC
        boolean loop= true;
       int pos = 0;
      CompatibleVector badLocs = new CompatibleVector();
      while(loop){
        char c= '"';
        int start = formStr.indexOf(c, pos);
        if (start==-1) {    // process single quotes as well
          c= '\'';
          start = formStr.indexOf(c, pos);
        }
        if (start != -1){
          int end = formStr.indexOf(c, start+1);
          end += 1//include trailing quote
          // check for being a part of a reference ... 
          if (end < formStr.length() && formStr.charAt(end)=='!') {// then it's part of a reference
            end++;
            while (end < formStr.length() && loop) {
              c= formStr.charAt(end);
              if (!(Character.isLetterOrDigit(c) || c==':' || c=='$') || c=='-' || c=='+')
                loop= false;
              else
                end++;
            }
          }       
          for (int y = start; y<end; y++){
            //make sure it is not a segment of a previous operand, like <> and >;
            badLocs.add(Integer.valueOf(y));
          }
          if (end==0){ // means it didn't find an end quote
            end= formStr.length()-1;
            loop = false;
          }else{
            pos = end;
            loop= true;
          }
        }else{
          loop = false;
        }
      } 
       
       
      if(formStr.indexOf(",") == -1){
        locs.add(formStr);
      }else
       // Handle each parameter (delimited by ,)   
       // fill the badLocs vector with string locations we should disregard for comma proccesing
       for (int i = 0; i<formStr.length();i++){
          int openparen = formStr.indexOf("(", i);
          if (openparen != -1){
            if (!badLocs.contains(Integer.valueOf(openparen))) {
              int closeparen = getMatchOperator(formStr, openparen, '(', ')');
              if (closeparen==-1) closeparen= formStr.length();
              for (i=openparen;i<closeparen;i++ ){
                Integer in = Integer.valueOf(i);
                badLocs.add(in);
              }             
            } else // open paren nested in quoted string
              i= openparen+1;
            }           
          } else  // 20081112 KSC
            break;
      }
      // lets do the same for the array items
      for (int i = 0; i<formStr.length();i++){
          int openparen = formStr.indexOf("{", i);
          if (openparen != -1){
            if (!badLocs.contains(Integer.valueOf(openparen))) {
              int closeparen = getMatchOperator(formStr, openparen, '{', '}');
              if (closeparen==-1) closeparen= formStr.length();
              for (i=openparen;i<closeparen;i++ ){
                Integer in = Integer.valueOf(i);
                badLocs.add(in);
              }
            } else // open paren nested in quoted string
              i= openparen+1;
            }
          } else  // 20081112 KSC
            break;
      }
         // now check bad locations:
        int placeholder = 0;
        int holder = 0;
        while (holder != -1){
          int i = formStr.indexOf(",", holder);
          if (i != -1){
            Integer ing = Integer.valueOf(i);
            if (!badLocs.contains(ing)){
              String s = formStr.substring(placeholder, i);
              locs.add(s);
              placeholder = i+1;
            }
          holder = i+1;
View Full Code Here

     * parse a given string into known Ptg operators
     * @param s
     * @return
     */
    private static CompatibleVector parsePtgOperators(String s, boolean bUnary) {
    CompatibleVector ret= new CompatibleVector();
    s= StringTool.allTrim(s);

    for (int i=0;i<XLSRecordFactory.ptgOps.length; i++){
      String ptgOpStr= XLSRecordFactory.ptgOps[i][0];
      if (s.startsWith("\"") || s.startsWith("'")) { 
        int end = s.substring(1).indexOf(s.charAt(0));
        end += 1//include trailing quote
        // TEST IF The quoted item is a sheet name
        if (end < s.length() && s.charAt(end)=='!') {// then it's part of a reference
          end++;
          boolean loop= true;
          while (end < s.length() && loop) {  // if the quoted string is a sheet ref, get rest of reference
            char c= s.charAt(end);
            if (c=='#' && s.endsWith("#REF!")) {
              end+=5;
              loop= false;
            } else if (!(Character.isLetterOrDigit(c) || c==':' || c=='$') || c=='-' || c=='+') {
              loop= false;
            } else
              end++;
          }
        }
        ret.add(s.substring(0, end+1));
        s= s.substring(end+1);
        bUnary= false;
        if (!s.equals(""))               
          ret.addAll(parsePtgOperators(s, bUnary));
        break;
      }

      int x = s.indexOf(ptgOpStr);
      if(x>-1) {  // found instance of an operator
        // if encounter a parenthesis, must determine if it is an expression limit OR
        // if it is part of a complex range, in which case the expression must be kept together
        if (ptgOpStr.equals("(")) {
          int end = getMatchOperator(s, x, '(', ')');
          ret.add(s)// add entire
          break;
/*          String ss= s.substring(x, end+1);         
          ret.add(ss.substring(x));  // add entire  
          if (FormulaParser.isComplexRange(ss)) {
//            ret.add(ss.substring(x+1));  // skip beginning paren as screws up later parsing
            ret.add(ss.substring(x)); 
            s= s.substring(end+1);
            bUnary= false;
            if (!s.isEmpty())               
              ret.addAll(parsePtgOperators(s, bUnary));
            break;
          }
        } else if (ptgOpStr.equals(")")) {
          try {
            String ss= s.substring(x+1);
            char nextChar= ss.charAt(0);
            if (nextChar==' ') {  // see if there is another operand after the space
              ss= ss.trim();
              if (ss.length()>0 && ss.matches("[^(a-zA-Z].*")) {
                nextChar= ss.charAt(0);
              }
            }
            // complex ranges can contain parentheses in combo with these operators: :, (
            if (nextChar==' ' || nextChar==',' || nextChar==':' || nextChar==')')
              continue;  // keep complex range expression together            
          } catch (Exception e) { ; }
*/         
        }
        if (ptgOpStr.equals(")"))  // parens are there to keep expression together
          continue
        if (x > 0) {// process prefix, if any - unary since it's the first operand
          // exception here-- error range in the form of "Sheet!#REF! (eg) needs to be kept whole
          if (!(XLSRecordFactory.ptgLookup[i][1].equals("PtgErr") && s.charAt(x-1)=='!')) {             
            ret.addAll(parsePtgOperators(s.substring(0, x), bUnary));
            bUnary= false;
          }
          else // keep entire error reference together
            ptgOpStr= s;
          }         
        }
        x= x+ ptgOpStr.length();       
        if (bUnary) {
          // unary ops +, - ... have a diff't Ptg than regular vers of the operator
                    if(ptgOpStr.equals("-")&&x==1&&ptgOpStr.length()>1)break; //negative number, NOT a unary -
          for (int j= 0; j < XLSRecordFactory.ptgPrefixOperators.length; j++) {
            if (ptgOpStr.startsWith(XLSRecordFactory.ptgPrefixOperators[j][0].toString())) {
              ptgOpStr= XLSRecordFactory.ptgPrefixOperators[j][1].toString();
            }
          }
        }       
        ret.add(ptgOpStr);
        if (x < s.length()) // process suffix, if any
          ret.addAll(parsePtgOperators(s.substring(x), true));
        break;
     
    }
    if (ret.isEmpty())
      ret.add(s);
    return ret;
    }
View Full Code Here

     * Parses a string and returns an array based on contents
     * Assumed to be 1 "final-level" operand i.e a range, complex range, a op b[...]  
     */
    private static CompatibleVector splitString(String formStr, boolean bIsComplete){
    // Use a vector, and the collections methods to sort in natural order
    CompatibleVector locs = new CompatibleVector();
    CompatibleVector retVect =  new CompatibleVector();
   
    // check for escaped string literals & add positions to vector if needed
    formStr= StringTool.allTrim(formStr);
    if (formStr.equals("")) {
      retVect.add(formStr);
      return retVect;
    }
    if (true) {
      retVect.addAll(parsePtgOperators(formStr, bIsComplete));    // cleanString if not an array formula????  s= cleanString(s);
      bIsComplete= false;
      return retVect;
    }
   
    // 20081207 KSC: redo completely to handle complex formula strings e.g. strings containing quoted commas, parens ...
    // first, pre-process to parse quoted strings, parentheses and array formulas
    boolean isArray= false;
    boolean loop = true;
    String s= "";
    boolean inRange= false;
    char prevc= 0;
    for (int i= 0; i < formStr.length(); i++) {
      char c= formStr.charAt(i);     
      if (c=='"' || c=='\'') {       
/*        if (!s.equals("")) {
          locs.add(s);
          s= "";
        }
*/       
        int end = formStr.indexOf(c, i+1);
        end += 1//include trailing quote
        // TEST IF The quoted item is a sheet name
        if (end < formStr.length() && formStr.charAt(end)=='!') {// then it's part of a reference
          end++;
          loop= true;
          while (end < formStr.length() && loop) {  // if the quoted string is a sheet ref, get rest of reference
            c= formStr.charAt(end);
            if (c=='#' && formStr.endsWith("#REF!")) {
              end+=5;
              loop= false;
            } else if (!(Character.isLetterOrDigit(c) || c==':' || c=='$') || c=='-' || c=='+') {
              loop= false;
            } else
              end++;
          }
        }
        locs.add(s+formStr.substring(i, end));
        s+= formStr.substring(i, end);
        i= end-1;
      } else if (c=='(') {  // may be a complex range if s==""
        if (!s.equals("") && !inRange) {
//          char prevc= s.charAt(s.length()-1);
          if (!(prevc==' ' || prevc==':' || prevc==',' || prevc=='(')) {
            locs.add(s);
            s= "";
          } else // DO NOT split apart complex ranges - they parse to PtgMemFuncs
            //Logger.logInfo("FormulaParser.splitString:  PtgMemFunc" + formStr);
            s+= c;
            inRange= true;
          }
        }
      } else if (c==':') {
        if (prevc==')' && locs.size()>0)   // complex range in style of: F(x):Y(x)
          s= (String) locs.get(locs.size()-1) + '(' + s;         
        inRange= true;
        s+= c;       
      } else  if (c=='{') {
        if (!s.equals("")) {
          locs.add(s);
          s= "";
        }
        int end = formStr.indexOf("}", i+1);
        end += 1//include trailing }
        locs.add(formStr.substring(i, end));
        i= end-1;
      } else
        s+= c;
      if (c!=' ')
        prevc= c;
    }
    if (!s.equals("")) {
      locs.add(s);
      s= "";
    }
   
    // loop through the possible operator ptg's and get locations & length of them   
    for (int j= 0; j < locs.size(); j++) {
      s= (String) locs.get(j);
      if (s.startsWith("\"") || s.startsWith("'")) 
        retVect.add(s)// quoted strings     
      else {
        if (s.startsWith("{"))  // it's an array formula
          isArray= true; // Do what?? else, cleanString??
        retVect.addAll(parsePtgOperators(s, bIsComplete));    // cleanString if not an array formula????  s= cleanString(s);
      }
      bIsComplete= false// already parsed part of the formula string so cannot be unary :)
    }       
    return retVect; 
    }
View Full Code Here

    /** returns all of the Columns in this WorkSheet
   
        @return ColHandle[] Columns
    */
    public ColHandle[] getColumns(){
        List<ColHandle> columns = new CompatibleVector();
       
        for (Colinfo c : mysheet.getColinfos()) {
          try{
            int start = c.getColFirst();
             int end = c.getColLast();
              for (int i = start; i<=end; i++){
                try{
                  columns.add(this.getCol(i));
                }catch(ColumnNotFoundException e){};
              }
          }catch(Exception ex){
            ;
          }
        }
       
        return columns.toArray( new ColHandle[ columns.size() ] );
    }
View Full Code Here

     */
  public void close() {
    if (mysheet!=null)
      mysheet.close();
    addedrows.clear();
    addedrows= new CompatibleVector();
    mysheet= null;
    mybook= null;
    wbh= null;
    dateFormats.clear();
    dateFormats= null;
View Full Code Here

    throws CellNotFoundException{
        if(false)
            return null;
        // first get the ptgArea3d from the parsed expression
        try{
            CompatibleVector cellhandles = new CompatibleVector();
            CellRange[] rngz = this.getCellRanges();
            if (rngz!=null) { 
              for(int t=0;t<rngz.length;t++) {
                 try{
                   CellHandle[] cells = rngz[t].getCells();
                     // for(int b = (cells.length-1);b>=0;b--)cellhandles.add(cells[b]);
                   for(int b =0; b< cells.length;b++)
                     if(cells[b]!=null)
                       cellhandles.add(cells[b]);
                 }catch(Exception ex){
                   Logger.logWarn("Could not get cells for range: " + rngz[t]);
                 }
              }
            } else {
              return null;
            }
            CellHandle[] ret = new CellHandle[cellhandles.size()];
            return (CellHandle[]) cellhandles.toArray(ret);
        }catch(Exception e){
          if(e instanceof CellNotFoundException)
            throw (CellNotFoundException)e;
          throw new CellNotFoundException(e.toString());
        }
View Full Code Here

TOP

Related Classes of com.extentech.toolkit.CompatibleVector

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.