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.

EscherAggregate.java 52KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155
  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.hssf.record;
  16. import java.io.ByteArrayOutputStream;
  17. import java.io.IOException;
  18. import java.util.*;
  19. import org.apache.poi.ddf.DefaultEscherRecordFactory;
  20. import org.apache.poi.ddf.EscherBoolProperty;
  21. import org.apache.poi.ddf.EscherChildAnchorRecord;
  22. import org.apache.poi.ddf.EscherClientAnchorRecord;
  23. import org.apache.poi.ddf.EscherClientDataRecord;
  24. import org.apache.poi.ddf.EscherContainerRecord;
  25. import org.apache.poi.ddf.EscherDgRecord;
  26. import org.apache.poi.ddf.EscherDggRecord;
  27. import org.apache.poi.ddf.EscherOptRecord;
  28. import org.apache.poi.ddf.EscherProperties;
  29. import org.apache.poi.ddf.EscherRecord;
  30. import org.apache.poi.ddf.EscherRecordFactory;
  31. import org.apache.poi.ddf.EscherSerializationListener;
  32. import org.apache.poi.ddf.EscherSimpleProperty;
  33. import org.apache.poi.ddf.EscherSpRecord;
  34. import org.apache.poi.ddf.EscherSpgrRecord;
  35. import org.apache.poi.ddf.EscherTextboxRecord;
  36. import org.apache.poi.hssf.model.AbstractShape;
  37. import org.apache.poi.hssf.model.CommentShape;
  38. import org.apache.poi.hssf.model.ConvertAnchor;
  39. import org.apache.poi.hssf.model.DrawingManager2;
  40. import org.apache.poi.hssf.model.TextboxShape;
  41. import org.apache.poi.hssf.usermodel.*;
  42. import org.apache.poi.util.POILogFactory;
  43. import org.apache.poi.util.POILogger;
  44. /**
  45. * This class is used to aggregate the MSODRAWING and OBJ record
  46. * combinations. This is necessary due to the bizare way in which
  47. * these records are serialized. What happens is that you get a
  48. * combination of MSODRAWING -> OBJ -> MSODRAWING -> OBJ records
  49. * but the escher records are serialized _across_ the MSODRAWING
  50. * records.
  51. * <p/>
  52. * It gets even worse when you start looking at TXO records.
  53. * <p/>
  54. * So what we do with this class is aggregate lazily. That is
  55. * we don't aggregate the MSODRAWING -> OBJ records unless we
  56. * need to modify them.
  57. * <p/>
  58. * At first document contains 4 types of records which belong to drawing layer.
  59. * There are can be such sequence of record:
  60. * <p/>
  61. * DrawingRecord
  62. * ContinueRecord
  63. * ...
  64. * ContinueRecord
  65. * ObjRecord | TextObjectRecord
  66. * .....
  67. * ContinueRecord
  68. * ...
  69. * ContinueRecord
  70. * ObjRecord | TextObjectRecord
  71. * NoteRecord
  72. * ...
  73. * NoteRecord
  74. * <p/>
  75. * To work with shapes we have to read data from Drawing and Continue records into single array of bytes and
  76. * build escher(office art) records tree from this array.
  77. * Each shape in drawing layer matches corresponding ObjRecord
  78. * Each textbox matches corresponding TextObjectRecord
  79. * <p/>
  80. * ObjRecord contains information about shape. Thus each ObjRecord corresponds EscherContainerRecord(SPGR)
  81. * <p/>
  82. * EscherAggrefate contains also NoteRecords
  83. * NoteRecords must be serial
  84. *
  85. * @author Glen Stampoultzis (glens at apache.org)
  86. */
  87. public final class EscherAggregate extends AbstractEscherHolderRecord {
  88. public static final short sid = 9876; // not a real sid - dummy value
  89. private static POILogger log = POILogFactory.getLogger(EscherAggregate.class);
  90. public static final short ST_MIN = (short) 0;
  91. public static final short ST_NOT_PRIMATIVE = ST_MIN;
  92. public static final short ST_RECTANGLE = (short) 1;
  93. public static final short ST_ROUNDRECTANGLE = (short) 2;
  94. public static final short ST_ELLIPSE = (short) 3;
  95. public static final short ST_DIAMOND = (short) 4;
  96. public static final short ST_ISOCELESTRIANGLE = (short) 5;
  97. public static final short ST_RIGHTTRIANGLE = (short) 6;
  98. public static final short ST_PARALLELOGRAM = (short) 7;
  99. public static final short ST_TRAPEZOID = (short) 8;
  100. public static final short ST_HEXAGON = (short) 9;
  101. public static final short ST_OCTAGON = (short) 10;
  102. public static final short ST_PLUS = (short) 11;
  103. public static final short ST_STAR = (short) 12;
  104. public static final short ST_ARROW = (short) 13;
  105. public static final short ST_THICKARROW = (short) 14;
  106. public static final short ST_HOMEPLATE = (short) 15;
  107. public static final short ST_CUBE = (short) 16;
  108. public static final short ST_BALLOON = (short) 17;
  109. public static final short ST_SEAL = (short) 18;
  110. public static final short ST_ARC = (short) 19;
  111. public static final short ST_LINE = (short) 20;
  112. public static final short ST_PLAQUE = (short) 21;
  113. public static final short ST_CAN = (short) 22;
  114. public static final short ST_DONUT = (short) 23;
  115. public static final short ST_TEXTSIMPLE = (short) 24;
  116. public static final short ST_TEXTOCTAGON = (short) 25;
  117. public static final short ST_TEXTHEXAGON = (short) 26;
  118. public static final short ST_TEXTCURVE = (short) 27;
  119. public static final short ST_TEXTWAVE = (short) 28;
  120. public static final short ST_TEXTRING = (short) 29;
  121. public static final short ST_TEXTONCURVE = (short) 30;
  122. public static final short ST_TEXTONRING = (short) 31;
  123. public static final short ST_STRAIGHTCONNECTOR1 = (short) 32;
  124. public static final short ST_BENTCONNECTOR2 = (short) 33;
  125. public static final short ST_BENTCONNECTOR3 = (short) 34;
  126. public static final short ST_BENTCONNECTOR4 = (short) 35;
  127. public static final short ST_BENTCONNECTOR5 = (short) 36;
  128. public static final short ST_CURVEDCONNECTOR2 = (short) 37;
  129. public static final short ST_CURVEDCONNECTOR3 = (short) 38;
  130. public static final short ST_CURVEDCONNECTOR4 = (short) 39;
  131. public static final short ST_CURVEDCONNECTOR5 = (short) 40;
  132. public static final short ST_CALLOUT1 = (short) 41;
  133. public static final short ST_CALLOUT2 = (short) 42;
  134. public static final short ST_CALLOUT3 = (short) 43;
  135. public static final short ST_ACCENTCALLOUT1 = (short) 44;
  136. public static final short ST_ACCENTCALLOUT2 = (short) 45;
  137. public static final short ST_ACCENTCALLOUT3 = (short) 46;
  138. public static final short ST_BORDERCALLOUT1 = (short) 47;
  139. public static final short ST_BORDERCALLOUT2 = (short) 48;
  140. public static final short ST_BORDERCALLOUT3 = (short) 49;
  141. public static final short ST_ACCENTBORDERCALLOUT1 = (short) 50;
  142. public static final short ST_ACCENTBORDERCALLOUT2 = (short) 51;
  143. public static final short ST_ACCENTBORDERCALLOUT3 = (short) 52;
  144. public static final short ST_RIBBON = (short) 53;
  145. public static final short ST_RIBBON2 = (short) 54;
  146. public static final short ST_CHEVRON = (short) 55;
  147. public static final short ST_PENTAGON = (short) 56;
  148. public static final short ST_NOSMOKING = (short) 57;
  149. public static final short ST_SEAL8 = (short) 58;
  150. public static final short ST_SEAL16 = (short) 59;
  151. public static final short ST_SEAL32 = (short) 60;
  152. public static final short ST_WEDGERECTCALLOUT = (short) 61;
  153. public static final short ST_WEDGERRECTCALLOUT = (short) 62;
  154. public static final short ST_WEDGEELLIPSECALLOUT = (short) 63;
  155. public static final short ST_WAVE = (short) 64;
  156. public static final short ST_FOLDEDCORNER = (short) 65;
  157. public static final short ST_LEFTARROW = (short) 66;
  158. public static final short ST_DOWNARROW = (short) 67;
  159. public static final short ST_UPARROW = (short) 68;
  160. public static final short ST_LEFTRIGHTARROW = (short) 69;
  161. public static final short ST_UPDOWNARROW = (short) 70;
  162. public static final short ST_IRREGULARSEAL1 = (short) 71;
  163. public static final short ST_IRREGULARSEAL2 = (short) 72;
  164. public static final short ST_LIGHTNINGBOLT = (short) 73;
  165. public static final short ST_HEART = (short) 74;
  166. public static final short ST_PICTUREFRAME = (short) 75;
  167. public static final short ST_QUADARROW = (short) 76;
  168. public static final short ST_LEFTARROWCALLOUT = (short) 77;
  169. public static final short ST_RIGHTARROWCALLOUT = (short) 78;
  170. public static final short ST_UPARROWCALLOUT = (short) 79;
  171. public static final short ST_DOWNARROWCALLOUT = (short) 80;
  172. public static final short ST_LEFTRIGHTARROWCALLOUT = (short) 81;
  173. public static final short ST_UPDOWNARROWCALLOUT = (short) 82;
  174. public static final short ST_QUADARROWCALLOUT = (short) 83;
  175. public static final short ST_BEVEL = (short) 84;
  176. public static final short ST_LEFTBRACKET = (short) 85;
  177. public static final short ST_RIGHTBRACKET = (short) 86;
  178. public static final short ST_LEFTBRACE = (short) 87;
  179. public static final short ST_RIGHTBRACE = (short) 88;
  180. public static final short ST_LEFTUPARROW = (short) 89;
  181. public static final short ST_BENTUPARROW = (short) 90;
  182. public static final short ST_BENTARROW = (short) 91;
  183. public static final short ST_SEAL24 = (short) 92;
  184. public static final short ST_STRIPEDRIGHTARROW = (short) 93;
  185. public static final short ST_NOTCHEDRIGHTARROW = (short) 94;
  186. public static final short ST_BLOCKARC = (short) 95;
  187. public static final short ST_SMILEYFACE = (short) 96;
  188. public static final short ST_VERTICALSCROLL = (short) 97;
  189. public static final short ST_HORIZONTALSCROLL = (short) 98;
  190. public static final short ST_CIRCULARARROW = (short) 99;
  191. public static final short ST_NOTCHEDCIRCULARARROW = (short) 100;
  192. public static final short ST_UTURNARROW = (short) 101;
  193. public static final short ST_CURVEDRIGHTARROW = (short) 102;
  194. public static final short ST_CURVEDLEFTARROW = (short) 103;
  195. public static final short ST_CURVEDUPARROW = (short) 104;
  196. public static final short ST_CURVEDDOWNARROW = (short) 105;
  197. public static final short ST_CLOUDCALLOUT = (short) 106;
  198. public static final short ST_ELLIPSERIBBON = (short) 107;
  199. public static final short ST_ELLIPSERIBBON2 = (short) 108;
  200. public static final short ST_FLOWCHARTPROCESS = (short) 109;
  201. public static final short ST_FLOWCHARTDECISION = (short) 110;
  202. public static final short ST_FLOWCHARTINPUTOUTPUT = (short) 111;
  203. public static final short ST_FLOWCHARTPREDEFINEDPROCESS = (short) 112;
  204. public static final short ST_FLOWCHARTINTERNALSTORAGE = (short) 113;
  205. public static final short ST_FLOWCHARTDOCUMENT = (short) 114;
  206. public static final short ST_FLOWCHARTMULTIDOCUMENT = (short) 115;
  207. public static final short ST_FLOWCHARTTERMINATOR = (short) 116;
  208. public static final short ST_FLOWCHARTPREPARATION = (short) 117;
  209. public static final short ST_FLOWCHARTMANUALINPUT = (short) 118;
  210. public static final short ST_FLOWCHARTMANUALOPERATION = (short) 119;
  211. public static final short ST_FLOWCHARTCONNECTOR = (short) 120;
  212. public static final short ST_FLOWCHARTPUNCHEDCARD = (short) 121;
  213. public static final short ST_FLOWCHARTPUNCHEDTAPE = (short) 122;
  214. public static final short ST_FLOWCHARTSUMMINGJUNCTION = (short) 123;
  215. public static final short ST_FLOWCHARTOR = (short) 124;
  216. public static final short ST_FLOWCHARTCOLLATE = (short) 125;
  217. public static final short ST_FLOWCHARTSORT = (short) 126;
  218. public static final short ST_FLOWCHARTEXTRACT = (short) 127;
  219. public static final short ST_FLOWCHARTMERGE = (short) 128;
  220. public static final short ST_FLOWCHARTOFFLINESTORAGE = (short) 129;
  221. public static final short ST_FLOWCHARTONLINESTORAGE = (short) 130;
  222. public static final short ST_FLOWCHARTMAGNETICTAPE = (short) 131;
  223. public static final short ST_FLOWCHARTMAGNETICDISK = (short) 132;
  224. public static final short ST_FLOWCHARTMAGNETICDRUM = (short) 133;
  225. public static final short ST_FLOWCHARTDISPLAY = (short) 134;
  226. public static final short ST_FLOWCHARTDELAY = (short) 135;
  227. public static final short ST_TEXTPLAINTEXT = (short) 136;
  228. public static final short ST_TEXTSTOP = (short) 137;
  229. public static final short ST_TEXTTRIANGLE = (short) 138;
  230. public static final short ST_TEXTTRIANGLEINVERTED = (short) 139;
  231. public static final short ST_TEXTCHEVRON = (short) 140;
  232. public static final short ST_TEXTCHEVRONINVERTED = (short) 141;
  233. public static final short ST_TEXTRINGINSIDE = (short) 142;
  234. public static final short ST_TEXTRINGOUTSIDE = (short) 143;
  235. public static final short ST_TEXTARCHUPCURVE = (short) 144;
  236. public static final short ST_TEXTARCHDOWNCURVE = (short) 145;
  237. public static final short ST_TEXTCIRCLECURVE = (short) 146;
  238. public static final short ST_TEXTBUTTONCURVE = (short) 147;
  239. public static final short ST_TEXTARCHUPPOUR = (short) 148;
  240. public static final short ST_TEXTARCHDOWNPOUR = (short) 149;
  241. public static final short ST_TEXTCIRCLEPOUR = (short) 150;
  242. public static final short ST_TEXTBUTTONPOUR = (short) 151;
  243. public static final short ST_TEXTCURVEUP = (short) 152;
  244. public static final short ST_TEXTCURVEDOWN = (short) 153;
  245. public static final short ST_TEXTCASCADEUP = (short) 154;
  246. public static final short ST_TEXTCASCADEDOWN = (short) 155;
  247. public static final short ST_TEXTWAVE1 = (short) 156;
  248. public static final short ST_TEXTWAVE2 = (short) 157;
  249. public static final short ST_TEXTWAVE3 = (short) 158;
  250. public static final short ST_TEXTWAVE4 = (short) 159;
  251. public static final short ST_TEXTINFLATE = (short) 160;
  252. public static final short ST_TEXTDEFLATE = (short) 161;
  253. public static final short ST_TEXTINFLATEBOTTOM = (short) 162;
  254. public static final short ST_TEXTDEFLATEBOTTOM = (short) 163;
  255. public static final short ST_TEXTINFLATETOP = (short) 164;
  256. public static final short ST_TEXTDEFLATETOP = (short) 165;
  257. public static final short ST_TEXTDEFLATEINFLATE = (short) 166;
  258. public static final short ST_TEXTDEFLATEINFLATEDEFLATE = (short) 167;
  259. public static final short ST_TEXTFADERIGHT = (short) 168;
  260. public static final short ST_TEXTFADELEFT = (short) 169;
  261. public static final short ST_TEXTFADEUP = (short) 170;
  262. public static final short ST_TEXTFADEDOWN = (short) 171;
  263. public static final short ST_TEXTSLANTUP = (short) 172;
  264. public static final short ST_TEXTSLANTDOWN = (short) 173;
  265. public static final short ST_TEXTCANUP = (short) 174;
  266. public static final short ST_TEXTCANDOWN = (short) 175;
  267. public static final short ST_FLOWCHARTALTERNATEPROCESS = (short) 176;
  268. public static final short ST_FLOWCHARTOFFPAGECONNECTOR = (short) 177;
  269. public static final short ST_CALLOUT90 = (short) 178;
  270. public static final short ST_ACCENTCALLOUT90 = (short) 179;
  271. public static final short ST_BORDERCALLOUT90 = (short) 180;
  272. public static final short ST_ACCENTBORDERCALLOUT90 = (short) 181;
  273. public static final short ST_LEFTRIGHTUPARROW = (short) 182;
  274. public static final short ST_SUN = (short) 183;
  275. public static final short ST_MOON = (short) 184;
  276. public static final short ST_BRACKETPAIR = (short) 185;
  277. public static final short ST_BRACEPAIR = (short) 186;
  278. public static final short ST_SEAL4 = (short) 187;
  279. public static final short ST_DOUBLEWAVE = (short) 188;
  280. public static final short ST_ACTIONBUTTONBLANK = (short) 189;
  281. public static final short ST_ACTIONBUTTONHOME = (short) 190;
  282. public static final short ST_ACTIONBUTTONHELP = (short) 191;
  283. public static final short ST_ACTIONBUTTONINFORMATION = (short) 192;
  284. public static final short ST_ACTIONBUTTONFORWARDNEXT = (short) 193;
  285. public static final short ST_ACTIONBUTTONBACKPREVIOUS = (short) 194;
  286. public static final short ST_ACTIONBUTTONEND = (short) 195;
  287. public static final short ST_ACTIONBUTTONBEGINNING = (short) 196;
  288. public static final short ST_ACTIONBUTTONRETURN = (short) 197;
  289. public static final short ST_ACTIONBUTTONDOCUMENT = (short) 198;
  290. public static final short ST_ACTIONBUTTONSOUND = (short) 199;
  291. public static final short ST_ACTIONBUTTONMOVIE = (short) 200;
  292. public static final short ST_HOSTCONTROL = (short) 201;
  293. public static final short ST_TEXTBOX = (short) 202;
  294. public static final short ST_NIL = (short) 0x0FFF;
  295. protected HSSFPatriarch patriarch;
  296. /**
  297. * Maps shape container objects to their {@link TextObjectRecord} or {@link ObjRecord}
  298. */
  299. private final Map<EscherRecord, Record> shapeToObj = new HashMap<EscherRecord, Record>();
  300. private DrawingManager2 drawingManager;
  301. private short drawingGroupId;
  302. /**
  303. * list of "tail" records that need to be serialized after all drawing group records
  304. */
  305. private List<Record> tailRec = new ArrayList<Record>();
  306. public EscherAggregate() {
  307. buildBaseTree();
  308. }
  309. public EscherAggregate(DrawingManager2 drawingManager) {
  310. this.drawingManager = drawingManager;
  311. }
  312. public DrawingManager2 getDrawingManager() {
  313. return drawingManager;
  314. }
  315. /**
  316. * @return Returns the current sid.
  317. */
  318. public short getSid() {
  319. return sid;
  320. }
  321. /**
  322. * Calculates the string representation of this record. This is
  323. * simply a dump of all the records.
  324. */
  325. public String toString() {
  326. String nl = System.getProperty("line.separtor");
  327. StringBuffer result = new StringBuffer();
  328. result.append('[').append(getRecordName()).append(']' + nl);
  329. for (Iterator iterator = getEscherRecords().iterator(); iterator.hasNext(); ) {
  330. EscherRecord escherRecord = (EscherRecord) iterator.next();
  331. result.append(escherRecord.toString());
  332. }
  333. result.append("[/").append(getRecordName()).append(']' + nl);
  334. return result.toString();
  335. }
  336. public String toXml(String tab) {
  337. StringBuilder builder = new StringBuilder();
  338. builder.append(tab).append("<").append(getRecordName()).append(">\n");
  339. for (Iterator iterator = getEscherRecords().iterator(); iterator.hasNext(); ) {
  340. EscherRecord escherRecord = (EscherRecord) iterator.next();
  341. builder.append(escherRecord.toXml(tab + "\t"));
  342. }
  343. builder.append(tab).append("</").append(getRecordName()).append(">\n");
  344. return builder.toString();
  345. }
  346. private static boolean isDrawingLayerRecord(final short sid) {
  347. return sid == DrawingRecord.sid ||
  348. sid == ContinueRecord.sid ||
  349. sid == ObjRecord.sid ||
  350. sid == TextObjectRecord.sid;
  351. }
  352. /**
  353. * Collapses the drawing records into an aggregate.
  354. * read Drawing and Continue records into single byte array, create Escher tree from byte array, create map <EscherRecord, Record>
  355. */
  356. public static EscherAggregate createAggregate(List records, int locFirstDrawingRecord, DrawingManager2 drawingManager) {
  357. // Keep track of any shape records created so we can match them back to the object id's.
  358. // Textbox objects are also treated as shape objects.
  359. final List<EscherRecord> shapeRecords = new ArrayList<EscherRecord>();
  360. EscherRecordFactory recordFactory = new DefaultEscherRecordFactory() {
  361. public EscherRecord createRecord(byte[] data, int offset) {
  362. EscherRecord r = super.createRecord(data, offset);
  363. if (r.getRecordId() == EscherClientDataRecord.RECORD_ID || r.getRecordId() == EscherTextboxRecord.RECORD_ID) {
  364. shapeRecords.add(r);
  365. }
  366. return r;
  367. }
  368. };
  369. // Create one big buffer
  370. ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  371. EscherAggregate agg = new EscherAggregate(drawingManager);
  372. int loc = locFirstDrawingRecord;
  373. while (loc + 1 < records.size()
  374. && (isDrawingLayerRecord(sid(records, loc)))) {
  375. try {
  376. if (!(sid(records, loc) == DrawingRecord.sid || sid(records, loc) == ContinueRecord.sid)) {
  377. loc++;
  378. continue;
  379. }
  380. if (sid(records, loc) == DrawingRecord.sid) {
  381. buffer.write(((DrawingRecord) records.get(loc)).getRecordData());
  382. } else {
  383. buffer.write(((ContinueRecord) records.get(loc)).getData());
  384. }
  385. } catch (IOException e) {
  386. throw new RuntimeException("Couldn't get data from drawing/continue records", e);
  387. }
  388. loc++;
  389. }
  390. // Decode the shapes
  391. // agg.escherRecords = new ArrayList();
  392. int pos = 0;
  393. while (pos < buffer.size()) {
  394. EscherRecord r = recordFactory.createRecord(buffer.toByteArray(), pos);
  395. int bytesRead = r.fillFields(buffer.toByteArray(), pos, recordFactory);
  396. agg.addEscherRecord(r);
  397. pos += bytesRead;
  398. }
  399. // Associate the object records with the shapes
  400. loc = locFirstDrawingRecord + 1;
  401. int shapeIndex = 0;
  402. while (loc < records.size()
  403. && (isDrawingLayerRecord(sid(records, loc)))) {
  404. if (!isObjectRecord(records, loc)) {
  405. loc++;
  406. continue;
  407. }
  408. Record objRecord = (Record) records.get(loc);
  409. agg.shapeToObj.put(shapeRecords.get(shapeIndex++), objRecord);
  410. loc++;
  411. }
  412. // any NoteRecords that follow the drawing block must be aggregated and and saved in the tailRec collection
  413. // TODO remove this logic. 'tail' records should be inserted in the main record stream
  414. while (loc < records.size()) {
  415. if (sid(records, loc) == NoteRecord.sid) {
  416. NoteRecord r = (NoteRecord) records.get(loc);
  417. agg.tailRec.add(r);
  418. } else {
  419. break;
  420. }
  421. loc++;
  422. }
  423. int locLastDrawingRecord = loc;
  424. // replace drawing block with the created EscherAggregate
  425. records.subList(locFirstDrawingRecord, locLastDrawingRecord).clear();
  426. records.add(locFirstDrawingRecord, agg);
  427. return agg;
  428. }
  429. /**
  430. * Serializes this aggregate to a byte array. Since this is an aggregate
  431. * record it will effectively serialize the aggregated records.
  432. *
  433. * @param offset The offset into the start of the array.
  434. * @param data The byte array to serialize to.
  435. * @return The number of bytes serialized.
  436. */
  437. public int serialize(int offset, byte[] data) {
  438. convertUserModelToRecords();
  439. // Determine buffer size
  440. List records = getEscherRecords();
  441. int size = getEscherRecordSize(records);
  442. byte[] buffer = new byte[size];
  443. // Serialize escher records into one big data structure and keep note of ending offsets.
  444. final List spEndingOffsets = new ArrayList();
  445. final List shapes = new ArrayList();
  446. int pos = 0;
  447. for (Iterator iterator = records.iterator(); iterator.hasNext(); ) {
  448. EscherRecord e = (EscherRecord) iterator.next();
  449. pos += e.serialize(pos, buffer, new EscherSerializationListener() {
  450. public void beforeRecordSerialize(int offset, short recordId, EscherRecord record) {
  451. }
  452. public void afterRecordSerialize(int offset, short recordId, int size, EscherRecord record) {
  453. if (recordId == EscherClientDataRecord.RECORD_ID || recordId == EscherTextboxRecord.RECORD_ID) {
  454. spEndingOffsets.add(offset);
  455. shapes.add(record);
  456. }
  457. }
  458. });
  459. }
  460. // todo: fix this
  461. shapes.add(0, null);
  462. spEndingOffsets.add(0, null);
  463. // Split escher records into separate MSODRAWING and OBJ, TXO records. (We don't break on
  464. // the first one because it's the patriach).
  465. pos = offset;
  466. int writtenEscherBytes = 0;
  467. int i;
  468. for (i = 1; i < shapes.size(); i++) {
  469. int endOffset = (Integer) spEndingOffsets.get(i) - 1;
  470. int startOffset;
  471. if (i == 1)
  472. startOffset = 0;
  473. else
  474. startOffset = (Integer) spEndingOffsets.get(i - 1);
  475. byte[] drawingData = new byte[endOffset - startOffset + 1];
  476. System.arraycopy(buffer, startOffset, drawingData, 0, drawingData.length);
  477. pos += writeDataIntoDrawingRecord(0, drawingData, writtenEscherBytes, pos, data, i);
  478. writtenEscherBytes += drawingData.length;
  479. // Write the matching OBJ record
  480. Record obj = shapeToObj.get(shapes.get(i));
  481. pos += obj.serialize(pos, data);
  482. if (i == shapes.size() - 1 && endOffset < buffer.length - 1) {
  483. drawingData = new byte[buffer.length - endOffset - 1];
  484. System.arraycopy(buffer, endOffset + 1, drawingData, 0, drawingData.length);
  485. pos += writeDataIntoDrawingRecord(0, drawingData, writtenEscherBytes, pos, data, i);
  486. }
  487. }
  488. if ((pos - offset) < buffer.length-1){
  489. byte[] drawingData = new byte[buffer.length - (pos - offset)];
  490. System.arraycopy(buffer, (pos - offset), drawingData, 0, drawingData.length);
  491. pos += writeDataIntoDrawingRecord(0, drawingData, writtenEscherBytes, pos, data, i);
  492. }
  493. // write records that need to be serialized after all drawing group records
  494. for (i = 0; i < tailRec.size(); i++) {
  495. Record rec = tailRec.get(i);
  496. pos += rec.serialize(pos, data);
  497. }
  498. int bytesWritten = pos - offset;
  499. if (bytesWritten != getRecordSize())
  500. throw new RecordFormatException(bytesWritten + " bytes written but getRecordSize() reports " + getRecordSize());
  501. return bytesWritten;
  502. }
  503. private int writeDataIntoDrawingRecord(int temp, byte[] drawingData, int writtenEscherBytes, int pos, byte[] data, int i) {
  504. //First record in drawing layer MUST be DrawingRecord
  505. if (writtenEscherBytes + drawingData.length > RecordInputStream.MAX_RECORD_DATA_SIZE && i != 1) {
  506. for (int j = 0; j < drawingData.length; j += RecordInputStream.MAX_RECORD_DATA_SIZE) {
  507. ContinueRecord drawing = new ContinueRecord(Arrays.copyOfRange(drawingData, j, Math.min(j + RecordInputStream.MAX_RECORD_DATA_SIZE, drawingData.length)));
  508. temp += drawing.serialize(pos + temp, data);
  509. }
  510. } else {
  511. for (int j = 0; j < drawingData.length; j += RecordInputStream.MAX_RECORD_DATA_SIZE) {
  512. if (j == 0) {
  513. DrawingRecord drawing = new DrawingRecord();
  514. drawing.setData(Arrays.copyOfRange(drawingData, j, Math.min(j + RecordInputStream.MAX_RECORD_DATA_SIZE, drawingData.length)));
  515. temp += drawing.serialize(pos + temp, data);
  516. } else {
  517. ContinueRecord drawing = new ContinueRecord(Arrays.copyOfRange(drawingData, j, Math.min(j + RecordInputStream.MAX_RECORD_DATA_SIZE, drawingData.length)));
  518. temp += drawing.serialize(pos + temp, data);
  519. }
  520. }
  521. }
  522. return temp;
  523. }
  524. /**
  525. * How many bytes do the raw escher records contain.
  526. *
  527. * @param records List of escher records
  528. * @return the number of bytes
  529. */
  530. private int getEscherRecordSize(List records) {
  531. int size = 0;
  532. for (Iterator iterator = records.iterator(); iterator.hasNext(); )
  533. size += ((EscherRecord) iterator.next()).getRecordSize();
  534. return size;
  535. }
  536. public int getRecordSize() {
  537. // TODO - convert this to RecordAggregate
  538. convertUserModelToRecords();
  539. // To determine size of aggregate record we have to know size of each DrawingRecord because if DrawingRecord
  540. // is split into several continue records we have to add header size to total EscherAggregate size
  541. int continueRecordsHeadersSize = 0;
  542. // Determine buffer size
  543. List<EscherRecord> records = getEscherRecords();
  544. int rawEscherSize = getEscherRecordSize(records);
  545. byte[] buffer = new byte[rawEscherSize];
  546. final List<Integer> spEndingOffsets = new ArrayList<Integer>();
  547. int pos = 0;
  548. for (EscherRecord e : records) {
  549. pos += e.serialize(pos, buffer, new EscherSerializationListener() {
  550. public void beforeRecordSerialize(int offset, short recordId, EscherRecord record) {
  551. }
  552. public void afterRecordSerialize(int offset, short recordId, int size, EscherRecord record) {
  553. if (recordId == EscherClientDataRecord.RECORD_ID || recordId == EscherTextboxRecord.RECORD_ID) {
  554. spEndingOffsets.add(offset);
  555. }
  556. }
  557. });
  558. }
  559. spEndingOffsets.add(0, 0);
  560. for (int i = 1; i < spEndingOffsets.size(); i++) {
  561. if (i == spEndingOffsets.size() - 1 && spEndingOffsets.get(i) < pos) {
  562. continueRecordsHeadersSize += 4;
  563. }
  564. if (spEndingOffsets.get(i) - spEndingOffsets.get(i - 1) <= RecordInputStream.MAX_RECORD_DATA_SIZE) {
  565. continue;
  566. }
  567. continueRecordsHeadersSize += ((spEndingOffsets.get(i) - spEndingOffsets.get(i - 1)) / RecordInputStream.MAX_RECORD_DATA_SIZE) * 4;
  568. }
  569. int drawingRecordSize = rawEscherSize + (shapeToObj.size()) * 4;
  570. if (rawEscherSize != 0 && spEndingOffsets.size()==1/**EMPTY**/){
  571. continueRecordsHeadersSize +=4;
  572. }
  573. int objRecordSize = 0;
  574. for (Iterator iterator = shapeToObj.values().iterator(); iterator.hasNext(); ) {
  575. Record r = (Record) iterator.next();
  576. objRecordSize += r.getRecordSize();
  577. }
  578. int tailRecordSize = 0;
  579. for (Iterator iterator = tailRec.iterator(); iterator.hasNext(); ) {
  580. Record r = (Record) iterator.next();
  581. tailRecordSize += r.getRecordSize();
  582. }
  583. return drawingRecordSize + objRecordSize + tailRecordSize + continueRecordsHeadersSize;
  584. }
  585. /**
  586. * Associates an escher record to an OBJ record or a TXO record.
  587. */
  588. public Object associateShapeToObjRecord(EscherRecord r, Record objRecord) {
  589. return shapeToObj.put(r, objRecord);
  590. }
  591. public HSSFPatriarch getPatriarch() {
  592. return patriarch;
  593. }
  594. public void setPatriarch(HSSFPatriarch patriarch) {
  595. this.patriarch = patriarch;
  596. }
  597. /**
  598. * Converts the Records into UserModel
  599. * objects on the bound HSSFPatriarch
  600. */
  601. public void convertRecordsToUserModel() {
  602. if (patriarch == null) {
  603. throw new IllegalStateException("Must call setPatriarch() first");
  604. }
  605. // The top level container ought to have
  606. // the DgRecord and the container of one container
  607. // per shape group (patriach overall first)
  608. EscherContainerRecord topContainer = getEscherContainer();
  609. if (topContainer == null) {
  610. return;
  611. }
  612. topContainer = topContainer.getChildContainers().get(0);
  613. List tcc = topContainer.getChildContainers();
  614. if (tcc.size() == 0) {
  615. throw new IllegalStateException("No child escher containers at the point that should hold the patriach data, and one container per top level shape!");
  616. }
  617. // First up, get the patriach position
  618. // This is in the first EscherSpgrRecord, in
  619. // the first container, with a EscherSRecord too
  620. EscherContainerRecord patriachContainer =
  621. (EscherContainerRecord) tcc.get(0);
  622. EscherSpgrRecord spgr = null;
  623. for (Iterator<EscherRecord> it = patriachContainer.getChildIterator(); it.hasNext(); ) {
  624. EscherRecord r = it.next();
  625. if (r instanceof EscherSpgrRecord) {
  626. spgr = (EscherSpgrRecord) r;
  627. break;
  628. }
  629. }
  630. if (spgr != null) {
  631. patriarch.setCoordinates(
  632. spgr.getRectX1(), spgr.getRectY1(),
  633. spgr.getRectX2(), spgr.getRectY2()
  634. );
  635. }
  636. convertRecordsToUserModelRecursive(tcc, patriarch, null);
  637. // Now, clear any trace of what records make up
  638. // the patriarch
  639. // Otherwise, everything will go horribly wrong
  640. // when we try to write out again....
  641. // clearEscherRecords();
  642. drawingManager.getDgg().setFileIdClusters(new EscherDggRecord.FileIdCluster[0]);
  643. // TODO: Support converting our records
  644. // back into shapes
  645. // log.log(POILogger.WARN, "Not processing objects into Patriarch!");
  646. }
  647. private static void convertRecordsToUserModelRecursive(List tcc, HSSFShapeContainer container, HSSFShape parent) {
  648. // Now process the containers for each group
  649. // and objects
  650. for (int i = 1; i < tcc.size(); i++) {
  651. EscherContainerRecord shapeContainer = (EscherContainerRecord) tcc.get(i);
  652. // Could be a group, or a base object
  653. if (shapeContainer.getRecordId() == EscherContainerRecord.SPGR_CONTAINER) {
  654. // Group
  655. final int shapeChildren = shapeContainer.getChildRecords().size();
  656. if (shapeChildren > 0) {
  657. HSSFShapeGroup group = new HSSFShapeGroup(parent, new HSSFClientAnchor());
  658. addToParentOrContainer(group, container, parent);
  659. EscherContainerRecord groupContainer = (EscherContainerRecord) shapeContainer.getChild(0);
  660. convertRecordsToUserModel(groupContainer, group);
  661. if (shapeChildren > 1) {
  662. convertRecordsToUserModelRecursive(shapeContainer.getChildRecords(), container, group);
  663. }
  664. } else {
  665. log.log(POILogger.WARN,
  666. "Found drawing group without children.");
  667. }
  668. } else if (shapeContainer.getRecordId() == EscherContainerRecord.SP_CONTAINER) {
  669. EscherSpRecord spRecord = shapeContainer
  670. .getChildById(EscherSpRecord.RECORD_ID);
  671. int type = spRecord.getShapeType();
  672. switch (type) {
  673. case ST_TEXTBOX:
  674. HSSFTextbox box = new HSSFTextbox(parent,
  675. new HSSFClientAnchor());
  676. addToParentOrContainer(box, container, parent);
  677. convertRecordsToUserModel(shapeContainer, box);
  678. break;
  679. case ST_PICTUREFRAME:
  680. // Duplicated from
  681. // org.apache.poi.hslf.model.Picture.getPictureIndex()
  682. EscherOptRecord opt = (EscherOptRecord) getEscherChild(
  683. shapeContainer, EscherOptRecord.RECORD_ID);
  684. EscherSimpleProperty prop = (EscherSimpleProperty) opt.lookup(
  685. EscherProperties.BLIP__BLIPTODISPLAY);
  686. if (prop == null) {
  687. log.log(POILogger.WARN,
  688. "Picture index for picture shape not found.");
  689. } else {
  690. int pictureIndex = prop.getPropertyValue();
  691. EscherClientAnchorRecord anchorRecord = (EscherClientAnchorRecord) getEscherChild(
  692. shapeContainer,
  693. EscherClientAnchorRecord.RECORD_ID);
  694. EscherChildAnchorRecord childRecord = (EscherChildAnchorRecord) getEscherChild(
  695. shapeContainer,
  696. EscherChildAnchorRecord.RECORD_ID);
  697. if (anchorRecord != null && childRecord != null) {
  698. log.log(POILogger.WARN, "Picture with both CLIENT and CHILD anchor: " + type);
  699. }
  700. HSSFAnchor anchor;
  701. if (anchorRecord != null) {
  702. anchor = toClientAnchor(anchorRecord);
  703. } else {
  704. anchor = toChildAnchor(childRecord);
  705. }
  706. HSSFPicture picture = new HSSFPicture(parent, anchor);
  707. picture.setPictureIndex(pictureIndex);
  708. addToParentOrContainer(picture, container, parent);
  709. }
  710. break;
  711. default:
  712. final HSSFSimpleShape shape = new HSSFSimpleShape(parent,
  713. new HSSFClientAnchor());
  714. addToParentOrContainer(shape, container, parent);
  715. convertRecordsToUserModel(shapeContainer, shape);
  716. log.log(POILogger.WARN, "Unhandled shape type: "
  717. + type);
  718. break;
  719. }
  720. } else {
  721. log.log(POILogger.WARN, "Unexpected record id of shape group.");
  722. }
  723. }
  724. }
  725. private static void addToParentOrContainer(HSSFShape shape, HSSFShapeContainer container, HSSFShape parent) {
  726. if (parent instanceof HSSFShapeGroup)
  727. ((HSSFShapeGroup) parent).addShape(shape);
  728. else if (container instanceof HSSFPatriarch)
  729. ((HSSFPatriarch) container).addShape(shape);
  730. else
  731. container.getChildren().add(shape);
  732. }
  733. public static HSSFClientAnchor toClientAnchor(EscherClientAnchorRecord anchorRecord) {
  734. HSSFClientAnchor anchor = new HSSFClientAnchor();
  735. anchor.setAnchorType(anchorRecord.getFlag());
  736. anchor.setCol1(anchorRecord.getCol1());
  737. anchor.setCol2(anchorRecord.getCol2());
  738. anchor.setDx1(anchorRecord.getDx1());
  739. anchor.setDx2(anchorRecord.getDx2());
  740. anchor.setDy1(anchorRecord.getDy1());
  741. anchor.setDy2(anchorRecord.getDy2());
  742. anchor.setRow1(anchorRecord.getRow1());
  743. anchor.setRow2(anchorRecord.getRow2());
  744. return anchor;
  745. }
  746. public static HSSFChildAnchor toChildAnchor(EscherChildAnchorRecord anchorRecord) {
  747. HSSFChildAnchor anchor = new HSSFChildAnchor();
  748. // anchor.setAnchorType(anchorRecord.getFlag());
  749. // anchor.setCol1( anchorRecord.getCol1() );
  750. // anchor.setCol2( anchorRecord.getCol2() );
  751. anchor.setDx1(anchorRecord.getDx1());
  752. anchor.setDx2(anchorRecord.getDx2());
  753. anchor.setDy1(anchorRecord.getDy1());
  754. anchor.setDy2(anchorRecord.getDy2());
  755. // anchor.setRow1( anchorRecord.getRow1() );
  756. // anchor.setRow2( anchorRecord.getRow2() );
  757. return anchor;
  758. }
  759. private static void convertRecordsToUserModel(EscherContainerRecord shapeContainer, Object model) {
  760. for (Iterator<EscherRecord> it = shapeContainer.getChildIterator(); it.hasNext(); ) {
  761. EscherRecord r = it.next();
  762. if (r instanceof EscherSpgrRecord) {
  763. // This may be overriden by a later EscherClientAnchorRecord
  764. EscherSpgrRecord spgr = (EscherSpgrRecord) r;
  765. if (model instanceof HSSFShapeGroup) {
  766. HSSFShapeGroup g = (HSSFShapeGroup) model;
  767. g.setCoordinates(
  768. spgr.getRectX1(), spgr.getRectY1(),
  769. spgr.getRectX2(), spgr.getRectY2()
  770. );
  771. } else {
  772. throw new IllegalStateException("Got top level anchor but not processing a group");
  773. }
  774. } else if (r instanceof EscherClientAnchorRecord) {
  775. EscherClientAnchorRecord car = (EscherClientAnchorRecord) r;
  776. if (model instanceof HSSFShape) {
  777. HSSFShape g = (HSSFShape) model;
  778. g.getAnchor().setDx1(car.getDx1());
  779. g.getAnchor().setDx2(car.getDx2());
  780. g.getAnchor().setDy1(car.getDy1());
  781. g.getAnchor().setDy2(car.getDy2());
  782. } else {
  783. throw new IllegalStateException("Got top level anchor but not processing a group or shape");
  784. }
  785. } else if (r instanceof EscherTextboxRecord) {
  786. EscherTextboxRecord tbr = (EscherTextboxRecord) r;
  787. // Also need to find the TextObjectRecord too
  788. // TODO
  789. } else if (r instanceof EscherSpRecord) {
  790. // Use flags if needed
  791. final EscherSpRecord spr = (EscherSpRecord) r;
  792. if (model instanceof HSSFShape) {
  793. final HSSFShape s = (HSSFShape) model;
  794. }
  795. } else if (r instanceof EscherOptRecord) {
  796. // Use properties if needed
  797. } else {
  798. //System.err.println(r);
  799. }
  800. }
  801. }
  802. public void clear() {
  803. clearEscherRecords();
  804. shapeToObj.clear();
  805. // lastShapeId = 1024;
  806. }
  807. protected String getRecordName() {
  808. return "ESCHERAGGREGATE";
  809. }
  810. // =============== Private methods ========================
  811. private static boolean isObjectRecord(List records, int loc) {
  812. return sid(records, loc) == ObjRecord.sid || sid(records, loc) == TextObjectRecord.sid;
  813. }
  814. private void convertUserModelToRecords() {
  815. if (patriarch != null) {
  816. shapeToObj.clear();
  817. tailRec.clear();
  818. clearEscherRecords();
  819. if (patriarch.getChildren().size() != 0) {
  820. convertPatriarch(patriarch);
  821. EscherContainerRecord dgContainer = (EscherContainerRecord) getEscherRecord(0);
  822. EscherContainerRecord spgrContainer = null;
  823. Iterator<EscherRecord> iter = dgContainer.getChildIterator();
  824. while (iter.hasNext()) {
  825. EscherRecord child = iter.next();
  826. if (child.getRecordId() == EscherContainerRecord.SPGR_CONTAINER) {
  827. spgrContainer = (EscherContainerRecord) child;
  828. }
  829. }
  830. convertShapes(patriarch, spgrContainer, shapeToObj);
  831. patriarch = null;
  832. }
  833. }
  834. }
  835. private void convertShapes(HSSFShapeContainer parent, EscherContainerRecord escherParent, Map shapeToObj) {
  836. if (escherParent == null) throw new IllegalArgumentException("Parent record required");
  837. List shapes = parent.getChildren();
  838. for (Iterator iterator = shapes.iterator(); iterator.hasNext(); ) {
  839. HSSFShape shape = (HSSFShape) iterator.next();
  840. if (shape instanceof HSSFShapeGroup) {
  841. convertGroup((HSSFShapeGroup) shape, escherParent, shapeToObj);
  842. } else {
  843. AbstractShape shapeModel = AbstractShape.createShape(
  844. shape,
  845. drawingManager.allocateShapeId(drawingGroupId));
  846. shapeToObj.put(findClientData(shapeModel.getSpContainer()), shapeModel.getObjRecord());
  847. if (shapeModel instanceof TextboxShape) {
  848. EscherRecord escherTextbox = ((TextboxShape) shapeModel).getEscherTextbox();
  849. shapeToObj.put(escherTextbox, ((TextboxShape) shapeModel).getTextObjectRecord());
  850. // escherParent.addChildRecord(escherTextbox);
  851. if (shapeModel instanceof CommentShape) {
  852. CommentShape comment = (CommentShape) shapeModel;
  853. tailRec.add(comment.getNoteRecord());
  854. }
  855. }
  856. escherParent.addChildRecord(shapeModel.getSpContainer());
  857. }
  858. }
  859. // drawingManager.newCluster( (short)1 );
  860. // drawingManager.newCluster( (short)2 );
  861. }
  862. private void convertGroup(HSSFShapeGroup shape, EscherContainerRecord escherParent, Map shapeToObj) {
  863. EscherContainerRecord spgrContainer = new EscherContainerRecord();
  864. EscherContainerRecord spContainer = new EscherContainerRecord();
  865. EscherSpgrRecord spgr = new EscherSpgrRecord();
  866. EscherSpRecord sp = new EscherSpRecord();
  867. EscherOptRecord opt = new EscherOptRecord();
  868. EscherRecord anchor;
  869. EscherClientDataRecord clientData = new EscherClientDataRecord();
  870. spgrContainer.setRecordId(EscherContainerRecord.SPGR_CONTAINER);
  871. spgrContainer.setOptions((short) 0x000F);
  872. spContainer.setRecordId(EscherContainerRecord.SP_CONTAINER);
  873. spContainer.setOptions((short) 0x000F);
  874. spgr.setRecordId(EscherSpgrRecord.RECORD_ID);
  875. spgr.setOptions((short) 0x0001);
  876. spgr.setRectX1(shape.getX1());
  877. spgr.setRectY1(shape.getY1());
  878. spgr.setRectX2(shape.getX2());
  879. spgr.setRectY2(shape.getY2());
  880. sp.setRecordId(EscherSpRecord.RECORD_ID);
  881. sp.setOptions((short) 0x0002);
  882. int shapeId = drawingManager.allocateShapeId(drawingGroupId);
  883. sp.setShapeId(shapeId);
  884. if (shape.getAnchor() instanceof HSSFClientAnchor)
  885. sp.setFlags(EscherSpRecord.FLAG_GROUP | EscherSpRecord.FLAG_HAVEANCHOR);
  886. else
  887. sp.setFlags(EscherSpRecord.FLAG_GROUP | EscherSpRecord.FLAG_HAVEANCHOR | EscherSpRecord.FLAG_CHILD);
  888. opt.setRecordId(EscherOptRecord.RECORD_ID);
  889. opt.setOptions((short) 0x0023);
  890. opt.addEscherProperty(new EscherBoolProperty(EscherProperties.PROTECTION__LOCKAGAINSTGROUPING, 0x00040004));
  891. opt.addEscherProperty(new EscherBoolProperty(EscherProperties.GROUPSHAPE__PRINT, 0x00080000));
  892. anchor = ConvertAnchor.createAnchor(shape.getAnchor());
  893. // clientAnchor.setCol1( ( (HSSFClientAnchor) shape.getAnchor() ).getCol1() );
  894. // clientAnchor.setRow1( (short) ( (HSSFClientAnchor) shape.getAnchor() ).getRow1() );
  895. // clientAnchor.setDx1( (short) shape.getAnchor().getDx1() );
  896. // clientAnchor.setDy1( (short) shape.getAnchor().getDy1() );
  897. // clientAnchor.setCol2( ( (HSSFClientAnchor) shape.getAnchor() ).getCol2() );
  898. // clientAnchor.setRow2( (short) ( (HSSFClientAnchor) shape.getAnchor() ).getRow2() );
  899. // clientAnchor.setDx2( (short) shape.getAnchor().getDx2() );
  900. // clientAnchor.setDy2( (short) shape.getAnchor().getDy2() );
  901. clientData.setRecordId(EscherClientDataRecord.RECORD_ID);
  902. clientData.setOptions((short) 0x0000);
  903. spgrContainer.addChildRecord(spContainer);
  904. spContainer.addChildRecord(spgr);
  905. spContainer.addChildRecord(sp);
  906. spContainer.addChildRecord(opt);
  907. spContainer.addChildRecord(anchor);
  908. spContainer.addChildRecord(clientData);
  909. ObjRecord obj = new ObjRecord();
  910. CommonObjectDataSubRecord cmo = new CommonObjectDataSubRecord();
  911. cmo.setObjectType(CommonObjectDataSubRecord.OBJECT_TYPE_GROUP);
  912. cmo.setObjectId(shapeId);
  913. cmo.setLocked(true);
  914. cmo.setPrintable(true);
  915. cmo.setAutofill(true);
  916. cmo.setAutoline(true);
  917. GroupMarkerSubRecord gmo = new GroupMarkerSubRecord();
  918. EndSubRecord end = new EndSubRecord();
  919. obj.addSubRecord(cmo);
  920. obj.addSubRecord(gmo);
  921. obj.addSubRecord(end);
  922. shapeToObj.put(clientData, obj);
  923. escherParent.addChildRecord(spgrContainer);
  924. convertShapes(shape, spgrContainer, shapeToObj);
  925. }
  926. private EscherRecord findClientData(EscherContainerRecord spContainer) {
  927. for (Iterator<EscherRecord> iterator = spContainer.getChildIterator(); iterator.hasNext(); ) {
  928. EscherRecord r = iterator.next();
  929. if (r.getRecordId() == EscherClientDataRecord.RECORD_ID) {
  930. return r;
  931. }
  932. }
  933. throw new IllegalArgumentException("Can not find client data record");
  934. }
  935. private void buildBaseTree(){
  936. EscherContainerRecord dgContainer = new EscherContainerRecord();
  937. EscherContainerRecord spgrContainer = new EscherContainerRecord();
  938. EscherContainerRecord spContainer1 = new EscherContainerRecord();
  939. EscherSpgrRecord spgr = new EscherSpgrRecord();
  940. EscherSpRecord sp1 = new EscherSpRecord();
  941. dgContainer.setRecordId(EscherContainerRecord.DG_CONTAINER);
  942. dgContainer.setOptions((short) 0x000F);
  943. EscherDgRecord dg = new EscherDgRecord();
  944. dg.setRecordId( EscherDgRecord.RECORD_ID );
  945. short dgId = 1;
  946. dg.setOptions((short) (dgId << 4));
  947. dg.setNumShapes(1);
  948. dg.setLastMSOSPID(1024);
  949. drawingGroupId = dg.getDrawingGroupId();
  950. spgrContainer.setRecordId(EscherContainerRecord.SPGR_CONTAINER);
  951. spgrContainer.setOptions((short) 0x000F);
  952. spContainer1.setRecordId(EscherContainerRecord.SP_CONTAINER);
  953. spContainer1.setOptions((short) 0x000F);
  954. spgr.setRecordId(EscherSpgrRecord.RECORD_ID);
  955. spgr.setOptions((short) 0x0001); // version
  956. spgr.setRectX1(0);
  957. spgr.setRectY1(0);
  958. spgr.setRectX2(1023);
  959. spgr.setRectY2(255);
  960. sp1.setRecordId(EscherSpRecord.RECORD_ID);
  961. sp1.setOptions((short) 0x0002);
  962. sp1.setShapeId(1024);
  963. sp1.setFlags(EscherSpRecord.FLAG_GROUP | EscherSpRecord.FLAG_PATRIARCH);
  964. dgContainer.addChildRecord(dg);
  965. dgContainer.addChildRecord(spgrContainer);
  966. spgrContainer.addChildRecord(spContainer1);
  967. spContainer1.addChildRecord(spgr);
  968. spContainer1.addChildRecord(sp1);
  969. addEscherRecord(dgContainer);
  970. }
  971. private void convertPatriarch(HSSFPatriarch patriarch) {
  972. EscherContainerRecord dgContainer = new EscherContainerRecord();
  973. EscherDgRecord dg;
  974. EscherContainerRecord spgrContainer = new EscherContainerRecord();
  975. EscherContainerRecord spContainer1 = new EscherContainerRecord();
  976. EscherSpgrRecord spgr = new EscherSpgrRecord();
  977. EscherSpRecord sp1 = new EscherSpRecord();
  978. dgContainer.setRecordId(EscherContainerRecord.DG_CONTAINER);
  979. dgContainer.setOptions((short) 0x000F);
  980. dg = drawingManager.createDgRecord();
  981. drawingGroupId = dg.getDrawingGroupId();
  982. // dg.setOptions( (short) ( drawingId << 4 ) );
  983. // dg.setNumShapes( getNumberOfShapes( patriarch ) );
  984. // dg.setLastMSOSPID( 0 ); // populated after all shape id's are assigned.
  985. spgrContainer.setRecordId(EscherContainerRecord.SPGR_CONTAINER);
  986. spgrContainer.setOptions((short) 0x000F);
  987. spContainer1.setRecordId(EscherContainerRecord.SP_CONTAINER);
  988. spContainer1.setOptions((short) 0x000F);
  989. spgr.setRecordId(EscherSpgrRecord.RECORD_ID);
  990. spgr.setOptions((short) 0x0001); // version
  991. spgr.setRectX1(patriarch.getX1());
  992. spgr.setRectY1(patriarch.getY1());
  993. spgr.setRectX2(patriarch.getX2());
  994. spgr.setRectY2(patriarch.getY2());
  995. sp1.setRecordId(EscherSpRecord.RECORD_ID);
  996. sp1.setOptions((short) 0x0002);
  997. sp1.setShapeId(drawingManager.allocateShapeId(dg.getDrawingGroupId()));
  998. sp1.setFlags(EscherSpRecord.FLAG_GROUP | EscherSpRecord.FLAG_PATRIARCH);
  999. dgContainer.addChildRecord(dg);
  1000. dgContainer.addChildRecord(spgrContainer);
  1001. spgrContainer.addChildRecord(spContainer1);
  1002. spContainer1.addChildRecord(spgr);
  1003. spContainer1.addChildRecord(sp1);
  1004. addEscherRecord(dgContainer);
  1005. }
  1006. private static short sid(List records, int loc) {
  1007. return ((Record) records.get(loc)).getSid();
  1008. }
  1009. // Duplicated from org.apache.poi.hslf.model.Shape
  1010. /**
  1011. * Helper method to return escher child by record ID
  1012. *
  1013. * @return escher record or <code>null</code> if not found.
  1014. */
  1015. private static EscherRecord getEscherChild(EscherContainerRecord owner,
  1016. int recordId) {
  1017. for (Iterator iterator = owner.getChildRecords().iterator(); iterator
  1018. .hasNext(); ) {
  1019. EscherRecord escherRecord = (EscherRecord) iterator.next();
  1020. if (escherRecord.getRecordId() == recordId)
  1021. return escherRecord;
  1022. }
  1023. return null;
  1024. }
  1025. /**
  1026. * Returns the mapping of {@link EscherClientDataRecord} and {@link EscherTextboxRecord}
  1027. * to their {@link TextObjectRecord} or {@link ObjRecord} .
  1028. * <p/>
  1029. * We need to access it outside of EscherAggregate when building shapes
  1030. *
  1031. * @return
  1032. */
  1033. public Map<EscherRecord, Record> getShapeToObjMapping() {
  1034. return Collections.unmodifiableMap(shapeToObj);
  1035. }
  1036. /**
  1037. *
  1038. * @return tails records. We need to access them when building shapes.
  1039. * Every HSSFComment shape has a link to a NoteRecord from the tailRec collection.
  1040. */
  1041. public List<Record> getTailRecords(){
  1042. return Collections.unmodifiableList(tailRec);
  1043. }
  1044. }