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.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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.EscherClientDataRecord;
  19. import org.apache.poi.ddf.EscherContainerRecord;
  20. import org.apache.poi.ddf.EscherOptRecord;
  21. import org.apache.poi.ddf.EscherProperties;
  22. import org.apache.poi.ddf.EscherProperty;
  23. import org.apache.poi.ddf.EscherPropertyFactory;
  24. import org.apache.poi.ddf.EscherRecord;
  25. import org.apache.poi.ddf.EscherSimpleProperty;
  26. import org.apache.poi.ddf.EscherSpRecord;
  27. import org.apache.poi.hslf.model.*;
  28. import org.apache.poi.hslf.record.InteractiveInfo;
  29. import org.apache.poi.hslf.record.InteractiveInfoAtom;
  30. import org.apache.poi.hslf.record.OEShapeAtom;
  31. import org.apache.poi.hslf.record.Record;
  32. import org.apache.poi.hslf.record.RecordTypes;
  33. import org.apache.poi.hslf.usermodel.*;
  34. import org.apache.poi.sl.usermodel.ShapeContainer;
  35. import org.apache.poi.sl.usermodel.ShapeType;
  36. import org.apache.poi.util.POILogFactory;
  37. import org.apache.poi.util.POILogger;
  38. /**
  39. * Create a <code>Shape</code> object depending on its type
  40. *
  41. * @author Yegor Kozlov
  42. */
  43. public final class HSLFShapeFactory {
  44. // For logging
  45. protected static final POILogger logger = POILogFactory.getLogger(HSLFShapeFactory.class);
  46. /**
  47. * Create a new shape from the data provided.
  48. */
  49. public static HSLFShape createShape(EscherContainerRecord spContainer, ShapeContainer<HSLFShape> parent){
  50. if (spContainer.getRecordId() == EscherContainerRecord.SPGR_CONTAINER){
  51. return createShapeGroup(spContainer, parent);
  52. }
  53. return createSimpleShape(spContainer, parent);
  54. }
  55. public static HSLFGroupShape createShapeGroup(EscherContainerRecord spContainer, ShapeContainer<HSLFShape> parent){
  56. HSLFGroupShape group = null;
  57. EscherRecord opt = HSLFShape.getEscherChild((EscherContainerRecord)spContainer.getChild(0), (short)0xF122);
  58. if(opt != null){
  59. try {
  60. EscherPropertyFactory f = new EscherPropertyFactory();
  61. List<EscherProperty> props = f.createProperties( opt.serialize(), 8, opt.getInstance() );
  62. EscherSimpleProperty p = (EscherSimpleProperty)props.get(0);
  63. if(p.getPropertyNumber() == 0x39F && p.getPropertyValue() == 1){
  64. group = new HSLFTable(spContainer, parent);
  65. } else {
  66. group = new HSLFGroupShape(spContainer, parent);
  67. }
  68. } catch (Exception e){
  69. logger.log(POILogger.WARN, e.getMessage());
  70. group = new HSLFGroupShape(spContainer, parent);
  71. }
  72. } else {
  73. group = new HSLFGroupShape(spContainer, parent);
  74. }
  75. return group;
  76. }
  77. public static HSLFShape createSimpleShape(EscherContainerRecord spContainer, ShapeContainer<HSLFShape> parent){
  78. HSLFShape shape = null;
  79. EscherSpRecord spRecord = spContainer.getChildById(EscherSpRecord.RECORD_ID);
  80. ShapeType type = ShapeType.forId(spRecord.getShapeType(), false);
  81. switch (type){
  82. case TEXT_BOX:
  83. shape = new HSLFTextBox(spContainer, parent);
  84. break;
  85. case HOST_CONTROL:
  86. case FRAME: {
  87. InteractiveInfo info = getClientDataRecord(spContainer, RecordTypes.InteractiveInfo.typeID);
  88. OEShapeAtom oes = getClientDataRecord(spContainer, RecordTypes.OEShapeAtom.typeID);
  89. if(info != null && info.getInteractiveInfoAtom() != null){
  90. switch(info.getInteractiveInfoAtom().getAction()){
  91. case InteractiveInfoAtom.ACTION_OLE:
  92. shape = new OLEShape(spContainer, parent);
  93. break;
  94. case InteractiveInfoAtom.ACTION_MEDIA:
  95. shape = new MovieShape(spContainer, parent);
  96. break;
  97. default:
  98. break;
  99. }
  100. } else if (oes != null){
  101. shape = new OLEShape(spContainer, parent);
  102. }
  103. if(shape == null) shape = new HSLFPictureShape(spContainer, parent);
  104. break;
  105. }
  106. case LINE:
  107. shape = new Line(spContainer, parent);
  108. break;
  109. case NOT_PRIMITIVE: {
  110. EscherOptRecord opt = HSLFShape.getEscherChild(spContainer, EscherOptRecord.RECORD_ID);
  111. EscherProperty prop = HSLFShape.getEscherProperty(opt, EscherProperties.GEOMETRY__VERTICES);
  112. if(prop != null)
  113. shape = new HSLFFreeformShape(spContainer, parent);
  114. else {
  115. logger.log(POILogger.WARN, "Creating AutoShape for a NotPrimitive shape");
  116. shape = new HSLFAutoShape(spContainer, parent);
  117. }
  118. break;
  119. }
  120. default:
  121. shape = new HSLFAutoShape(spContainer, parent);
  122. break;
  123. }
  124. return shape;
  125. }
  126. @SuppressWarnings("unchecked")
  127. protected static <T extends Record> T getClientDataRecord(EscherContainerRecord spContainer, int recordType) {
  128. Record oep = null;
  129. for (Iterator<EscherRecord> it = spContainer.getChildIterator(); it.hasNext();) {
  130. EscherRecord obj = it.next();
  131. if (obj.getRecordId() == EscherClientDataRecord.RECORD_ID) {
  132. byte[] data = obj.serialize();
  133. Record[] records = Record.findChildRecords(data, 8, data.length - 8);
  134. for (int j = 0; j < records.length; j++) {
  135. if (records[j].getRecordType() == recordType) {
  136. return (T)records[j];
  137. }
  138. }
  139. }
  140. }
  141. return (T)oep;
  142. }
  143. }