01: /*
02: (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
03: All rights reserved - see end of file.
04: $Id: FileModelAssembler.java,v 1.9 2008/01/03 15:19:05 chris-dollin Exp $
05: */
06:
07: package com.hp.hpl.jena.assembler.assemblers;
08:
09: import java.io.File;
10:
11: import com.hp.hpl.jena.assembler.*;
12: import com.hp.hpl.jena.graph.Graph;
13: import com.hp.hpl.jena.graph.impl.*;
14: import com.hp.hpl.jena.graph.impl.FileGraph.NotifyOnClose;
15: import com.hp.hpl.jena.rdf.model.*;
16: import com.hp.hpl.jena.shared.*;
17: import com.hp.hpl.jena.util.FileUtils;
18:
19: public class FileModelAssembler extends NamedModelAssembler implements
20: Assembler {
21: protected Model openEmptyModel(Assembler a, Resource root, Mode mode) {
22: checkType(root, JA.FileModel);
23: File fullName = getFileName(root);
24: boolean mayCreate = mode.permitCreateNew(root, fullName
25: .toString());
26: boolean mayReuse = mode.permitUseExisting(root, fullName
27: .toString());
28: boolean create = mayCreate;
29: boolean strict = mayCreate != mayReuse;
30: String lang = getLanguage(root, fullName);
31: ReificationStyle style = getReificationStyle(root);
32: return createFileModel(fullName, lang, create, strict, style);
33: }
34:
35: public Model createFileModel(File fullName, String lang,
36: boolean create, boolean strict, ReificationStyle style) {
37: NotifyOnClose notify = NotifyOnClose.ignore;
38: Graph fileGraph = new FileGraph(notify, fullName, lang, create,
39: strict, style);
40: return ModelFactory.createModelForGraph(fileGraph);
41: }
42:
43: protected String getLanguage(Resource root, File fullName) {
44: Statement s = getUniqueStatement(root, JA.fileEncoding);
45: return s == null ? FileUtils.guessLang(fullName.toString())
46: : getString(s);
47: }
48:
49: protected File getFileName(Resource root) {
50: String name = getModelName(root);
51: boolean mapName = getBoolean(root, JA.mapName, false);
52: String dir = getDirectoryName(root);
53: return new File(dir, (mapName ? FileGraphMaker.toFilename(name)
54: : name));
55: }
56:
57: private boolean getBoolean(Resource root, Property p,
58: boolean ifAbsent) {
59: Resource r = getUniqueResource(root, p);
60: return r == null ? ifAbsent : r.equals(JA.True);
61: }
62:
63: private String getDirectoryName(Resource root) {
64: return getRequiredResource(root, JA.directory).getURI()
65: .replaceFirst("file:", "");
66: }
67: }
68:
69: /*
70: * (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
71: * All rights reserved.
72: *
73: * Redistribution and use in source and binary forms, with or without
74: * modification, are permitted provided that the following conditions
75: * are met:
76: * 1. Redistributions of source code must retain the above copyright
77: * notice, this list of conditions and the following disclaimer.
78: * 2. Redistributions in binary form must reproduce the above copyright
79: * notice, this list of conditions and the following disclaimer in the
80: * documentation and/or other materials provided with the distribution.
81: * 3. The name of the author may not be used to endorse or promote products
82: * derived from this software without specific prior written permission.
83: *
84: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
85: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
86: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
87: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
88: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
89: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
90: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
91: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
92: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
93: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
94: */
|