KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > data > JahiaDOMObject


1 //
2
// ____.
3
// __/\ ______| |__/\. _______
4
// __ .____| | \ | +----+ \
5
// _______| /--| | | - \ _ | : - \_________
6
// \\______: :---| : : | : | \________>
7
// |__\---\_____________:______: :____|____:_____\
8
// /_____|
9
//
10
// . . . i n j a h i a w e t r u s t . . .
11
//
12
//
13
//
14
// JahiaDOMObject
15
//
16
// NK 25.07.2001
17
//
18

19 package org.jahia.data;
20
21 import java.io.FileOutputStream JavaDoc;
22
23 import javax.xml.transform.OutputKeys JavaDoc;
24 import javax.xml.transform.Transformer JavaDoc;
25 import javax.xml.transform.TransformerFactory JavaDoc;
26 import javax.xml.transform.dom.DOMSource JavaDoc;
27 import javax.xml.transform.stream.StreamResult JavaDoc;
28
29 import org.jahia.exceptions.JahiaException;
30 import org.w3c.dom.Document JavaDoc;
31 import org.w3c.dom.Element JavaDoc;
32
33
34 /**
35  * A DOM Object typically contains a DOM Document
36  *
37  * @author Khue Nguyen
38  * @version 1.0
39  */

40 public abstract class JahiaDOMObject {
41
42
43     protected Document JavaDoc xmlDoc = null;
44
45
46     //--------------------------------------------------------------------------
47
/**
48      * save the document to a file
49      *
50      * @param String destFileName
51      */

52     public void save (String JavaDoc destFileName) throws JahiaException{
53         saveFile(destFileName);
54     }
55
56     //--------------------------------------------------------------------------
57
/**
58      * return a reference to the DOM Document
59      *
60      * @return Document the DOM Document
61      */

62     public Document JavaDoc getDocument () {
63         return xmlDoc;
64     }
65
66     //--------------------------------------------------------------------------
67
/**
68      * return a DOM as string
69      *
70      * @return Document the DOM Document
71      */

72     public String JavaDoc toString () {
73         Element JavaDoc el = (Element JavaDoc)xmlDoc.getDocumentElement();
74         if ( el != null ){
75             return el.toString();
76         }
77         return null;
78     }
79
80     //--------------------------------------------------------------------------
81
private void saveFile(String JavaDoc destinationFileName)
82     throws JahiaException {
83
84         try {
85
86             xmlDoc.normalize(); // cleanup DOM tree a little
87

88             TransformerFactory JavaDoc tfactory = TransformerFactory.newInstance();
89
90             // This creates a transformer that does a simple identity transform,
91
// and thus can be used for all intents and purposes as a serializer.
92
Transformer JavaDoc serializer = tfactory.newTransformer();
93
94             serializer.setOutputProperty(OutputKeys.METHOD, "xml");
95             serializer.setOutputProperty(OutputKeys.INDENT, "yes");
96             FileOutputStream JavaDoc fileStream = new FileOutputStream JavaDoc(destinationFileName);
97             serializer.transform(new DOMSource JavaDoc(xmlDoc),
98                              new StreamResult JavaDoc(fileStream));
99
100         } catch ( Throwable JavaDoc t ){
101             throw new JahiaException( "XMLPortlets",
102                                         "Exception " + t.getMessage(),
103                                         JahiaException.ERROR_SEVERITY,
104                                         JahiaException.SERVICE_ERROR);
105         }
106
107     }
108
109 }
110
Popular Tags