19package org.openoffice.xmerge.converter.dom;
21import java.io.InputStream;
22import java.io.OutputStream;
23import java.io.StringWriter;
24import java.io.ByteArrayOutputStream;
25import java.io.IOException;
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;
35import org.w3c.dom.Node;
36import org.w3c.dom.Document;
37import org.xml.sax.SAXException;
47 private static DocumentBuilderFactory
factory =
48 DocumentBuilderFactory.newInstance();
64 this(
name,ext,
true,
false);
86 private DOMDocument(String name, String ext,
boolean namespaceAware,
89 factory.setValidating(validating);
90 factory.setNamespaceAware(namespaceAware);
107 if (temp.endsWith(ext)) {
109 int nlen =
name.length();
110 int endIndex = nlen - ext.length();
170 public void read(InputStream is)
throws IOException {
173 DocumentBuilder builder =
factory.newDocumentBuilder();
175 }
catch (ParserConfigurationException
ex) {
177 }
catch (SAXException
ex) {
189 public void write(OutputStream os)
throws IOException {
194 os.write(contentBytes);
212 ByteArrayOutputStream baos =
new ByteArrayOutputStream();
214 java.lang.reflect.Constructor<?>
con;
215 java.lang.reflect.Method meth;
217 String domImpl = doc.getClass().getName();
219 System.err.println(
"type b " + domImpl);
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");
234 meth = jaxpDoc.getMethod(
"write",
235 new Class[]{
Class.forName(
"java.io.OutputStream")});
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");
242 meth = crimsonDoc.getMethod(
"write",
243 new Class[]{
Class.forName(
"java.io.OutputStream")});
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");
251 =
Class.forName(
"org.apache.xml.serialize.XMLSerializer");
255 con = xercesSer.getConstructor(
new Class[]{
Class.forName(
"java.io.OutputStream"),
256 Class.forName(
"org.apache.xml.serialize.OutputFormat")});
259 meth = xercesSer.getMethod(
"serialize",
260 new Class[]{
Class.forName(
"org.w3c.dom.Document")});
266 meth.invoke(serializer,
new Object[]{doc});
267 }
else if (domImpl.equals(
"gnu.xml.dom.DomDocument")) {
268 System.out.println(
"Using GNU");
270 Class<?> gnuSer =
Class.forName(
"gnu.xml.dom.ls.DomLSSerializer");
273 meth = gnuSer.getMethod(
"serialize",
274 new Class[]{
Class.forName(
"org.w3c.dom.Node"),
275 Class.forName(
"java.io.OutputStream")});
278 Object serializer = gnuSer.newInstance();
281 meth.invoke(serializer,
new Object[]{doc, baos});
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();
294 IOException ex2 =
new IOException(
"No appropriate API (JAXP/Xerces) to serialize XML document: " + domImpl);
303 IOException newEx =
new IOException(e.getMessage());
308 byte bytes[] = baos.toByteArray();
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.
static final int INFO
Informational messages.
static void log(int flag, String msg)
Log message based on the flag type.
Provides general purpose utilities.
Provides interfaces for converting between two Document formats, and supports a "merge" interface for...
std::vector< sal_uInt8 > bytes