KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > store > AbstractXmlOrderArchive


1 /*
2  * Created on Jan 17, 2005
3  */

4 package com.openedit.store;
5
6 import java.io.File JavaDoc;
7 import java.io.FileInputStream JavaDoc;
8 import java.io.FileOutputStream JavaDoc;
9 import java.io.IOException JavaDoc;
10
11 import org.dom4j.Document;
12 import org.dom4j.DocumentHelper;
13 import org.dom4j.Element;
14 import org.dom4j.io.OutputFormat;
15 import org.dom4j.io.SAXReader;
16 import org.dom4j.io.XMLWriter;
17
18 import com.openedit.util.FileUtils;
19
20 /**
21  * @author dbrown
22  *
23  */

24 public abstract class AbstractXmlOrderArchive extends BaseOrderArchive
25 {
26
27     protected void writeXmlFile( Element inRoot, File JavaDoc inFile ) throws StoreException
28     {
29         // write to a temporary file first, in case there's an error
30
File JavaDoc tempFile = new File JavaDoc( inFile.getParentFile(), inFile.getName() + ".temp" );
31
32         inFile.getParentFile().mkdirs();
33         FileOutputStream JavaDoc fos = null;
34         try
35         {
36             fos = new FileOutputStream JavaDoc( tempFile );
37             XMLWriter writer = new XMLWriter( fos, OutputFormat.createPrettyPrint() );
38             writer.write( inRoot );
39         }
40         catch( IOException JavaDoc e )
41         {
42             throw new StoreException( e );
43         }
44         finally
45         {
46             FileUtils.safeClose( fos );
47         }
48
49         inFile.delete();
50         if ( !tempFile.renameTo(inFile) )
51         {
52             throw new StoreException( "Unable to rename " + tempFile + " to " + inFile );
53         }
54     }
55
56     protected Element getRootElement( File JavaDoc inFile, String JavaDoc inElementName ) throws StoreException
57     {
58         if ( inFile.exists() )
59         {
60             SAXReader reader = new SAXReader();
61             Document document;
62             FileInputStream JavaDoc stream = null;
63             try
64             {
65                 stream = new FileInputStream JavaDoc(inFile);
66                 document = reader.read(stream);
67                 return document.getRootElement();
68             }
69             catch( Exception JavaDoc e )
70             {
71                 throw new StoreException( e );
72             }
73             finally
74             {
75                 if ( stream != null )
76                 {
77                     try
78                     {
79                         stream.close();
80                     }
81                     catch ( IOException JavaDoc e )
82                     {
83                         throw new StoreException( e );
84                     }
85                 }
86             }
87         }
88         else
89         {
90             return DocumentHelper.createElement( inElementName );
91         }
92     }
93
94     protected File JavaDoc getOrdersDirectory( Store inStore )
95     {
96         return new File JavaDoc( inStore.getStoreDirectory(), "orders" );
97     }
98
99
100 }
101
Popular Tags