KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > internal > dynamic > DocumentWriter


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.help.internal.dynamic;
12
13 import java.io.ByteArrayOutputStream JavaDoc;
14 import java.io.UnsupportedEncodingException JavaDoc;
15 import java.util.Properties JavaDoc;
16
17 import javax.xml.transform.OutputKeys JavaDoc;
18 import javax.xml.transform.Transformer JavaDoc;
19 import javax.xml.transform.TransformerConfigurationException JavaDoc;
20 import javax.xml.transform.TransformerException JavaDoc;
21 import javax.xml.transform.TransformerFactory JavaDoc;
22 import javax.xml.transform.dom.DOMSource JavaDoc;
23 import javax.xml.transform.stream.StreamResult JavaDoc;
24
25 import org.eclipse.help.internal.UAElement;
26 import org.w3c.dom.Document JavaDoc;
27 import org.w3c.dom.DocumentType JavaDoc;
28 import org.w3c.dom.Element JavaDoc;
29
30 public class DocumentWriter {
31
32     private Transformer JavaDoc transformer;
33
34     public String JavaDoc writeString(UAElement element, boolean xmlDecl) throws TransformerException JavaDoc, TransformerConfigurationException JavaDoc {
35         return writeString(element.element, xmlDecl);
36     }
37     
38     public String JavaDoc writeString(Element element, boolean xmlDecl) throws TransformerException JavaDoc, TransformerConfigurationException JavaDoc {
39         byte[] bytes = writeBytes(element, xmlDecl);
40         String JavaDoc encoding = transformer.getOutputProperty(OutputKeys.ENCODING);
41         if (encoding == null) {
42             encoding = "UTF-8"; //$NON-NLS-1$
43
}
44         try {
45             return new String JavaDoc(bytes, encoding);
46         }
47         catch (UnsupportedEncodingException JavaDoc e) {
48             return new String JavaDoc(bytes);
49         }
50     }
51     
52     public byte[] writeBytes(UAElement element, boolean xmlDecl) throws TransformerException JavaDoc, TransformerConfigurationException JavaDoc {
53         return writeBytes(element.element, xmlDecl);
54     }
55
56     public byte[] writeBytes(Element element, boolean xmlDecl) throws TransformerException JavaDoc, TransformerConfigurationException JavaDoc {
57         Document JavaDoc document = element.getOwnerDocument();
58         if (transformer == null) {
59             TransformerFactory JavaDoc factory = TransformerFactory.newInstance();
60             transformer = factory.newTransformer();
61             transformer.setOutputProperty(OutputKeys.METHOD, "xml"); //$NON-NLS-1$
62
}
63         DocumentType JavaDoc docType = document.getDoctype();
64         Properties JavaDoc props = transformer.getOutputProperties();
65         if (docType != null) {
66             props.setProperty(OutputKeys.DOCTYPE_PUBLIC, docType.getPublicId());
67             props.setProperty(OutputKeys.DOCTYPE_SYSTEM, docType.getSystemId());
68         }
69         else {
70             props.remove(OutputKeys.DOCTYPE_PUBLIC);
71             props.remove(OutputKeys.DOCTYPE_SYSTEM);
72         }
73         props.setProperty(OutputKeys.OMIT_XML_DECLARATION, xmlDecl ? "no" : "yes"); //$NON-NLS-1$ //$NON-NLS-2$
74
transformer.setOutputProperties(props);
75         
76         DOMSource JavaDoc source = new DOMSource JavaDoc(element);
77         ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
78         StreamResult JavaDoc result = new StreamResult JavaDoc(out);
79         transformer.transform(source, result);
80         return out.toByteArray();
81     }
82 }
83
Popular Tags