]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.reflection/src/org/simantics/scl/reflection/OntologyVersions.java
StyleBase runs cleanup in wrong thread
[simantics/platform.git] / bundles / org.simantics.scl.reflection / src / org / simantics / scl / reflection / OntologyVersions.java
1 package org.simantics.scl.reflection;
2
3 import java.net.URL;
4 import java.util.Collection;
5 import java.util.HashMap;
6 import java.util.Map;
7 import java.util.regex.Matcher;
8 import java.util.regex.Pattern;
9
10 import org.eclipse.core.internal.runtime.PlatformActivator;
11 import org.osgi.framework.Bundle;
12 import org.osgi.framework.Version;
13
14 public class OntologyVersions {
15
16         private static final boolean PRINT = false;
17         
18         private static OntologyVersions INSTANCE;
19         
20         private final Pattern versionExtractPattern = Pattern.compile("^.*-(\\d+\\.\\d+)");
21
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;
26         
27         private OntologyVersions() {
28
29                 Map<String, Version> versions = new HashMap<String, Version>(); 
30                 
31                 if(PRINT) System.err.println("== Ontology report ==");
32         for(Bundle bundle : PlatformActivator.getContext().getBundles()) {
33                 
34                 URL url = bundle.getEntry("graph.tg");
35                 if (url==null) continue;
36                 try {
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);
42                 } finally {
43                 }
44                 
45         }
46         
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);
55         }
56         
57                 if(PRINT) System.err.println("== Ontology report ends ==");
58         
59         unversionedPattern = Pattern.compile("(" + joinKeys(unversionedToCurrent) + ")");
60         currentPattern = Pattern.compile("(" + joinKeys(currentToUnversioned) + ")");
61                 
62         }
63         
64         public static OntologyVersions getInstance() {
65                 if(INSTANCE == null) {
66                         INSTANCE = new OntologyVersions();
67                 }
68                 return INSTANCE;
69         }
70
71     private String joinKeys(Map<String,String> tokens) {
72         Collection<String> keys = tokens.keySet();
73         StringBuilder b = new StringBuilder();
74         boolean first = true;
75         for(String key : keys) {
76                 if(!first) b.append("|");
77                 first = false;
78                 b.append(key);
79         }
80         return b.toString();
81     }
82     
83     /*
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
86      * 
87      * @param text Any string
88      */
89     public String currentVersion(String text) {
90         
91         Matcher matcher = unversionedPattern.matcher(text);
92
93         StringBuffer sb = new StringBuffer(text.length());
94         while(matcher.find()) {
95             matcher.appendReplacement(sb, unversionedToCurrent.get(matcher.group(1)));
96         }
97         matcher.appendTail(sb);
98         return sb.toString();
99
100     }
101     
102     /*
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
105      * 
106      * @param text Any string
107      */
108     public String unversioned(String text) {
109         
110         Matcher matcher = currentPattern.matcher(text);
111
112         StringBuffer sb = new StringBuffer(text.length());
113         while(matcher.find()) {
114             matcher.appendReplacement(sb, currentToUnversioned.get(matcher.group(1)));
115         }
116         matcher.appendTail(sb);
117         return sb.toString();
118
119     }
120
121     /**
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
125      * 
126      * @param text Any string
127      */
128     public String currentOntologyVersion(String ontologyURI) {
129         String versionedURI = currentVersion(ontologyURI);
130         Matcher m = versionExtractPattern.matcher(versionedURI);
131         if (!m.matches())
132             throw new IllegalArgumentException("Cannot extract version from ontology URI '" + ontologyURI + "' with pattern " + versionExtractPattern.pattern());
133         return m.group(1);
134     }
135     
136 }