LibreOffice Module xmerge (master) 1
DOMDocument.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.dom;
20
21import java.io.InputStream;
22import java.io.OutputStream;
23import java.io.StringWriter;
24import java.io.ByteArrayOutputStream;
25import java.io.IOException;
26
27import javax.xml.parsers.DocumentBuilderFactory;
28import javax.xml.parsers.DocumentBuilder;
29import javax.xml.parsers.ParserConfigurationException;
30import javax.xml.transform.TransformerFactory;
31import javax.xml.transform.Transformer;
32import javax.xml.transform.stream.StreamResult;
33import javax.xml.transform.dom.DOMSource;
34
35import org.w3c.dom.Node;
36import org.w3c.dom.Document;
37import org.xml.sax.SAXException;
39
43public class DOMDocument
44 implements org.openoffice.xmerge.Document {
45
47 private static DocumentBuilderFactory factory =
48 DocumentBuilderFactory.newInstance();
49
51 private Document contentDoc = null;
52
53 private String documentName = null;
54 private String fileName = null;
55 private String fileExt = null;
56
63 public DOMDocument(String name,String ext) {
64 this(name,ext,true, false);
65 }
66
73 return fileExt;
74 }
75
86 private DOMDocument(String name, String ext,boolean namespaceAware,
87 boolean validating) {
88
89 factory.setValidating(validating);
90 factory.setNamespaceAware(namespaceAware);
91 this.fileExt = ext;
92 this.documentName = trimDocumentName(name);
93 this.fileName = documentName + getFileExtension();
94 }
95
103 private String trimDocumentName(String name) {
104 String temp = name.toLowerCase();
105 String ext = getFileExtension();
106
107 if (temp.endsWith(ext)) {
108 // strip the extension
109 int nlen = name.length();
110 int endIndex = nlen - ext.length();
111 name = name.substring(0,endIndex);
112 }
113
114 return name;
115 }
116
128
129 return contentDoc;
130 }
131
138 public void setContentDOM( Node newDom) {
139 contentDoc=(Document)newDom;
140 }
141
147 public String getName() {
148
149 return documentName;
150 }
151
159
160 return fileName;
161 }
162
170 public void read(InputStream is) throws IOException {
171 Debug.log(Debug.INFO, "reading file");
172 try {
173 DocumentBuilder builder = factory.newDocumentBuilder();
174 contentDoc = builder.parse(is);
175 } catch (ParserConfigurationException ex) {
176 System.out.println("Error:"+ ex);
177 } catch (SAXException ex) {
178 System.out.println("Error:"+ ex);
179 }
180 }
181
189 public void write(OutputStream os) throws IOException {
190
191 // set bytes for writing to output stream
192 byte contentBytes[] = docToBytes(contentDoc);
193
194 os.write(contentBytes);
195 }
196
209 private byte[] docToBytes(Document doc)
210 throws IOException {
211
212 ByteArrayOutputStream baos = new ByteArrayOutputStream();
213
214 java.lang.reflect.Constructor<?> con;
215 java.lang.reflect.Method meth;
216
217 String domImpl = doc.getClass().getName();
218
219 System.err.println("type b " + domImpl);
220
221 /*
222 * We may have multiple XML parsers in the Classpath.
223 * Depending on which one is first, the actual type of
224 * doc may vary. Need a way to find out which API is being
225 * used and use an appropriate serialization method.
226 */
227 try {
228 // First of all try for JAXP 1.0
229 if (domImpl.equals("com.sun.xml.tree.XmlDocument")) {
230 System.out.println("Using JAXP");
231 Class<?> jaxpDoc = Class.forName("com.sun.xml.tree.XmlDocument");
232
233 // The method is in the XMLDocument class itself, not a helper
234 meth = jaxpDoc.getMethod("write",
235 new Class[]{Class.forName("java.io.OutputStream")});
236
237 meth.invoke(doc, new Object[]{baos});
238 } else if (domImpl.equals("org.apache.crimson.tree.XmlDocument")) {
239 System.out.println("Using Crimson");
240 Class<?> crimsonDoc = Class.forName("org.apache.crimson.tree.XmlDocument");
241 // The method is in the XMLDocument class itself, not a helper
242 meth = crimsonDoc.getMethod("write",
243 new Class[]{Class.forName("java.io.OutputStream")});
244
245 meth.invoke(doc, new Object[]{baos});
246 } else if (domImpl.equals("org.apache.xerces.dom.DocumentImpl")
247 || domImpl.equals("org.apache.xerces.dom.DeferredDocumentImpl")) {
248 System.out.println("Using Xerces");
249 // Try for Xerces
250 Class<?> xercesSer
251 = Class.forName("org.apache.xml.serialize.XMLSerializer");
252
253 // Get the OutputStream constructor
254 // May want to use the OutputFormat parameter at some stage too
255 con = xercesSer.getConstructor(new Class[]{Class.forName("java.io.OutputStream"),
256 Class.forName("org.apache.xml.serialize.OutputFormat")});
257
258 // Get the serialize method
259 meth = xercesSer.getMethod("serialize",
260 new Class[]{Class.forName("org.w3c.dom.Document")});
261
262 // Get an instance
263 Object serializer = con.newInstance(new Object[]{baos, null});
264
265 // Now call serialize to write the document
266 meth.invoke(serializer, new Object[]{doc});
267 } else if (domImpl.equals("gnu.xml.dom.DomDocument")) {
268 System.out.println("Using GNU");
269
270 Class<?> gnuSer = Class.forName("gnu.xml.dom.ls.DomLSSerializer");
271
272 // Get the serialize method
273 meth = gnuSer.getMethod("serialize",
274 new Class[]{Class.forName("org.w3c.dom.Node"),
275 Class.forName("java.io.OutputStream")});
276
277 // Get an instance
278 Object serializer = gnuSer.newInstance();
279
280 // Now call serialize to write the document
281 meth.invoke(serializer, new Object[]{doc, baos});
282 } else {
283 // We don't have another parser
284 try {
285 DOMSource domSource = new DOMSource(doc);
286 StringWriter writer = new StringWriter();
287 StreamResult result = new StreamResult(writer);
288 TransformerFactory tf = TransformerFactory.newInstance();
289 Transformer transformer = tf.newTransformer();
290 transformer.transform(domSource, result);
291 return writer.toString().getBytes();
292 } catch (Exception e) {
293 // We don't have another parser
294 IOException ex2 = new IOException("No appropriate API (JAXP/Xerces) to serialize XML document: " + domImpl);
295 ex2.initCause(e);
296 throw ex2;
297 }
298 }
299 }
300 catch (Exception e) {
301 // We may get some other errors, but the bottom line is that
302 // the steps being executed no longer work
303 IOException newEx = new IOException(e.getMessage());
304 newEx.initCause(e);
305 throw newEx;
306 }
307
308 byte bytes[] = baos.toByteArray();
309
310 return bytes;
311 }
312}
An implementation of Document for StarOffice documents.
void setContentDOM(Node newDom)
Sets the Content of the Document to the contents of the supplied Node list.
Document contentDoc
DOM Document of content.xml.
Document getContentDOM()
Return a DOM Document object of the document content file.
byte[] docToBytes(Document doc)
Write out a org.w3c.dom.Document object into a byte array.
void write(OutputStream os)
Write out content to the supplied OutputStream.
String getFileExtension()
Returns the file extension of the Document represented.
String getName()
Return the name of the Document.
void read(InputStream is)
Read the Office Document from the specified InputStream.
static DocumentBuilderFactory factory
Factory for DocumentBuilder objects.
DOMDocument(String name, String ext, boolean namespaceAware, boolean validating)
Constructor with arguments to set namespaceAware and validating flags.
String getFileName()
Return the file name of the Document, possibly with the standard extension.
String trimDocumentName(String name)
Removes the file extension from the Document name.
DOMDocument(String name, String ext)
Default constructor.
This class is used for logging debug messages.
Definition: Debug.java:39
static final int INFO
Informational messages.
Definition: Debug.java:42
static void log(int flag, String msg)
Log message based on the flag type.
Definition: Debug.java:205
const char * name
@ Exception
con
Provides general purpose utilities.
Provides interfaces for converting between two Document formats, and supports a "merge" interface for...
Definition: Convert.java:19
std::vector< sal_uInt8 > bytes
Any result