View | Details | Raw Unified | Return to bug 45126
Collapse All | Expand All

(-)java/org/apache/poi/hssf/model/Workbook.java (-8 / +42 lines)
Lines 57-62 Link Here
57
57
58
public class Workbook implements Model
58
public class Workbook implements Model
59
{
59
{
60
    private static final String EXCEL_BUILT_IN_TITLES_ = "Excel_BuiltIn_Titles_";
60
    private static final int   DEBUG       = POILogger.DEBUG;
61
    private static final int   DEBUG       = POILogger.DEBUG;
61
62
62
//    public static Workbook currentBook = null;
63
//    public static Workbook currentBook = null;
Lines 1895-1918 Link Here
1895
     */
1896
     */
1896
    public NameRecord addName(NameRecord name)
1897
    public NameRecord addName(NameRecord name)
1897
    {
1898
    {
1898
        
1899
        getOrCreateLinkTable().addName(name);
1900
1899
1900
        LinkTable linkTable = getOrCreateLinkTable();
1901
        if (checkNameAlreadyExists(linkTable, name)) {
1902
            throw new IllegalArgumentException(
1903
                    "You are trying to assign an already existing builtname record: "
1904
                            + name.getNameText());
1905
1906
        }
1907
        linkTable.addName(name);
1908
1901
        return name;
1909
        return name;
1902
    }
1910
    }
1911
    
1912
    /**
1913
     * checks if the given name is already included in the given linkTable
1914
     * @param linkTable
1915
     * @param name
1916
     * @return
1917
     */
1918
    private boolean checkNameAlreadyExists(LinkTable linkTable, NameRecord name) {
1919
        for (int i = 0; i < linkTable.getNumNames(); i++) {
1920
            NameRecord nameRecord = linkTable.getNameRecord(i);
1921
            if (nameRecord.getNameText().equals(name.getNameText())) {
1922
                return true;
1923
            }
1924
        }
1925
        return false;
1926
    }
1903
1927
1904
    /**Generates a NameRecord to represent a built-in region
1928
    /**Generates a NameRecord to represent a built-in region
1905
     * @return a new NameRecord unless the index is invalid
1929
     * @return a new NameRecord unless the index is invalid
1906
     */
1930
     */
1907
    public NameRecord createBuiltInName(byte builtInName, int index)
1931
    public NameRecord createBuiltInName(byte builtInName, int index)
1908
    {
1932
    {
1909
        if (index == -1 || index+1 > Short.MAX_VALUE) 
1933
        if (index == -1 || index + 1 > Short.MAX_VALUE)
1910
            throw new IllegalArgumentException("Index is not valid ["+index+"]");
1934
            throw new IllegalArgumentException("Index is not valid [" + index
1911
        
1935
                    + "]");
1912
        NameRecord name = new NameRecord(builtInName, (short)(index));
1936
1913
                
1937
        NameRecord name = new NameRecord(builtInName, (short) (index));
1938
        String prefix = EXCEL_BUILT_IN_TITLES_ + index + " ";
1939
        int cont = 0;
1940
        while (checkNameAlreadyExists(linkTable, name)) {
1941
            cont++;
1942
            name = new NameRecord();
1943
            String built_in_name = prefix + cont;
1944
            name.setNameText(built_in_name);
1945
        }
1946
1947
1914
        addName(name);
1948
        addName(name);
1915
        
1949
1916
        return name;
1950
        return name;
1917
    }
1951
    }
1918
1952
(-)testcases/org/apache/poi/hssf/usermodel/TestNamedRange.java (-7 / +10 lines)
Lines 394-408 Link Here
394
		assertNotNull("Print Area Not Found (Sheet 1)", retrievedPrintArea);
394
		assertNotNull("Print Area Not Found (Sheet 1)", retrievedPrintArea);
395
		assertEquals(reference, retrievedPrintArea);
395
		assertEquals(reference, retrievedPrintArea);
396
396
397
		String retrievedPrintArea2 = workbook.getPrintArea(1);
397
		// TODO: the following statements are no more valid: 
398
		assertNotNull("Print Area Not Found (Sheet 2)", retrievedPrintArea2);
398
		// no duplicated builtInName name in the same document;
399
		assertEquals(reference2, retrievedPrintArea2);
399
		
400
//		String retrievedPrintArea2 = workbook.getPrintArea(1);
401
//		assertNotNull("Print Area Not Found (Sheet 2)", retrievedPrintArea2);
402
//		assertEquals(reference2, retrievedPrintArea2);
403
//
404
//		String retrievedPrintArea3 = workbook.getPrintArea(2);
405
//		assertNotNull("Print Area Not Found (Sheet 3)", retrievedPrintArea3);
406
//		assertEquals(reference3, retrievedPrintArea3);
400
407
401
		String retrievedPrintArea3 = workbook.getPrintArea(2);
402
		assertNotNull("Print Area Not Found (Sheet 3)", retrievedPrintArea3);
403
		assertEquals(reference3, retrievedPrintArea3);
404
408
405
406
	}
409
	}
407
410
408
	/**
411
	/**

Return to bug 45126