1 6 7 package com.hp.hpl.jena.graph.impl; 8 9 import com.hp.hpl.jena.graph.*; 10 11 import java.io.*; 12 import java.util.*; 13 14 import com.hp.hpl.jena.shared.*; 15 import com.hp.hpl.jena.util.CollectionFactory; 16 import com.hp.hpl.jena.util.iterator.*; 17 import com.hp.hpl.jena.vocabulary.*; 18 19 27 public class FileGraphMaker extends BaseGraphMaker 28 { 29 private String fileBase; 30 private boolean deleteOnClose; 31 private Map created = CollectionFactory.createHashedMap(); 32 33 39 public FileGraphMaker( String root ) 40 { this( root, ReificationStyle.Minimal ); } 41 42 49 public FileGraphMaker( String root, ReificationStyle style ) 50 { this( root, style, false ); } 51 52 61 public FileGraphMaker( String root, ReificationStyle style, boolean deleteOnClose ) 62 { 63 super( style ); 64 this.fileBase = root; 65 this.deleteOnClose = deleteOnClose; 66 } 67 68 72 public Node getMakerClass() 73 { return JenaModelSpec.FileMakerSpec.asNode(); } 74 75 79 public String getFileBase() 80 { return fileBase; } 81 82 protected void augmentDescription( Graph g, Node self ) 83 { g.add( Triple.create( self, JenaModelSpec.fileBase.asNode(), Node.createLiteral( fileBase, "", false ) ) ); } 84 85 89 public Graph createGraph() 90 { return FileGraph.create(); } 91 92 public Graph createGraph( String name, boolean strict ) 93 { 94 File f = withRoot( name ); 95 FileGraph already = (FileGraph) created.get( f ); 96 if (already == null) 97 return remember( f, new FileGraph( f, true, strict, style ) ); 98 else 99 { 100 if (strict) throw new AlreadyExistsException( name ); 101 else return already.openAgain(); 102 } 103 } 104 105 public Graph openGraph( String name, boolean strict ) 106 { 107 File f = withRoot( name ); 108 return created.containsKey( f ) 109 ? ((FileGraph) created.get( f )).openAgain() 110 : remember( f, new FileGraph( f, false, strict, style ) ) 111 ; 112 } 113 114 private File withRoot( String name ) 115 { return new File( fileBase, toFilename( name ) ); } 116 117 125 public static String toFilename( String name ) 126 { 127 return replaceBy( name, "_/:", "USC" ); 128 } 135 136 private static String replaceBy( String x, String from, String to ) 137 { 138 int len = x.length(); 139 StringBuffer result = new StringBuffer ( len + 10 ); 140 for (int i = 0; i < len; i += 1) 141 { 142 char ch = x.charAt( i ); 143 int where = from.indexOf( ch ); 144 if (where < 0) result.append( ch ); 145 else result.append( '_' ).append( to.charAt( where ) ); 146 } 147 return result.toString(); 148 } 149 150 157 public static String toGraphname( String fileName ) 158 { 159 StringBuffer result = new StringBuffer ( fileName.length() ); 160 int here = 0; 161 while (true) 162 { 163 int ubar = fileName.indexOf( '_', here ); 164 if (ubar < 0) break; 165 result.append( fileName.substring( here, ubar ) ); 166 char ch = fileName.charAt( ubar + 1 ); 167 if (ch == 'S') result.append( '/' ); 168 else if (ch == 'U') result.append( '_' ); 169 else if (ch == 'C') result.append( ':' ); 170 here = ubar + 2; 171 } 172 result.append( fileName.substring( here ) ); 173 return result.toString(); 174 } 180 181 public void removeGraph( String name ) 182 { forget( withRoot( name ) ).delete(); } 183 184 private FileGraph remember( File f, FileGraph g ) 185 { 186 created.put( f, g ); 187 return g; 188 } 189 190 private File forget( File f ) 191 { 192 created.remove( f ); 193 return f; 194 } 195 196 public boolean hasGraph( String name ) 197 { 198 File f = withRoot( name ); 199 return created.containsKey( f ) || f.exists(); 200 } 201 202 public void close() 203 { 204 if (deleteOnClose) 205 { 206 Iterator it = created.keySet().iterator(); 207 while (it.hasNext()) ((File) it.next()).delete(); 208 } 209 } 210 211 214 private static Map1 unconvert = new Map1() 215 { public Object map1( Object x ) 216 { return toGraphname( (String ) x ); } 217 }; 218 219 225 public static FilenameFilter graphName() 226 { return new FilenameFilter() 227 { 228 public boolean accept( File file, String name ) 229 { return !new File( file, name ).isDirectory() 230 && FileGraph.isPlausibleGraphName( name ); } 231 }; } 232 233 240 public ExtendedIterator listGraphs() 241 { String [] fileNames = new File( fileBase ).list( graphName() ); 242 Set allNames = CollectionFactory.createHashedSet( Arrays.asList( fileNames ) ); 243 Iterator it = created.keySet().iterator(); 244 while (it.hasNext()) allNames.add( ((File) it.next()).getName() ); 245 return WrappedIterator.create( allNames.iterator() ) .mapWith( unconvert ); } 246 } 247 248 | Popular Tags |