2012-06-08 13:47:37 -04:00
|
|
|
/*
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership.
|
|
|
|
* The ASF licenses this file to You under the Apache License, Version 2.0
|
|
|
|
* (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package org.apache.poi.hssf.usermodel;
|
|
|
|
|
2012-07-01 05:38:08 -04:00
|
|
|
import org.apache.poi.ddf.*;
|
2012-07-13 11:45:01 -04:00
|
|
|
import org.apache.poi.hssf.record.*;
|
|
|
|
import org.apache.poi.poifs.filesystem.DirectoryNode;
|
2012-06-08 13:47:37 -04:00
|
|
|
|
2012-07-13 11:45:01 -04:00
|
|
|
import java.util.Iterator;
|
2012-06-08 13:47:37 -04:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
/**
|
2013-08-23 14:36:41 -04:00
|
|
|
* Factory class for producing Excel Shapes from Escher records
|
2012-06-08 13:47:37 -04:00
|
|
|
*/
|
|
|
|
public class HSSFShapeFactory {
|
2012-08-06 17:06:12 -04:00
|
|
|
/**
|
|
|
|
* build shape tree from escher container
|
|
|
|
* @param container root escher container from which escher records must be taken
|
|
|
|
* @param agg - EscherAggregate
|
|
|
|
* @param out - shape container to which shapes must be added
|
|
|
|
* @param root - node to create HSSFObjectData shapes
|
|
|
|
*/
|
2012-07-13 11:45:01 -04:00
|
|
|
public static void createShapeTree(EscherContainerRecord container, EscherAggregate agg, HSSFShapeContainer out, DirectoryNode root) {
|
|
|
|
if (container.getRecordId() == EscherContainerRecord.SPGR_CONTAINER) {
|
2012-07-19 15:02:43 -04:00
|
|
|
ObjRecord obj = null;
|
2012-07-30 16:37:34 -04:00
|
|
|
EscherClientDataRecord clientData = ((EscherContainerRecord) container.getChild(0)).getChildById(EscherClientDataRecord.RECORD_ID);
|
|
|
|
if (null != clientData) {
|
2012-07-19 15:02:43 -04:00
|
|
|
obj = (ObjRecord) agg.getShapeToObjMapping().get(clientData);
|
|
|
|
}
|
|
|
|
HSSFShapeGroup group = new HSSFShapeGroup(container, obj);
|
2012-06-08 13:47:37 -04:00
|
|
|
List<EscherContainerRecord> children = container.getChildContainers();
|
|
|
|
// skip the first child record, it is group descriptor
|
2012-07-13 11:45:01 -04:00
|
|
|
for (int i = 0; i < children.size(); i++) {
|
2012-06-08 13:47:37 -04:00
|
|
|
EscherContainerRecord spContainer = children.get(i);
|
2012-08-06 17:06:12 -04:00
|
|
|
if (i != 0) {
|
2012-07-13 11:45:01 -04:00
|
|
|
createShapeTree(spContainer, agg, group, root);
|
2012-06-08 13:47:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
out.addShape(group);
|
2012-07-13 11:45:01 -04:00
|
|
|
} else if (container.getRecordId() == EscherContainerRecord.SP_CONTAINER) {
|
2012-06-08 13:47:37 -04:00
|
|
|
Map<EscherRecord, Record> shapeToObj = agg.getShapeToObjMapping();
|
|
|
|
ObjRecord objRecord = null;
|
|
|
|
TextObjectRecord txtRecord = null;
|
|
|
|
|
2012-07-13 11:45:01 -04:00
|
|
|
for (EscherRecord record : container.getChildRecords()) {
|
|
|
|
switch (record.getRecordId()) {
|
2012-06-08 13:47:37 -04:00
|
|
|
case EscherClientDataRecord.RECORD_ID:
|
2012-07-13 11:45:01 -04:00
|
|
|
objRecord = (ObjRecord) shapeToObj.get(record);
|
2012-06-08 13:47:37 -04:00
|
|
|
break;
|
|
|
|
case EscherTextboxRecord.RECORD_ID:
|
2012-07-13 11:45:01 -04:00
|
|
|
txtRecord = (TextObjectRecord) shapeToObj.get(record);
|
2012-06-08 13:47:37 -04:00
|
|
|
break;
|
2016-04-12 08:03:24 -04:00
|
|
|
default:
|
|
|
|
break;
|
2012-06-08 13:47:37 -04:00
|
|
|
}
|
|
|
|
}
|
2012-07-30 16:37:34 -04:00
|
|
|
if (isEmbeddedObject(objRecord)) {
|
2012-07-13 11:45:01 -04:00
|
|
|
HSSFObjectData objectData = new HSSFObjectData(container, objRecord, root);
|
|
|
|
out.addShape(objectData);
|
|
|
|
return;
|
|
|
|
}
|
2012-06-26 07:21:13 -04:00
|
|
|
CommonObjectDataSubRecord cmo = (CommonObjectDataSubRecord) objRecord.getSubRecords().get(0);
|
2015-05-11 16:00:18 -04:00
|
|
|
final HSSFShape shape;
|
2012-06-26 07:21:13 -04:00
|
|
|
switch (cmo.getObjectType()) {
|
|
|
|
case CommonObjectDataSubRecord.OBJECT_TYPE_PICTURE:
|
|
|
|
shape = new HSSFPicture(container, objRecord);
|
|
|
|
break;
|
|
|
|
case CommonObjectDataSubRecord.OBJECT_TYPE_RECTANGLE:
|
2012-07-11 08:08:38 -04:00
|
|
|
shape = new HSSFSimpleShape(container, objRecord, txtRecord);
|
2012-06-26 07:21:13 -04:00
|
|
|
break;
|
2012-07-06 13:00:20 -04:00
|
|
|
case CommonObjectDataSubRecord.OBJECT_TYPE_LINE:
|
|
|
|
shape = new HSSFSimpleShape(container, objRecord);
|
|
|
|
break;
|
2012-07-11 18:22:24 -04:00
|
|
|
case CommonObjectDataSubRecord.OBJECT_TYPE_COMBO_BOX:
|
|
|
|
shape = new HSSFCombobox(container, objRecord);
|
|
|
|
break;
|
2012-07-01 05:38:08 -04:00
|
|
|
case CommonObjectDataSubRecord.OBJECT_TYPE_MICROSOFT_OFFICE_DRAWING:
|
|
|
|
EscherOptRecord optRecord = container.getChildById(EscherOptRecord.RECORD_ID);
|
2015-05-11 16:00:18 -04:00
|
|
|
if(optRecord == null) {
|
|
|
|
shape = new HSSFSimpleShape(container, objRecord, txtRecord);
|
2012-07-01 05:38:08 -04:00
|
|
|
} else {
|
2015-05-11 16:00:18 -04:00
|
|
|
EscherProperty property = optRecord.lookup(EscherProperties.GEOMETRY__VERTICES);
|
|
|
|
if (null != property) {
|
|
|
|
shape = new HSSFPolygon(container, objRecord, txtRecord);
|
|
|
|
} else {
|
|
|
|
shape = new HSSFSimpleShape(container, objRecord, txtRecord);
|
|
|
|
}
|
2012-07-01 05:38:08 -04:00
|
|
|
}
|
|
|
|
break;
|
2012-06-26 07:21:13 -04:00
|
|
|
case CommonObjectDataSubRecord.OBJECT_TYPE_TEXT:
|
|
|
|
shape = new HSSFTextbox(container, objRecord, txtRecord);
|
|
|
|
break;
|
2012-06-28 06:56:55 -04:00
|
|
|
case CommonObjectDataSubRecord.OBJECT_TYPE_COMMENT:
|
|
|
|
shape = new HSSFComment(container, objRecord, txtRecord, agg.getNoteRecordByObj(objRecord));
|
|
|
|
break;
|
2012-06-26 07:21:13 -04:00
|
|
|
default:
|
2012-07-30 16:37:34 -04:00
|
|
|
shape = new HSSFSimpleShape(container, objRecord, txtRecord);
|
2012-06-08 13:47:37 -04:00
|
|
|
}
|
2012-07-30 16:37:34 -04:00
|
|
|
out.addShape(shape);
|
2012-06-08 13:47:37 -04:00
|
|
|
}
|
|
|
|
}
|
2012-07-13 11:45:01 -04:00
|
|
|
|
|
|
|
private static boolean isEmbeddedObject(ObjRecord obj) {
|
|
|
|
Iterator<SubRecord> subRecordIter = obj.getSubRecords().iterator();
|
|
|
|
while (subRecordIter.hasNext()) {
|
|
|
|
SubRecord sub = subRecordIter.next();
|
|
|
|
if (sub instanceof EmbeddedObjectRefSubRecord) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2012-06-08 13:47:37 -04:00
|
|
|
}
|