1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.document;
\r
14 import java.io.IOException;
\r
15 import java.math.BigInteger;
\r
16 import java.nio.file.Files;
\r
17 import java.nio.file.Path;
\r
18 import java.security.MessageDigest;
\r
19 import java.security.NoSuchAlgorithmException;
\r
20 import java.util.regex.Matcher;
\r
21 import java.util.regex.Pattern;
\r
23 import org.simantics.Simantics;
\r
24 import org.simantics.databoard.Bindings;
\r
25 import org.simantics.db.ReadGraph;
\r
26 import org.simantics.db.Resource;
\r
27 import org.simantics.db.common.request.PossibleIndexRoot;
\r
28 import org.simantics.db.exception.DatabaseException;
\r
29 import org.simantics.image2.ontology.ImageResource;
\r
31 public class DocumentDialect {
\r
33 final static public DocumentDialect INSTANCE = new DocumentDialect();
\r
35 public static final String SIMANTICS_INTERNAL_URI_PREFIX = "http://simantics-internal/";
\r
37 private static final String HTTP = "http://";
\r
38 private static final String ROOT = "root:/";
\r
39 private static final String IMAGE = "image";
\r
40 private static final String MEDIA = "media";
\r
42 final Pattern imageOrMediaPattern = Pattern.compile("\\[\\[([Ii]mage|[Mm]edia)(?::([^\\]]+))?\\]\\]");
\r
44 private static String digest(byte[] bytes) throws NoSuchAlgorithmException {
\r
45 MessageDigest md = MessageDigest.getInstance("MD5");
\r
47 BigInteger number = new BigInteger(1, md.digest());
\r
48 return number.toString(16);
\r
51 private static String imageExtension(ReadGraph graph, Resource image) throws DatabaseException {
\r
52 ImageResource IMAGE = ImageResource.getInstance(graph);
\r
53 if (graph.isInstanceOf(image, IMAGE.PngImage))
\r
55 else if (graph.isInstanceOf(image, IMAGE.JpegImage))
\r
60 public String transform(ReadGraph graph, Resource res, String type, String options) {
\r
62 String[] parts = options.split("\\|");
\r
63 if (parts.length > 0) {
\r
64 String uri = parts[0];
\r
66 Resource indexRoot = graph.syncRequest(new PossibleIndexRoot(res));
\r
67 if (indexRoot == null) return null;
\r
68 String rootURI = graph.getURI(indexRoot);
\r
70 uri = uri.replace(ROOT, rootURI);
\r
72 // (Apros #12268) For more user-friendly linking, allow users to
\r
73 // write white space as ' ' in the wiki-style image links instead
\r
74 // of having to write %20.
\r
75 uri = uri.replace(" ", "%20");
\r
77 if (IMAGE.equals(type)) {
\r
78 Resource image = graph.getPossibleResource(uri);
\r
82 String extension = imageExtension(graph, image);
\r
83 if (extension == null)
\r
86 byte[] bytes = graph.getValue(image, Bindings.BYTE_ARRAY);
\r
87 String digest = digest(bytes);
\r
89 Path dir = Simantics.getTemporaryDirectory("documentImages").toPath();
\r
90 Files.createDirectories(dir);
\r
92 Path f = dir.resolve(digest + extension);
\r
93 if (!Files.exists(f))
\r
94 Files.write(f, bytes);
\r
96 StringBuilder sb = new StringBuilder(128);
\r
97 sb.append("[[File: ").append(f.toUri());
\r
98 for (int i = 1; i < parts.length; i++)
\r
99 sb.append("|").append(parts[i]);
\r
101 return sb.toString();
\r
102 } else if (MEDIA.equals(type)) {
\r
103 Resource image = graph.getPossibleResource(uri);
\r
107 StringBuilder sb = new StringBuilder();
\r
108 sb.append("[").append(SIMANTICS_INTERNAL_URI_PREFIX).append(uri);
\r
109 for (int i = 1; i < parts.length; i++)
\r
110 sb.append(" ").append(parts[i]);
\r
112 return sb.toString();
\r
116 } catch (DatabaseException e) {
\r
117 } catch (NoSuchAlgorithmException e) {
\r
118 } catch (IOException e) {
\r
124 public String apply(ReadGraph graph, Resource res, String markup) throws DatabaseException {
\r
126 StringBuffer sb = new StringBuffer();
\r
127 Matcher matcher = imageOrMediaPattern.matcher(markup);
\r
128 while(matcher.find()) {
\r
129 matcher.appendReplacement(sb, "");
\r
130 String type = matcher.group(1);
\r
131 String options = matcher.group(2);
\r
132 String match = transform(graph, res, type.toLowerCase(), options);
\r
133 if(match != null) sb.append(match);
\r
134 else sb.append("[[Image:" + options + "]]");
\r
136 matcher.appendTail(sb);
\r
137 return sb.toString();
\r