]> source.dussan.org Git - poi.git/commitdiff
Merged revisions 638786-638802,638805-638811,638813-638814,638816-639230,639233-63924...
authorNick Burch <nick@apache.org>
Sat, 9 Aug 2008 10:26:39 +0000 (10:26 +0000)
committerNick Burch <nick@apache.org>
Sat, 9 Aug 2008 10:26:39 +0000 (10:26 +0000)
https://svn.apache.org/repos/asf/poi/trunk

........
  r684067 | josh | 2008-08-08 20:49:02 +0100 (Fri, 08 Aug 2008) | 1 line

  Standardised toString methods on ScalarConstantPtg subclasses
........
  r684075 | josh | 2008-08-08 21:14:24 +0100 (Fri, 08 Aug 2008) | 1 line

  improved error message for FormulaParser when the formula has a leading equals sign
........

git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@684216 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/hssf/model/FormulaParser.java
src/java/org/apache/poi/hssf/record/formula/BoolPtg.java
src/java/org/apache/poi/hssf/record/formula/ErrPtg.java
src/java/org/apache/poi/hssf/record/formula/IntPtg.java
src/java/org/apache/poi/hssf/record/formula/MissingArgPtg.java
src/java/org/apache/poi/hssf/record/formula/NumberPtg.java
src/java/org/apache/poi/hssf/record/formula/ScalarConstantPtg.java
src/java/org/apache/poi/hssf/record/formula/StringPtg.java
src/ooxml/java/org/apache/poi/xwpf/XWPFDocument.java
src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java
src/testcases/org/apache/poi/hssf/model/TestFormulaParser.java

index eb3f2ad6caa5fbf591c78867b1d5341e389169cb..d72d08eeaffa098d5eaf28d7a60eb6a303a62eb6 100644 (file)
@@ -20,7 +20,6 @@ package org.apache.poi.hssf.model;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Stack;
-import java.util.regex.Pattern;
 
 //import PTGs .. since we need everything, import *
 import org.apache.poi.hssf.record.formula.*;
@@ -138,9 +137,16 @@ public final class FormulaParser {
 
     /** Report What Was Expected */
     private RuntimeException expected(String s) {
-        String msg = "Parse error near char " + (pointer-1) + " '" + look + "'"
-            + " in specified formula '" + formulaString + "'. Expected "
-            + s;
+        String msg;
+        
+        if (look == '=' && formulaString.substring(0, pointer-1).trim().length() < 1) {
+            msg = "The specified formula '" + formulaString 
+                + "' starts with an equals sign which is not allowed.";
+        } else {
+            msg = "Parse error near char " + (pointer-1) + " '" + look + "'"
+                + " in specified formula '" + formulaString + "'. Expected "
+                + s;
+        }
         return new FormulaParseException(msg);
     }
 
index 3d9c496604ed3bf7920cc06ffd1b16a3dd649d0c..ae2fd541cc28bcdfc82b9fb5a3fb52df6b79a9f0 100644 (file)
@@ -17,7 +17,6 @@
 
 package org.apache.poi.hssf.record.formula;
 
-import org.apache.poi.ss.usermodel.Workbook;
 import org.apache.poi.hssf.record.RecordInputStream;
 
 /**
@@ -30,36 +29,30 @@ import org.apache.poi.hssf.record.RecordInputStream;
 public final class BoolPtg extends ScalarConstantPtg {
     public final static int  SIZE = 2;
     public final static byte sid  = 0x1d;
-    private final boolean field_1_value;
+    private final boolean _value;
 
-    public BoolPtg(RecordInputStream in)
-    {
-        field_1_value = (in.readByte() == 1);
+    public BoolPtg(RecordInputStream in) {
+        _value = (in.readByte() == 1);
     }
 
-
     public BoolPtg(String formulaToken) {
-        field_1_value = (formulaToken.equals("TRUE"));
+        _value = (formulaToken.equalsIgnoreCase("TRUE"));
     }
 
-    public boolean getValue()
-    {
-        return field_1_value;
+    public boolean getValue() {
+        return _value;
     }
 
-    public void writeBytes(byte [] array, int offset)
-    {
+    public void writeBytes(byte [] array, int offset) {
         array[ offset + 0 ] = sid;
-        array[ offset + 1 ] = (byte) (field_1_value ? 1 : 0);
+        array[ offset + 1 ] = (byte) (_value ? 1 : 0);
     }
 
-    public int getSize()
-    {
+    public int getSize() {
         return SIZE;
     }
 
-    public String toFormulaString(Workbook book)
-    {
-        return field_1_value ? "TRUE" : "FALSE";
+    protected String toFormulaString() {
+        return _value ? "TRUE" : "FALSE";
     }
 }
index 27a2b29de52cb3bb5ae2cb101fe4e5382e7ea57d..aef580d5e430124445b747d3d92e2991819a1262 100644 (file)
@@ -17,7 +17,6 @@
 
 package org.apache.poi.hssf.record.formula;
 
-import org.apache.poi.ss.usermodel.Workbook;
 import org.apache.poi.hssf.record.RecordInputStream;
 import org.apache.poi.hssf.usermodel.HSSFErrorConstants;
 
@@ -68,7 +67,7 @@ public final class ErrPtg extends ScalarConstantPtg {
         array[offset + 1] = (byte)field_1_error_code;
     }
 
-    public String toFormulaString(Workbook book) {
+    protected String toFormulaString() {
         return HSSFErrorConstants.getText(field_1_error_code);
     }
 
index 609b3ea451a99988687b402a89bd10660a57639e..c4abb7b1bc2bd9e563c8280b14e9afaced69c3b6 100644 (file)
@@ -17,9 +17,8 @@
 
 package org.apache.poi.hssf.record.formula;
 
-import org.apache.poi.util.LittleEndian;
-import org.apache.poi.ss.usermodel.Workbook;
 import org.apache.poi.hssf.record.RecordInputStream;
+import org.apache.poi.util.LittleEndian;
 
 /**
  * Integer (unsigned short integer)
@@ -60,8 +59,7 @@ public final class IntPtg extends ScalarConstantPtg {
         return field_1_value;
     }
 
-    public void writeBytes(byte [] array, int offset)
-    {
+    public void writeBytes(byte [] array, int offset) {
         array[ offset + 0 ] = sid;
         LittleEndian.putUShort(array, offset + 1, getValue());
     }
@@ -70,15 +68,7 @@ public final class IntPtg extends ScalarConstantPtg {
         return SIZE;
     }
 
-    public String toFormulaString(Workbook book) {
+    protected String toFormulaString() {
         return String.valueOf(getValue());
     }
-
-    public String toString() {
-        StringBuffer sb = new StringBuffer(64);
-        sb.append(getClass().getName()).append(" [");
-        sb.append(field_1_value);
-        sb.append("]");
-        return sb.toString();
-    }
 }
index 19f56cff741df4613f2ba98dfa1319453ea04336..ffcf423b4f1559aa994bc0efec57667cd3a9d1ca 100644 (file)
@@ -17,8 +17,6 @@
 
 package org.apache.poi.hssf.record.formula;
 
-import org.apache.poi.ss.usermodel.Workbook;
-
 /**
  * Missing Function Arguments
  *
@@ -31,22 +29,20 @@ public final class MissingArgPtg extends ScalarConstantPtg {
     public final static byte sid  = 0x16;
    
     public static final Ptg instance = new MissingArgPtg();
-    private MissingArgPtg()
-    {
+
+    private MissingArgPtg() {
+       // enforce singleton
     }
      
-    public void writeBytes(byte [] array, int offset)
-    {
+    public void writeBytes(byte [] array, int offset) {
         array[ offset + 0 ] = sid;
     }
 
-    public int getSize()
-    {
+    public int getSize() {
         return SIZE;
     }
    
-    public String toFormulaString(Workbook book)
-    {
+    protected String toFormulaString() {
         return " ";
     }
 }
index f02275860c4be929ac75f44c7792aac571efa238..d7f7195e05051374e68bd64742af5178de871eb8 100644 (file)
@@ -17,9 +17,8 @@
 
 package org.apache.poi.hssf.record.formula;
 
-import org.apache.poi.util.LittleEndian;
-import org.apache.poi.ss.usermodel.Workbook;
 import org.apache.poi.hssf.record.RecordInputStream;
+import org.apache.poi.util.LittleEndian;
 
 /**
  * Number
@@ -34,8 +33,7 @@ public final class NumberPtg extends ScalarConstantPtg {
     private final double field_1_value;
         
     /** Create a NumberPtg from a byte array read from disk */
-    public NumberPtg(RecordInputStream in)
-    {
+    public NumberPtg(RecordInputStream in) {
         this(in.readDouble());
     }
     
@@ -52,33 +50,22 @@ public final class NumberPtg extends ScalarConstantPtg {
         field_1_value = value;
     }
     
-    public double getValue()
-    {
+    public double getValue() {
         return field_1_value;
     }
 
-    public void writeBytes(byte [] array, int offset)
-    {
+    public void writeBytes(byte [] array, int offset) {
         array[ offset + 0 ] = sid;
         LittleEndian.putDouble(array, offset + 1, getValue());
     }
 
-    public int getSize()
-    {
+    public int getSize() {
         return SIZE;
     }
 
-    public String toFormulaString(Workbook book)
-    {
+    protected String toFormulaString() {
         // TODO - java's rendering of double values is not quite same as excel's
+        // Maybe use HSSFDataFormatter?
         return String.valueOf(field_1_value);
     }
-    
-    public String toString() {
-        StringBuffer sb = new StringBuffer(64);
-        sb.append(getClass().getName()).append(" [");
-        sb.append(field_1_value);
-        sb.append("]");
-        return sb.toString();
-    }
 }
index 8b00d327fccdfe525fa01015125396e0168a108c..bb1613c9acc5d93f51465cb57767882d8cdf2602 100644 (file)
@@ -17,6 +17,8 @@
 \r
 package org.apache.poi.hssf.record.formula;\r
 \r
+import org.apache.poi.ss.usermodel.Workbook;\r
+\r
 /**\r
  * @author Josh Micich\r
  */\r
@@ -24,7 +26,22 @@ abstract class ScalarConstantPtg extends Ptg {
        public boolean isBaseToken() {\r
                return true;\r
        }\r
-    public final byte getDefaultOperandClass() {\r
-        return Ptg.CLASS_VALUE;\r
-    }\r
+\r
+       public final byte getDefaultOperandClass() {\r
+               return Ptg.CLASS_VALUE;\r
+       }\r
+\r
+       public final String toFormulaString(Workbook book) {\r
+               return toFormulaString();\r
+       }\r
+\r
+       protected abstract String toFormulaString();\r
+\r
+       public final String toString() {\r
+               StringBuffer sb = new StringBuffer(64);\r
+               sb.append(getClass().getName()).append(" [");\r
+               sb.append(toFormulaString());\r
+               sb.append("]");\r
+               return sb.toString();\r
+       }\r
 }\r
index 88b4d9843d59852aa05ca2654f4e5b4c9f5d09a1..7007b48fdb71b13798f0bafa005aed0e9f8e26fe 100644 (file)
 
 package org.apache.poi.hssf.record.formula;
 
-import org.apache.poi.ss.usermodel.Workbook;
+import org.apache.poi.hssf.record.RecordInputStream;
 import org.apache.poi.util.BitField;
 import org.apache.poi.util.BitFieldFactory;
 import org.apache.poi.util.StringUtil;
-import org.apache.poi.hssf.record.RecordInputStream;
 
 /**
  * String Stores a String value in a formula value stored in the format
@@ -55,8 +54,6 @@ public final class StringPtg extends ScalarConstantPtg {
         } else {
             field_3_string = in.readCompressedUnicode(field_1_length);
         }
-
-        // setValue(new String(data, offset+3, data[offset+1] + 256*data[offset+2]));
     }
 
     /**
@@ -100,7 +97,7 @@ public final class StringPtg extends ScalarConstantPtg {
         }
     }
 
-    public String toFormulaString(Workbook book) {
+    protected String toFormulaString() {
         String value = field_3_string;
         int len = value.length();
         StringBuffer sb = new StringBuffer(len + 4);
@@ -117,12 +114,4 @@ public final class StringPtg extends ScalarConstantPtg {
         sb.append(FORMULA_DELIMITER);
         return sb.toString();
     }
-
-    public String toString() {
-        StringBuffer sb = new StringBuffer(64);
-        sb.append(getClass().getName()).append(" [");
-        sb.append(field_3_string);
-        sb.append("]");
-        return sb.toString();
-    }
 }
index d7fa573bba080f1bbd94a55dfb876b7aeb556192..219cacec142a28b6c7f88a246b9c1d0d055677a8 100644 (file)
@@ -104,22 +104,21 @@ public class XWPFDocument extends POIXMLDocument {
                if(commentsRel != null && commentsRel.size() > 0) {
                        PackagePart commentsPart = getTargetPart(commentsRel.getRelationship(0));
                        CommentsDocument cmntdoc = CommentsDocument.Factory.parse(commentsPart.getInputStream());
-                       for(CTComment ctcomment : cmntdoc.getComments().getCommentArray())
-                       {
+                       for(CTComment ctcomment : cmntdoc.getComments().getCommentArray()) {
                                comments.add(new XWPFComment(ctcomment));
                        }
-                       
-                       for(CTTbl table : getDocumentBody().getTblArray())
-                       {
-                               tables.add(new XWPFTable(table));
-                       }
                }
                
+               // Get any tables
+               for(CTTbl table : getDocumentBody().getTblArray()) {
+                       tables.add(new XWPFTable(table));
+               }
+               
+               /// Process embedded document parts
         this.embedds = new LinkedList<PackagePart>();
         for(PackageRelationship rel : getCorePart().getRelationshipsByType(OLE_OBJECT_REL_TYPE)) {
             embedds.add(getTargetPart(rel));
         }
-        
         for(PackageRelationship rel : getCorePart().getRelationshipsByType(PACK_OBJECT_REL_TYPE)) {
             embedds.add(getTargetPart(rel));
         }
index 57527cac01ed349aa403307f7528920b0571ffb5..813ae140d41fe4d5152232dd59cae982a6c22f10 100644 (file)
@@ -18,9 +18,13 @@ package org.apache.poi.xwpf.usermodel;
 
 import org.apache.poi.xwpf.model.XMLParagraph;
 import org.apache.poi.xwpf.XWPFDocument;
+import org.apache.xmlbeans.XmlObject;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPicture;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText;
+import org.w3c.dom.NodeList;
+import org.w3c.dom.Text;
 
 /**
  * Sketch of XWPF paragraph class
@@ -32,6 +36,7 @@ public class XWPFParagraph extends XMLParagraph
      * TODO - replace with RichText String
      */
     private StringBuffer text = new StringBuffer();
+    private StringBuffer pictureText = new StringBuffer();
     
     public XWPFParagraph(CTP prgrph, XWPFDocument docRef)
     {
@@ -49,6 +54,21 @@ public class XWPFParagraph extends XMLParagraph
                         texts[k].getStringValue()
                 );
             }
+            
+            // Loop over pictures inside our
+            //  paragraph, looking for text in them
+            CTPicture[] picts = rs[j].getPictArray();
+            for (int k = 0; k < picts.length; k++) {
+                XmlObject[] t = picts[k].selectPath("declare namespace w='http://schemas.openxmlformats.org/wordprocessingml/2006/main' .//w:t");
+                for (int m = 0; m < t.length; m++) {
+                    NodeList kids = t[m].getDomNode().getChildNodes();
+                    for (int n = 0; n < kids.getLength(); n++) {
+                        if (kids.item(n) instanceof Text) {
+                            pictureText.append("\n" + kids.item(n).getNodeValue());
+                        }
+                    }
+                }
+            }
         }
     }
     
@@ -64,6 +84,10 @@ public class XWPFParagraph extends XMLParagraph
         return docRef;
     }
     
+    /**
+     * Return the textual content of the paragraph, 
+     *  including text from pictures in it.
+     */
     public String getText() {
         return text.toString();
     }
index a21850887ca7280187f0b02f8b621d5383ffef4c..e1909baa0a9cc6d661f524f58694fe88d774846d 100644 (file)
@@ -156,10 +156,10 @@ public final class TestFormulaParser extends TestCase {
                HSSFRow row = sheet.createRow(0);
                HSSFCell cell;
 
-               cell = row.createCell((short)0);
+               cell = row.createCell(0);
                cell.setCellFormula("NoQuotesNeeded!A1");
 
-               cell = row.createCell((short)1);
+               cell = row.createCell(1);
                cell.setCellFormula("'Quotes Needed Here &#$@'!A1");
        }
 
@@ -226,7 +226,7 @@ public final class TestFormulaParser extends TestCase {
                HSSFRow row = sheet.createRow(0);
                HSSFCell cell;
 
-               cell = row.createCell((short)0);
+               cell = row.createCell(0);
                cell.setCellFormula("Cash_Flow!A1");
        }
 
@@ -259,7 +259,7 @@ public final class TestFormulaParser extends TestCase {
 
                HSSFSheet sheet = wb.createSheet("Test");
                HSSFRow row = sheet.createRow(0);
-               HSSFCell cell = row.createCell((short)0);
+               HSSFCell cell = row.createCell(0);
                String formula = null;
 
                cell.setCellFormula("1.3E21/3");
@@ -330,7 +330,7 @@ public final class TestFormulaParser extends TestCase {
 
                HSSFSheet sheet = wb.createSheet("Test");
                HSSFRow row = sheet.createRow(0);
-               HSSFCell cell = row.createCell((short)0);
+               HSSFCell cell = row.createCell(0);
                String formula = null;
 
                // starts from decimal point
@@ -369,7 +369,7 @@ public final class TestFormulaParser extends TestCase {
 
                HSSFSheet sheet = wb.createSheet("Test");
                HSSFRow row = sheet.createRow(0);
-               HSSFCell cell = row.createCell((short)0);
+               HSSFCell cell = row.createCell(0);
                String formula = null;
 
                cell.setCellFormula("A1.A2");
@@ -566,6 +566,7 @@ public final class TestFormulaParser extends TestCase {
                confirmParseErrorLiteral(ErrPtg.NAME_INVALID, "#NAME?");
                confirmParseErrorLiteral(ErrPtg.NUM_ERROR, "#NUM!");
                confirmParseErrorLiteral(ErrPtg.N_A, "#N/A");
+               parseFormula("HLOOKUP(F7,#REF!,G7,#REF!)");
        }
 
        private static void confirmParseErrorLiteral(ErrPtg expectedToken, String formula) {
@@ -603,7 +604,7 @@ public final class TestFormulaParser extends TestCase {
                wb.setSheetName(0, "Sheet1");
 
                HSSFRow row = sheet.createRow(0);
-               HSSFCell cell = row.createCell((short)0);
+               HSSFCell cell = row.createCell(0);
                cell.setCellFormula("right(\"test\"\"ing\", 3)");
                String actualCellFormula = cell.getCellFormula();
                if("RIGHT(\"test\"ing\",3)".equals(actualCellFormula)) {
@@ -670,7 +671,7 @@ public final class TestFormulaParser extends TestCase {
                wb.setSheetName(0, "Sheet1");
 
                HSSFRow row = sheet.createRow(0);
-               HSSFCell cell = row.createCell((short)0);
+               HSSFCell cell = row.createCell(0);
                cell.setCellFormula("SUM(A32769:A32770)");
                if("SUM(A-32767:A-32766)".equals(cell.getCellFormula())) {
                        fail("Identified bug 44539");
@@ -768,6 +769,13 @@ public final class TestFormulaParser extends TestCase {
                } catch (FormulaParseException e) {
                        assertEquals("Parse error near char 10 ';' in specified formula 'round(3.14;2)'. Expected ',' or ')'", e.getMessage());
                }
+
+               try {
+                       parseFormula(" =2+2");
+                       throw new AssertionFailedError("Didn't get parse exception as expected");
+               } catch (FormulaParseException e) {
+                       assertEquals("The specified formula ' =2+2' starts with an equals sign which is not allowed.", e.getMessage());
+               }
        }
        
        /**
@@ -811,7 +819,7 @@ public final class TestFormulaParser extends TestCase {
                assertEquals(2, ptgs.length);
                assertEquals(NamePtg.class, ptgs[0].getClass());
 
-               HSSFCell cell = sheet.createRow(0).createCell((short)0);
+               HSSFCell cell = sheet.createRow(0).createCell(0);
                cell.setCellFormula("count(pfy1)");
                assertEquals("COUNT(pfy1)", cell.getCellFormula());
                try {
@@ -823,6 +831,5 @@ public final class TestFormulaParser extends TestCase {
                        }
                }
                cell.setCellFormula("count(fp1)"); // plain cell ref, col is in range
-               
        }
 }