LibreOffice Module xmerge (master) 1
EmbeddedXMLObject.java
Go to the documentation of this file.
1/*
2 * This file is part of the LibreOffice project.
3 *
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 *
8 * This file incorporates work covered by the following license notice:
9 *
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
17 */
18
19package org.openoffice.xmerge.converter.xml;
20
21import java.io.IOException;
22
23import javax.xml.parsers.DocumentBuilder;
24import javax.xml.parsers.DocumentBuilderFactory;
25import javax.xml.parsers.ParserConfigurationException;
26
27import org.w3c.dom.Document;
28import org.w3c.dom.DOMException;
29import org.w3c.dom.Element;
30import org.w3c.dom.Node;
31
32import org.xml.sax.SAXException;
33
63public class EmbeddedXMLObject extends EmbeddedObject {
64
65 // Entries for the subdocuments that constitute this object;
66 private Document contentDOM = null;
67 private Document settingsDOM = null;
68 private Document stylesDOM = null;
69
70 private DocumentBuilder builder = null;
71
78 public EmbeddedXMLObject(String name, String type) {
79 super(name, type);
80 }
81
91 EmbeddedXMLObject(String name, String type, OfficeZip source) {
92 super(name, type, source);
93 }
94
103 public Document getContentDOM() throws SAXException, IOException {
104
105 if (contentDOM == null) {
106 contentDOM = getNamedDOM("content.xml");
107 }
108
109 return contentDOM;
110 }
111
117 public void setContentDOM(Document content) {
118 contentDOM = content;
119 hasChanged = true;
120 }
121
130 public Document getSettingsDOM() throws SAXException, IOException {
131
132 if (settingsDOM == null) {
133 settingsDOM = getNamedDOM("settings.xml");
134 }
135
136 return settingsDOM;
137 }
138
144 public void setSettingsDOM(Document settings) {
145 settingsDOM = settings;
146 hasChanged = true;
147 }
148
157 public Document getStylesDOM() throws SAXException, IOException {
158
159 if (stylesDOM == null) {
160 stylesDOM = getNamedDOM("styles.xml");
161 }
162
163 return stylesDOM;
164 }
165
171 public void setStylesDOM(Document styles) {
173 hasChanged = true;
174 }
175
188 private Document getNamedDOM(String name) throws SAXException, IOException {
189 if (zipFile == null) {
190 return null;
191 }
192
193 try {
194 if (builder == null) {
195 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
196
197 factory.setValidating(false);
198 builder = factory.newDocumentBuilder();
199 }
200
201 byte[] data = zipFile.getNamedBytes((objName + "/" + name));
202 if (data != null) {
203 return OfficeDocument.parse(builder, data);
204 } else {
205 return null;
206 }
207
208 } catch (ParserConfigurationException pce) {
209 throw new SAXException(pce);
210 }
211 }
212
220 @Override
221 void write(OfficeZip zip) throws IOException {
222 if (hasChanged) {
223 if (contentDOM != null) {
224 zip.setNamedBytes((objName + "/content.xml"),
225 OfficeDocument.docToBytes(contentDOM));
226 }
227 if (settingsDOM != null) {
228 zip.setNamedBytes((objName + "/settings.xml"),
229 OfficeDocument.docToBytes(settingsDOM));
230 }
231 if (stylesDOM != null) {
232 zip.setNamedBytes((objName + "/styles.xml"),
233 OfficeDocument.docToBytes(stylesDOM));
234 }
235 }
236 }
237
244 @Override
245 void writeManifestData(Document manifestDoc) throws DOMException {
246 Node root = manifestDoc.getDocumentElement();
247
248 if (contentDOM != null) {
249 Element contentNode = manifestDoc.createElement(OfficeConstants.TAG_MANIFEST_FILE);
250
251 contentNode.setAttribute(OfficeConstants.ATTRIBUTE_MANIFEST_FILE_TYPE, "text/xml");
252 contentNode.setAttribute(OfficeConstants.ATTRIBUTE_MANIFEST_FILE_PATH, (objName + "/content.xml"));
253
254 root.appendChild(contentNode);
255 }
256
257 if (settingsDOM != null) {
258 Element settingsNode = manifestDoc.createElement(OfficeConstants.TAG_MANIFEST_FILE);
259
260 settingsNode.setAttribute(OfficeConstants.ATTRIBUTE_MANIFEST_FILE_TYPE, "text/xml");
261 settingsNode.setAttribute(OfficeConstants.ATTRIBUTE_MANIFEST_FILE_PATH, (objName + "/settings.xml"));
262
263 root.appendChild(settingsNode);
264 }
265
266 if (stylesDOM != null) {
267 Element stylesNode = manifestDoc.createElement(OfficeConstants.TAG_MANIFEST_FILE);
268
269 stylesNode.setAttribute(OfficeConstants.ATTRIBUTE_MANIFEST_FILE_TYPE, "text/xml");
270 stylesNode.setAttribute(OfficeConstants.ATTRIBUTE_MANIFEST_FILE_PATH, (objName + "/styles.xml"));
271 }
272
273 Element objectNode = manifestDoc.createElement(OfficeConstants.TAG_MANIFEST_FILE);
274
275 objectNode.setAttribute(OfficeConstants.ATTRIBUTE_MANIFEST_FILE_TYPE, objType);
276 objectNode.setAttribute(OfficeConstants.ATTRIBUTE_MANIFEST_FILE_PATH, (objName + "/"));
277
278 root.appendChild(objectNode);
279 }
280}
boolean hasChanged
Flag indicating if this document has changed since reading or is new.
OfficeZip zipFile
Representation of the file from which this object was read.
This class represents those embedded objects in an OpenOffice.org document that have an XML represent...
EmbeddedXMLObject(String name, String type)
Constructor for an embedded object stored using an XML representation.
void setSettingsDOM(Document settings)
Sets the settings data for the embedded object.
Document getSettingsDOM()
Returns the settings data for this embedded object.
Document getStylesDOM()
Returns the style data for this embedded object.
void setContentDOM(Document content)
Sets the content data for the embedded object.
Document getNamedDOM(String name)
This method extracts the data for the given XML file from the SX? file and creates a DOM representati...
Document getContentDOM()
Returns the content data for this embedded object.
void setStylesDOM(Document styles)
Sets the styles data for the embedded object.
const char * name
ResultType type
PointerStyles const styles[]