001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.poi.hslf.record;
018:
019: import org.apache.poi.ddf.*;
020: import org.apache.poi.util.LittleEndian;
021:
022: import java.io.OutputStream;
023: import java.io.IOException;
024: import java.io.ByteArrayOutputStream;
025: import java.util.List;
026: import java.util.Iterator;
027:
028: /**
029: * Container records which always exists inside Document.
030: * It always acts as a holder for escher DGG container
031: * which may contain which Escher BStore container information
032: * about pictures containes in the presentation (if any).
033: *
034: * @author Yegor Kozlov
035: */
036: public class PPDrawingGroup extends RecordAtom {
037:
038: private byte[] _header;
039: private EscherContainerRecord dggContainer;
040:
041: protected PPDrawingGroup(byte[] source, int start, int len) {
042: // Get the header
043: _header = new byte[8];
044: System.arraycopy(source, start, _header, 0, 8);
045:
046: // Get the contents for now
047: byte[] contents = new byte[len];
048: System.arraycopy(source, start, contents, 0, len);
049:
050: DefaultEscherRecordFactory erf = new DefaultEscherRecordFactory();
051: EscherRecord child = erf.createRecord(contents, 0);
052: child.fillFields(contents, 0, erf);
053: dggContainer = (EscherContainerRecord) child.getChild(0);
054: }
055:
056: /**
057: * We are type 1035
058: */
059: public long getRecordType() {
060: return RecordTypes.PPDrawingGroup.typeID;
061: }
062:
063: /**
064: * We're pretending to be an atom, so return null
065: */
066: public Record[] getChildRecords() {
067: return null;
068: }
069:
070: public void writeOut(OutputStream out) throws IOException {
071: ByteArrayOutputStream bout = new ByteArrayOutputStream();
072: List child = dggContainer.getChildRecords();
073: for (int i = 0; i < child.size(); i++) {
074: EscherRecord r = (EscherRecord) child.get(i);
075: if (r.getRecordId() == EscherContainerRecord.BSTORE_CONTAINER) {
076: EscherContainerRecord bstore = (EscherContainerRecord) r;
077:
078: ByteArrayOutputStream b2 = new ByteArrayOutputStream();
079: List blip = bstore.getChildRecords();
080: for (Iterator it = blip.iterator(); it.hasNext();) {
081: EscherBSERecord bse = (EscherBSERecord) it.next();
082: byte[] b = new byte[36 + 8];
083: bse.serialize(0, b);
084: b2.write(b);
085: }
086: byte[] bstorehead = new byte[8];
087: LittleEndian.putShort(bstorehead, 0, bstore
088: .getOptions());
089: LittleEndian.putShort(bstorehead, 2, bstore
090: .getRecordId());
091: LittleEndian.putInt(bstorehead, 4, b2.size());
092: bout.write(bstorehead);
093: bout.write(b2.toByteArray());
094:
095: } else {
096: bout.write(r.serialize());
097: }
098: }
099: int size = bout.size();
100:
101: // Update the size (header bytes 5-8)
102: LittleEndian.putInt(_header, 4, size + 8);
103:
104: // Write out our header
105: out.write(_header);
106:
107: byte[] dgghead = new byte[8];
108: LittleEndian.putShort(dgghead, 0, dggContainer.getOptions());
109: LittleEndian.putShort(dgghead, 2, dggContainer.getRecordId());
110: LittleEndian.putInt(dgghead, 4, size);
111: out.write(dgghead);
112:
113: // Finally, write out the children
114: out.write(bout.toByteArray());
115:
116: }
117:
118: public EscherContainerRecord getDggContainer() {
119: return dggContainer;
120: }
121: }
|