KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > data > xml > JahiaXmlDocument


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
// JahiaXmlDocument
15
//
16
// NK 29.01.2001
17
//
18
//
19

20 package org.jahia.data.xml;
21
22
23 import java.io.FileInputStream JavaDoc;
24 import java.io.FileNotFoundException JavaDoc;
25 import java.io.FileOutputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27
28 import javax.xml.parsers.DocumentBuilder JavaDoc;
29 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
30 import javax.xml.parsers.ParserConfigurationException JavaDoc;
31 import javax.xml.transform.OutputKeys JavaDoc;
32 import javax.xml.transform.Transformer JavaDoc;
33 import javax.xml.transform.TransformerConfigurationException JavaDoc;
34 import javax.xml.transform.TransformerException JavaDoc;
35 import javax.xml.transform.TransformerFactory JavaDoc;
36 import javax.xml.transform.dom.DOMSource JavaDoc;
37 import javax.xml.transform.stream.StreamResult JavaDoc;
38
39 import org.jahia.exceptions.JahiaException;
40 import org.jahia.registries.ServicesRegistry;
41 import org.jahia.utils.JahiaConsole;
42 import org.w3c.dom.Document JavaDoc;
43 import org.xml.sax.EntityResolver JavaDoc;
44 import org.xml.sax.SAXException JavaDoc;
45
46
47 /**
48  * An abstract class to handle Xml documents
49  *
50  * @author Khue ng
51  * @version 1.0
52  */

53 public abstract class JahiaXmlDocument {
54
55     /** The xml Document **/
56     //protected XmlDocument m_XMLDocument;
57
protected Document JavaDoc m_XMLDocument;
58     /** The Full Path to the xml file **/
59     protected String JavaDoc m_DocPath;
60
61
62     private static final String JavaDoc CANT_READ_FILE_MSG = "Can't read XML file";
63     private static final String JavaDoc ERROR_READING_FILE_MSG = "Error reading file";
64     private static final String JavaDoc PARAMETER_TAG = "parameter";
65     private static final String JavaDoc PARAMETER_TAG_NAME_ATTRIBUTE = "name";
66
67
68     /**
69      * Handle xml document using default parser behavior
70      *
71      * @param (String) path, the full path to a xml file
72      */

73     public JahiaXmlDocument (String JavaDoc docPath) throws JahiaException
74     {
75         m_DocPath = docPath;
76         //File doc = new File(m_DocPath);
77

78         try {
79                 loadFile(m_DocPath);
80         } catch ( Throwable JavaDoc t ){
81               throw new JahiaException( "JahiaXmlDocument",
82                                             "Exception while loading to the file" + m_DocPath + t.getMessage(),
83                                             JahiaException.ERROR_SEVERITY,
84                                             JahiaException.SERVICE_ERROR, t);
85         }
86     }
87
88
89     /**
90      * Handle xml document using a gived parser
91      *
92      * @param (String) path, the full path to a xml file
93      * @param (Parser) parser, the parser to use
94      */

95     public JahiaXmlDocument (String JavaDoc docPath, org.xml.sax.helpers.ParserAdapter JavaDoc parser)
96
97     //com.sun.xml.parser.Parser parser)
98
throws JahiaException {
99
100
101         m_DocPath = docPath;
102         //File doc = new File(m_DocPath);
103

104         try {
105                 loadFile(m_DocPath);
106         } catch ( Throwable JavaDoc t ){
107               throw new JahiaException( "JahiaXmlDocument",
108                                             "Exception while loading to the file" + m_DocPath + t.getMessage(),
109                                             JahiaException.ERROR_SEVERITY,
110                                             JahiaException.SERVICE_ERROR, t);
111         }
112     }
113
114
115    /**
116     * To be override method. Codes to extract data from xml document.
117     */

118    public abstract void extractDocumentData() throws JahiaException ;
119
120
121
122    /**
123     * Write Document to the xml text file
124     *
125     */

126    public void write() throws JahiaException {
127
128         try {
129                 saveFile(m_DocPath);
130         } catch ( Throwable JavaDoc t ){
131               throw new JahiaException( "JahiaXmlDocument",
132                                             "Exception while writing to the file" + m_DocPath + t.getMessage(),
133                                             JahiaException.ERROR_SEVERITY,
134                                             JahiaException.SERVICE_ERROR);
135         }
136
137    }
138
139
140
141
142     private void loadFile(String JavaDoc sourceFileName)
143     throws ParserConfigurationException JavaDoc, IOException JavaDoc, SAXException JavaDoc {
144         DocumentBuilderFactory JavaDoc dfactory = DocumentBuilderFactory.newInstance();
145         //dfactory.setValidating(true); // create only parsers that are validating
146

147         EntityResolver JavaDoc et = null;
148         try {
149             et = ServicesRegistry.getInstance().getJahiaWebAppsDeployerService().getDtdEntityResolver();
150         } catch ( Throwable JavaDoc t ){
151             JahiaConsole.println("JahiaXmlDocument.loadFile","Entityresolver is null");
152         }
153         DocumentBuilder JavaDoc docBuilder = dfactory.newDocumentBuilder();
154         if ( et != null ){
155             docBuilder.setEntityResolver(et);
156         }
157         //docBuilder.setEntityResolver();
158
FileInputStream JavaDoc sourceStream = new FileInputStream JavaDoc(sourceFileName);
159         m_XMLDocument = docBuilder.parse(sourceStream);
160         m_XMLDocument.normalize(); // clean up DOM tree a little
161

162         //JahiaConsole.println("JahiaXmlDocument.loadFile",sourceFileName + " loaded successfully");
163
}
164
165
166     private void saveFile(String JavaDoc destinationFileName)
167     throws TransformerConfigurationException JavaDoc, FileNotFoundException JavaDoc,
168            TransformerException JavaDoc
169     {
170
171             m_XMLDocument.normalize(); // cleanup DOM tree a little
172

173             TransformerFactory JavaDoc tfactory = TransformerFactory.newInstance();
174
175             // This creates a transformer that does a simple identity transform,
176
// and thus can be used for all intents and purposes as a serializer.
177
Transformer JavaDoc serializer = tfactory.newTransformer();
178
179             serializer.setOutputProperty(OutputKeys.METHOD, "xml");
180             serializer.setOutputProperty(OutputKeys.INDENT, "yes");
181             FileOutputStream JavaDoc fileStream = new FileOutputStream JavaDoc(destinationFileName);
182
183             serializer.transform(new DOMSource JavaDoc(m_XMLDocument),
184                                  new StreamResult JavaDoc(fileStream));
185
186         try {
187             fileStream.flush();
188             fileStream.close();
189             fileStream = null;
190         } catch ( IOException JavaDoc ioe ) {
191         }
192     }
193
194
195
196
197
198
199
200
201
202
203
204 } // end JahiaXmlDocument
205
Popular Tags