source: trunk/src/org/expeditee/encryption/items/surrogates/Surrogate.java@ 1408

Last change on this file since 1408 was 1408, checked in by bln4, 5 years ago

Implementation of the surrogate system.
When you set an item to have a encryption label, a surrogate for that item is generated.
The newly updated EncryptedExpReader/Writer has been updated to maintain the connection between the item and its surrogate.

Coming up next:
Surrogate mode. The ability to simulate viewing and editing an encrypted frame with a limited set of labels.

File size: 5.6 KB
Line 
1package org.expeditee.encryption.items.surrogates;
2
3import java.lang.reflect.InvocationTargetException;
4import java.lang.reflect.Method;
5import java.util.ArrayList;
6import java.util.HashMap;
7import java.util.LinkedHashMap;
8import java.util.List;
9import java.util.Map;
10import java.util.Set;
11
12import org.expeditee.io.Conversion;
13import org.expeditee.io.DefaultFrameWriter;
14import org.expeditee.items.Item;
15import org.expeditee.items.Line;
16import org.expeditee.items.Text;
17
18public class Surrogate {
19
20 private static Map<String, Boolean> propertyInheritanceDefault = new HashMap<String, Boolean>();
21
22 {
23 for (Character tag: DefaultFrameWriter.getItemTags().keySet()) {
24 if (tag == 'T') {
25 propertyInheritanceDefault.put(tag + "", false);
26 } else {
27 propertyInheritanceDefault.put(tag + "", true);
28 }
29 }
30
31 for (String tag: DefaultFrameWriter.getItemTagsExt().keySet()) {
32 propertyInheritanceDefault.put(tag, true);
33 }
34 }
35
36 private Item surrogateItem = null;
37 private Map<String, Boolean> propertyInheritance;
38
39 public Surrogate(Item surrogateItem) {
40 this.surrogateItem = surrogateItem;
41 this.propertyInheritance = new HashMap<String, Boolean>(propertyInheritanceDefault);
42 }
43
44 public String toString() {
45 String nl = System.getProperty("line.separator");
46 List<String> lines = toStringList();
47 return lines.stream().map(l -> l + nl).reduce("", (acc, l) -> acc + l);
48 }
49
50 public List<String> toStringList() {
51 List<String> lines = new ArrayList<String>();
52 Map<Character,Method> itemTags = new LinkedHashMap<Character, Method>(DefaultFrameWriter.getItemTags());
53 Map<String,Method> itemTagsExt = new LinkedHashMap<String, Method>(DefaultFrameWriter.getItemTagsExt());
54
55 try {
56 Method methodS = itemTags.get('S');
57 Object o = methodS.invoke(surrogateItem);
58 o = Conversion.ConvertToExpeditee(methodS, o);
59 lines.add("S " + o);
60 lines.add("SurrogateFor " + surrogateItem.getClassic().getID());
61 } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
62 e.printStackTrace();
63 }
64 itemTags.remove('S');
65 itemTagsExt.remove("_el");
66
67
68 for(Character itemTag: itemTags.keySet()) {
69 boolean propertyIsInherited = isPropertyIsInherited(itemTag + "");
70 if (propertyIsInherited) {
71 continue;
72 }
73
74 Object value = null;
75 if (itemTag == 'T') {
76 value = "REDACTED";
77 } else {
78 try {
79 Method method = itemTags.get(itemTag);
80 Object o = method.invoke(surrogateItem);
81 value = Conversion.ConvertToExpeditee(method, o);
82 } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
83 e.printStackTrace();
84 }
85 }
86 lines.add(itemTag + " " + value);
87 }
88 for (String itemTag: itemTagsExt.keySet()) {
89 boolean propertyIsInherited = isPropertyIsInherited(itemTag);
90 if (propertyIsInherited) {
91 continue;
92 }
93 try {
94 Method method = itemTagsExt.get(itemTag);
95 Object o = method.invoke(surrogateItem);
96 o = Conversion.ConvertToExpeditee(method, o);
97 lines.add(itemTag + " " + o);
98 } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
99 e.printStackTrace();
100 }
101 }
102
103 return lines;
104 }
105
106 public Item getSurrogateItem() {
107 return surrogateItem;
108 }
109
110 public boolean isPropertyIsInherited(String tag) {
111 if (this.propertyInheritance.containsKey(tag)) {
112 return this.propertyInheritance.get(tag);
113 } else {
114 return false;
115 }
116 }
117
118 public void setPropertyIsInherited(String tag, boolean value) {
119 this.propertyInheritance.put(tag, value);
120 }
121
122 public static Surrogate forItem(Item item) {
123 Item surrogateItem = item.copy();
124 surrogateItem.setID(item.getParent().getNextItemID());
125 surrogateItem.setAsSurrogateFor(item);
126
127 if (item.isLineEnd()) {
128 handleLineEnd(surrogateItem);
129 }
130
131 if (surrogateItem instanceof Text && !surrogateItem.isAnnotation()) {
132 surrogateItem.setText("REDACTED");
133 }
134
135 Surrogate surrogate = new Surrogate(surrogateItem);
136 item.addToSurrogates(surrogate);
137 return surrogate;
138 }
139
140 private static Surrogate forItem(Line line) {
141 Item startItem = line.getStartItem();
142 Item endItem = line.getEndItem();
143
144 if (!startItem.isSurrogate()) {
145 Set<Surrogate> surrogates = startItem.getSurrogates();
146 surrogates.removeIf(surrogate -> !surrogate.surrogateItem.isLineEnd());
147 if (surrogates.isEmpty()) {
148 Surrogate surrogate = Surrogate.forItem(startItem);
149 startItem.addToSurrogates(surrogate);
150 startItem = surrogate.surrogateItem;
151 } else {
152 startItem = surrogates.stream().findFirst().get().surrogateItem;
153 }
154 }
155
156 if (!endItem.isSurrogate()) {
157 Set<Surrogate> surrogates = endItem.getSurrogates();
158 surrogates.removeIf(surrogate -> !surrogate.surrogateItem.isLineEnd());
159 if (endItem.getSurrogates().isEmpty()) {
160 Surrogate surrogate = Surrogate.forItem(endItem);
161 endItem.addToSurrogates(surrogate);
162 endItem = surrogate.surrogateItem;
163 } else {
164 endItem = endItem.getSurrogates().stream().findFirst().get().surrogateItem;
165 }
166 }
167
168 Line ret = new Line(startItem, endItem, line.getParent().getNextItemID());
169 ret.setAsSurrogateFor(line);
170 Surrogate surrogate = new Surrogate(ret);
171 line.addToSurrogates(surrogate);
172 return surrogate;
173 }
174
175 private static void handleLineEnd(Item lineEndSurrogate) {
176 Item lineEndClassic = lineEndSurrogate.getClassic();
177 List<Item> lines = new ArrayList<Item>(lineEndClassic.getLines());
178 lines.removeIf(line -> !line.getSurrogates().isEmpty());
179
180 for(Item line: lines) {
181 Surrogate.forItem((Line) line);
182 }
183 }
184}
Note: See TracBrowser for help on using the repository browser.