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