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.File;
\r
15 import java.io.IOException;
\r
16 import java.math.BigInteger;
\r
17 import java.security.MessageDigest;
\r
18 import java.security.NoSuchAlgorithmException;
\r
19 import java.util.regex.Matcher;
\r
20 import java.util.regex.Pattern;
\r
22 import org.simantics.Simantics;
\r
23 import org.simantics.databoard.Bindings;
\r
24 import org.simantics.db.ReadGraph;
\r
25 import org.simantics.db.Resource;
\r
26 import org.simantics.db.common.request.PossibleIndexRoot;
\r
27 import org.simantics.db.exception.DatabaseException;
\r
28 import org.simantics.image2.ontology.ImageResource;
\r
29 import org.simantics.utils.FileUtils;
\r
32 public class DocumentDialect {
\r
34 final Pattern imagePattern = Pattern.compile("\\[\\[Image(?::([^\\]]+))?\\]\\]");
\r
36 final static public DocumentDialect INSTANCE = new DocumentDialect();
\r
38 public String transform(ReadGraph graph, Resource res, String options) {
\r
42 String[] parts = options.split("\\|");
\r
43 if(parts.length > 0) {
\r
45 String uri = parts[0];
\r
46 Resource indexRoot = graph.syncRequest(new PossibleIndexRoot(res));
\r
47 if(indexRoot == null) return null;
\r
48 String rootURI = graph.getURI(indexRoot);
\r
49 uri = uri.replace("root:/", rootURI);
\r
50 // (Apros #12268) For easier image linking, allow users to
\r
51 // write white space as ' ' in the wiki-style image links.
\r
52 uri = uri.replace(" ", "%20");
\r
53 Resource image = graph.getPossibleResource(uri);
\r
54 if(image == null) return null;
\r
55 byte[] bytes = graph.getValue(image, Bindings.BYTE_ARRAY);
\r
57 md = MessageDigest.getInstance("MD5");
\r
59 BigInteger number = new BigInteger(1, md.digest());
\r
60 String digest = number.toString(16);
\r
61 File dir = Simantics.getTemporaryDirectory("documentImages");
\r
64 ImageResource IMAGE = ImageResource.getInstance(graph);
\r
67 if(graph.isInstanceOf(image, IMAGE.PngImage)) extension = ".png";
\r
68 else if(graph.isInstanceOf(image, IMAGE.JpegImage)) extension = ".jpg";
\r
71 File f = new File(dir, digest + extension);
\r
73 FileUtils.writeFile(f, bytes);
\r
74 StringBuilder sb = new StringBuilder();
\r
75 sb.append("[[File: " + f.toURI());
\r
76 for(int i=1;i<parts.length;i++) {
\r
78 sb.append(parts[i]);
\r
81 return sb.toString();
\r
84 } catch (DatabaseException e) {
\r
85 } catch (NoSuchAlgorithmException e) {
\r
86 } catch (IOException e) {
\r
93 public String apply(ReadGraph graph, Resource res, String markup) throws DatabaseException {
\r
95 StringBuffer sb = new StringBuffer();
\r
96 Matcher matcher = imagePattern.matcher(markup);
\r
97 while(matcher.find()) {
\r
98 matcher.appendReplacement(sb, "");
\r
99 String options = matcher.group(1);
\r
100 String match = transform(graph, res, options);
\r
101 if(match != null) sb.append(match);
\r
102 else sb.append("[[Image:" + options + "]]");
\r
104 matcher.appendTail(sb);
\r
105 return sb.toString();
\r