From 99d53d932f707cf9c2e7a64b07418fbb077f1408 Mon Sep 17 00:00:00 2001 From: Andreas Beeker Date: Sun, 16 Aug 2020 14:32:54 +0000 Subject: [PATCH] #64036 - Replace reflection calls in factories for Java 9+ migrated XDGF factories git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1880900 13f79535-47bb-0310-9956-ffa450edef68 --- .../usermodel/section/GeometryRowTypes.java | 89 +++++++++++++++++++ .../usermodel/section/GeometrySection.java | 8 +- .../xdgf/usermodel/section/XDGFSection.java | 39 +------- .../usermodel/section/XDGFSectionTypes.java | 77 ++++++++++++++++ .../section/geometry/GeometryRowFactory.java | 61 ------------- .../apache/poi/xdgf/util/ObjectFactory.java | 57 ------------ 6 files changed, 173 insertions(+), 158 deletions(-) create mode 100644 src/ooxml/java/org/apache/poi/xdgf/usermodel/section/GeometryRowTypes.java create mode 100644 src/ooxml/java/org/apache/poi/xdgf/usermodel/section/XDGFSectionTypes.java delete mode 100644 src/ooxml/java/org/apache/poi/xdgf/usermodel/section/geometry/GeometryRowFactory.java delete mode 100644 src/ooxml/java/org/apache/poi/xdgf/util/ObjectFactory.java diff --git a/src/ooxml/java/org/apache/poi/xdgf/usermodel/section/GeometryRowTypes.java b/src/ooxml/java/org/apache/poi/xdgf/usermodel/section/GeometryRowTypes.java new file mode 100644 index 0000000000..b841da4ada --- /dev/null +++ b/src/ooxml/java/org/apache/poi/xdgf/usermodel/section/GeometryRowTypes.java @@ -0,0 +1,89 @@ +/* ==================================================================== + 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.xdgf.usermodel.section; + +import java.util.Map; +import java.util.function.Function; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import com.microsoft.schemas.office.visio.x2012.main.RowType; +import org.apache.poi.ooxml.POIXMLException; +import org.apache.poi.util.Internal; +import org.apache.poi.xdgf.usermodel.section.geometry.ArcTo; +import org.apache.poi.xdgf.usermodel.section.geometry.Ellipse; +import org.apache.poi.xdgf.usermodel.section.geometry.EllipticalArcTo; +import org.apache.poi.xdgf.usermodel.section.geometry.GeometryRow; +import org.apache.poi.xdgf.usermodel.section.geometry.InfiniteLine; +import org.apache.poi.xdgf.usermodel.section.geometry.LineTo; +import org.apache.poi.xdgf.usermodel.section.geometry.MoveTo; +import org.apache.poi.xdgf.usermodel.section.geometry.NURBSTo; +import org.apache.poi.xdgf.usermodel.section.geometry.PolyLineTo; +import org.apache.poi.xdgf.usermodel.section.geometry.RelCubBezTo; +import org.apache.poi.xdgf.usermodel.section.geometry.RelEllipticalArcTo; +import org.apache.poi.xdgf.usermodel.section.geometry.RelLineTo; +import org.apache.poi.xdgf.usermodel.section.geometry.RelMoveTo; +import org.apache.poi.xdgf.usermodel.section.geometry.RelQuadBezTo; +import org.apache.poi.xdgf.usermodel.section.geometry.SplineKnot; +import org.apache.poi.xdgf.usermodel.section.geometry.SplineStart; + +@Internal +enum GeometryRowTypes { + ARC_TO("ArcTo", ArcTo::new), + ELLIPSE("Ellipse", Ellipse::new), + ELLIPTICAL_ARC_TO("EllipticalArcTo", EllipticalArcTo::new), + INFINITE_LINE("InfiniteLine", InfiniteLine::new), + LINE_TO("LineTo", LineTo::new), + MOVE_TO("MoveTo", MoveTo::new), + NURBS_TO("NURBSTo", NURBSTo::new), + // Note - two different spellings depending on version used...! + POLYLINE_TO("PolylineTo", PolyLineTo::new), + REL_CUB_BEZ_TO("RelCubBezTo", RelCubBezTo::new), + REL_ELLIPTICAL_ARC_TO("RelEllipticalArcTo", RelEllipticalArcTo::new), + REL_LINE_TO("RelLineTo", RelLineTo::new), + REL_MOVE_TO("RelMoveTo", RelMoveTo::new), + REL_QUAD_BEZ_TO("RelQuadBezTo", RelQuadBezTo::new), + SPLINE_KNOT("SplineKnot", SplineKnot::new), + SPLINE_START("SplineStart", SplineStart::new) + ; + + public String rowType; + public Function constructor; + + GeometryRowTypes(String rowType, Function constructor) { + this.rowType = rowType; + this.constructor = constructor; + } + + public String getRowType() { + return rowType; + } + + public static GeometryRow load(RowType row) { + final String name = row.getT(); + GeometryRowTypes l = LOOKUP.get(name); + if (l == null) { + final String typeName = row.schemaType().getName().getLocalPart(); + throw new POIXMLException("Invalid '" + typeName + "' name '" + name + "'"); + } + return l.constructor.apply(row); + } + + private static final Map LOOKUP = + Stream.of(values()).collect(Collectors.toMap(GeometryRowTypes::getRowType, Function.identity())); +} diff --git a/src/ooxml/java/org/apache/poi/xdgf/usermodel/section/GeometrySection.java b/src/ooxml/java/org/apache/poi/xdgf/usermodel/section/GeometrySection.java index cb212e343b..b9da05f1f5 100644 --- a/src/ooxml/java/org/apache/poi/xdgf/usermodel/section/GeometrySection.java +++ b/src/ooxml/java/org/apache/poi/xdgf/usermodel/section/GeometrySection.java @@ -23,6 +23,8 @@ import java.util.Map.Entry; import java.util.SortedMap; import java.util.TreeMap; +import com.microsoft.schemas.office.visio.x2012.main.RowType; +import com.microsoft.schemas.office.visio.x2012.main.SectionType; import org.apache.poi.ooxml.POIXMLException; import org.apache.poi.xdgf.geom.SplineCollector; import org.apache.poi.xdgf.usermodel.XDGFCell; @@ -30,14 +32,10 @@ import org.apache.poi.xdgf.usermodel.XDGFShape; import org.apache.poi.xdgf.usermodel.XDGFSheet; import org.apache.poi.xdgf.usermodel.section.geometry.Ellipse; import org.apache.poi.xdgf.usermodel.section.geometry.GeometryRow; -import org.apache.poi.xdgf.usermodel.section.geometry.GeometryRowFactory; import org.apache.poi.xdgf.usermodel.section.geometry.InfiniteLine; import org.apache.poi.xdgf.usermodel.section.geometry.SplineKnot; import org.apache.poi.xdgf.usermodel.section.geometry.SplineStart; -import com.microsoft.schemas.office.visio.x2012.main.RowType; -import com.microsoft.schemas.office.visio.x2012.main.SectionType; - public class GeometrySection extends XDGFSection { GeometrySection _master; @@ -52,7 +50,7 @@ public class GeometrySection extends XDGFSection { if (_rows.containsKey(row.getIX())) throw new POIXMLException("Index element '" + row.getIX() + "' already exists"); - _rows.put(row.getIX(), GeometryRowFactory.load(row)); + _rows.put(row.getIX(), GeometryRowTypes.load(row)); } } diff --git a/src/ooxml/java/org/apache/poi/xdgf/usermodel/section/XDGFSection.java b/src/ooxml/java/org/apache/poi/xdgf/usermodel/section/XDGFSection.java index d3cc1a9d16..c6340456a2 100644 --- a/src/ooxml/java/org/apache/poi/xdgf/usermodel/section/XDGFSection.java +++ b/src/ooxml/java/org/apache/poi/xdgf/usermodel/section/XDGFSection.java @@ -20,49 +20,18 @@ package org.apache.poi.xdgf.usermodel.section; import java.util.HashMap; import java.util.Map; -import org.apache.poi.ooxml.POIXMLException; +import com.microsoft.schemas.office.visio.x2012.main.CellType; +import com.microsoft.schemas.office.visio.x2012.main.SectionType; import org.apache.poi.util.Internal; import org.apache.poi.xdgf.usermodel.XDGFCell; import org.apache.poi.xdgf.usermodel.XDGFSheet; -import org.apache.poi.xdgf.util.ObjectFactory; - -import com.microsoft.schemas.office.visio.x2012.main.CellType; -import com.microsoft.schemas.office.visio.x2012.main.SectionType; public abstract class XDGFSection { - static final ObjectFactory _sectionTypes; - - static { - _sectionTypes = new ObjectFactory<>(); - try { - _sectionTypes.put("LineGradient", GenericSection.class, SectionType.class, XDGFSheet.class); - _sectionTypes.put("FillGradient", GenericSection.class, SectionType.class, XDGFSheet.class); - _sectionTypes.put("Character", CharacterSection.class, SectionType.class, XDGFSheet.class); - _sectionTypes.put("Paragraph", GenericSection.class, SectionType.class, XDGFSheet.class); - _sectionTypes.put("Tabs", GenericSection.class, SectionType.class, XDGFSheet.class); - _sectionTypes.put("Scratch", GenericSection.class, SectionType.class, XDGFSheet.class); - _sectionTypes.put("Connection", GenericSection.class, SectionType.class, XDGFSheet.class); - _sectionTypes.put("ConnectionABCD", GenericSection.class, SectionType.class, XDGFSheet.class); - _sectionTypes.put("Field", GenericSection.class, SectionType.class, XDGFSheet.class); - _sectionTypes.put("Control", GenericSection.class, SectionType.class, XDGFSheet.class); - _sectionTypes.put("Geometry", GeometrySection.class, SectionType.class, XDGFSheet.class); - _sectionTypes.put("Actions", GenericSection.class, SectionType.class, XDGFSheet.class); - _sectionTypes.put("Layer", GenericSection.class, SectionType.class, XDGFSheet.class); - _sectionTypes.put("User", GenericSection.class, SectionType.class, XDGFSheet.class); - _sectionTypes.put("Property", GenericSection.class, SectionType.class, XDGFSheet.class); - _sectionTypes.put("Hyperlink", GenericSection.class, SectionType.class, XDGFSheet.class); - _sectionTypes.put("Reviewer", GenericSection.class, SectionType.class, XDGFSheet.class); - _sectionTypes.put("Annotation", GenericSection.class, SectionType.class, XDGFSheet.class); - _sectionTypes.put("ActionTag", GenericSection.class, SectionType.class, XDGFSheet.class); - } catch (NoSuchMethodException | SecurityException e) { - throw new POIXMLException("Internal error"); - } - } public static XDGFSection load(SectionType section, XDGFSheet containingSheet) { - return _sectionTypes.load(section.getN(), section, containingSheet); + return XDGFSectionTypes.load(section, containingSheet); } @@ -72,7 +41,7 @@ public abstract class XDGFSection { protected Map _cells = new HashMap<>(); - public XDGFSection(SectionType section, XDGFSheet containingSheet) { + protected XDGFSection(SectionType section, XDGFSheet containingSheet) { _section = section; _containingSheet = containingSheet; diff --git a/src/ooxml/java/org/apache/poi/xdgf/usermodel/section/XDGFSectionTypes.java b/src/ooxml/java/org/apache/poi/xdgf/usermodel/section/XDGFSectionTypes.java new file mode 100644 index 0000000000..8aa0bdca4d --- /dev/null +++ b/src/ooxml/java/org/apache/poi/xdgf/usermodel/section/XDGFSectionTypes.java @@ -0,0 +1,77 @@ +/* ==================================================================== + 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.xdgf.usermodel.section; + +import java.util.Map; +import java.util.function.BiFunction; +import java.util.function.Function; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import com.microsoft.schemas.office.visio.x2012.main.SectionType; +import org.apache.poi.ooxml.POIXMLException; +import org.apache.poi.util.Internal; +import org.apache.poi.xdgf.usermodel.XDGFSheet; + +@Internal +enum XDGFSectionTypes { + LINE_GRADIENT("LineGradient", GenericSection::new), + FILL_GRADIENT("FillGradient", GenericSection::new), + CHARACTER("Character", CharacterSection::new), + PARAGRAPH("Paragraph", GenericSection::new), + TABS("Tabs", GenericSection::new), + SCRATCH("Scratch", GenericSection::new), + CONNECTION("Connection", GenericSection::new), + CONNECTION_ABCD("ConnectionABCD", GenericSection::new), + FIELD("Field", GenericSection::new), + CONTROL("Control", GenericSection::new), + GEOMETRY("Geometry", GeometrySection::new), + ACTIONS("Actions", GenericSection::new), + LAYER("Layer", GenericSection::new), + USER("User", GenericSection::new), + PROPERTY("Property", GenericSection::new), + HYPERLINK("Hyperlink", GenericSection::new), + REVIEWER("Reviewer", GenericSection::new), + ANNOTATION("Annotation", GenericSection::new), + ACTION_TAG("ActionTag", GenericSection::new); + + public String sectionType; + public BiFunction constructor; + + XDGFSectionTypes(String sectionType, BiFunction constructor) { + this.sectionType = sectionType; + this.constructor = constructor; + } + + public String getSectionType() { + return sectionType; + } + + public static XDGFSection load(SectionType section, XDGFSheet containingSheet) { + final String name = section.getN(); + XDGFSectionTypes l = LOOKUP.get(name); + if (l == null) { + final String typeName = section.schemaType().getName().getLocalPart(); + throw new POIXMLException("Invalid '" + typeName + "' name '" + name + "'"); + } + return l.constructor.apply(section, containingSheet); + } + + private static final Map LOOKUP = + Stream.of(values()).collect(Collectors.toMap(XDGFSectionTypes::getSectionType, Function.identity())); +} diff --git a/src/ooxml/java/org/apache/poi/xdgf/usermodel/section/geometry/GeometryRowFactory.java b/src/ooxml/java/org/apache/poi/xdgf/usermodel/section/geometry/GeometryRowFactory.java deleted file mode 100644 index eb9eefdb9d..0000000000 --- a/src/ooxml/java/org/apache/poi/xdgf/usermodel/section/geometry/GeometryRowFactory.java +++ /dev/null @@ -1,61 +0,0 @@ -/* ==================================================================== - 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.xdgf.usermodel.section.geometry; - -import org.apache.poi.ooxml.POIXMLException; -import org.apache.poi.xdgf.util.ObjectFactory; - -import com.microsoft.schemas.office.visio.x2012.main.RowType; - -public class GeometryRowFactory { - - static final ObjectFactory _rowTypes; - - static { - _rowTypes = new ObjectFactory<>(); - try { - _rowTypes.put("ArcTo", ArcTo.class, RowType.class); - _rowTypes.put("Ellipse", Ellipse.class, RowType.class); - _rowTypes.put("EllipticalArcTo", EllipticalArcTo.class, - RowType.class); - _rowTypes.put("InfiniteLine", InfiniteLine.class, RowType.class); - _rowTypes.put("LineTo", LineTo.class, RowType.class); - _rowTypes.put("MoveTo", MoveTo.class, RowType.class); - _rowTypes.put("NURBSTo", NURBSTo.class, RowType.class); - // Note - two different spellings depending on version used...! - _rowTypes.put("PolylineTo", PolyLineTo.class, RowType.class); - _rowTypes.put("PolyLineTo", PolyLineTo.class, RowType.class); - _rowTypes.put("RelCubBezTo", RelCubBezTo.class, RowType.class); - _rowTypes.put("RelEllipticalArcTo", RelEllipticalArcTo.class, - RowType.class); - _rowTypes.put("RelLineTo", RelLineTo.class, RowType.class); - _rowTypes.put("RelMoveTo", RelMoveTo.class, RowType.class); - _rowTypes.put("RelQuadBezTo", RelQuadBezTo.class, RowType.class); - _rowTypes.put("SplineKnot", SplineKnot.class, RowType.class); - _rowTypes.put("SplineStart", SplineStart.class, RowType.class); - } catch (NoSuchMethodException | SecurityException e) { - throw new POIXMLException("Internal error", e); - } - - } - - public static GeometryRow load(RowType row) { - return _rowTypes.load(row.getT(), row); - } - -} diff --git a/src/ooxml/java/org/apache/poi/xdgf/util/ObjectFactory.java b/src/ooxml/java/org/apache/poi/xdgf/util/ObjectFactory.java deleted file mode 100644 index 237d3d105d..0000000000 --- a/src/ooxml/java/org/apache/poi/xdgf/util/ObjectFactory.java +++ /dev/null @@ -1,57 +0,0 @@ -/* ==================================================================== - 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.xdgf.util; - -import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationTargetException; -import java.util.HashMap; -import java.util.Map; - -import org.apache.poi.ooxml.POIXMLException; -import org.apache.xmlbeans.XmlObject; - - -public class ObjectFactory { - - Map> _types = new HashMap<>(); - - public void put(String typeName, Class cls, Class... varargs) throws NoSuchMethodException, SecurityException { - _types.put(typeName, cls.getDeclaredConstructor(varargs)); - } - - public T load(String name, Object... varargs) { - Constructor constructor = _types.get(name); - if (constructor == null) { - - @SuppressWarnings("unchecked") - X xmlObject = (X) varargs[0]; - - String typeName = xmlObject.schemaType().getName().getLocalPart(); - throw new POIXMLException("Invalid '" + typeName + "' name '" + name + "'"); - } - - try { - return constructor.newInstance(varargs); - } catch (InvocationTargetException e) { - throw new POIXMLException(e.getCause()); - } catch (Exception e) { - throw new POIXMLException(e); - } - } - -} -- 2.39.5