Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

HSSFShapeFactory.java 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.apache.poi.hssf.usermodel;
  18. import org.apache.poi.ddf.EscherClientDataRecord;
  19. import org.apache.poi.ddf.EscherContainerRecord;
  20. import org.apache.poi.ddf.EscherRecord;
  21. import org.apache.poi.ddf.EscherSpRecord;
  22. import org.apache.poi.ddf.EscherSpgrRecord;
  23. import org.apache.poi.ddf.EscherTextboxRecord;
  24. import org.apache.poi.hssf.model.TextboxShape;
  25. import org.apache.poi.hssf.record.CommonObjectDataSubRecord;
  26. import org.apache.poi.hssf.record.EscherAggregate;
  27. import org.apache.poi.hssf.record.NoteRecord;
  28. import org.apache.poi.hssf.record.ObjRecord;
  29. import org.apache.poi.hssf.record.Record;
  30. import org.apache.poi.hssf.record.TextObjectRecord;
  31. import org.apache.poi.hssf.usermodel.drawing.HSSFShapeType;
  32. import java.lang.reflect.Constructor;
  33. import java.lang.reflect.InvocationTargetException;
  34. import java.util.HashMap;
  35. import java.util.List;
  36. import java.util.Map;
  37. /**
  38. * @author evgeniy
  39. * date: 05.06.12
  40. */
  41. public class HSSFShapeFactory {
  42. private static final Map<Short, Class> shapeTypeToClass = new HashMap<Short, Class>(HSSFShapeType.values().length);
  43. private static final ReflectionConstructorShapeCreator shapeCreator = new ReflectionConstructorShapeCreator(shapeTypeToClass);
  44. static {
  45. for (HSSFShapeType type: HSSFShapeType.values()){
  46. shapeTypeToClass.put(type.getType(), type.getShape());
  47. }
  48. }
  49. private static class ReflectionConstructorShapeCreator {
  50. private final Map<Short, Class> shapeTypeToClass;
  51. private ReflectionConstructorShapeCreator(Map<Short, Class> shapeTypeToClass) {
  52. this.shapeTypeToClass = shapeTypeToClass;
  53. }
  54. public HSSFShape createNewShape(Short type, EscherContainerRecord spContainer, ObjRecord objRecord){
  55. if (!shapeTypeToClass.containsKey(type)){
  56. return new HSSFUnknownShape(spContainer, objRecord);
  57. }
  58. Class clazz = shapeTypeToClass.get(type);
  59. if (null == clazz){
  60. System.out.println("No class attached to shape type: "+type);
  61. return new HSSFUnknownShape(spContainer, objRecord);
  62. }
  63. try{
  64. Constructor constructor = clazz.getConstructor(new Class[]{EscherContainerRecord.class, ObjRecord.class});
  65. return (HSSFShape) constructor.newInstance(spContainer, objRecord);
  66. } catch (NoSuchMethodException e) {
  67. throw new IllegalStateException(clazz.getName() +" doesn't have required for shapes constructor");
  68. } catch (Exception e) {
  69. throw new IllegalStateException("Couldn't create new instance of " + clazz.getName());
  70. }
  71. }
  72. }
  73. public static HSSFShape createShape(EscherRecord container, ObjRecord objRecord){
  74. if (0 == container.getChildRecords().size()){
  75. throw new IllegalArgumentException("Couldn't create shape from empty escher container");
  76. }
  77. if (container.getChild(0) instanceof EscherSpgrRecord){
  78. return new HSSFShapeGroup((EscherContainerRecord) container, objRecord);
  79. }
  80. //TODO implement cases for all shapes
  81. return new HSSFUnknownShape(container, objRecord);
  82. }
  83. public static HSSFShapeGroup createShapeGroup(){
  84. return null;
  85. }
  86. public static HSSFShapeGroup createSimpleShape(EscherRecord container, ObjRecord objRecord){
  87. return null;
  88. }
  89. public static void createShapeTree(EscherContainerRecord container, EscherAggregate agg, HSSFShapeContainer out){
  90. if(container.getRecordId() == EscherContainerRecord.SPGR_CONTAINER){
  91. HSSFShapeGroup group = new HSSFShapeGroup(container,
  92. null /* shape containers don't have a associated Obj record*/);
  93. List<EscherContainerRecord> children = container.getChildContainers();
  94. // skip the first child record, it is group descriptor
  95. for(int i = 0; i < children.size(); i++) {
  96. EscherContainerRecord spContainer = children.get(i);
  97. if(i == 0){
  98. EscherSpgrRecord spgr = (EscherSpgrRecord)spContainer.getChildById(EscherSpgrRecord.RECORD_ID);
  99. group.setCoordinates(
  100. spgr.getRectX1(), spgr.getRectY1(),
  101. spgr.getRectX2(), spgr.getRectY2()
  102. );
  103. } else {
  104. createShapeTree(spContainer, agg, group);
  105. }
  106. }
  107. out.addShape(group);
  108. } else if (container.getRecordId() == EscherContainerRecord.SP_CONTAINER){
  109. Map<EscherRecord, Record> shapeToObj = agg.getShapeToObjMapping();
  110. EscherSpRecord spRecord = null;
  111. ObjRecord objRecord = null;
  112. TextObjectRecord txtRecord = null;
  113. for(EscherRecord record : container.getChildRecords()) {
  114. switch(record.getRecordId()) {
  115. case EscherSpRecord.RECORD_ID:
  116. spRecord = (EscherSpRecord)record;
  117. break;
  118. case EscherClientDataRecord.RECORD_ID:
  119. objRecord = (ObjRecord)shapeToObj.get(record);
  120. break;
  121. case EscherTextboxRecord.RECORD_ID:
  122. txtRecord = (TextObjectRecord)shapeToObj.get(record);
  123. break;
  124. }
  125. }
  126. if (null != objRecord){
  127. HSSFShape shape = shapeCreator.createNewShape(spRecord.getShapeType(), container, objRecord);
  128. out.addShape(shape);
  129. }
  130. if (null != txtRecord){
  131. //TODO resolve textbox
  132. // TextboxShape shape = new TextboxShape(container, txtRecord);
  133. // out.a
  134. }
  135. //
  136. // //TODO decide what shape to create based on ObjRecord / EscherSpRecord
  137. // HSSFShape shape = new HSSFUnknownShape(container, objRecord);
  138. // out.addShape(shape);
  139. }
  140. }
  141. }