1 4 package com.openedit.store; 5 6 import java.io.File ; 7 import java.io.FileInputStream ; 8 import java.io.FileOutputStream ; 9 import java.io.IOException ; 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 24 public abstract class AbstractXmlOrderArchive extends BaseOrderArchive 25 { 26 27 protected void writeXmlFile( Element inRoot, File inFile ) throws StoreException 28 { 29 File tempFile = new File ( inFile.getParentFile(), inFile.getName() + ".temp" ); 31 32 inFile.getParentFile().mkdirs(); 33 FileOutputStream fos = null; 34 try 35 { 36 fos = new FileOutputStream ( tempFile ); 37 XMLWriter writer = new XMLWriter( fos, OutputFormat.createPrettyPrint() ); 38 writer.write( inRoot ); 39 } 40 catch( IOException 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 inFile, String inElementName ) throws StoreException 57 { 58 if ( inFile.exists() ) 59 { 60 SAXReader reader = new SAXReader(); 61 Document document; 62 FileInputStream stream = null; 63 try 64 { 65 stream = new FileInputStream (inFile); 66 document = reader.read(stream); 67 return document.getRootElement(); 68 } 69 catch( Exception 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 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 getOrdersDirectory( Store inStore ) 95 { 96 return new File ( inStore.getStoreDirectory(), "orders" ); 97 } 98 99 100 } 101 | Popular Tags |