1 6 7 package com.hp.hpl.jena.graph.impl; 8 9 import com.hp.hpl.jena.util.FileUtils; 10 import com.hp.hpl.jena.graph.TransactionHandler; 11 import com.hp.hpl.jena.mem.GraphMem; 12 import com.hp.hpl.jena.rdf.model.Model; 13 import com.hp.hpl.jena.rdf.model.impl.ModelCom; 14 import com.hp.hpl.jena.shared.*; 15 16 import java.io.*; 17 18 25 public class FileGraph extends GraphMem 26 { 27 30 public FileGraph( File f, boolean create, boolean strict ) 31 { this( f, create, strict, ReificationStyle.Minimal ); } 32 33 36 protected File name; 37 38 42 protected Model model; 43 44 48 protected String lang; 49 50 61 public FileGraph( File f, boolean create, boolean strict, ReificationStyle style ) 62 { 63 super( style ); 64 this.name = f; 65 this.model = new ModelCom( this ); 66 this.lang = FileUtils.guessLang( this.name.toString() ); 67 if (create) 68 { 69 if (f.exists() && strict) throw new AlreadyExistsException( f.toString() ); 70 } 71 else 72 readModel( this.model, strict ); 73 } 74 75 protected void readModel( Model m, boolean strict ) 76 { readModelFrom( m, strict, name ); } 77 78 protected void readModelFrom( Model m, boolean strict, File name ) 79 { 80 FileInputStream in = null; 81 try 82 { 83 in = new FileInputStream( name ); 84 model.read( in, "", this.lang ); 85 } 86 catch (FileNotFoundException f) 87 { if (strict) throw new DoesNotExistException( name.toString() ); } 88 finally 89 { 90 if (in != null) try {in.close();} catch (IOException ignore) {} 91 } 92 } 93 94 97 public FileGraph( String s, boolean create ) 98 { this( new File( s ), create, true ); } 99 100 public static FileGraph create() 101 { return new FileGraph( FileUtils.tempFileName( "xxx", ".rdf" ), true, true ); } 102 103 111 public static boolean isPlausibleGraphName( String name ) 112 { return FileUtils.guessLang( name, null ) != null; } 113 114 117 public void close() 118 { 119 saveContents( name ); 120 super.close(); 121 } 122 123 126 public void delete() 127 { name.delete(); } 128 129 136 protected void saveContents( File targetName ) 137 { 138 try 139 { 140 File intermediate = new File( targetName.getPath() + ".new" ); 141 FileOutputStream out = new FileOutputStream( intermediate ); 142 model.write( out, lang ); 143 out.close(); 144 updateFrom( targetName, intermediate ); 145 } 146 catch (Exception e) 147 { throw new JenaException( e ); } 148 } 149 150 158 protected void updateFrom( File targetName, File intermediate ) 159 { 160 if (intermediate.renameTo( targetName ) == false) 161 { 162 if (targetName.exists()) mustDelete( targetName ); 163 mustRename( intermediate, targetName ); 164 } 165 } 166 167 protected void mustDelete( File f ) 168 { if (f.delete() == false) throw new JenaException( "could not delete " + f ); } 169 170 protected void mustRename( File from, File to ) 171 { 172 if (from.renameTo( to ) == false) 173 throw new JenaException( "could not rename " + from + " to " + to ); 174 } 175 176 public TransactionHandler getTransactionHandler() 177 { if (th == null) th = new FileGraphTransactionHandler( this ); 178 return th; } 179 180 protected TransactionHandler th; 181 } 182 183 | Popular Tags |