1 package org.simantics.scl.reflection;
4 import java.util.Collection;
5 import java.util.HashMap;
7 import java.util.regex.Matcher;
8 import java.util.regex.Pattern;
10 import org.eclipse.core.internal.runtime.PlatformActivator;
11 import org.osgi.framework.Bundle;
12 import org.osgi.framework.Version;
14 public class OntologyVersions {
16 private static final boolean PRINT = false;
18 private static OntologyVersions INSTANCE;
20 private final Pattern versionExtractPattern = Pattern.compile("^.*-(\\d+\\.\\d+)");
22 private Map<String, String> unversionedToCurrent = new HashMap<String, String>();
23 private Map<String, String> currentToUnversioned = new HashMap<String, String>();
24 final private Pattern unversionedPattern;
25 final private Pattern currentPattern;
27 private OntologyVersions() {
29 Map<String, Version> versions = new HashMap<String, Version>();
31 if(PRINT) System.err.println("== Ontology report ==");
32 for(Bundle bundle : PlatformActivator.getContext().getBundles()) {
34 URL url = bundle.getEntry("graph.tg");
35 if (url==null) continue;
37 String name = (String) bundle.getHeaders().get("Bundle-Name");
38 Version osgiVersion = bundle.getVersion();
39 Version previous = versions.get(name);
40 if(previous == null || osgiVersion.compareTo(previous) > 0) versions.put(name, osgiVersion);
41 if(PRINT) System.err.println("found: " + name + ":" + osgiVersion);
47 for(Map.Entry<String, Version> entry : versions.entrySet()) {
48 int minor = entry.getValue().getMinor();
49 int major = entry.getValue().getMajor();
50 String unversioned = entry.getKey() + "-0.0";
51 String versioned = entry.getKey() + "-" + major + "." + minor;
52 unversionedToCurrent.put(unversioned, versioned);
53 currentToUnversioned.put(versioned, unversioned);
54 if(PRINT) System.err.println("latest: " + versioned);
57 if(PRINT) System.err.println("== Ontology report ends ==");
59 unversionedPattern = Pattern.compile("(" + joinKeys(unversionedToCurrent) + ")");
60 currentPattern = Pattern.compile("(" + joinKeys(currentToUnversioned) + ")");
64 public static OntologyVersions getInstance() {
65 if(INSTANCE == null) {
66 INSTANCE = new OntologyVersions();
71 private String joinKeys(Map<String,String> tokens) {
72 Collection<String> keys = tokens.keySet();
73 StringBuilder b = new StringBuilder();
75 for(String key : keys) {
76 if(!first) b.append("|");
84 * Replaces all references to URIs with unversioned ontologies with current versions
85 * e.g. http://www.simantics.org/Layer0-0.0 => http://www.simantics.org/Layer0-1.0
87 * @param text Any string
89 public String currentVersion(String text) {
91 Matcher matcher = unversionedPattern.matcher(text);
93 StringBuffer sb = new StringBuffer(text.length());
94 while(matcher.find()) {
95 matcher.appendReplacement(sb, unversionedToCurrent.get(matcher.group(1)));
97 matcher.appendTail(sb);
103 * Replaces all references to URIs from current ontologies with unversioned ones
104 * e.g. http://www.simantics.org/Layer0-1.0 => http://www.simantics.org/Layer0-0.0
106 * @param text Any string
108 public String unversioned(String text) {
110 Matcher matcher = currentPattern.matcher(text);
112 StringBuffer sb = new StringBuffer(text.length());
113 while(matcher.find()) {
114 matcher.appendReplacement(sb, currentToUnversioned.get(matcher.group(1)));
116 matcher.appendTail(sb);
117 return sb.toString();
122 * Calculates the current version of the specified version-agnostic URI and
123 * returns only the version part.
124 * e.g. http://www.simantics.org/Layer0-0.0 => 1.1
126 * @param text Any string
128 public String currentOntologyVersion(String ontologyURI) {
129 String versionedURI = currentVersion(ontologyURI);
130 Matcher m = versionExtractPattern.matcher(versionedURI);
132 throw new IllegalArgumentException("Cannot extract version from ontology URI '" + ontologyURI + "' with pattern " + versionExtractPattern.pattern());