1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package org.openoffice.xmerge.converter.xml;
25 
26 import org.w3c.dom.Document;
27 import org.w3c.dom.DOMException;
28 import org.w3c.dom.Element;
29 
30 
31 /**
32  * This class represents embedded object's in an OpenOffice.org document that
33  * have a binary representation.
34  */
35 public class EmbeddedBinaryObject extends EmbeddedObject {
36 
37     /** The object's binary representation. */
38     protected byte[] objData = null;
39 
40     /**
41      * Constructor for an embedded object stored using an XML representation.
42      *
43      * @param   name    The name of the object.
44      * @param   type    The mime-type of the object.  See the class summary.
45      */
EmbeddedBinaryObject(String name, String type)46     public EmbeddedBinaryObject(String name, String type) {
47         super(name, type);
48     }
49 
50 
51     /**
52      * Package private constructor for use when reading an object from a
53      * compressed SX? file.
54      *
55      * @param   name    The name of the object.
56      * @param   type    The mime-type of the object.  See the class summary.
57      * @param   source  The OfficeZip representation of the SX? file that stores
58      *                  the object.
59      */
EmbeddedBinaryObject(String name, String type, OfficeZip source)60     EmbeddedBinaryObject(String name, String type, OfficeZip source) {
61         super(name, type, source);
62     }
63 
64 
65     /**
66      * This method returns the data for this object.
67      *
68      * @return  A <code>byte</code> array containing the object's data.
69      */
getBinaryData()70     public byte[] getBinaryData() {
71 
72         if (objData == null) {
73             // See if we came from a Zip file
74             if (zipFile != null) {
75                 objData = zipFile.getNamedBytes(objName);
76             }
77         }
78 
79         return objData;
80     }
81 
82 
83     /**
84      * Sets the data for this object.
85      *
86      * @param   data    A <code>byte</code> array containing data for the object.
87      */
setBinaryData(byte[] data)88     public void setBinaryData(byte[] data) {
89         objData = data;
90         hasChanged = true;
91     }
92 
93     /**
94      * Package private method for writing the data of the EmbeddedObject to a
95      * SX? file.
96      *
97      * @param   zip     An <code>OfficeZip</code> instance representing the file
98      *                  the data is to be written to.
99      */
write(OfficeZip zip)100     void write(OfficeZip zip) {
101         if (hasChanged) {
102             zip.setNamedBytes(objName, objData);
103         }
104     }
105 
106 
107     /**
108      * Package private method that constructs the manifest.xml entries for this
109      * embedded object.
110      */
writeManifestData(Document manifestDoc)111     void writeManifestData(Document manifestDoc) throws DOMException {
112         Element objNode = manifestDoc.createElement(OfficeConstants.TAG_MANIFEST_FILE);
113 
114         objNode.setAttribute(OfficeConstants.ATTRIBUTE_MANIFEST_FILE_TYPE, objType);
115         objNode.setAttribute(OfficeConstants.ATTRIBUTE_MANIFEST_FILE_PATH,  objName);
116 
117         manifestDoc.getDocumentElement().appendChild(objNode);
118     }
119 
120 }
121 
122