You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PresetGeometries.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * ====================================================================
  3. * Licensed to the Apache Software Foundation (ASF) under one or more
  4. * contributor license agreements. See the NOTICE file distributed with
  5. * this work for additional information regarding copyright ownership.
  6. * The ASF licenses this file to You under the Apache License, Version 2.0
  7. * (the "License"); you may not use this file except in compliance with
  8. * the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. * ====================================================================
  18. */
  19. package org.apache.poi.sl.draw.geom;
  20. import java.io.IOException;
  21. import java.io.InputStream;
  22. import java.util.Map;
  23. import java.util.Objects;
  24. import java.util.Set;
  25. import java.util.TreeMap;
  26. import javax.xml.stream.XMLInputFactory;
  27. import javax.xml.stream.XMLStreamException;
  28. import javax.xml.stream.XMLStreamReader;
  29. import javax.xml.transform.stream.StreamSource;
  30. import org.apache.poi.util.POILogFactory;
  31. import org.apache.poi.util.POILogger;
  32. import org.apache.poi.util.XMLHelper;
  33. public final class PresetGeometries {
  34. private static final POILogger LOG = POILogFactory.getLogger(PresetGeometries.class);
  35. private final Map<String, CustomGeometry> map = new TreeMap<>();
  36. private static class SingletonHelper{
  37. private static final PresetGeometries INSTANCE = new PresetGeometries();
  38. }
  39. public static PresetGeometries getInstance(){
  40. return SingletonHelper.INSTANCE;
  41. }
  42. private PresetGeometries() {
  43. // use a local object first to not assign a partly constructed object in case of failure
  44. try {
  45. try (InputStream is = PresetGeometries.class.getResourceAsStream("presetShapeDefinitions.xml")) {
  46. XMLInputFactory staxFactory = XMLHelper.newXMLInputFactory();
  47. XMLStreamReader sr = staxFactory.createXMLStreamReader(new StreamSource(is));
  48. try {
  49. PresetParser p = new PresetParser(PresetParser.Mode.FILE);
  50. p.parse(sr);
  51. p.getGeom().forEach(map::put);
  52. } finally {
  53. sr.close();
  54. }
  55. }
  56. } catch (IOException | XMLStreamException e){
  57. throw new RuntimeException(e);
  58. }
  59. }
  60. /**
  61. * Convert a single CustomGeometry object, i.e. from xmlbeans
  62. */
  63. public static CustomGeometry convertCustomGeometry(XMLStreamReader staxReader) {
  64. try {
  65. PresetParser p = new PresetParser(PresetParser.Mode.SHAPE);
  66. p.parse(staxReader);
  67. return p.getGeom().values().stream().findFirst().orElse(null);
  68. } catch (XMLStreamException e) {
  69. LOG.log(POILogger.ERROR, "Unable to parse single custom geometry", e);
  70. return null;
  71. }
  72. }
  73. public CustomGeometry get(String name) {
  74. return name == null ? null : map.get(name);
  75. }
  76. public Set<String> keySet() {
  77. return map.keySet();
  78. }
  79. public int size() {
  80. return map.size();
  81. }
  82. @SuppressWarnings("EqualsWhichDoesntCheckParameterClass")
  83. @Override
  84. public boolean equals(Object o) {
  85. return (this == o);
  86. }
  87. @Override
  88. public int hashCode() {
  89. return Objects.hash(map);
  90. }
  91. }