KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > graph > impl > FileGraph


1 /*
2   (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3   [See end of file]
4   $Id: FileGraph.java,v 1.26 2005/03/10 14:54:30 chris-dollin Exp $
5 */

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 /**
19     A FileGraph is a memory-based graph that is optionally read in from a file
20     when it is created, and is written back when it is closed. It supports
21     (weak) transactions by using checkpoint files.
22     
23     @author hedgehog
24 */

25 public class FileGraph extends GraphMem
26     {
27     /**
28         See FileGraph( f, create, strict, Reifier.ReificationStyle ).
29     */

30     public FileGraph( File f, boolean create, boolean strict )
31         { this( f, create, strict, ReificationStyle.Minimal ); }
32     
33     /**
34         The File-name of this graph, used to name it in the filing system
35     */

36     protected File name;
37     
38     /**
39         A model used to wrap the graph for the IO operations (since these are not
40         yet available at the graph level).
41     */

42     protected Model model;
43     
44     /**
45         The language used to read and write the graph, guessed from the filename's
46         suffix.
47     */

48     protected String JavaDoc lang;
49     
50     /**
51         Construct a new FileGraph who's name is given by the specified File,
52         If create is true, this is a new file, and any existing file will be destroyed;
53         if create is false, this is an existing file, and its current contents will
54         be loaded. The language code for the file is guessed from its suffix.
55         
56         @param f the File naming the associated file-system file
57         @param create true to create a new one, false to read an existing one
58         @param strict true to throw exceptions for create: existing, open: not found
59         @param style the reification style for the graph
60     */

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     /**
95         As for FileGraph(File,boolean), except the name is given as a String.
96      */

97     public FileGraph( String JavaDoc 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     /**
104         Answer true iff the filename string given is plausibly the name of a
105         graph, ie, may have RDF content. We appeal to FileUtils - if it can
106         guess an RDF language name, we deliver true, otherwise false.
107         
108         @param name the leaf component of a filename
109         @return true if it is likely to be an RDF file
110     */

111     public static boolean isPlausibleGraphName( String JavaDoc name )
112         { return FileUtils.guessLang( name, null ) != null; }
113         
114     /**
115         Write out and then close this FileGraph.
116     */

117     public void close()
118         {
119         saveContents( name );
120         super.close();
121         }
122     
123     /**
124        Delete the backing file. Primarily intended for test cleanup.
125     */

126     public void delete()
127         { name.delete(); }
128
129     /**
130         The graph is written out to the
131         named file in the language guessed from the suffix, and then the
132         parent close is invoked. The write-out goes to an intermediate file
133         first, which is then renamed to the correct name, to try and ensure
134         that the output is either done completely or not at all.
135     */

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 JavaDoc e)
147             { throw new JenaException( e ); }
148         }
149         
150     /**
151         The file intermediate has the new file contents. We want to move
152         them to the current file. renameTo doesn't have a powerful enough
153         semantics, so we anticipate failure and attempt to bypass it ...
154     <p>
155         If the rename works, that's fine. If it fails, we delete the old file if it
156         exists, and try again.
157     */

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 /*
184     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
185     All rights reserved.
186
187     Redistribution and use in source and binary forms, with or without
188     modification, are permitted provided that the following conditions
189     are met:
190
191     1. Redistributions of source code must retain the above copyright
192        notice, this list of conditions and the following disclaimer.
193
194     2. Redistributions in binary form must reproduce the above copyright
195        notice, this list of conditions and the following disclaimer in the
196        documentation and/or other materials provided with the distribution.
197
198     3. The name of the author may not be used to endorse or promote products
199        derived from this software without specific prior written permission.
200
201     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
202     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
203     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
204     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
205     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
206     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
207     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
208     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
209     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
210     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
211 */
Popular Tags