X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.document%2Fsrc%2Forg%2Fsimantics%2Fdocument%2FDocumentDialect.java;h=5aa7b385b36b5501b15c61f7d9648be8a3148251;hb=HEAD;hp=9fc8e98a7ebb85927f2e76b13dcf8fc9b0094959;hpb=969bd23cab98a79ca9101af33334000879fb60c5;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.document/src/org/simantics/document/DocumentDialect.java b/bundles/org.simantics.document/src/org/simantics/document/DocumentDialect.java index 9fc8e98a7..5aa7b385b 100644 --- a/bundles/org.simantics.document/src/org/simantics/document/DocumentDialect.java +++ b/bundles/org.simantics.document/src/org/simantics/document/DocumentDialect.java @@ -1,109 +1,141 @@ -/******************************************************************************* - * Copyright (c) 2007, 2010 Association for Decentralized Information Management - * in Industry THTH ry. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * VTT Technical Research Centre of Finland - initial API and implementation - *******************************************************************************/ -package org.simantics.document; - -import java.io.File; -import java.io.IOException; -import java.math.BigInteger; -import java.security.MessageDigest; -import java.security.NoSuchAlgorithmException; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -import org.simantics.Simantics; -import org.simantics.databoard.Bindings; -import org.simantics.db.ReadGraph; -import org.simantics.db.Resource; -import org.simantics.db.common.request.PossibleIndexRoot; -import org.simantics.db.exception.DatabaseException; -import org.simantics.image2.ontology.ImageResource; -import org.simantics.utils.FileUtils; - - -public class DocumentDialect { - - final Pattern imagePattern = Pattern.compile("\\[\\[Image(?::([^\\]]+))?\\]\\]"); - - final static public DocumentDialect INSTANCE = new DocumentDialect(); - - public String transform(ReadGraph graph, Resource res, String options) { - - try { - - String[] parts = options.split("\\|"); - if(parts.length > 0) { - - String uri = parts[0]; - Resource indexRoot = graph.syncRequest(new PossibleIndexRoot(res)); - if(indexRoot == null) return null; - String rootURI = graph.getURI(indexRoot); - uri = uri.replace("root:/", rootURI); - // (Apros #12268) For easier image linking, allow users to - // write white space as ' ' in the wiki-style image links. - uri = uri.replace(" ", "%20"); - Resource image = graph.getPossibleResource(uri); - if(image == null) return null; - byte[] bytes = graph.getValue(image, Bindings.BYTE_ARRAY); - MessageDigest md; - md = MessageDigest.getInstance("MD5"); - md.update(bytes); - BigInteger number = new BigInteger(1, md.digest()); - String digest = number.toString(16); - File dir = Simantics.getTemporaryDirectory("documentImages"); - dir.mkdirs(); - - ImageResource IMAGE = ImageResource.getInstance(graph); - - String extension; - if(graph.isInstanceOf(image, IMAGE.PngImage)) extension = ".png"; - else if(graph.isInstanceOf(image, IMAGE.JpegImage)) extension = ".jpg"; - else return null; - - File f = new File(dir, digest + extension); - if(!f.exists()) - FileUtils.writeFile(f, bytes); - StringBuilder sb = new StringBuilder(); - sb.append("[[File: " + f.toURI()); - for(int i=1;i 0) { + String uri = parts[0]; + + Resource indexRoot = graph.syncRequest(new PossibleIndexRoot(res)); + if (indexRoot == null) return null; + String rootURI = graph.getURI(indexRoot); + + uri = uri.replace(ROOT, rootURI); + + // (Apros #12268) For more user-friendly linking, allow users to + // write white space as ' ' in the wiki-style image links instead + // of having to write %20. + uri = uri.replace(" ", "%20"); + + if (IMAGE.equals(type)) { + Resource image = graph.getPossibleResource(uri); + if (image == null) + return null; + + String extension = imageExtension(graph, image); + if (extension == null) + return null; + + byte[] bytes = graph.getValue(image, Bindings.BYTE_ARRAY); + String digest = digest(bytes); + + Path dir = Simantics.getTemporaryDirectory("documentImages").toPath(); + Files.createDirectories(dir); + + Path f = dir.resolve(digest + extension); + if (!Files.exists(f)) + Files.write(f, bytes); + + StringBuilder sb = new StringBuilder(128); + sb.append("[[File: ").append(f.toUri()); + for (int i = 1; i < parts.length; i++) + sb.append("|").append(parts[i]); + sb.append("]]"); + return sb.toString(); + } else if (MEDIA.equals(type)) { + Resource image = graph.getPossibleResource(uri); + if (image == null) + return null; + + StringBuilder sb = new StringBuilder(); + sb.append("[").append(SIMANTICS_INTERNAL_URI_PREFIX).append(uri); + for (int i = 1; i < parts.length; i++) + sb.append(" ").append(parts[i]); + sb.append("]"); + return sb.toString(); + } + + } + } catch (DatabaseException e) { + } catch (NoSuchAlgorithmException e) { + } catch (IOException e) { + } + + return null; + } + + public String apply(ReadGraph graph, Resource res, String markup) throws DatabaseException { + + StringBuffer sb = new StringBuffer(); + Matcher matcher = imageOrMediaPattern.matcher(markup); + while(matcher.find()) { + matcher.appendReplacement(sb, ""); + String type = matcher.group(1); + String options = matcher.group(2); + String match = transform(graph, res, type.toLowerCase(), options); + if(match != null) sb.append(match); + else sb.append("[[Image:" + options + "]]"); + } + matcher.appendTail(sb); + return sb.toString(); + + } + } \ No newline at end of file