From 0f299e5b5f6b0b8f27e379ddf669499fea1cb572 Mon Sep 17 00:00:00 2001 From: Yegor Kozlov Date: Sun, 3 Mar 2013 16:16:12 +0000 Subject: [PATCH] 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 --- .../poi/hssf/usermodel/HSSFWorkbook.java | 3 +- .../poi/ss/formula/udf/IndexedUDFFinder.java | 54 +++++++++++ .../poi/xssf/model/IndexedUDFFinder.java | 93 ++++++++----------- .../usermodel/XSSFEvaluationWorkbook.java | 2 +- .../poi/xssf/usermodel/XSSFWorkbook.java | 2 +- 5 files changed, 95 insertions(+), 59 deletions(-) create mode 100644 src/java/org/apache/poi/ss/formula/udf/IndexedUDFFinder.java diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java index f3076d3ccc..8f2a33f6b5 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java @@ -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); diff --git a/src/java/org/apache/poi/ss/formula/udf/IndexedUDFFinder.java b/src/java/org/apache/poi/ss/formula/udf/IndexedUDFFinder.java new file mode 100644 index 0000000000..279df4fb99 --- /dev/null +++ b/src/java/org/apache/poi/ss/formula/udf/IndexedUDFFinder.java @@ -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 _funcMap; + + public IndexedUDFFinder(UDFFinder... usedToolPacks) { + super(usedToolPacks); + _funcMap = new HashMap(); + } + + 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(); + } +} diff --git a/src/ooxml/java/org/apache/poi/xssf/model/IndexedUDFFinder.java b/src/ooxml/java/org/apache/poi/xssf/model/IndexedUDFFinder.java index 2814a89a84..475c0612c0 100644 --- a/src/ooxml/java/org/apache/poi/xssf/model/IndexedUDFFinder.java +++ b/src/ooxml/java/org/apache/poi/xssf/model/IndexedUDFFinder.java @@ -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 _funcMap; - - public IndexedUDFFinder(UDFFinder... usedToolPacks) { - super(usedToolPacks); - _funcMap = new HashMap(); - } - - 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); + } +} diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFEvaluationWorkbook.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFEvaluationWorkbook.java index e9ba3c7bb1..a49502f737 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFEvaluationWorkbook.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFEvaluationWorkbook.java @@ -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; /** diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java index d7a0e00b98..ab23bbf36f 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java @@ -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;