poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From n...@apache.org
Subject svn commit: r684990 [1/3] - in /poi/branches/ooxml: ./ src/documentation/content/xdocs/ src/java/org/apache/poi/hssf/model/ src/java/org/apache/poi/hssf/record/ src/java/org/apache/poi/hssf/record/formula/ src/java/org/apache/poi/hssf/usermodel/ src/oo...
Date Mon, 11 Aug 2008 23:58:56 GMT
Author: nick
Date: Mon Aug 11 16:58:54 2008
New Revision: 684990

URL: http://svn.apache.org/viewvc?rev=684990&view=rev
Log:
Merged revisions 638786-638802,638805-638811,638813-638814,638816-639230,639233-639241,639243-639253,639255-639486,639488-639601,639603-639835,639837-639917,639919-640056,640058-640710,640712-641156,641158-641184,641186-641795,641797-641798,641800-641933,641935-641963,641965-641966,641968-641995,641997-642230,642232-642562,642564-642565,642568-642570,642572-642573,642576-642736,642739-642877,642879,642881-642890,642892-642903,642905-642945,642947-643624,643626-643653,643655-643669,643671,643673-643830,643832-643833,643835-644342,644344-644472,644474-644508,644510-645347,645349-645351,645353-645559,645561-645565,645568-645951,645953-646193,646195-646311,646313-646404,646406-646665,646667-646853,646855-646869,646871-647151,647153-647185,647187-647277,647279-647566,647568-647573,647575,647578-647711,647714-647737,647739-647823,647825-648155,648157-648202,648204-648273,648275,648277-648302,648304-648333,648335-648588,648590-648622,648625-648673,648675-649141,649144,649146-649556,
 649558-649795,649799,649801-649910,649912-649913,649915-650128,650131-650132,650134-650137,650140-650914,650916-651991,651993-652284,652286-652287,652289,652291,652293-652297,652299-652328,652330-652425,652427-652445,652447-652560,652562-652933,652935,652937-652993,652995-653116,653118-653124,653126-653483,653487-653519,653522-653550,653552-653607,653609-653667,653669-653674,653676-653814,653817-653830,653832-653891,653893-653944,653946-654055,654057-654355,654357-654365,654367-654648,654651-655215,655217-655277,655279-655281,655283-655911,655913-656212,656214,656216-656251,656253-656698,656700-656756,656758-656892,656894-657135,657137-657165,657168-657179,657181-657354,657356-657357,657359-657701,657703-657874,657876-658032,658034-658284,658286,658288-658301,658303-658307,658309-658321,658323-658335,658337-658348,658351,658353-658832,658834-658983,658985,658987-659066,659068-659402,659404-659428,659430-659451,659453-659454,659456-659461,659463-659477,659479-659524,659526-65
 9571,659574,659576-660255,660257-660262,660264-660279,660281-660343,660345-660473,660475-660827,660829-660833,660835-660888,660890-663321,663323-663435,663437-663764,663766-663854,663856-664219,664221-664489,664494-664514,664516-668013,668015-668142,668144-668152,668154,668156-668256,668258,668260-669139,669141-669455,669457-669657,669659-669808,669810-670189,670191-671321,671323-672229,672231-672549,672551-672552,672554-672561,672563-672566,672568,672571-673049,673051-673852,673854-673862,673864-673986,673988-673996,673998-674347,674349-674890,674892-674910,674912-674936,674938-674952,674954-675078,675080-675085,675087-675217,675219-675660,675662-675670,675672-675716,675718-675726,675728-675733,675735-675775,675777-675782,675784,675786-675791,675794-675852,675854-676200,676202,676204,676206-676220,676222-676309,676311-676456,676458-676994,676996-677027,677030-677040,677042-677056,677058-677375,677377-677968,677970-677971,677973,677975-677994,677996-678286,678288-678538,6785
 40-680393,680395-680469,680471-680529,680531-680852,680854-681529,681531-681571,681573-682224,682226,682228,682231-682281,682283-682335,682337-682507,682509,682512-682517,682519-682532,682534-682619,682622-682777,682779-682998,683000-683019,683021-683022,683024-683080,683082-683092,683094-683095,683097-683127,683129-683131,683133-683166,683168-683698,683700-683705,683707-683757,683759-683787,683789-683870,683872-683879,683881-683900,683902-684066,684068-684074,684076-684222,684224-684254,684257-684281,684283-684286,684288-684292,684294-684298,684300-684301,684303-684308,684310-684317,684320,684323-684335,684337-684348,684350-684354,684356-684361,684363-684369,684371-684453,684455-684986 via svnmerge from 
https://svn.apache.org/repos/asf/poi/trunk

........
  r684884 | josh | 2008-08-11 20:28:58 +0100 (Mon, 11 Aug 2008) | 1 line
  
  deleted obsolete comment (should have been done in c669809)
........
  r684938 | josh | 2008-08-11 22:24:19 +0100 (Mon, 11 Aug 2008) | 1 line
  
  Refinements to fix for bug 45126.  Excel does not produce any records like 'Excel_Name_Record_Titles_*' 
........
  r684939 | nick | 2008-08-11 22:25:17 +0100 (Mon, 11 Aug 2008) | 1 line
  
  CHPXs and PAPXs are apparently cp based, but are really byte based! Work around this
........
  r684959 | nick | 2008-08-11 23:07:37 +0100 (Mon, 11 Aug 2008) | 1 line
  
  Get insert based HWPF tests working fine, delete ones still problematic
........
  r684971 | josh | 2008-08-11 23:55:38 +0100 (Mon, 11 Aug 2008) | 1 line
  
  initial work on supporting calls to add-in functions
........
  r684986 | nick | 2008-08-12 00:42:39 +0100 (Tue, 12 Aug 2008) | 1 line
  
  Finally get all HWPF tests to pass again, by working around how evil PAPX/CHPX/SEPX byte references are
........

Added:
    poi/branches/ooxml/src/scratchpad/src/org/apache/poi/hwpf/model/BytePropertyNode.java
      - copied unchanged from r684986, poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/model/BytePropertyNode.java
    poi/branches/ooxml/src/testcases/org/apache/poi/hssf/data/testNames.xls
      - copied unchanged from r684986, poi/trunk/src/testcases/org/apache/poi/hssf/data/testNames.xls
Removed:
    poi/branches/ooxml/src/java/org/apache/poi/hssf/record/ExternSheetSubRecord.java
Modified:
    poi/branches/ooxml/   (props changed)
    poi/branches/ooxml/src/documentation/content/xdocs/changes.xml
    poi/branches/ooxml/src/documentation/content/xdocs/status.xml
    poi/branches/ooxml/src/java/org/apache/poi/hssf/model/FormulaParser.java
    poi/branches/ooxml/src/java/org/apache/poi/hssf/model/LinkTable.java
    poi/branches/ooxml/src/java/org/apache/poi/hssf/model/Workbook.java
    poi/branches/ooxml/src/java/org/apache/poi/hssf/record/ExternSheetRecord.java
    poi/branches/ooxml/src/java/org/apache/poi/hssf/record/NameRecord.java
    poi/branches/ooxml/src/java/org/apache/poi/hssf/record/SupBookRecord.java
    poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/AbstractFunctionPtg.java
    poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/NameXPtg.java
    poi/branches/ooxml/src/java/org/apache/poi/hssf/usermodel/HSSFName.java
    poi/branches/ooxml/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java
    poi/branches/ooxml/src/ooxml/interfaces-jdk14/org/apache/poi/ss/usermodel/Name.java
    poi/branches/ooxml/src/ooxml/interfaces-jdk14/org/apache/poi/ss/usermodel/Workbook.java
    poi/branches/ooxml/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Name.java
    poi/branches/ooxml/src/scratchpad/src/org/apache/poi/hwpf/HWPFDocument.java
    poi/branches/ooxml/src/scratchpad/src/org/apache/poi/hwpf/model/CHPBinTable.java
    poi/branches/ooxml/src/scratchpad/src/org/apache/poi/hwpf/model/CHPFormattedDiskPage.java
    poi/branches/ooxml/src/scratchpad/src/org/apache/poi/hwpf/model/CHPX.java
    poi/branches/ooxml/src/scratchpad/src/org/apache/poi/hwpf/model/PAPBinTable.java
    poi/branches/ooxml/src/scratchpad/src/org/apache/poi/hwpf/model/PAPFormattedDiskPage.java
    poi/branches/ooxml/src/scratchpad/src/org/apache/poi/hwpf/model/PAPX.java
    poi/branches/ooxml/src/scratchpad/src/org/apache/poi/hwpf/model/PropertyNode.java
    poi/branches/ooxml/src/scratchpad/src/org/apache/poi/hwpf/model/SEPX.java
    poi/branches/ooxml/src/scratchpad/src/org/apache/poi/hwpf/model/SectionTable.java
    poi/branches/ooxml/src/scratchpad/src/org/apache/poi/hwpf/model/TextPiece.java
    poi/branches/ooxml/src/scratchpad/src/org/apache/poi/hwpf/model/TextPieceTable.java
    poi/branches/ooxml/src/scratchpad/src/org/apache/poi/hwpf/model/types/FIBAbstractType.java
    poi/branches/ooxml/src/scratchpad/src/org/apache/poi/hwpf/usermodel/Range.java
    poi/branches/ooxml/src/scratchpad/testcases/org/apache/poi/hwpf/model/TestCHPBinTable.java
    poi/branches/ooxml/src/scratchpad/testcases/org/apache/poi/hwpf/model/TestPAPBinTable.java
    poi/branches/ooxml/src/scratchpad/testcases/org/apache/poi/hwpf/model/TestSectionTable.java
    poi/branches/ooxml/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestProblems.java
    poi/branches/ooxml/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestRangeDelete.java
    poi/branches/ooxml/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestRangeInsertion.java
    poi/branches/ooxml/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestRangeProperties.java
    poi/branches/ooxml/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestRangeReplacement.java
    poi/branches/ooxml/src/testcases/org/apache/poi/hssf/data/FormulaEvalTestData.xls
    poi/branches/ooxml/src/testcases/org/apache/poi/hssf/model/TestFormulaParser.java
    poi/branches/ooxml/src/testcases/org/apache/poi/hssf/record/formula/TestExternalFunctionFormulas.java
    poi/branches/ooxml/src/testcases/org/apache/poi/hssf/record/formula/eval/TestExternalFunction.java
    poi/branches/ooxml/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java
    poi/branches/ooxml/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java
    poi/branches/ooxml/src/testcases/org/apache/poi/hssf/usermodel/TestNamedRange.java

Propchange: poi/branches/ooxml/
------------------------------------------------------------------------------
--- svnmerge-integrated (original)
+++ svnmerge-integrated Mon Aug 11 16:58:54 2008
@@ -1 +1 @@
-/poi/trunk:1-638784,638786-639486,639488-639601,639603-640056,640058-642562,642564-642566,642568-642574,642576-642736,642739-650914,650916-684495
+/poi/trunk:1-638784,638786-639486,639488-639601,639603-640056,640058-642562,642564-642566,642568-642574,642576-642736,642739-650914,650916-684986

Modified: poi/branches/ooxml/src/documentation/content/xdocs/changes.xml
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/documentation/content/xdocs/changes.xml?rev=684990&r1=684989&r2=684990&view=diff
==============================================================================
--- poi/branches/ooxml/src/documentation/content/xdocs/changes.xml (original)
+++ poi/branches/ooxml/src/documentation/content/xdocs/changes.xml Mon Aug 11 16:58:54 2008
@@ -58,6 +58,7 @@
            <action dev="POI-DEVELOPERS" type="add">Created a common interface for handling Excel files, irrespective of if they are .xls or .xlsx</action>
         </release>
         <release version="3.1.1-alpha1" date="2008-??-??">
+           <action dev="POI-DEVELOPERS" type="fix">Big improvement in how HWPF handles unicode text, and more sanity checking of text ranges within HWPF</action>
            <action dev="POI-DEVELOPERS" type="add">Include headers and footers int he extracted text from HWPF's WordExtractor</action>
            <action dev="POI-DEVELOPERS" type="add">Added support to HWPF for headers and footers</action>
            <action dev="POI-DEVELOPERS" type="fix">Improve how HWPF deals with unicode internally. Should avoid some odd behaviour when manipulating unicode text</action>

Modified: poi/branches/ooxml/src/documentation/content/xdocs/status.xml
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/documentation/content/xdocs/status.xml?rev=684990&r1=684989&r2=684990&view=diff
==============================================================================
--- poi/branches/ooxml/src/documentation/content/xdocs/status.xml (original)
+++ poi/branches/ooxml/src/documentation/content/xdocs/status.xml Mon Aug 11 16:58:54 2008
@@ -55,6 +55,7 @@
            <action dev="POI-DEVELOPERS" type="add">Created a common interface for handling Excel files, irrespective of if they are .xls or .xlsx</action>
         </release>
         <release version="3.1.1-alpha1" date="2008-??-??">
+           <action dev="POI-DEVELOPERS" type="fix">Big improvement in how HWPF handles unicode text, and more sanity checking of text ranges within HWPF</action>
            <action dev="POI-DEVELOPERS" type="add">Include headers and footers int he extracted text from HWPF's WordExtractor</action>
            <action dev="POI-DEVELOPERS" type="add">Added support to HWPF for headers and footers</action>
            <action dev="POI-DEVELOPERS" type="fix">Improve how HWPF deals with unicode internally. Should avoid some odd behaviour when manipulating unicode text</action>

Modified: poi/branches/ooxml/src/java/org/apache/poi/hssf/model/FormulaParser.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/java/org/apache/poi/hssf/model/FormulaParser.java?rev=684990&r1=684989&r2=684990&view=diff
==============================================================================
--- poi/branches/ooxml/src/java/org/apache/poi/hssf/model/FormulaParser.java (original)
+++ poi/branches/ooxml/src/java/org/apache/poi/hssf/model/FormulaParser.java Mon Aug 11 16:58:54 2008
@@ -25,6 +25,7 @@
 import org.apache.poi.hssf.record.formula.*;
 import org.apache.poi.hssf.record.formula.function.FunctionMetadata;
 import org.apache.poi.hssf.record.formula.function.FunctionMetadataRegistry;
+import org.apache.poi.ss.usermodel.Name;
 import org.apache.poi.ss.usermodel.Workbook;
 import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
@@ -370,13 +371,32 @@
      * @param name case preserved function name (as it was entered/appeared in the formula).
      */
     private ParseNode function(String name) {
-        NamePtg nameToken = null;
-        // Note regarding parameter -
-        if(!AbstractFunctionPtg.isInternalFunctionName(name)) {
-            // external functions get a Name token which points to a defined name record
-            nameToken = new NamePtg(name, this.book);
-
+        Ptg nameToken = null;
+        if(!AbstractFunctionPtg.isBuiltInFunctionName(name)) {
+        	// user defined function
             // in the token tree, the name is more or less the first argument
+        	
+        
+        	int nameIndex = book.getNameIndex(name);
+        	if (nameIndex >= 0) {
+        		Name hName = book.getNameAt(nameIndex);
+        		if (!hName.isFunctionName()) {
+        			throw new FormulaParseException("Attempt to use name '" + name 
+        					+ "' as a function, but defined name in workbook does not refer to a function");
+        		}
+        		
+        		// calls to user-defined functions within the workbook 
+        		// get a Name token which points to a defined name record
+        		nameToken = new NamePtg(name, this.book);
+        	} else {
+				if(book instanceof HSSFWorkbook) {
+					nameToken = ((HSSFWorkbook)book).getNameXPtg(name);
+				}
+        		if (nameToken == null) {
+        			throw new FormulaParseException("Name '" + name 
+        					+ "' is completely unknown in the current workbook");
+        		}
+        	}
         }
 
         Match('(');
@@ -390,11 +410,11 @@
      * Generates the variable function ptg for the formula.
      * <p>
      * For IF Formulas, additional PTGs are added to the tokens
-     * @param name
+     * @param name a {@link NamePtg} or {@link NameXPtg} or <code>null</code>
      * @param numArgs
      * @return Ptg a null is returned if we're in an IF formula, it needs extreme manipulation and is handled in this function
      */
-    private ParseNode getFunction(String name, NamePtg namePtg, ParseNode[] args) {
+    private ParseNode getFunction(String name, Ptg namePtg, ParseNode[] args) {
 
         FunctionMetadata fm = FunctionMetadataRegistry.getFunctionByName(name.toUpperCase());
         int numArgs = args.length;

Modified: poi/branches/ooxml/src/java/org/apache/poi/hssf/model/LinkTable.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/java/org/apache/poi/hssf/model/LinkTable.java?rev=684990&r1=684989&r2=684990&view=diff
==============================================================================
--- poi/branches/ooxml/src/java/org/apache/poi/hssf/model/LinkTable.java (original)
+++ poi/branches/ooxml/src/java/org/apache/poi/hssf/model/LinkTable.java Mon Aug 11 16:58:54 2008
@@ -25,11 +25,11 @@
 import org.apache.poi.hssf.record.CRNRecord;
 import org.apache.poi.hssf.record.CountryRecord;
 import org.apache.poi.hssf.record.ExternSheetRecord;
-import org.apache.poi.hssf.record.ExternSheetSubRecord;
 import org.apache.poi.hssf.record.ExternalNameRecord;
 import org.apache.poi.hssf.record.NameRecord;
 import org.apache.poi.hssf.record.Record;
 import org.apache.poi.hssf.record.SupBookRecord;
+import org.apache.poi.hssf.record.formula.NameXPtg;
 
 /**
  * Link Table (OOO pdf reference: 4.10.3 ) <p/>
@@ -122,6 +122,19 @@
 		public String getNameText(int definedNameIndex) {
 			return _externalNameRecords[definedNameIndex].getText();
 		}
+
+		/**
+		 * Performs case-insensitive search
+		 * @return -1 if not found
+		 */
+		public int getIndexOfName(String name) {
+			for (int i = 0; i < _externalNameRecords.length; i++) {
+				if(_externalNameRecords[i].getText().equalsIgnoreCase(name)) {
+					return i;
+				}
+			}
+			return -1;
+		}
 	}
 
 	private final ExternalBookBlock[] _externalBookBlocks;
@@ -192,14 +205,18 @@
 	}
 
 
-	public NameRecord getSpecificBuiltinRecord(byte name, int sheetIndex) {
+	/**
+	 * @param builtInCode a BUILTIN_~ constant from {@link NameRecord}
+	 * @param sheetNumber 1-based sheet number
+	 */
+	public NameRecord getSpecificBuiltinRecord(byte builtInCode, int sheetNumber) {
 
 		Iterator iterator = _definedNames.iterator();
 		while (iterator.hasNext()) {
 			NameRecord record = ( NameRecord ) iterator.next();
 
 			//print areas are one based
-			if (record.getBuiltInName() == name && record.getIndexToSheet() == sheetIndex) {
+			if (record.getBuiltInName() == builtInCode && record.getSheetNumber() == sheetNumber) {
 				return record;
 			}
 		}
@@ -241,69 +258,56 @@
 		_definedNames.remove(namenum);
 	}
 
-    /**
-     * checks if the given name is already included in the linkTable
-     */
-    public boolean nameAlreadyExists(NameRecord name)
-    {
-    	// Check to ensure no other names have the same case-insensitive name
-    	for ( int i = getNumNames()-1; i >=0; i-- ) {
-    		NameRecord rec = getNameRecord(i);
-    		if (rec != name) {
-    			if (isDuplicatedNames(name, rec))
-    				return true;
-    		}
-    	}
-    	return false;
-    }
-    
-    private boolean isDuplicatedNames(NameRecord firstName, NameRecord lastName)
-    {
-    	return lastName.getNameText().equalsIgnoreCase(firstName.getNameText()) 
-    		&& isSameSheetNames(firstName, lastName);
-    }
-    private boolean isSameSheetNames(NameRecord firstName, NameRecord lastName)
-    {
-    	return lastName.getEqualsToIndexToSheet() == firstName.getEqualsToIndexToSheet();
-    }
-
-    
-	public short getIndexToSheet(short num) {
-		return _externSheetRecord.getREFRecordAt(num).getIndexToFirstSupBook();
+	/**
+	 * checks if the given name is already included in the linkTable
+	 */
+	public boolean nameAlreadyExists(NameRecord name)
+	{
+		// Check to ensure no other names have the same case-insensitive name
+		for ( int i = getNumNames()-1; i >=0; i-- ) {
+			NameRecord rec = getNameRecord(i);
+			if (rec != name) {
+				if (isDuplicatedNames(name, rec))
+					return true;
+			}
+		}
+		return false;
+	}
+	
+	private static boolean isDuplicatedNames(NameRecord firstName, NameRecord lastName) {
+		return lastName.getNameText().equalsIgnoreCase(firstName.getNameText()) 
+			&& isSameSheetNames(firstName, lastName);
+	}
+	private static boolean isSameSheetNames(NameRecord firstName, NameRecord lastName) {
+		return lastName.getSheetNumber() == firstName.getSheetNumber();
 	}
 
-	public int getSheetIndexFromExternSheetIndex(int externSheetNumber) {
-		if (externSheetNumber >= _externSheetRecord.getNumOfREFStructures()) {
+	
+	public int getIndexToSheet(int extRefIndex) {
+		return _externSheetRecord.getFirstSheetIndexFromRefIndex(extRefIndex);
+	}
+
+	public int getSheetIndexFromExternSheetIndex(int extRefIndex) {
+		if (extRefIndex >= _externSheetRecord.getNumOfRefs()) {
 			return -1;
 		}
-		return _externSheetRecord.getREFRecordAt(externSheetNumber).getIndexToFirstSupBook();
+		return _externSheetRecord.getFirstSheetIndexFromRefIndex(extRefIndex);
 	}
 
-	public short addSheetIndexToExternSheet(short sheetNumber) {
-
-		ExternSheetSubRecord record = new ExternSheetSubRecord();
-		record.setIndexToFirstSupBook(sheetNumber);
-		record.setIndexToLastSupBook(sheetNumber);
-		_externSheetRecord.addREFRecord(record);
-		_externSheetRecord.setNumOfREFStructures((short)(_externSheetRecord.getNumOfREFStructures() + 1));
-		return (short)(_externSheetRecord.getNumOfREFStructures() - 1);
+	public int addSheetIndexToExternSheet(int sheetNumber) {
+		// TODO - what about the first parameter (extBookIndex)?
+		return _externSheetRecord.addRef(0, sheetNumber, sheetNumber);
 	}
 
-	public short checkExternSheet(int sheetNumber) {
+	public short checkExternSheet(int sheetIndex) {
 
 		//Trying to find reference to this sheet
-		int nESRs = _externSheetRecord.getNumOfREFStructures();
-		for(short i=0; i< nESRs; i++) {
-			ExternSheetSubRecord esr = _externSheetRecord.getREFRecordAt(i);
-
-			if (esr.getIndexToFirstSupBook() ==  sheetNumber
-					&& esr.getIndexToLastSupBook() == sheetNumber){
-				return i;
-			}
+		int i = _externSheetRecord.getRefIxForSheet(sheetIndex);
+		if (i>=0) {
+			return (short)i;
 		}
-
 		//We Haven't found reference to this sheet
-		return addSheetIndexToExternSheet((short) sheetNumber);
+		return (short)addSheetIndexToExternSheet((short) sheetIndex);
 	}
 
 
@@ -324,11 +328,31 @@
 	}
 
 	public int getNumberOfREFStructures() {
-		return _externSheetRecord.getNumOfREFStructures();
+		return _externSheetRecord.getNumOfRefs();
 	}
 
 	public String resolveNameXText(int refIndex, int definedNameIndex) {
-		short extBookIndex = _externSheetRecord.getREFRecordAt(refIndex).getIndexToSupBook();
+		int extBookIndex = _externSheetRecord.getExtbookIndexFromRefIndex(refIndex);
 		return _externalBookBlocks[extBookIndex].getNameText(definedNameIndex);
 	}
+
+	public NameXPtg getNameXPtg(String name) {
+		// first find any external book block that contains the name:
+		for (int i = 0; i < _externalBookBlocks.length; i++) {
+			int definedNameIndex = _externalBookBlocks[i].getIndexOfName(name);
+			if (definedNameIndex < 0) {
+				continue;
+			}
+			// found it.
+			int sheetRefIndex = findRefIndexFromExtBookIndex(i); 
+			if (sheetRefIndex >= 0) {
+				return new NameXPtg(sheetRefIndex, definedNameIndex);
+			}
+		}
+		return null;
+	}
+
+	private int findRefIndexFromExtBookIndex(int extBookIndex) {
+		return _externSheetRecord.findRefIndexFromExtBookIndex(extBookIndex); 
+	}
 }

Modified: poi/branches/ooxml/src/java/org/apache/poi/hssf/model/Workbook.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/java/org/apache/poi/hssf/model/Workbook.java?rev=684990&r1=684989&r2=684990&view=diff
==============================================================================
--- poi/branches/ooxml/src/java/org/apache/poi/hssf/model/Workbook.java (original)
+++ poi/branches/ooxml/src/java/org/apache/poi/hssf/model/Workbook.java Mon Aug 11 16:58:54 2008
@@ -15,21 +15,70 @@
    limitations under the License.
 ==================================================================== */
 
-
 package org.apache.poi.hssf.model;
 
-import org.apache.poi.ddf.*;
-import org.apache.poi.hssf.record.*;
-import org.apache.poi.hssf.util.HSSFColor;
-import org.apache.poi.hssf.util.SheetReferences;
-import org.apache.poi.util.POILogFactory;
-import org.apache.poi.util.POILogger;
-
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Locale;
 
+import org.apache.poi.ddf.EscherBSERecord;
+import org.apache.poi.ddf.EscherBoolProperty;
+import org.apache.poi.ddf.EscherContainerRecord;
+import org.apache.poi.ddf.EscherDggRecord;
+import org.apache.poi.ddf.EscherOptRecord;
+import org.apache.poi.ddf.EscherProperties;
+import org.apache.poi.ddf.EscherRGBProperty;
+import org.apache.poi.ddf.EscherRecord;
+import org.apache.poi.ddf.EscherSplitMenuColorsRecord;
+import org.apache.poi.hssf.record.BOFRecord;
+import org.apache.poi.hssf.record.BackupRecord;
+import org.apache.poi.hssf.record.BookBoolRecord;
+import org.apache.poi.hssf.record.BoundSheetRecord;
+import org.apache.poi.hssf.record.CodepageRecord;
+import org.apache.poi.hssf.record.CountryRecord;
+import org.apache.poi.hssf.record.DSFRecord;
+import org.apache.poi.hssf.record.DateWindow1904Record;
+import org.apache.poi.hssf.record.DrawingGroupRecord;
+import org.apache.poi.hssf.record.EOFRecord;
+import org.apache.poi.hssf.record.ExtSSTRecord;
+import org.apache.poi.hssf.record.ExtendedFormatRecord;
+import org.apache.poi.hssf.record.ExternSheetRecord;
+import org.apache.poi.hssf.record.FileSharingRecord;
+import org.apache.poi.hssf.record.FnGroupCountRecord;
+import org.apache.poi.hssf.record.FontRecord;
+import org.apache.poi.hssf.record.FormatRecord;
+import org.apache.poi.hssf.record.HideObjRecord;
+import org.apache.poi.hssf.record.HyperlinkRecord;
+import org.apache.poi.hssf.record.InterfaceEndRecord;
+import org.apache.poi.hssf.record.InterfaceHdrRecord;
+import org.apache.poi.hssf.record.MMSRecord;
+import org.apache.poi.hssf.record.NameRecord;
+import org.apache.poi.hssf.record.PaletteRecord;
+import org.apache.poi.hssf.record.PasswordRecord;
+import org.apache.poi.hssf.record.PasswordRev4Record;
+import org.apache.poi.hssf.record.PrecisionRecord;
+import org.apache.poi.hssf.record.ProtectRecord;
+import org.apache.poi.hssf.record.ProtectionRev4Record;
+import org.apache.poi.hssf.record.RecalcIdRecord;
+import org.apache.poi.hssf.record.Record;
+import org.apache.poi.hssf.record.RefreshAllRecord;
+import org.apache.poi.hssf.record.SSTRecord;
+import org.apache.poi.hssf.record.StyleRecord;
+import org.apache.poi.hssf.record.SupBookRecord;
+import org.apache.poi.hssf.record.TabIdRecord;
+import org.apache.poi.hssf.record.UnicodeString;
+import org.apache.poi.hssf.record.UseSelFSRecord;
+import org.apache.poi.hssf.record.WindowOneRecord;
+import org.apache.poi.hssf.record.WindowProtectRecord;
+import org.apache.poi.hssf.record.WriteAccessRecord;
+import org.apache.poi.hssf.record.WriteProtectRecord;
+import org.apache.poi.hssf.record.formula.NameXPtg;
+import org.apache.poi.hssf.util.HSSFColor;
+import org.apache.poi.hssf.util.SheetReferences;
+import org.apache.poi.util.POILogFactory;
+import org.apache.poi.util.POILogger;
+
 /**
  * Low level model implementation of a Workbook.  Provides creational methods
  * for settings and objects contained in the workbook object.
@@ -54,18 +103,13 @@
  * @see org.apache.poi.hssf.usermodel.HSSFWorkbook
  * @version 1.0-pre
  */
-
-public class Workbook implements Model
-{
+public final class Workbook implements Model {
     private static final int   DEBUG       = POILogger.DEBUG;
 
-//    public static Workbook currentBook = null;
-
     /**
      * constant used to set the "codepage" wherever "codepage" is set in records
-     * (which is duplciated in more than one record)
+     * (which is duplicated in more than one record)
      */
-
     private final static short CODEPAGE    = ( short ) 0x4b0;
 
     /**
@@ -105,8 +149,6 @@
 
     private static POILogger   log = POILogFactory.getLogger(Workbook.class);
 
-    protected static final String EXCEL_REPEATING_NAME_PREFIX_ = "Excel_Name_Record_Titles_";
-
     /**
      * Creates new Workbook with no intitialization --useless right now
      * @see #createWorkbook(List)
@@ -250,9 +292,9 @@
         for ( ; k < recs.size(); k++) {
             Record rec = ( Record ) recs.get(k);
             switch (rec.getSid()) {
-            	case HyperlinkRecord.sid:
-            		retval.hyperlinks.add(rec);
-            		break;
+                case HyperlinkRecord.sid:
+                    retval.hyperlinks.add(rec);
+                    break;
             }
         }
         
@@ -358,22 +400,22 @@
     }
 
 
-	/**Retrieves the Builtin NameRecord that matches the name and index
-	 * There shouldn't be too many names to make the sequential search too slow
-	 * @param name byte representation of the builtin name to match
-	 * @param sheetIndex Index to match
-	 * @return null if no builtin NameRecord matches
-	 */
-    public NameRecord getSpecificBuiltinRecord(byte name, int sheetIndex)
+    /**Retrieves the Builtin NameRecord that matches the name and index
+     * There shouldn't be too many names to make the sequential search too slow
+     * @param name byte representation of the builtin name to match
+     * @param sheetNumber 1-based sheet number
+     * @return null if no builtin NameRecord matches
+     */
+    public NameRecord getSpecificBuiltinRecord(byte name, int sheetNumber)
     {
-        return getOrCreateLinkTable().getSpecificBuiltinRecord(name, sheetIndex);
+        return getOrCreateLinkTable().getSpecificBuiltinRecord(name, sheetNumber);
     }
 
-	/**
-	 * Removes the specified Builtin NameRecord that matches the name and index
-	 * @param name byte representation of the builtin to match
-	 * @param sheetIndex zero-based sheet reference
-	 */
+    /**
+     * Removes the specified Builtin NameRecord that matches the name and index
+     * @param name byte representation of the builtin to match
+     * @param sheetIndex zero-based sheet reference
+     */
     public void removeBuiltinRecord(byte name, int sheetIndex) {
         linkTable.removeBuiltinRecord(name, sheetIndex);
         // TODO - do we need "this.records.remove(...);" similar to that in this.removeName(int namenum) {}?
@@ -413,18 +455,18 @@
      * Retrieves the index of the given font
      */
     public int getFontIndex(FontRecord font) {
-    	for(int i=0; i<=numfonts; i++) {
+        for(int i=0; i<=numfonts; i++) {
             FontRecord thisFont =
                 ( FontRecord ) records.get((records.getFontpos() - (numfonts - 1)) + i);
             if(thisFont == font) {
-            	// There is no 4!
-            	if(i > 3) {
-            		return (i+1);
-            	}
-            	return i;
+                // There is no 4!
+                if(i > 3) {
+                    return (i+1);
+                }
+                return i;
             }
-    	}
-    	throw new IllegalArgumentException("Could not find that font!"); 
+        }
+        throw new IllegalArgumentException("Could not find that font!"); 
     }
 
     /**
@@ -451,7 +493,7 @@
      *  so you'll need to update those yourself!
      */
     public void removeFontRecord(FontRecord rec) {
-    	records.remove(rec); // this updates FontPos for us
+        records.remove(rec); // this updates FontPos for us
         numfonts--;
     }
 
@@ -468,19 +510,23 @@
     /**
      * Sets the BOF for a given sheet
      *
-     * @param sheetnum the number of the sheet to set the positing of the bof for
+     * @param sheetIndex the number of the sheet to set the positing of the bof for
      * @param pos the actual bof position
      */
 
-    public void setSheetBof(int sheetnum, int pos) {
+    public void setSheetBof(int sheetIndex, int pos) {
         if (log.check( POILogger.DEBUG ))
-            log.log(DEBUG, "setting bof for sheetnum =", new Integer(sheetnum),
+            log.log(DEBUG, "setting bof for sheetnum =", new Integer(sheetIndex),
                 " at pos=", new Integer(pos));
-        checkSheets(sheetnum);
-        (( BoundSheetRecord ) boundsheets.get(sheetnum))
+        checkSheets(sheetIndex);
+        getBoundSheetRec(sheetIndex)
         .setPositionOfBof(pos);
     }
 
+    private BoundSheetRecord getBoundSheetRec(int sheetIndex) {
+        return ((BoundSheetRecord) boundsheets.get(sheetIndex));
+    }
+
     /**
      * Returns the position of the backup record.
      */
@@ -516,7 +562,7 @@
     {
         for ( int i = 0; i < boundsheets.size(); i++ )
         {
-            BoundSheetRecord boundSheetRecord = (BoundSheetRecord) boundsheets.get( i );
+            BoundSheetRecord boundSheetRecord = getBoundSheetRec(i);
             if (excludeSheetIdx != i && name.equalsIgnoreCase(boundSheetRecord.getSheetname()))
                 return true;
         }
@@ -533,35 +579,33 @@
      */    
     public void setSheetName(int sheetnum, String sheetname, short encoding ) {
         checkSheets(sheetnum);
-        BoundSheetRecord sheet = (BoundSheetRecord)boundsheets.get( sheetnum );
+        BoundSheetRecord sheet = getBoundSheetRec(sheetnum);
         sheet.setSheetname(sheetname);
         sheet.setSheetnameLength( (byte)sheetname.length() );
-		sheet.setCompressedUnicodeFlag( (byte)encoding );
+        sheet.setCompressedUnicodeFlag( (byte)encoding );
     }
     
     /**
-	 * sets the order of appearance for a given sheet.
-	 *
-	 * @param sheetname the name of the sheet to reorder
-	 * @param pos the position that we want to insert the sheet into (0 based)
-	 */
+     * sets the order of appearance for a given sheet.
+     *
+     * @param sheetname the name of the sheet to reorder
+     * @param pos the position that we want to insert the sheet into (0 based)
+     */
     
     public void setSheetOrder(String sheetname, int pos ) {
-	int sheetNumber = getSheetIndex(sheetname);
-	//remove the sheet that needs to be reordered and place it in the spot we want
-	boundsheets.add(pos, boundsheets.remove(sheetNumber));	
+    int sheetNumber = getSheetIndex(sheetname);
+    //remove the sheet that needs to be reordered and place it in the spot we want
+    boundsheets.add(pos, boundsheets.remove(sheetNumber));    
     }
 
     /**
      * gets the name for a given sheet.
      *
-     * @param sheetnum the sheet number (0 based)
+     * @param sheetIndex the sheet number (0 based)
      * @return sheetname the name for the sheet
      */
-
-    public String getSheetName(int sheetnum) {
-        return (( BoundSheetRecord ) boundsheets.get(sheetnum))
-        .getSheetname();
+    public String getSheetName(int sheetIndex) {
+        return getBoundSheetRec(sheetIndex).getSheetname();
     }
 
     /**
@@ -572,8 +616,7 @@
      */
 
     public boolean isSheetHidden(int sheetnum) {
-        BoundSheetRecord bsr = ( BoundSheetRecord ) boundsheets.get(sheetnum);
-        return bsr.isHidden();
+        return getBoundSheetRec(sheetnum).isHidden();
     }
 
     /**
@@ -584,8 +627,7 @@
      */
     
     public void setSheetHidden(int sheetnum, boolean hidden) {
-        BoundSheetRecord bsr = ( BoundSheetRecord ) boundsheets.get(sheetnum);
-        bsr.setHidden(hidden);
+        getBoundSheetRec(sheetnum).setHidden(hidden);
     }
     /**
      * get the sheet's index
@@ -627,11 +669,13 @@
         }
     }
 
-    public void removeSheet(int sheetnum) {
-        if (boundsheets.size() > sheetnum) {
-            records.remove(records.getBspos() - (boundsheets.size() - 1) + sheetnum);
-//            records.bspos--;
-            boundsheets.remove(sheetnum);
+    /**
+     * @param sheetIndex zero based sheet index
+     */
+    public void removeSheet(int sheetIndex) {
+        if (boundsheets.size() > sheetIndex) {
+            records.remove(records.getBspos() - (boundsheets.size() - 1) + sheetIndex);
+            boundsheets.remove(sheetIndex);
             fixTabIdRecord();
         }
         
@@ -642,20 +686,18 @@
         // However, the sheet index must be adjusted, or
         //  excel will break. (Sheet index is either 0 for
         //  global, or 1 based index to sheet)
-        int sheetNum1Based = sheetnum + 1;
+        int sheetNum1Based = sheetIndex + 1;
         for(int i=0; i<getNumNames(); i++) {
-        	NameRecord nr = getNameRecord(i);
-        	
-        	if(nr.getIndexToSheet() == sheetNum1Based) {
-        		// Excel re-writes these to point to no sheet
-        		nr.setEqualsToIndexToSheet((short)0);
-        	} else if(nr.getIndexToSheet() > sheetNum1Based) {
-        		// Bump down by one, so still points
-        		//  at the same sheet
-        		nr.setEqualsToIndexToSheet((short)(
-        				nr.getEqualsToIndexToSheet()-1
-        		));
-        	}
+            NameRecord nr = getNameRecord(i);
+            
+            if(nr.getSheetNumber() == sheetNum1Based) {
+                // Excel re-writes these to point to no sheet
+                nr.setSheetNumber(0);
+            } else if(nr.getSheetNumber() > sheetNum1Based) {
+                // Bump down by one, so still points
+                //  at the same sheet
+                nr.setSheetNumber(nr.getSheetNumber()-1);
+            }
         }
     }
 
@@ -721,7 +763,7 @@
      *  so you'll need to update those yourself!
      */
     public void removeExFormatRecord(ExtendedFormatRecord rec) {
-    	records.remove(rec); // this updates XfPos for us
+        records.remove(rec); // this updates XfPos for us
         numxfs--;
     }
 
@@ -813,9 +855,9 @@
 //
 //            Record record = records.get(k);
 ////             Let's skip RECALCID records, as they are only use for optimization
-//	    if(record.getSid() != RecalcIdRecord.sid || ((RecalcIdRecord)record).isNeeded()) {
+//        if(record.getSid() != RecalcIdRecord.sid || ((RecalcIdRecord)record).isNeeded()) {
 //                pos += record.serialize(pos, retval);   // rec.length;
-//	    }
+//        }
 //        }
 //        log.log(DEBUG, "Exiting serialize workbook");
 //        return retval;
@@ -858,7 +900,7 @@
                 if (record instanceof BoundSheetRecord) {
                      if(!wroteBoundSheets) {
                         for (int i = 0; i < boundsheets.size(); i++) {
-                            len+= ((BoundSheetRecord)boundsheets.get(i))
+                            len+= getBoundSheetRec(i)
                                              .serialize(pos+offset+len, data);
                         }
                         wroteBoundSheets = true;
@@ -1141,8 +1183,8 @@
         retval.setWidth(( short ) 0x3a5c);
         retval.setHeight(( short ) 0x23be);
         retval.setOptions(( short ) 0x38);
-        retval.setSelectedTab(( short ) 0x0);
-        retval.setDisplayedTab(( short ) 0x0);
+        retval.setActiveSheetIndex( 0x0);
+        retval.setFirstVisibleTab(0x0);
         retval.setNumSelectedTabs(( short ) 1);
         retval.setTabWidthRatio(( short ) 0x258);
         return retval;
@@ -1821,10 +1863,10 @@
 
         // from Russia with love ;)
         if ( Locale.getDefault().toString().equals( "ru_RU" ) ) {
-	        retval.setCurrentCountry(( short ) 7);
+            retval.setCurrentCountry(( short ) 7);
         }
         else {
-	        retval.setCurrentCountry(( short ) 1);
+            retval.setCurrentCountry(( short ) 1);
         }
 
         return retval;
@@ -1889,15 +1931,15 @@
      * @return sheet name
      */
     public String findSheetNameFromExternSheet(short num){
-        String result="";
 
-        short indexToSheet = linkTable.getIndexToSheet(num);
+        int indexToSheet = linkTable.getIndexToSheet(num);
         
-        if (indexToSheet>-1) { //error check, bail out gracefully!
-            result = getSheetName(indexToSheet);
+        if (indexToSheet < 0) {
+            // TODO - what does '-1' mean here?
+            //error check, bail out gracefully!
+            return "";
         }
-
-        return result;
+        return getSheetName(indexToSheet);
     }
 
     /**
@@ -1950,10 +1992,10 @@
      */
     public NameRecord addName(NameRecord name)
     {
-    	
+        
         LinkTable linkTable = getOrCreateLinkTable();
         if(linkTable.nameAlreadyExists(name)) {
-        	throw new IllegalArgumentException(
+            throw new IllegalArgumentException(
                 "You are trying to assign a duplicated name record: "
                 + name.getNameText());
         }
@@ -1964,27 +2006,18 @@
     
     /**
      * Generates a NameRecord to represent a built-in region
-     * @return a new NameRecord unless the index is invalid
+     * @return a new NameRecord
      */
-    public NameRecord createBuiltInName(byte builtInName, int index)
-    {
-        if (index == -1 || index+1 > Short.MAX_VALUE) 
-            throw new IllegalArgumentException("Index is not valid ["+index+"]");
+    public NameRecord createBuiltInName(byte builtInName, int sheetNumber) {
+        if (sheetNumber < 0 || sheetNumber+1 > Short.MAX_VALUE) {
+            throw new IllegalArgumentException("Sheet number ["+sheetNumber+"]is not valid ");
+        }
         
-        NameRecord name = new NameRecord(builtInName, (short)(index));
+        NameRecord name = new NameRecord(builtInName, sheetNumber);
         
-        String prefix = EXCEL_REPEATING_NAME_PREFIX_ + index + "_";
-        int cont = 0;
         while(linkTable.nameAlreadyExists(name)) {
-        	cont++;
-        	String altNameName = prefix + cont;
-        	
-        	// It would be better to set a different builtInName here.
-        	// It does not seem possible, so we create it as a 
-        	//  non built-in name from this point on
-        	name = new NameRecord();
-        	name.setNameText(altNameName);
-            name.setNameTextLength((byte)altNameName.length());
+            throw new RuntimeException("Builtin (" + builtInName 
+                    + ") already exists for sheet (" + sheetNumber + ")");
         }
         addName(name);
         return name;
@@ -1992,16 +2025,15 @@
 
 
     /** removes the name
-     * @param namenum name index
+     * @param nameIndex name index
      */
-    public void removeName(int namenum){
+    public void removeName(int nameIndex){
         
-        if (linkTable.getNumNames() > namenum) {
+        if (linkTable.getNumNames() > nameIndex) {
             int idx = findFirstRecordLocBySid(NameRecord.sid);
-            records.remove(idx + namenum);
-            linkTable.removeName(namenum);
+            records.remove(idx + nameIndex);
+            linkTable.removeName(nameIndex);
         }
-
     }
 
     /**
@@ -2011,19 +2043,19 @@
      * @return the format id of a format that matches or -1 if none found and createIfNotFound
      */
     public short getFormat(String format, boolean createIfNotFound) {
-	Iterator iterator;
-	for (iterator = formats.iterator(); iterator.hasNext();) {
-	    FormatRecord r = (FormatRecord)iterator.next();
-	    if (r.getFormatString().equals(format)) {
-		return r.getIndexCode();
-	    }
-	}
-
-	if (createIfNotFound) {
-	    return createFormat(format);
-	}
+    Iterator iterator;
+    for (iterator = formats.iterator(); iterator.hasNext();) {
+        FormatRecord r = (FormatRecord)iterator.next();
+        if (r.getFormatString().equals(format)) {
+        return r.getIndexCode();
+        }
+    }
+
+    if (createIfNotFound) {
+        return createFormat(format);
+    }
 
-	return -1;
+    return -1;
     }
 
     /**
@@ -2031,7 +2063,7 @@
      * @return ArrayList of FormatRecords in the notebook
      */
     public ArrayList getFormats() {
-	return formats;
+    return formats;
     }
 
     /**
@@ -2043,7 +2075,7 @@
      */
     public short createFormat( String format )
     {
-//        ++xfpos;	//These are to ensure that positions are updated properly
+//        ++xfpos;    //These are to ensure that positions are updated properly
 //        ++palettepos;
 //        ++bspos;
         FormatRecord rec = new FormatRecord();
@@ -2113,7 +2145,7 @@
 
     public List getHyperlinks()
     {
-    	return hyperlinks;
+        return hyperlinks;
     }
     
     public List getRecords()
@@ -2170,54 +2202,54 @@
      * Finds the primary drawing group, if one already exists
      */
     public void findDrawingGroup() {
-    	// Need to find a DrawingGroupRecord that
-    	//  contains a EscherDggRecord
-    	for(Iterator rit = records.iterator(); rit.hasNext();) {
-    		Record r = (Record)rit.next();
-    		
-    		if(r instanceof DrawingGroupRecord) {
-            	DrawingGroupRecord dg =	(DrawingGroupRecord)r;
-            	dg.processChildRecords();
-            	
-            	EscherContainerRecord cr =
-            		dg.getEscherContainer();
-            	if(cr == null) {
-            		continue;
-            	}
-            	
-            	EscherDggRecord dgg = null;
-            	for(Iterator it = cr.getChildRecords().iterator(); it.hasNext();) {
-            		Object er = it.next();
-            		if(er instanceof EscherDggRecord) {
-            			dgg = (EscherDggRecord)er;
-            		}
-            	}
-            	
-            	if(dgg != null) {
-            		drawingManager = new DrawingManager2(dgg);
-            		return;
-            	}
-    		}
-    	}
+        // Need to find a DrawingGroupRecord that
+        //  contains a EscherDggRecord
+        for(Iterator rit = records.iterator(); rit.hasNext();) {
+            Record r = (Record)rit.next();
+            
+            if(r instanceof DrawingGroupRecord) {
+                DrawingGroupRecord dg =    (DrawingGroupRecord)r;
+                dg.processChildRecords();
+                
+                EscherContainerRecord cr =
+                    dg.getEscherContainer();
+                if(cr == null) {
+                    continue;
+                }
+                
+                EscherDggRecord dgg = null;
+                for(Iterator it = cr.getChildRecords().iterator(); it.hasNext();) {
+                    Object er = it.next();
+                    if(er instanceof EscherDggRecord) {
+                        dgg = (EscherDggRecord)er;
+                    }
+                }
+                
+                if(dgg != null) {
+                    drawingManager = new DrawingManager2(dgg);
+                    return;
+                }
+            }
+        }
 
-    	// Look for the DrawingGroup record
+        // Look for the DrawingGroup record
         int dgLoc = findFirstRecordLocBySid(DrawingGroupRecord.sid);
         
-    	// If there is one, does it have a EscherDggRecord?
+        // If there is one, does it have a EscherDggRecord?
         if(dgLoc != -1) {
-        	DrawingGroupRecord dg =
-        		(DrawingGroupRecord)records.get(dgLoc);
-        	EscherDggRecord dgg = null;
-        	for(Iterator it = dg.getEscherRecords().iterator(); it.hasNext();) {
-        		Object er = it.next();
-        		if(er instanceof EscherDggRecord) {
-        			dgg = (EscherDggRecord)er;
-        		}
-        	}
-        	
-        	if(dgg != null) {
-        		drawingManager = new DrawingManager2(dgg);
-        	}
+            DrawingGroupRecord dg =
+                (DrawingGroupRecord)records.get(dgLoc);
+            EscherDggRecord dgg = null;
+            for(Iterator it = dg.getEscherRecords().iterator(); it.hasNext();) {
+                Object er = it.next();
+                if(er instanceof EscherDggRecord) {
+                    dgg = (EscherDggRecord)er;
+                }
+            }
+            
+            if(dgg != null) {
+                drawingManager = new DrawingManager2(dgg);
+            }
         }
     }
 
@@ -2379,7 +2411,7 @@
      */
     public boolean isWriteProtected() {
         if (this.fileShare == null) {
-        	return false;
+            return false;
         }
         FileSharingRecord frec = getFileSharing();
         return (frec.getReadOnly() == 1);
@@ -2419,6 +2451,8 @@
     public String resolveNameXText(int refIndex, int definedNameIndex) {
         return linkTable.resolveNameXText(refIndex, definedNameIndex);
     }
-}
-
 
+    public NameXPtg getNameXPtg(String name) {
+        return getOrCreateLinkTable().getNameXPtg(name);
+    }
+}

Modified: poi/branches/ooxml/src/java/org/apache/poi/hssf/record/ExternSheetRecord.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/java/org/apache/poi/hssf/record/ExternSheetRecord.java?rev=684990&r1=684989&r2=684990&view=diff
==============================================================================
--- poi/branches/ooxml/src/java/org/apache/poi/hssf/record/ExternSheetRecord.java (original)
+++ poi/branches/ooxml/src/java/org/apache/poi/hssf/record/ExternSheetRecord.java Mon Aug 11 16:58:54 2008
@@ -1,4 +1,3 @@
-
 /* ====================================================================
    Licensed to the Apache Software Foundation (ASF) under one or more
    contributor license agreements.  See the NOTICE file distributed with
@@ -15,29 +14,85 @@
    See the License for the specific language governing permissions and
    limitations under the License.
 ==================================================================== */
-        
 
 package org.apache.poi.hssf.record;
 
-import org.apache.poi.util.LittleEndian;
-
 import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.poi.util.LittleEndian;
 
 /**
- * Title:        Extern Sheet <P>
- * Description:  A List of Inndexes to SupBook <P>
- * REFERENCE:  <P>
+ * EXTERNSHEET (0x0017)<br/>
+ * A List of Indexes to  EXTERNALBOOK (supplemental book) Records <p/>
+ * 
  * @author Libin Roman (Vista Portal LDT. Developer)
- * @version 1.0-pre
  */
-
 public class ExternSheetRecord extends Record {
-    public final static short sid = 0x17;
-    private short             field_1_number_of_REF_sturcutres;
-    private ArrayList         field_2_REF_structures;
+    public final static short sid = 0x0017;
+    private List         _list;
+    
+    private final class RefSubRecord {
+    	public static final int ENCODED_SIZE = 6;
+
+    	/** index to External Book Block (which starts with a EXTERNALBOOK record) */
+        private int _extBookIndex;
+        private int _firstSheetIndex; // may be -1 (0xFFFF)
+        private int _lastSheetIndex;  // may be -1 (0xFFFF)
+        
+        
+        /** a Constructor for making new sub record
+         */
+        public RefSubRecord(int extBookIndex, int firstSheetIndex, int lastSheetIndex) {
+    		_extBookIndex = extBookIndex;
+    		_firstSheetIndex = firstSheetIndex;
+    		_lastSheetIndex = lastSheetIndex;
+        }
+        
+        /**
+         * @param in the RecordInputstream to read the record from
+         */
+        public RefSubRecord(RecordInputStream in) {
+            this(in.readShort(), in.readShort(), in.readShort());
+        }
+        public int getExtBookIndex(){
+            return _extBookIndex;
+        }
+        public int getFirstSheetIndex(){
+            return _firstSheetIndex;
+        }
+        public int getLastSheetIndex(){
+            return _lastSheetIndex;
+        }
+        
+        public String toString() {
+            StringBuffer buffer = new StringBuffer();
+            buffer.append("extBook=").append(_extBookIndex);
+            buffer.append(" firstSheet=").append(_firstSheetIndex);
+            buffer.append(" lastSheet=").append(_lastSheetIndex);
+            return buffer.toString();
+        }
+        
+        /**
+         * called by the class that is responsible for writing this sucker.
+         * Subclasses should implement this so that their data is passed back in a
+         * byte array.
+         *
+         * @param offset to begin writing at
+         * @param data byte array containing instance data
+         * @return number of bytes written
+         */
+        public void serialize(int offset, byte [] data) {
+            LittleEndian.putUShort(data, 0 + offset, _extBookIndex);
+            LittleEndian.putUShort(data, 2 + offset, _firstSheetIndex);
+            LittleEndian.putUShort(data, 4 + offset, _lastSheetIndex);
+        }
+    }    
+    
+    
     
     public ExternSheetRecord() {
-        field_2_REF_structures = new ArrayList();
+        _list = new ArrayList();
     }
     
     /**
@@ -68,72 +123,60 @@
      * @param in the RecordInputstream to read the record from
      */
     protected void fillFields(RecordInputStream in) {
-        field_2_REF_structures           = new ArrayList();
+        _list           = new ArrayList();
         
-        field_1_number_of_REF_sturcutres = in.readShort();
+        int nItems  = in.readShort();
         
-        for (int i = 0 ; i < field_1_number_of_REF_sturcutres ; ++i) {
-            ExternSheetSubRecord rec = new ExternSheetSubRecord(in);
+        for (int i = 0 ; i < nItems ; ++i) {
+            RefSubRecord rec = new RefSubRecord(in);
             
-            field_2_REF_structures.add( rec);
+            _list.add( rec);
         }
     }
     
-    /** 
-     * sets the number of the REF structors , that is in Excel file
-     * @param numStruct number of REF structs
-     */
-    public void setNumOfREFStructures(short numStruct) {
-        field_1_number_of_REF_sturcutres = numStruct;
-    }
-    
+
     /**  
-     * return the number of the REF structors , that is in Excel file
-     * @return number of REF structs
+     * @return number of REF structures
      */
-    public short getNumOfREFStructures() {
-        return field_1_number_of_REF_sturcutres;
+    public int getNumOfRefs() {
+        return _list.size();
     }
     
     /** 
      * adds REF struct (ExternSheetSubRecord)
      * @param rec REF struct
      */
-    public void addREFRecord(ExternSheetSubRecord rec) {
-        field_2_REF_structures.add(rec);
+    public void addREFRecord(RefSubRecord rec) {
+        _list.add(rec);
     }
     
     /** returns the number of REF Records, which is in model
      * @return number of REF records
      */
     public int getNumOfREFRecords() {
-        return field_2_REF_structures.size();
+        return _list.size();
     }
     
-    /** returns the REF record (ExternSheetSubRecord)
-     * @param elem index to place
-     * @return REF record
-     */
-    public ExternSheetSubRecord getREFRecordAt(int elem) {
-        ExternSheetSubRecord result = ( ExternSheetSubRecord ) field_2_REF_structures.get(elem);
-        
-        return result;
-    }
     
     public String toString() {
-        StringBuffer buffer = new StringBuffer();
-        
-        buffer.append("[EXTERNSHEET]\n");
-        buffer.append("   numOfRefs     = ").append(getNumOfREFStructures()).append("\n");
-        for (int k=0; k < this.getNumOfREFRecords(); k++) {
-            buffer.append("refrec         #").append(k).append('\n');
-            buffer.append(getREFRecordAt(k).toString());
-            buffer.append("----refrec     #").append(k).append('\n');
+        StringBuffer sb = new StringBuffer();
+        int nItems = _list.size();
+        sb.append("[EXTERNSHEET]\n");
+        sb.append("   numOfRefs     = ").append(nItems).append("\n");
+        for (int i=0; i < nItems; i++) {
+            sb.append("refrec         #").append(i).append(": ");
+            sb.append(getRef(i).toString());
+            sb.append('\n');
         }
-        buffer.append("[/EXTERNSHEET]\n");
+        sb.append("[/EXTERNSHEET]\n");
         
         
-        return buffer.toString();
+        return sb.toString();
+    }
+    
+    
+    private int getDataSize() {
+    	return 2 + _list.size() * RefSubRecord.ENCODED_SIZE;
     }
     
     /**
@@ -146,24 +189,29 @@
      * @return number of bytes written
      */
     public int serialize(int offset, byte [] data) {
-        LittleEndian.putShort(data, 0 + offset, sid);
-        LittleEndian.putShort(data, 2 + offset,(short)(2 + (getNumOfREFRecords() *6)));
+        int dataSize = getDataSize();
         
-        LittleEndian.putShort(data, 4 + offset, getNumOfREFStructures());
+        int nItems = _list.size();
+
+        LittleEndian.putShort(data, 0 + offset, sid);
+		LittleEndian.putUShort(data, 2 + offset, dataSize);
+        LittleEndian.putUShort(data, 4 + offset, nItems);
         
         int pos = 6 ;
         
-        for (int k = 0; k < getNumOfREFRecords(); k++) {
-            ExternSheetSubRecord record = getREFRecordAt(k);
-            System.arraycopy(record.serialize(), 0, data, pos + offset, 6);
-            
+        for (int i = 0; i < nItems; i++) {
+            getRef(i).serialize(offset + pos, data);
             pos +=6;
         }
-        return getRecordSize();
+        return dataSize + 4;
     }
+
+	private RefSubRecord getRef(int i) {
+		return (RefSubRecord) _list.get(i);
+	}
     
     public int getRecordSize() {
-        return 4 + 2 + getNumOfREFRecords() * 6;
+        return 4 + getDataSize();
     }
     
     /**
@@ -172,4 +220,44 @@
     public short getSid() {
         return sid;
     }
+
+	public int getExtbookIndexFromRefIndex(int refIndex) {
+        return getRef(refIndex).getExtBookIndex();
+	}
+
+	/**
+	 * @return -1 if not found
+	 */
+	public int findRefIndexFromExtBookIndex(int extBookIndex) {
+        int nItems = _list.size();
+        for (int i = 0; i < nItems; i++) {
+            if (getRef(i).getExtBookIndex() == extBookIndex) {
+            	return i;
+            }
+        }
+		return -1;
+	}
+
+	public int getFirstSheetIndexFromRefIndex(int extRefIndex) {
+		return getRef(extRefIndex).getFirstSheetIndex();
+	}
+
+	/**
+	 * @return index of newly added ref
+	 */
+	public int addRef(int extBookIndex, int firstSheetIndex, int lastSheetIndex) {
+		_list.add(new RefSubRecord(extBookIndex, firstSheetIndex, lastSheetIndex));
+		return _list.size() - 1;
+	}
+
+	public int getRefIxForSheet(int sheetIndex) {
+        int nItems = _list.size();
+        for (int i = 0; i < nItems; i++) {
+            RefSubRecord ref = getRef(i);
+			if (ref.getFirstSheetIndex() == sheetIndex && ref.getLastSheetIndex() == sheetIndex) {
+            	return i;
+            }
+        }
+		return -1;
+	}
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org


Mime
View raw message