1 package org.simantics.document.ui.graphfile;
3 import java.io.BufferedReader;
5 import java.io.FileInputStream;
6 import java.io.IOException;
7 import java.io.InputStreamReader;
8 import java.io.PrintStream;
9 import java.util.Collection;
10 import java.util.HashSet;
13 import org.eclipse.core.runtime.IProgressMonitor;
14 import org.simantics.Simantics;
15 import org.simantics.db.ReadGraph;
16 import org.simantics.db.Resource;
17 import org.simantics.db.WriteGraph;
18 import org.simantics.db.common.request.ReadRequest;
19 import org.simantics.db.common.request.WriteRequest;
20 import org.simantics.db.common.request.WriteResultRequest;
21 import org.simantics.db.exception.DatabaseException;
22 import org.simantics.document.DocumentResource;
23 import org.simantics.graphfile.ontology.GraphFileResource;
24 import org.simantics.graphfile.util.GraphFileUtil;
25 import org.simantics.layer0.Layer0;
26 import org.simantics.utils.ui.ExceptionUtils;
30 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
33 public class FileDocumentUtil {
36 * Imports file, sets its L0.hasName, and and adds it to a library
38 * Note: if library relation is L0.ConsistsOf, L0.HasName is set to next available unique name.
43 * @throws DatabaseException
45 public static Resource importFile(final String fileName, final Resource lib, final Resource rel) throws DatabaseException {
46 return Simantics.getSession().syncRequest(new WriteResultRequest<Resource>() {
48 public Resource perform(WriteGraph graph) throws DatabaseException {
49 return importFile(graph, fileName,lib,rel);
56 public static void importFileAsync(final String fileName, final Resource lib, final Resource rel) {
57 Simantics.getSession().asyncRequest(new WriteRequest() {
60 public void perform(WriteGraph graph) throws DatabaseException {
61 importFile(graph, fileName,lib,rel);
64 },new org.simantics.utils.datastructures.Callback<DatabaseException>() {
67 public void run(DatabaseException parameter) {
68 if (parameter != null)
69 ExceptionUtils.logAndShowError("Cannot import file " + fileName, parameter);
78 * Imports file, sets its L0.HasName, and and adds it to a library
80 * Note: if library relation is L0.ConsistsOf, L0.HasName is set to next available unique name.
86 * @throws DatabaseException
88 public static Resource importFile(WriteGraph graph, String fileName, Resource lib, Resource rel) throws DatabaseException{
89 Layer0 l0 = Layer0.getInstance(graph);
90 Resource fileResource = importFile(graph, fileName);
91 graph.claim(lib, rel, fileResource);
92 File file = new File(fileName);
93 String name = file.getName();
94 graph.claimLiteral(fileResource, l0.HasName, name);
95 setUniqueName(graph, fileResource, lib, rel);
99 public static Resource importFileWithName(WriteGraph graph, String fileName) throws DatabaseException{
100 Layer0 l0 = Layer0.getInstance(graph);
101 Resource fileResource = importFile(graph, fileName);
102 File file = new File(fileName);
103 String name = file.getName();
104 graph.claimLiteral(fileResource, l0.HasName, name);
109 * Imports folder of documents recursively (including all sub folders).
111 * @param folderName Name of imported folder
112 * @param lib Library, where imported folder is attached.
113 * @param folderType Type of folders
114 * @param relation Relation used to create file/folder hierarchy
115 * @return the imported folder
116 * @throws DatabaseException
118 public static Resource importFolderWithName(WriteGraph graph, String folderName, Resource lib, Resource folderType, Resource relation, IProgressMonitor monitor) throws Exception{
119 Resource folderRes = importFolderWithName(graph, folderName, folderType, relation,monitor);
120 graph.claim(lib, relation, folderRes);
121 FileDocumentUtil.createUniqueName(graph, folderRes);
126 * Imports folder of documents recursively (including all sub folders).
128 * @param folderName Name of imported folder
129 * @param folderType Type of folders
130 * @param relation Relation used to create file/folder hierarchy
131 * @param monitor ProgessMonitor or null
132 * @return the imported folder
133 * @throws DatabaseException
135 public static Resource importFolderWithName(WriteGraph graph, String folderName, Resource folderType, Resource relation, IProgressMonitor monitor) throws Exception{
136 Layer0 l0 = Layer0.getInstance(graph);
137 File folder = new File(folderName);
138 Resource rootFolderRes = graph.newResource();
139 graph.claim(rootFolderRes, l0.InstanceOf, folderType);
140 graph.claimLiteral(rootFolderRes, l0.HasName, folder.getName());
141 importFolder(graph, folder, rootFolderRes, relation, monitor);
142 return rootFolderRes;
146 * Imports folder of documents recursively (including all sub folders).
148 * @param folder Imported folder
149 * @param folderResource Resource folder matching file system folder
150 * @param relation Relation used to create file/folder hierarchy
151 * @throws DatabaseException
153 public static void importFolder(WriteGraph graph, File folder, Resource folderResource, Resource relation, IProgressMonitor monitor) throws Exception{
154 if (monitor != null) {
155 int count = _countFiles(folder);
156 monitor.beginTask("Import files", count);
158 _importFolder(graph, folder, folderResource, relation, monitor);
163 private static void _importFolder(WriteGraph graph, File folder, Resource folderResource, Resource relation, IProgressMonitor monitor) throws Exception{
164 Layer0 l0 = Layer0.getInstance(graph);
165 File files[] = folder.listFiles();
166 for (File f : files) {
167 if (f.isDirectory()) {
168 Resource newFolderRes = graph.newResource();
169 graph.claim(newFolderRes, l0.InstanceOf, graph.getSingleType(folderResource));
170 graph.claim(folderResource, relation, newFolderRes);
171 graph.claimLiteral(newFolderRes, l0.HasName, f.getName());
172 _importFolder(graph, f, newFolderRes, relation,monitor);
174 Resource fileRes = null;
177 fileRes = importURL(graph, f);
178 fileRes = importFileWithName(graph, f.getAbsolutePath());
180 graph.claim(folderResource, relation, fileRes);
187 private static int _countFiles(File folder) {
190 File files[] = folder.listFiles();
191 for (File f : files) {
192 if (f.isDirectory()) {
193 count += _countFiles(f);
202 public static void createUniqueName(WriteGraph graph, Resource document) throws DatabaseException {
203 Layer0 l0 = Layer0.getInstance(graph);
204 Resource lib = graph.getPossibleObject(document, l0.PartOf);
207 setUniqueName(graph, document, lib, l0.ConsistsOf);
210 public static void setUniqueName(WriteGraph graph, Resource res, Resource lib, Resource rel) throws DatabaseException{
211 Layer0 l0 = Layer0.getInstance(graph);
212 Set<String> names = new HashSet<String>();
213 for (Resource r : graph.getObjects(lib, rel)) {
216 names.add((String)graph.getRelatedValue(r, l0.HasName));
218 String name = graph.getRelatedValue(res, l0.HasName);
219 if (!names.contains(name))
223 String proposal = name +" (" + i +")";
224 if (!names.contains(proposal)) {
225 graph.claimLiteral(res, l0.HasName, proposal);
239 * @throws DatabaseException
241 public static Resource importFile(WriteGraph graph, String fileName) throws DatabaseException{
242 Layer0 l0 = Layer0.getInstance(graph);
243 DocumentResource doc = DocumentResource.getInstance(graph);
245 Resource fileResource = graph.newResource();
246 graph.claim(fileResource, l0.InstanceOf, doc.FileDocument);
248 GraphFileUtil.toGraph(graph,fileName, fileResource);
250 } catch (IOException e) {
251 throw new DatabaseException(e);
258 * Exports graph folder recursively to file system.
260 * @param folderResource
263 * @throws DatabaseException
265 public static void exportDocumentFolder(final Resource folderResource, final File folder, final Resource relation, boolean useResourceNames, final IProgressMonitor monitor) throws Exception{
266 Simantics.getSession().syncRequest(new ReadRequest() {
269 public void run(ReadGraph graph) throws DatabaseException {
271 exportDocumentFolder(graph, folderResource, folder, relation, useResourceNames, monitor);
272 } catch (Exception e) {
273 throw new DatabaseException(e);
282 * Exports graph folder recursively to file system.
284 * @param folderResource
287 * @throws DatabaseException
289 public static void exportDocumentFolder(ReadGraph graph, Resource folderResource, File folder, Resource relation, boolean useResourceNames, IProgressMonitor monitor) throws Exception{
290 Layer0 l0 = Layer0.getInstance(graph);
291 DocumentResource doc = DocumentResource.getInstance(graph);
292 GraphFileResource gf = GraphFileResource.getInstance(graph);
293 Set<String> names = new HashSet<String>();
294 Collection<Resource> folderType = graph.getPrincipalTypes(folderResource);
295 for (Resource r : graph.getObjects(folderResource, relation)) {
296 if (graph.isInstanceOf(r, doc.Document)) {
298 boolean canExport = false;
299 if (graph.isInstanceOf(r, doc.FileDocument)) {
300 name = graph.getRelatedValue(r, useResourceNames ? gf.HasResourceName : l0.HasName);
302 } else if (graph.isInstanceOf(r, doc.UrlDocument)) {
303 name = graph.getRelatedValue(r, l0.HasName) +".url";
307 name = resolveName(folder, name, names, true);
308 File file = new File(folder.getAbsolutePath()+"/"+name);
309 if (graph.isInstanceOf(r, doc.FileDocument)) {
310 GraphFileUtil.writeDataToFile(graph,r, file);
311 } else if (graph.isInstanceOf(r, doc.UrlDocument)) {
312 String url = graph.getRelatedValue(r, doc.HasUrl);
313 String n = graph.getRelatedValue(r, l0.HasName);
314 exportUrl(file, n, url);
321 Collection<Resource> type = graph.getPrincipalTypes(r);
322 if (type.size() == folderType.size() && folderType.containsAll(type)) {
323 String name = graph.getRelatedValue(r, l0.HasName);
324 name = resolveName(folder, name, names, false);
325 File subFolder = new File(folder.getAbsolutePath()+"/"+name);
326 if (!subFolder.exists()) {
327 if (!subFolder.mkdir()) {
332 exportDocumentFolder(graph, r, subFolder, relation, useResourceNames, monitor);
339 * Print URL to a file (Windows specific format?)
342 * @throws DatabaseException
344 private static void exportUrl(File toFile, String name, String url) throws Exception{
345 PrintStream os = new PrintStream(toFile,"UTF-8");
346 os.println("[InternetShortcut]");
347 os.println("URL="+url);
348 os.println("name="+name);
353 public static Resource importURL(WriteGraph graph, File file) throws Exception{
357 BufferedReader is = null;
359 is = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
360 while ((s = is.readLine()) != null) {
361 if (s.startsWith("URL=")) {
362 url = s.substring(4);
363 } else if (s.startsWith("name=")) {
364 name = s.substring(5);
375 Layer0 l0 = Layer0.getInstance(graph);
376 DocumentResource doc = DocumentResource.getInstance(graph);
378 Resource fileResource = graph.newResource();
379 graph.claim(fileResource, l0.InstanceOf, doc.UrlDocument);
381 name = file.getName();
382 name = unescape(name);
383 name = name.substring(0,name.length()-4);
385 graph.claimLiteral(fileResource, l0.HasName, name);
386 graph.claimLiteral(fileResource, doc.HasUrl, url);
390 private static boolean isUrl(File file) throws Exception{
391 return (file.getAbsolutePath().endsWith(".url"));
394 private static final char ESCAPE = '%';
396 private static String escape(String s) {
398 int len = s.length();
399 StringBuilder sb = new StringBuilder(len);
400 for (int i = 0; i < len; i++) {
401 char ch = s.charAt(i);
402 if (ch < ' ' || ch >= 0x7F || ch == '/' || ch == '\\' || ch == ':' || ch == ESCAPE) {
407 sb.append(Integer.toHexString(ch));
412 return sb.toString();
415 private static String unescape(String s) {
416 int len = s.length();
417 StringBuilder sb = new StringBuilder(len);
418 for (int i = 0; i < len; i++) {
419 char ch = s.charAt(i);
422 num += s.charAt(++i);
423 num += s.charAt(++i);
424 ch = (char)Integer.decode(num).intValue();
428 return sb.toString();
432 private static String resolveName(File parentFolder, String proposal, Set<String> used, boolean file) {
433 String current = escape(proposal);
438 if (used.contains(current)) {
439 current = createFileName(proposal, i);
441 File subFile = new File(parentFolder.getAbsolutePath()+"/"+current);
442 if (!subFile.exists())
444 if (subFile.exists() && subFile.isFile() && subFile.canWrite()) {
452 if (used.contains(current)) {
453 current = proposal+i;
455 File subFolder = new File(parentFolder.getAbsolutePath()+"/"+current);
456 if (!subFolder.exists())
458 if (subFolder.exists() && subFolder.isDirectory()) {
468 private static String createFileName(String original, int i) {
469 int extIndex = original.lastIndexOf(".");
472 return original.substring(0,extIndex) + i + original.substring(extIndex);