Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

HSSFShapeFactory.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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.*;
  19. import org.apache.poi.hssf.record.*;
  20. import org.apache.poi.poifs.filesystem.DirectoryNode;
  21. import java.util.Iterator;
  22. import java.util.List;
  23. import java.util.Map;
  24. /**
  25. * Factory class for producing Excel Shapes from Escher records
  26. */
  27. public class HSSFShapeFactory {
  28. /**
  29. * build shape tree from escher container
  30. * @param container root escher container from which escher records must be taken
  31. * @param agg - EscherAggregate
  32. * @param out - shape container to which shapes must be added
  33. * @param root - node to create HSSFObjectData shapes
  34. */
  35. public static void createShapeTree(EscherContainerRecord container, EscherAggregate agg, HSSFShapeContainer out, DirectoryNode root) {
  36. if (container.getRecordId() == EscherContainerRecord.SPGR_CONTAINER) {
  37. ObjRecord obj = null;
  38. EscherClientDataRecord clientData = ((EscherContainerRecord) container.getChild(0)).getChildById(EscherClientDataRecord.RECORD_ID);
  39. if (null != clientData) {
  40. obj = (ObjRecord) agg.getShapeToObjMapping().get(clientData);
  41. }
  42. HSSFShapeGroup group = new HSSFShapeGroup(container, obj);
  43. List<EscherContainerRecord> children = container.getChildContainers();
  44. // skip the first child record, it is group descriptor
  45. for (int i = 0; i < children.size(); i++) {
  46. EscherContainerRecord spContainer = children.get(i);
  47. if (i != 0) {
  48. createShapeTree(spContainer, agg, group, root);
  49. }
  50. }
  51. out.addShape(group);
  52. } else if (container.getRecordId() == EscherContainerRecord.SP_CONTAINER) {
  53. Map<EscherRecord, Record> shapeToObj = agg.getShapeToObjMapping();
  54. ObjRecord objRecord = null;
  55. TextObjectRecord txtRecord = null;
  56. for (EscherRecord record : container.getChildRecords()) {
  57. switch (record.getRecordId()) {
  58. case EscherClientDataRecord.RECORD_ID:
  59. objRecord = (ObjRecord) shapeToObj.get(record);
  60. break;
  61. case EscherTextboxRecord.RECORD_ID:
  62. txtRecord = (TextObjectRecord) shapeToObj.get(record);
  63. break;
  64. }
  65. }
  66. if (isEmbeddedObject(objRecord)) {
  67. HSSFObjectData objectData = new HSSFObjectData(container, objRecord, root);
  68. out.addShape(objectData);
  69. return;
  70. }
  71. CommonObjectDataSubRecord cmo = (CommonObjectDataSubRecord) objRecord.getSubRecords().get(0);
  72. final HSSFShape shape;
  73. switch (cmo.getObjectType()) {
  74. case CommonObjectDataSubRecord.OBJECT_TYPE_PICTURE:
  75. shape = new HSSFPicture(container, objRecord);
  76. break;
  77. case CommonObjectDataSubRecord.OBJECT_TYPE_RECTANGLE:
  78. shape = new HSSFSimpleShape(container, objRecord, txtRecord);
  79. break;
  80. case CommonObjectDataSubRecord.OBJECT_TYPE_LINE:
  81. shape = new HSSFSimpleShape(container, objRecord);
  82. break;
  83. case CommonObjectDataSubRecord.OBJECT_TYPE_COMBO_BOX:
  84. shape = new HSSFCombobox(container, objRecord);
  85. break;
  86. case CommonObjectDataSubRecord.OBJECT_TYPE_MICROSOFT_OFFICE_DRAWING:
  87. EscherOptRecord optRecord = container.getChildById(EscherOptRecord.RECORD_ID);
  88. if(optRecord == null) {
  89. shape = new HSSFSimpleShape(container, objRecord, txtRecord);
  90. } else {
  91. EscherProperty property = optRecord.lookup(EscherProperties.GEOMETRY__VERTICES);
  92. if (null != property) {
  93. shape = new HSSFPolygon(container, objRecord, txtRecord);
  94. } else {
  95. shape = new HSSFSimpleShape(container, objRecord, txtRecord);
  96. }
  97. }
  98. break;
  99. case CommonObjectDataSubRecord.OBJECT_TYPE_TEXT:
  100. shape = new HSSFTextbox(container, objRecord, txtRecord);
  101. break;
  102. case CommonObjectDataSubRecord.OBJECT_TYPE_COMMENT:
  103. shape = new HSSFComment(container, objRecord, txtRecord, agg.getNoteRecordByObj(objRecord));
  104. break;
  105. default:
  106. shape = new HSSFSimpleShape(container, objRecord, txtRecord);
  107. }
  108. out.addShape(shape);
  109. }
  110. }
  111. private static boolean isEmbeddedObject(ObjRecord obj) {
  112. Iterator<SubRecord> subRecordIter = obj.getSubRecords().iterator();
  113. while (subRecordIter.hasNext()) {
  114. SubRecord sub = subRecordIter.next();
  115. if (sub instanceof EmbeddedObjectRefSubRecord) {
  116. return true;
  117. }
  118. }
  119. return false;
  120. }
  121. }