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.

HSLFShapeFactory.java 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.hslf.usermodel;
  16. import java.util.Iterator;
  17. import java.util.List;
  18. import org.apache.poi.ddf.*;
  19. import org.apache.poi.hslf.model.*;
  20. import org.apache.poi.hslf.record.*;
  21. import org.apache.poi.sl.usermodel.ShapeContainer;
  22. import org.apache.poi.sl.usermodel.ShapeType;
  23. import org.apache.poi.util.POILogFactory;
  24. import org.apache.poi.util.POILogger;
  25. /**
  26. * Create a <code>Shape</code> object depending on its type
  27. *
  28. * @author Yegor Kozlov
  29. */
  30. public final class HSLFShapeFactory {
  31. // For logging
  32. protected static final POILogger logger = POILogFactory.getLogger(HSLFShapeFactory.class);
  33. /**
  34. * Create a new shape from the data provided.
  35. */
  36. public static HSLFShape createShape(EscherContainerRecord spContainer, ShapeContainer<HSLFShape,HSLFTextParagraph> parent){
  37. if (spContainer.getRecordId() == EscherContainerRecord.SPGR_CONTAINER){
  38. return createShapeGroup(spContainer, parent);
  39. }
  40. return createSimpleShape(spContainer, parent);
  41. }
  42. public static HSLFGroupShape createShapeGroup(EscherContainerRecord spContainer, ShapeContainer<HSLFShape,HSLFTextParagraph> parent){
  43. boolean isTable = false;
  44. EscherContainerRecord ecr = (EscherContainerRecord)spContainer.getChild(0);
  45. EscherRecord opt = HSLFShape.getEscherChild(ecr, (short)RecordTypes.EscherUserDefined);
  46. if (opt != null) {
  47. EscherPropertyFactory f = new EscherPropertyFactory();
  48. List<EscherProperty> props = f.createProperties( opt.serialize(), 8, opt.getInstance() );
  49. for (EscherProperty ep : props) {
  50. if (ep.getPropertyNumber() == EscherProperties.GROUPSHAPE__TABLEPROPERTIES
  51. && ep instanceof EscherSimpleProperty
  52. && (((EscherSimpleProperty)ep).getPropertyValue() & 1) == 1) {
  53. isTable = true;
  54. break;
  55. }
  56. }
  57. }
  58. HSLFGroupShape group;
  59. if (isTable) {
  60. group = new HSLFTable(spContainer, parent);
  61. } else {
  62. group = new HSLFGroupShape(spContainer, parent);
  63. }
  64. return group;
  65. }
  66. public static HSLFShape createSimpleShape(EscherContainerRecord spContainer, ShapeContainer<HSLFShape,HSLFTextParagraph> parent){
  67. HSLFShape shape = null;
  68. EscherSpRecord spRecord = spContainer.getChildById(EscherSpRecord.RECORD_ID);
  69. ShapeType type = ShapeType.forId(spRecord.getShapeType(), false);
  70. switch (type){
  71. case TEXT_BOX:
  72. shape = new HSLFTextBox(spContainer, parent);
  73. break;
  74. case HOST_CONTROL:
  75. case FRAME:
  76. shape = createFrame(spContainer, parent);
  77. break;
  78. case LINE:
  79. shape = new HSLFLine(spContainer, parent);
  80. break;
  81. case NOT_PRIMITIVE:
  82. shape = createNonPrimitive(spContainer, parent);
  83. break;
  84. default:
  85. EscherTextboxRecord etr = spContainer.getChildById(EscherTextboxRecord.RECORD_ID);
  86. if (parent instanceof HSLFTable && etr != null) {
  87. shape = new HSLFTableCell(spContainer, (HSLFTable)parent);
  88. } else {
  89. shape = new HSLFAutoShape(spContainer, parent);
  90. }
  91. break;
  92. }
  93. return shape;
  94. }
  95. private static HSLFShape createFrame(EscherContainerRecord spContainer, ShapeContainer<HSLFShape,HSLFTextParagraph> parent) {
  96. InteractiveInfo info = getClientDataRecord(spContainer, RecordTypes.InteractiveInfo.typeID);
  97. if(info != null && info.getInteractiveInfoAtom() != null){
  98. switch(info.getInteractiveInfoAtom().getAction()){
  99. case InteractiveInfoAtom.ACTION_OLE:
  100. return new OLEShape(spContainer, parent);
  101. case InteractiveInfoAtom.ACTION_MEDIA:
  102. return new MovieShape(spContainer, parent);
  103. default:
  104. break;
  105. }
  106. }
  107. OEShapeAtom oes = getClientDataRecord(spContainer, RecordTypes.OEShapeAtom.typeID);
  108. if (oes != null){
  109. return new OLEShape(spContainer, parent);
  110. }
  111. return new HSLFPictureShape(spContainer, parent);
  112. }
  113. private static HSLFShape createNonPrimitive(EscherContainerRecord spContainer, ShapeContainer<HSLFShape,HSLFTextParagraph> parent) {
  114. AbstractEscherOptRecord opt = HSLFShape.getEscherChild(spContainer, EscherOptRecord.RECORD_ID);
  115. EscherProperty prop = HSLFShape.getEscherProperty(opt, EscherProperties.GEOMETRY__VERTICES);
  116. if(prop != null) {
  117. return new HSLFFreeformShape(spContainer, parent);
  118. }
  119. logger.log(POILogger.INFO, "Creating AutoShape for a NotPrimitive shape");
  120. return new HSLFAutoShape(spContainer, parent);
  121. }
  122. @SuppressWarnings("unchecked")
  123. protected static <T extends Record> T getClientDataRecord(EscherContainerRecord spContainer, int recordType) {
  124. for (Iterator<EscherRecord> it = spContainer.getChildIterator(); it.hasNext();) {
  125. EscherRecord obj = it.next();
  126. if (obj.getRecordId() == EscherClientDataRecord.RECORD_ID) {
  127. byte[] data = obj.serialize();
  128. for (Record r : Record.findChildRecords(data, 8, data.length - 8)) {
  129. if (r.getRecordType() == recordType) {
  130. return (T)r;
  131. }
  132. }
  133. }
  134. }
  135. return null;
  136. }
  137. }