1 | package de.ugoe.cs.swe.bnftools.ebnf.javaextensions;
|
---|
2 |
|
---|
3 | import java.io.IOException;
|
---|
4 | import java.io.UnsupportedEncodingException;
|
---|
5 | import java.security.MessageDigest;
|
---|
6 | import java.security.NoSuchAlgorithmException;
|
---|
7 | import java.util.Collection;
|
---|
8 | import java.util.HashMap;
|
---|
9 | import java.util.List;
|
---|
10 |
|
---|
11 | import org.eclipse.emf.compare.util.ModelUtils;
|
---|
12 | import org.eclipse.emf.ecore.EObject;
|
---|
13 | import org.eclipse.emf.ecore.util.EcoreUtil;
|
---|
14 |
|
---|
15 | import de.ugoe.cs.swe.bnftools.ebnf.DefinitionList;
|
---|
16 | import de.ugoe.cs.swe.bnftools.ebnf.Rule;
|
---|
17 |
|
---|
18 | public class SimpleBNFHelper {
|
---|
19 |
|
---|
20 | private static HashMap<String, Rule> ruleMap = new HashMap<String, Rule>();
|
---|
21 | // private static HashMap<String, Integer> indexMap = new HashMap<String, Integer>();
|
---|
22 |
|
---|
23 | public final static void dump(String aString) {
|
---|
24 | System.out.println(aString);
|
---|
25 | }
|
---|
26 |
|
---|
27 | // ----------------------------------------------------------------------
|
---|
28 |
|
---|
29 | public final static String definitionListToString(DefinitionList defList) {
|
---|
30 | return "hello";
|
---|
31 | }
|
---|
32 |
|
---|
33 | // ----------------------------------------------------------------------
|
---|
34 |
|
---|
35 | public final static String listToString(List<EObject> defList) {
|
---|
36 | if (defList == null)
|
---|
37 | return "***Java:listToString(List<EObject>defList: defList is null!!!***";
|
---|
38 | if (defList.size() == 0)
|
---|
39 | return "***Java:listToString(List<EObject>defList: defList is empty!!!***";
|
---|
40 |
|
---|
41 | try {
|
---|
42 | StringBuffer strBuf = new StringBuffer();
|
---|
43 | for (int i = 0; i < defList.size(); i++) {
|
---|
44 | strBuf.append(ModelUtils.serialize(defList.get(i)));
|
---|
45 | }
|
---|
46 |
|
---|
47 | // return strBuf.toString().replaceAll("[ \t\n\r]", "");
|
---|
48 | return strBuf.toString();
|
---|
49 | } catch (IOException e) {
|
---|
50 | return "***listToString(List<EObject>defList: IOException***";
|
---|
51 | }
|
---|
52 | }
|
---|
53 |
|
---|
54 | // ----------------------------------------------------------------------
|
---|
55 |
|
---|
56 | public final static String eobjectToString(EObject o) {
|
---|
57 | try {
|
---|
58 | StringBuffer strBuf = new StringBuffer();
|
---|
59 | strBuf.append(ModelUtils.serialize(o));
|
---|
60 |
|
---|
61 | // return strBuf.toString().replaceAll("[ \t\n\r]", "");
|
---|
62 | return strBuf.toString();
|
---|
63 | } catch (IOException e) {
|
---|
64 | return "***listToString(List<EObject>defList: IOException***";
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 |
|
---|
69 | // ----------------------------------------------------------------------
|
---|
70 |
|
---|
71 | public final static String listToHash(List<EObject> defList) {
|
---|
72 | if (defList == null)
|
---|
73 | return "***Java:listToHash(List<EObject>defList: defList is null!!!***";
|
---|
74 | if (defList.size() == 0)
|
---|
75 | return "***Java:listToHash(List<EObject>defList: defList is empty!!!***";
|
---|
76 |
|
---|
77 | try {
|
---|
78 | StringBuffer strBuf = new StringBuffer();
|
---|
79 | for (int i = 0; i < defList.size(); i++) {
|
---|
80 | strBuf.append(ModelUtils.serialize(defList.get(i)));
|
---|
81 | }
|
---|
82 |
|
---|
83 | String result = SHA1(strBuf.toString());
|
---|
84 | return SHA1(result);
|
---|
85 | } catch (IOException e) {
|
---|
86 | return "***Java:listToHash(List<EObject>defList: IOException***";
|
---|
87 | } catch (NoSuchAlgorithmException e) {
|
---|
88 | return "***Java:listToHash(List<EObject>defList: NoSuchAlgorithmException***";
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|
92 | // ----------------------------------------------------------------------
|
---|
93 |
|
---|
94 | public final static void storeRule(String key, Rule rule) {
|
---|
95 | ruleMap.put(key, rule);
|
---|
96 | }
|
---|
97 |
|
---|
98 | // ----------------------------------------------------------------------
|
---|
99 |
|
---|
100 | public final static Boolean existsRule(String key) {
|
---|
101 | if (ruleMap.get(key) != null)
|
---|
102 | return true;
|
---|
103 | else
|
---|
104 | return false;
|
---|
105 | }
|
---|
106 |
|
---|
107 | // ----------------------------------------------------------------------
|
---|
108 |
|
---|
109 | public final static Rule getRule(String key) {
|
---|
110 | return ruleMap.get(key);
|
---|
111 | }
|
---|
112 |
|
---|
113 | // ----------------------------------------------------------------------
|
---|
114 |
|
---|
115 | public final static EObject copyEObject(EObject o) {
|
---|
116 | return EcoreUtil.copy(o);
|
---|
117 | }
|
---|
118 |
|
---|
119 | // ----------------------------------------------------------------------
|
---|
120 |
|
---|
121 | public final static List<EObject> copyEObjectList(List<EObject> o) {
|
---|
122 | return (List<EObject>) EcoreUtil.copyAll(o);
|
---|
123 | }
|
---|
124 |
|
---|
125 | // ----------------------------------------------------------------------
|
---|
126 |
|
---|
127 | public final static List<DefinitionList> copyDefinitionList(List<DefinitionList> o) {
|
---|
128 | return (List<DefinitionList>) EcoreUtil.copyAll(o);
|
---|
129 | }
|
---|
130 |
|
---|
131 | // ----------------------------------------------------------------------
|
---|
132 |
|
---|
133 | private static String convertToHex(byte[] data) {
|
---|
134 | StringBuffer buf = new StringBuffer();
|
---|
135 | for (int i = 0; i < data.length; i++) {
|
---|
136 | int halfbyte = (data[i] >>> 4) & 0x0F;
|
---|
137 | int two_halfs = 0;
|
---|
138 | do {
|
---|
139 | if ((0 <= halfbyte) && (halfbyte <= 9))
|
---|
140 | buf.append((char) ('0' + halfbyte));
|
---|
141 | else
|
---|
142 | buf.append((char) ('a' + (halfbyte - 10)));
|
---|
143 | halfbyte = data[i] & 0x0F;
|
---|
144 | } while (two_halfs++ < 1);
|
---|
145 | }
|
---|
146 | return buf.toString();
|
---|
147 | }
|
---|
148 |
|
---|
149 | // ----------------------------------------------------------------------
|
---|
150 |
|
---|
151 | private static String SHA1(String text) throws NoSuchAlgorithmException,
|
---|
152 | UnsupportedEncodingException {
|
---|
153 | MessageDigest md;
|
---|
154 | md = MessageDigest.getInstance("SHA-1");
|
---|
155 | byte[] sha1hash = new byte[40];
|
---|
156 | md.update(text.getBytes("iso-8859-1"), 0, text.length());
|
---|
157 | sha1hash = md.digest();
|
---|
158 | return convertToHex(sha1hash);
|
---|
159 | }
|
---|
160 |
|
---|
161 | }
|
---|