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.

HSSFShapeFactory.java 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 Berlog
  39. * date: 05.06.12
  40. */
  41. public class HSSFShapeFactory {
  42. private static final Map<Short, HSSFShapeType> shapeTypeToClass = new HashMap<Short, HSSFShapeType>(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);
  47. }
  48. }
  49. private static class ReflectionConstructorShapeCreator {
  50. private final Map<Short, HSSFShapeType> shapeTypeToClass;
  51. private ReflectionConstructorShapeCreator(Map<Short, HSSFShapeType> 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).getShape();
  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 void createShapeTree(EscherContainerRecord container, EscherAggregate agg, HSSFShapeContainer out){
  74. if(container.getRecordId() == EscherContainerRecord.SPGR_CONTAINER){
  75. HSSFShapeGroup group = new HSSFShapeGroup(container,
  76. null /* shape containers don't have a associated Obj record*/);
  77. List<EscherContainerRecord> children = container.getChildContainers();
  78. // skip the first child record, it is group descriptor
  79. for(int i = 0; i < children.size(); i++) {
  80. EscherContainerRecord spContainer = children.get(i);
  81. if(i == 0){
  82. EscherSpgrRecord spgr = (EscherSpgrRecord)spContainer.getChildById(EscherSpgrRecord.RECORD_ID);
  83. } else {
  84. createShapeTree(spContainer, agg, group);
  85. }
  86. }
  87. out.addShape(group);
  88. } else if (container.getRecordId() == EscherContainerRecord.SP_CONTAINER){
  89. Map<EscherRecord, Record> shapeToObj = agg.getShapeToObjMapping();
  90. EscherSpRecord spRecord = null;
  91. ObjRecord objRecord = null;
  92. TextObjectRecord txtRecord = null;
  93. for(EscherRecord record : container.getChildRecords()) {
  94. switch(record.getRecordId()) {
  95. case EscherSpRecord.RECORD_ID:
  96. spRecord = (EscherSpRecord)record;
  97. break;
  98. case EscherClientDataRecord.RECORD_ID:
  99. objRecord = (ObjRecord)shapeToObj.get(record);
  100. break;
  101. case EscherTextboxRecord.RECORD_ID:
  102. txtRecord = (TextObjectRecord)shapeToObj.get(record);
  103. break;
  104. }
  105. }
  106. CommonObjectDataSubRecord cmo = (CommonObjectDataSubRecord) objRecord.getSubRecords().get(0);
  107. HSSFShape shape = null;
  108. switch (cmo.getObjectType()) {
  109. case CommonObjectDataSubRecord.OBJECT_TYPE_PICTURE:
  110. shape = new HSSFPicture(container, objRecord);
  111. break;
  112. case CommonObjectDataSubRecord.OBJECT_TYPE_RECTANGLE:
  113. shape = new HSSFSimpleShape(container, objRecord);
  114. break;
  115. case CommonObjectDataSubRecord.OBJECT_TYPE_TEXT:
  116. shape = new HSSFTextbox(container, objRecord, txtRecord);
  117. break;
  118. default:
  119. shape = new HSSFSimpleShape(container, objRecord);
  120. }
  121. if (null != shape){
  122. out.addShape(shape);
  123. }
  124. // if (null != objRecord){
  125. // HSSFShape shape = shapeCreator.createNewShape(spRecord.getShapeType(), container, objRecord);
  126. // out.addShape(shape);
  127. // }
  128. // if (null != txtRecord){
  129. // //TODO resolve textbox
  130. //// TextboxShape shape = new TextboxShape(container, txtRecord);
  131. //// out.a
  132. // }
  133. ////
  134. //// //TODO decide what shape to create based on ObjRecord / EscherSpRecord
  135. //// HSSFShape shape = new HSSFUnknownShape(container, objRecord);
  136. //// out.addShape(shape);
  137. }
  138. }
  139. }