CVS: jfor/src/org/jfor/jfor/converter FoUnitsConverter.java,1.8,1.9

Phil Chu <[email protected]> Wed, 22 Oct 2003 13:30:29 -0700
Newsgroups gmane.text.xml.jfor.cvs
Message-ID <[email protected]>
Update of /cvsroot/jfor/jfor/src/org/jfor/jfor/converter
In directory sc8-pr-cvs1:/tmp/cvs-serv24915/jfor/src/org/jfor/jfor/converter

Modified Files:
	FoUnitsConverter.java 
Log Message:
support for expression property values, e.g. "4in - -4pc"


Index: FoUnitsConverter.java
===================================================================
RCS file: /cvsroot/jfor/jfor/src/org/jfor/jfor/converter/FoUnitsConverter.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -C2 -d -r1.8 -r1.9
*** FoUnitsConverter.java	19 Oct 2003 19:44:21 -0000	1.8
--- FoUnitsConverter.java	22 Oct 2003 20:30:26 -0000	1.9
***************
*** 62,65 ****
--- 62,68 ----
  // $Id$
  // $Log$
+ // Revision 1.9  2003/10/22 20:30:26  philipchu
+ // support for expression property values, e.g. "4in - -4pc"
+ //
  // Revision 1.8  2003/10/19 19:44:21  philipchu
  // support for em and pc units
***************
*** 145,148 ****
--- 148,154 ----
      {
          foValue = foValue.trim();
+ 
+ 	// Phil Chu - handle empty properties in units.fo.xml test (but is it really required?)
+ 	if (foValue.length()==0) return 0;
          
          // break value into number and units
***************
*** 150,156 ****
          final StringBuffer units = new StringBuffer();
          
          for(int i=0; i < foValue.length(); i++) {
              final char c = foValue.charAt(i);
!             if(Character.isDigit(c) || c == '.') {
                  number.append(c);
              } else {
--- 156,169 ----
          final StringBuffer units = new StringBuffer();
          
+ 	// Phil Chu - handle expressions by using a tokenizer
+ 	// TODO: handle parentheses and functions, error check on missing tokens and bad operators
+ 	StringTokenizer toke = new StringTokenizer(foValue);
+ 	foValue = toke.nextToken();
+ 
          for(int i=0; i < foValue.length(); i++) {
              final char c = foValue.charAt(i);
! 
! 	    // Phil Chu - added check for minus to handle negative numbers
!             if(Character.isDigit(c) || c == '.' || c == '-') {
                  number.append(c);
              } else {
***************
*** 161,165 ****
          }
          
!         return numberToTwips(number.toString(),units.toString());
      }
      
--- 174,199 ----
          }
          
! 	// Phil Chu - if there is an operator, recurse on next operand
! 	float twips = numberToTwips(number.toString(),units.toString());
! 	if (toke.hasMoreTokens()) {
! 	    String op = toke.nextToken();
! 	    if (op.equals("-")) {
! 		twips -= convertToTwips(toke.nextToken());
! 	    }
! 	    if (op.equals("+")) {
! 		twips += convertToTwips(toke.nextToken());
! 	    }
! 	    if (op.equals("*")) {
! 		twips *= convertToTwips(toke.nextToken());
! 	    }
! 	    if (op.equals("div")) {
! 		twips /= convertToTwips(toke.nextToken());
! 	    }
! 	    if (op.equals("mod")) {
! 		twips %= convertToTwips(toke.nextToken());
! 	    }
! 	}
! 	return twips;
! 
      }
      



-------------------------------------------------------
This SF.net email is sponsored by OSDN developer relations
Here's your chance to show off your extensive product knowledge
We want to know what you know. Tell us and you have a chance to win $100
http://www.zoomerang.com/survey.zgi?HRPT1X3RYQNC5V4MLNSV3E54