]> source.dussan.org Git - poi.git/commitdiff
Add support for Chart Title Format records (bug #43721)
authorNick Burch <nick@apache.org>
Mon, 12 Nov 2007 21:49:37 +0000 (21:49 +0000)
committerNick Burch <nick@apache.org>
Mon, 12 Nov 2007 21:49:37 +0000 (21:49 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@594307 13f79535-47bb-0310-9956-ffa450edef68

src/documentation/content/xdocs/changes.xml
src/documentation/content/xdocs/status.xml
src/java/org/apache/poi/hssf/record/ChartTitleFormatRecord.java [new file with mode: 0644]
src/java/org/apache/poi/hssf/record/RecordFactory.java
src/testcases/org/apache/poi/hssf/data/WithFormattedGraphTitle.xls [new file with mode: 0644]
src/testcases/org/apache/poi/hssf/record/TestChartTitleFormatRecord.java [new file with mode: 0644]

index f88c1582f6d932520d409d58d12db9d4d78e8e16..ca6a4752539803b6e1799daa99bbde5c29d21f50 100644 (file)
@@ -36,6 +36,7 @@
 
                <!-- Don't forget to update status.xml too! -->
         <release version="3.0.2-FINAL" date="2007-??-??">
+            <action dev="POI-DEVELOPERS" type="add">43721 - [PATCH] Support for Chart Title Format records</action>
             <action dev="POI-DEVELOPERS" type="fix">42794 - [PATCH] Fix for BOF records from things like Access</action>
             <action dev="POI-DEVELOPERS" type="fix">43648 - Fix for IntPtg and short vs int</action>
             <action dev="POI-DEVELOPERS" type="fix">43751 - [PATCH] - Fix for handling rotated text in HSSFSheet.autoSizeColumn</action>
index 1dc3b60bcd5277947e0d8d157b236cc8c1fd5569..2b561f554934a8c353eae376be06ac5d57c2d83f 100644 (file)
@@ -33,6 +33,7 @@
        <!-- Don't forget to update changes.xml too! -->
     <changes>
         <release version="3.0.2-FINAL" date="2007-??-??">
+            <action dev="POI-DEVELOPERS" type="add">43721 - [PATCH] Support for Chart Title Format records</action>
             <action dev="POI-DEVELOPERS" type="fix">42794 - [PATCH] Fix for BOF records from things like Access</action>
             <action dev="POI-DEVELOPERS" type="fix">43648 - Fix for IntPtg and short vs int</action>
             <action dev="POI-DEVELOPERS" type="fix">43751 - [PATCH] - Fix for handling rotated text in HSSFSheet.autoSizeColumn</action>
diff --git a/src/java/org/apache/poi/hssf/record/ChartTitleFormatRecord.java b/src/java/org/apache/poi/hssf/record/ChartTitleFormatRecord.java
new file mode 100644 (file)
index 0000000..a9ec81e
--- /dev/null
@@ -0,0 +1,146 @@
+/* ====================================================================
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+==================================================================== */
+
+/*
+ * HSSF Chart Title Format Record Type
+ */
+package org.apache.poi.hssf.record;
+
+import org.apache.poi.util.LittleEndian;
+
+import java.util.ArrayList;
+
+/**
+ * Describes the formatting runs associated with a chart title.
+ */
+public class ChartTitleFormatRecord extends Record {
+       public static final short sid = 0x1050;
+       
+       private int             m_recs;
+       
+       private class CTFormat {
+               private short m_offset;
+               private short m_fontIndex;
+               
+               protected CTFormat(short offset,short fontIdx){
+                       m_offset = offset;
+                       m_fontIndex = fontIdx;
+               }
+               
+               public short getOffset(){
+                       return m_offset;
+               }
+               public void setOffset(short newOff){
+                       m_offset = newOff;
+               }
+               public short getFontIndex() {
+                       return m_fontIndex;
+               }
+       }
+       
+       private ArrayList m_formats;
+
+       public ChartTitleFormatRecord() {
+               super();
+       }
+
+       public ChartTitleFormatRecord(RecordInputStream in) {
+               super(in);
+       }
+
+       protected void validateSid(short id) {
+               if (id != sid)
+        {
+            throw new RecordFormatException("NOT A CHARTTITLEFORMAT RECORD");
+        }
+       }
+
+       protected void fillFields(RecordInputStream in) {
+               m_recs = in.readUShort();
+               int idx;
+               CTFormat ctf;
+               if (m_formats == null){
+                       m_formats = new ArrayList(m_recs);
+               }
+               for(idx=0;idx<m_recs;idx++) {
+                       ctf = new CTFormat(in.readShort(),in.readShort());
+                       m_formats.add(ctf);
+               }
+       }
+
+       public int serialize(int offset, byte [] data)
+    {
+        LittleEndian.putShort(data, 0 + offset, sid);
+        LittleEndian.putShort(data, 2 + offset,
+                              ( short ) (getRecordSize() - 4));   
+        int idx;
+        CTFormat ctf;
+        LittleEndian.putShort(data, 4 + offset,(short)m_formats.size());
+        for(idx=0;idx<m_formats.size();idx++){
+               ctf = (CTFormat)m_formats.get(idx);
+               LittleEndian.putShort(data, 6 + (idx * 4) + offset, ctf.getOffset());
+               LittleEndian.putShort(data, 8 + (idx * 4) + offset, ctf.getFontIndex());
+        }
+        
+        return getRecordSize();
+    }
+
+    public int getRecordSize()
+    {
+        return 4 + 2 + (4 * m_formats.size());
+    }
+    
+       public short getSid() {
+               return sid;
+       }
+       
+       public int getFormatCount() {
+               return m_formats.size();
+       }
+       
+       public void modifyFormatRun(short oldPos,short newLen) {
+               short shift = (short)0;
+               for(int idx=0;idx < m_formats.size();idx++) {
+                       CTFormat ctf = (CTFormat)m_formats.get(idx);
+                       if (shift != 0) {
+                               ctf.setOffset((short)(ctf.getOffset() + shift));
+                       } else if ((oldPos == ctf.getOffset()) && (idx < (m_formats.size() - 1))){
+                               CTFormat nextCTF = (CTFormat)m_formats.get(idx + 1);
+                               shift = (short)(newLen - (nextCTF.getOffset() - ctf.getOffset()));
+                       } 
+               }
+       }
+       
+       public String toString()
+    {
+        StringBuffer buffer = new StringBuffer();
+
+        buffer.append("[CHARTTITLEFORMAT]\n");
+        buffer.append("    .format_runs       = ").append(m_recs)
+            .append("\n");
+        int idx;
+        CTFormat ctf;
+        for(idx=0;idx<m_formats.size();idx++){
+               ctf = (CTFormat)m_formats.get(idx);
+               buffer.append("       .char_offset= ").append(ctf.getOffset());
+               buffer.append(",.fontidx= ").append(ctf.getFontIndex());
+            buffer.append("\n");
+        }
+        buffer.append("[/CHARTTITLEFORMAT]\n");
+        return buffer.toString();
+    }
+}
index 8f78212616ac3edde4ab1c75a529ff3b05b814c4..398576dc7c4c802d74d070a43f9c09afaecde65d 100644 (file)
@@ -74,7 +74,8 @@ public class RecordFactory
                 PaletteRecord.class, StringRecord.class, RecalcIdRecord.class, SharedFormulaRecord.class,
                 HorizontalPageBreakRecord.class, VerticalPageBreakRecord.class, 
                 WriteProtectRecord.class, FilePassRecord.class, PaneRecord.class,
-                NoteRecord.class, ObjectProtectRecord.class, ScenarioProtectRecord.class, FileSharingRecord.class
+                NoteRecord.class, ObjectProtectRecord.class, ScenarioProtectRecord.class, 
+                FileSharingRecord.class, ChartTitleFormatRecord.class
             };
     }
     private static Map           recordsMap  = recordsToMap(records);
diff --git a/src/testcases/org/apache/poi/hssf/data/WithFormattedGraphTitle.xls b/src/testcases/org/apache/poi/hssf/data/WithFormattedGraphTitle.xls
new file mode 100644 (file)
index 0000000..477b1f6
Binary files /dev/null and b/src/testcases/org/apache/poi/hssf/data/WithFormattedGraphTitle.xls differ
diff --git a/src/testcases/org/apache/poi/hssf/record/TestChartTitleFormatRecord.java b/src/testcases/org/apache/poi/hssf/record/TestChartTitleFormatRecord.java
new file mode 100644 (file)
index 0000000..d9931ef
--- /dev/null
@@ -0,0 +1,87 @@
+
+/* ====================================================================
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+==================================================================== */
+        
+
+package org.apache.poi.hssf.record;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.InputStream;
+import java.util.ArrayList;
+
+import org.apache.poi.hssf.eventusermodel.HSSFEventFactory;
+import org.apache.poi.hssf.eventusermodel.HSSFListener;
+import org.apache.poi.hssf.eventusermodel.HSSFRequest;
+import org.apache.poi.hssf.usermodel.HSSFSheet;
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
+import org.apache.poi.poifs.filesystem.POIFSFileSystem;
+
+import junit.framework.TestCase;
+
+public class TestChartTitleFormatRecord extends TestCase
+{
+    private String _test_file_path;
+    private static final String _test_file_path_property = "HSSF.testdata.path";
+
+    public TestChartTitleFormatRecord()
+    {
+        super();
+        _test_file_path = System.getProperty( _test_file_path_property ) +
+               File.separator + "WithFormattedGraphTitle.xls";
+    }
+
+    public void testRecord() throws Exception {
+       POIFSFileSystem fs = new POIFSFileSystem(
+                       new FileInputStream(_test_file_path)
+       );
+       
+       // Check we can open the file via usermodel
+       HSSFWorkbook hssf = new HSSFWorkbook(fs);
+       
+       // Now process it through eventusermodel, and
+       //  look out for the title records
+       ChartTitleFormatRecordGrabber grabber =
+               new ChartTitleFormatRecordGrabber();
+       InputStream din = fs.createDocumentInputStream("Workbook");
+        HSSFRequest req = new HSSFRequest();
+        req.addListenerForAllRecords(grabber);
+        HSSFEventFactory factory = new HSSFEventFactory();
+        factory.processEvents(req, din);
+        din.close();
+        
+        // Should've found one
+        assertEquals(1, grabber.chartTitleFormatRecords.size());
+        // And it should be of something interesting
+        ChartTitleFormatRecord r =
+               (ChartTitleFormatRecord)grabber.chartTitleFormatRecords.get(0);
+        assertEquals(3, r.getFormatCount());
+    }
+    
+    public static class ChartTitleFormatRecordGrabber implements HSSFListener {
+       private ArrayList chartTitleFormatRecords = new ArrayList();
+
+               public void processRecord(Record record) {
+                       if(record instanceof ChartTitleFormatRecord) {
+                               chartTitleFormatRecords.add(
+                                               record
+                               );
+                       }
+               }
+       
+    }
+}