Bugzilla 54625 - Register user-defined functions in instance scope instead of static

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1452060 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Yegor Kozlov 2013-03-03 16:16:12 +00:00
parent ec930fd66a
commit 0f299e5b5f
5 changed files with 95 additions and 59 deletions

View File

@ -57,6 +57,7 @@ import org.apache.poi.ss.util.WorkbookUtil;
import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.poi.ss.formula.udf.IndexedUDFFinder;
/**
@ -145,7 +146,7 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss
* The locator of user-defined functions.
* By default includes functions from the Excel Analysis Toolpack
*/
private UDFFinder _udfFinder = UDFFinder.DEFAULT;
private UDFFinder _udfFinder = new IndexedUDFFinder(UDFFinder.DEFAULT);
public static HSSFWorkbook create(InternalWorkbook book) {
return new HSSFWorkbook(book);

View File

@ -0,0 +1,54 @@
/* ====================================================================
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.ss.formula.udf;
import org.apache.poi.ss.formula.functions.FreeRefFunction;
import org.apache.poi.util.Internal;
import java.util.HashMap;
/**
* A UDFFinder that can retrieve functions both by name and by fake index.
*
* @author Yegor Kozlov
*/
@Internal
public class IndexedUDFFinder extends AggregatingUDFFinder {
private final HashMap<Integer, String> _funcMap;
public IndexedUDFFinder(UDFFinder... usedToolPacks) {
super(usedToolPacks);
_funcMap = new HashMap<Integer, String>();
}
public FreeRefFunction findFunction(String name) {
FreeRefFunction func = super.findFunction(name);
if (func != null) {
int idx = getFunctionIndex(name);
_funcMap.put(idx, name);
}
return func;
}
public String getFunctionName(int idx) {
return _funcMap.get(idx);
}
public int getFunctionIndex(String name) {
return name.hashCode();
}
}

View File

@ -1,56 +1,37 @@
/* ====================================================================
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.xssf.model;
import org.apache.poi.ss.formula.functions.FreeRefFunction;
import org.apache.poi.ss.formula.udf.AggregatingUDFFinder;
import org.apache.poi.ss.formula.udf.UDFFinder;
import org.apache.poi.util.Internal;
import java.util.HashMap;
/**
* A UDFFinder that can retrieve functions both by name and by fake index.
*
* @author Yegor Kozlov
*/
@Internal
public final class IndexedUDFFinder extends AggregatingUDFFinder {
private final HashMap<Integer, String> _funcMap;
public IndexedUDFFinder(UDFFinder... usedToolPacks) {
super(usedToolPacks);
_funcMap = new HashMap<Integer, String>();
}
public FreeRefFunction findFunction(String name) {
FreeRefFunction func = super.findFunction(name);
if (func != null) {
int idx = getFunctionIndex(name);
_funcMap.put(idx, name);
}
return func;
}
public String getFunctionName(int idx) {
return _funcMap.get(idx);
}
public int getFunctionIndex(String name) {
return name.hashCode();
}
}
/*
* ====================================================================
* 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.xssf.model;
import org.apache.poi.ss.formula.udf.UDFFinder;
/**
* IndexedUDFFinder was moved to the org.apache.poi.ss.formula.udf package.
* This class is left ONLY for backwards compatibity with existing code.
*
* @author Yegor Kozlov
* @deprecated
*/
@Deprecated
public final class IndexedUDFFinder extends org.apache.poi.ss.formula.udf.IndexedUDFFinder{
public IndexedUDFFinder(UDFFinder... usedToolPacks) {
super(usedToolPacks);
}
}

View File

@ -31,7 +31,7 @@ import org.apache.poi.ss.formula.FormulaParsingWorkbook;
import org.apache.poi.ss.formula.FormulaRenderingWorkbook;
import org.apache.poi.ss.formula.FormulaType;
import org.apache.poi.ss.formula.udf.UDFFinder;
import org.apache.poi.xssf.model.IndexedUDFFinder;
import org.apache.poi.ss.formula.udf.IndexedUDFFinder;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDefinedName;
/**

View File

@ -41,6 +41,7 @@ import org.apache.poi.openxml4j.opc.PackageRelationship;
import org.apache.poi.openxml4j.opc.PackageRelationshipTypes;
import org.apache.poi.openxml4j.opc.PackagingURIHelper;
import org.apache.poi.openxml4j.opc.TargetMode;
import org.apache.poi.ss.formula.udf.IndexedUDFFinder;
import org.apache.poi.ss.formula.udf.UDFFinder;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
@ -50,7 +51,6 @@ import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.ss.util.WorkbookUtil;
import org.apache.poi.util.*;
import org.apache.poi.xslf.usermodel.XSLFPictureData;
import org.apache.poi.xssf.model.*;
import org.apache.poi.xssf.usermodel.helpers.XSSFFormulaUtils;
import org.apache.xmlbeans.XmlException;