source: java/Framclipse/com.framsticks.framclipse/src/com/framsticks/framclipse/script/XMLConstantProvider.java @ 437

Last change on this file since 437 was 437, checked in by Mateusz Poszwa, 9 years ago

Added Framclipse as developed by Bartosz Kukawka and Tomek Maciejewski in 2010

  • Property svn:eol-style set to native
File size: 3.7 KB
Line 
1package com.framsticks.framclipse.script;
2
3import java.io.IOException;
4import java.io.InputStream;
5import java.util.Collections;
6import java.util.HashMap;
7import java.util.Map;
8
9import org.apache.commons.io.IOUtils;
10import org.eclipse.core.runtime.ILog;
11import org.eclipse.core.runtime.IStatus;
12import org.eclipse.core.runtime.Platform;
13import org.eclipse.core.runtime.Status;
14import org.osgi.framework.Bundle;
15
16import com.framsticks.framclipse.script.context.Context;
17import com.framsticks.framclipse.script.context.File;
18import com.framsticks.framclipse.script.context.Framscontext;
19import com.framsticks.framclipse.script.model.Framscript;
20import com.framsticks.framclipse.script.model.Type;
21import com.google.common.collect.Maps;
22import com.google.common.collect.Multimap;
23import com.google.common.collect.Multimaps;
24import com.google.inject.Inject;
25import com.thoughtworks.xstream.XStream;
26
27public class XMLConstantProvider implements ConstantProvider {
28
29        private static final Bundle BUNDLE = Platform.getBundle("com.framsticks.framclipse");
30        private static final ILog LOG = Platform.getLog(BUNDLE);
31
32        private final static Map<String, Type> COMMON_TYPES;
33        static {
34                Map<String, Type> nullContext = new HashMap<String, Type>(Type.BUILT_IN.length);
35                for (Type t : Type.BUILT_IN) {
36                        nullContext.put(t.getName(), t);
37                }
38                COMMON_TYPES = Collections.unmodifiableMap(nullContext);
39        }
40
41        private final Map<String, Map<String, Type>> types;
42
43        @Inject
44        public XMLConstantProvider(XStream xstream) {
45                Framscript framscript = loadFile(xstream, "framscript.xml");
46                Framscontext framscontext = loadFile(xstream, "framscontext.xml");
47                if (framscript != null && framscontext != null) {
48                        types = createTypeMappings(framscontext, framscript);
49                } else {
50                        types = Collections.emptyMap();
51                }
52        }
53
54        @SuppressWarnings("unchecked")
55        private <T> T loadFile(XStream xstream, String filename) {
56                InputStream input = null;
57                try {
58                        input = BUNDLE.getResource(filename).openStream();
59                        return (T) xstream.fromXML(input);
60                } catch (IOException e) {
61                        LOG.log(new Status(IStatus.ERROR, BUNDLE.getSymbolicName(), e.getLocalizedMessage()));
62                        return null;
63                } finally {
64                        IOUtils.closeQuietly(input);
65                }
66        }
67
68        private Map<String, Map<String, Type>> createTypeMappings(Framscontext framscontext, Framscript framscript) {
69                Map<String, Map<String, Type>> types = Maps.newHashMap();
70                Multimap<String, String> ext2ctx = ext2Ctx(framscontext);
71                Multimap<String, Type> ctx2type = ctx2Type(framscript);
72                for (String ext : ext2ctx.keySet()) {
73                        HashMap<String, Type> map = new HashMap<String, Type>();
74                        types.put(ext, map);
75                        for (String ctx : ext2ctx.get(ext)) {
76                                for (Type type : ctx2type.get(ctx)) {
77                                        map.put(type.getName(), type);
78                                }
79                        }
80                }
81                return types;
82        }
83
84        private Multimap<String, String> ext2Ctx(Framscontext framscontext) {
85                Multimap<String, String> ext2ctx = Multimaps.newHashMultimap();
86                for (File file : framscontext.getFiles()) {
87                        String ext = file.getPattern();
88                        ext = ext.substring(ext.indexOf('.') + 1, ext.length());
89                        if (file.getCode() != null) {
90                                for (Context context : file.getCode().getContexts()) {
91                                        ext2ctx.put(ext, context.getName());
92                                }
93                        }
94                }
95                return ext2ctx;
96        }
97
98        private Multimap<String, Type> ctx2Type(Framscript framscript) {
99                Multimap<String, Type> ctx2type = Multimaps.newHashMultimap();
100                for (Type type : framscript.getTypes()) {
101                        ctx2type.put(type.getContext(), type);
102                }
103                return ctx2type;
104        }
105
106        @Override
107        public Type getType(String name, String extension) {
108                Map<String, Type> types = this.types.get(extension);
109                Type result = types != null ? types.get(name) : null;
110                return result != null ? result : COMMON_TYPES.get(name);
111        }
112
113        @Override
114        public Map<String, Map<String, Type>> getTypes() {
115                return types;
116        }
117
118}
Note: See TracBrowser for help on using the repository browser.