Changeset 1408
- Timestamp:
- 06/21/19 12:12:55 (5 years ago)
- Location:
- trunk/src/org/expeditee
- Files:
-
- 3 added
- 10 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/expeditee/encryption/Actions.java
r1389 r1408 23 23 import org.expeditee.gui.Frame; 24 24 import org.expeditee.gui.FrameIO; 25 import org.expeditee.items.Item; 25 26 import org.expeditee.items.Text; 26 27 import org.expeditee.settings.UserSettings; … … 29 30 30 31 public class Actions implements CryptographyConstants { 32 33 public static void TestSurrogate(Item classic) { 34 System.out.println("Test surrogates: "); 35 } 31 36 32 37 public static Text GenerateSecret(String label) { -
trunk/src/org/expeditee/encryption/io/EncryptedExpReader.java
r1401 r1408 7 7 import java.io.InputStreamReader; 8 8 import java.io.Reader; 9 import java. io.UnsupportedEncodingException;9 import java.lang.reflect.Method; 10 10 import java.security.InvalidKeyException; 11 11 import java.security.NoSuchAlgorithmException; 12 import java.util.ArrayList; 12 13 import java.util.Arrays; 13 14 import java.util.Base64; 14 import java.util.Collection; 15 import java.util.List; 16 import java.util.function.Predicate; 15 17 16 18 import javax.crypto.BadPaddingException; … … 24 26 import org.expeditee.encryption.Label; 25 27 import org.expeditee.encryption.Label.LabelResult; 28 import org.expeditee.encryption.items.surrogates.EncryptionDetail; 29 import org.expeditee.encryption.items.surrogates.Surrogate; 26 30 import org.expeditee.gui.Frame; 31 import org.expeditee.gui.FrameIO; 32 import org.expeditee.io.Conversion; 27 33 import org.expeditee.io.ExpReader; 28 34 import org.expeditee.items.Item; … … 31 37 32 38 public class EncryptedExpReader extends ExpReader implements CryptographyConstants { 33 static final String ENCRYPTED_EXP_FLAG = "EncryptedExp"; 39 private static final String ENCRYPTED_EXP_FLAG = "EncryptedExp"; 40 private static final String labelProfile = "Profile"; 41 private static final String labelNone = "None"; 34 42 private SecretKey personalKey; 35 43 private boolean accessDenied = false; 36 44 37 public EncryptedExpReader( final String frameName) throws UnsupportedEncodingException{45 public EncryptedExpReader(String frameName) { 38 46 super(frameName); 39 47 } 40 48 41 49 public static boolean isEncryptedExpediteeFile(final String path) throws IOException { 42 50 final BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(path), "UTF-8")); … … 74 82 75 83 @Override 76 public Frame readFrame( finalBufferedReader reader) throws IOException {84 public Frame readFrame(BufferedReader reader) throws IOException { 77 85 if (accessDenied) { return null; } 78 else { 79 Frame unencryptedFrame = super.readFrame(reader); 80 individualItemDecryption(unencryptedFrame); 81 return unencryptedFrame; 82 } 83 } 84 85 private void individualItemDecryption(Frame frame) { 86 // Find items with their own encryption label. 87 Collection<Item> itemsWithEncryptionLabel = frame.getAllItems(); 88 itemsWithEncryptionLabel.removeIf(i -> i.getEncryptionLabel() == null || i.getEncryptionLabel().isEmpty()); 89 90 for (Item i: itemsWithEncryptionLabel) { 91 // Decrypt the Text Items 92 if (i instanceof Text) { 93 LabelResult res = Label.resolveKey(i.getEncryptionLabel()); 94 if (res == LabelResult.SuccessResolveLabelToKey) { 95 byte[] keyBytes = res.key; 96 if (keyBytes == null) { continue; } 97 SecretKeySpec key = new SecretKeySpec(keyBytes, SymmetricAlgorithm); 98 byte[] decryptedBytes = DecryptSymmetric(Base64.getDecoder().decode(i.getText()), key); 99 i.setText(new String(decryptedBytes)); 100 } 101 } 86 87 _reader = reader; 88 String next = ""; 89 Frame newFrame = new Frame(); 90 List<DelayedAction> delayedActions = new ArrayList<DelayedAction>(); 91 newFrame.setName(_frameName); 92 93 try { 94 // First read all the header lines 95 next = readTheHeader(newFrame); 96 97 // Now read all the items 98 next = readTheItems(newFrame, delayedActions); 99 100 // Read the lines 101 next = readTheLines(newFrame); 102 103 // Read the constraints 104 next = readTheConstraints(); 105 106 for(DelayedAction action: delayedActions) { 107 action.exec(); 108 } 109 110 // Read the stats 111 next = readTheStats(newFrame); 112 } catch (Exception e) { 113 e.printStackTrace(); 114 System.out.println("Error reading frame file line: " + next + " " + e.getMessage()); 115 } 116 117 _reader.close(); 118 FrameIO.setSavedProperties(newFrame); 119 newFrame.setChanged(false); 120 121 return newFrame; 122 } 123 124 @Override 125 protected String readTheItems(Frame newFrame, List<DelayedAction> delayedActions) throws IOException { 126 String next = null; 127 Item currentItem = null; 128 129 Predicate<String> endOfSection = s -> s.equals("Z") || s.equals("Z..."); 130 while (_reader.ready() && !endOfSection.test(next = _reader.readLine())) { 131 if (!isValidLine(next)) { 132 continue; 133 } 134 String tag = getTagEnc(next); 135 if (tag.equals("S")) { 136 currentItem = newItem(next); 137 _linePoints.put(currentItem.getID(), currentItem); 138 newFrame.addItem(currentItem); 139 currentItem.setEncryptionDetailForTag(tag + "", EncryptionDetail.UnencryptedOnSave); 140 } else if (currentItem != null && actionShouldBeDelayed(tag.charAt(0))) { 141 delayedActions.add(new DelayedAction(currentItem, next)); 142 } else if (currentItem != null) { 143 processBodyLine(currentItem, next); 144 } else { 145 System.err.println("Error while reading in frame (ExpReader): Found body line but no current item to apply it to."); 146 } 147 } 148 149 if (next.equals("Z...")) { 150 next = readTheSurrogateItems(newFrame, delayedActions); 151 } 152 153 return next; 154 } 155 156 @Override 157 protected void processBodyLine(Item item, String line) { 158 // separate the tag from the value 159 String tag = getTagEnc(line); 160 String value = getValue(line); 161 162 // Attempt to decrypt the line if necessary. 163 if (isEncryptedLine(line)) { 164 LabelResult res = Label.resolveKey(item.getEncryptionLabel()); 165 if (res == LabelResult.SuccessResolveLabelToKey) { 166 item.setEncryptionDetailForTag(tag, EncryptionDetail.ReencryptOnSave); 167 SecretKey key = new SecretKeySpec(res.key, SymmetricAlgorithm); 168 byte[] decryptedBytes = DecryptSymmetric(Base64.getDecoder().decode(value), key); 169 value = new String(decryptedBytes); 170 } else { 171 EncryptionDetail encryptionDetail = EncryptionDetail.UseUndecipheredValueOnSave.setUndecipheredValue(getValue(line)); 172 item.setEncryptionDetailForTag(tag, encryptionDetail); 173 return; 174 } 175 } else { 176 item.setEncryptionDetailForTag(tag, EncryptionDetail.UnencryptedOnSave); 177 } 178 179 // Process the line 180 Method toRun = tag.startsWith("_") ? _ItemTagsExt.get(tag) : _ItemTags.get(tag.charAt(0)); 181 if (toRun == null) { 182 System.out.println("Error accessing tag method: " + tag); 183 } 184 Object[] vals = Conversion.Convert(toRun, value); 185 try { 186 if (vals != null) { 187 toRun.invoke(item, vals); 188 } 189 } catch (Exception e) { 190 System.out.println("Error running tag method: " + tag); 191 e.printStackTrace(); 192 } 193 } 194 195 private String readTheSurrogateItems(Frame newFrame, List<DelayedAction> delayedActions) throws IOException { 196 String next = null; 197 Item currentItem = null; 198 199 Predicate<String> endOfSection = s -> s.equals("Z") || s.equals("Z..."); 200 while (_reader.ready() && !endOfSection.test(next = _reader.readLine())) { 201 if (!isValidLine(next)) { 202 continue; 203 } 204 205 String tag = getTagEnc(next); 206 if (tag.equals("S") && !next.startsWith("SurrogateFor")) { 207 currentItem = newItem(next); 208 currentItem.setEncryptionDetailForTag(tag + "", EncryptionDetail.UnencryptedOnSave); 209 } else if (next.startsWith("SurrogateFor")) { 210 String id = next.split(" ")[1]; 211 Item classic = newFrame.getItemWithID(Integer.parseInt(id)); 212 if (id == null) { 213 System.err.println("WARNING: Attempted to create surrogate for classic ID " + 214 id + " but was no item on frame " + newFrame.getName() + " exists."); 215 continue; 216 } 217 currentItem.setAsSurrogateFor(classic); 218 classic.addToSurrogates(new Surrogate(currentItem)); 219 } else if (currentItem != null && actionShouldBeDelayed(getTag(next))) { 220 delayedActions.add(new DelayedAction(currentItem, next)); 221 } else if (currentItem != null) { 222 processBodyLine(currentItem, next); 223 } else { 224 System.err.println("Error while reading in frame (ExpReader): Found body line but no current item to apply it to."); 225 } 226 } 227 228 return next; 229 } 230 231 protected static String getTagEnc(String line) { 232 char charAtZero = line.charAt(0); 233 if (charAtZero == '_') { 234 return line.split(" ")[0]; 235 } else { 236 return charAtZero + ""; 237 } 238 } 239 240 protected static String getValue(String line) { 241 String[] split = line.split(" "); 242 if (split.length >= 2) { 243 return line.substring(split[0].length()).trim(); 244 } else { 245 return null; 246 } 247 } 248 249 protected static boolean isEncryptedLine(String line) { 250 if (line.startsWith("S") || line.startsWith("_el")) { 251 return false; 252 } 253 if (line.length() > 2) { 254 return line.charAt(1) == 'E'; 255 } else { 256 return false; 102 257 } 103 258 } … … 125 280 126 281 private class EncryptedLineReader extends BufferedReader { 282 private boolean noneMode = false; 283 127 284 public EncryptedLineReader(Reader in) { 128 285 super(in); … … 140 297 return ""; 141 298 } 299 300 if (noneMode) { 301 return line; 302 } 303 142 304 if (line.startsWith(ENCRYPTED_EXP_FLAG)) { 143 305 String label = line.replace(ENCRYPTED_EXP_FLAG, ""); 144 306 // if using Profile label, use personal key 145 if (label. startsWith("Profile")) {307 if (label.equals(labelProfile)) { 146 308 Text text = KeyList.PersonalKey.get(); 147 309 byte[] keyBytes = Base64.getDecoder().decode(text.getData().get(0)); 148 310 personalKey = new SecretKeySpec(keyBytes, SymmetricAlgorithm); 311 return readLine(); 312 } else if (label.equals(labelNone)) { 313 noneMode = true; 149 314 return readLine(); 150 315 } else { -
trunk/src/org/expeditee/encryption/io/EncryptedExpWriter.java
r1401 r1408 2 2 3 3 import java.io.IOException; 4 import java.lang.reflect.InvocationTargetException; 5 import java.lang.reflect.Method; 4 6 import java.security.InvalidKeyException; 5 7 import java.security.NoSuchAlgorithmException; 8 import java.util.ArrayList; 6 9 import java.util.Arrays; 7 10 import java.util.Base64; 8 import java.util. Collection;11 import java.util.LinkedHashMap; 9 12 import java.util.List; 13 import java.util.function.Consumer; 10 14 11 15 import javax.crypto.BadPaddingException; … … 19 23 import org.expeditee.encryption.Label; 20 24 import org.expeditee.encryption.Label.LabelResult; 25 import org.expeditee.encryption.items.surrogates.EncryptionDetail; 26 import org.expeditee.encryption.items.surrogates.Surrogate; 21 27 import org.expeditee.gui.Frame; 22 28 import org.expeditee.gui.MessageBay; 29 import org.expeditee.io.Conversion; 23 30 import org.expeditee.io.ExpWriter; 24 31 import org.expeditee.items.Item; 32 import org.expeditee.items.Line; 25 33 import org.expeditee.items.Text; 26 34 import org.expeditee.settings.identity.secrets.KeyList; 35 import org.expeditee.stats.SessionStats; 27 36 28 37 public class EncryptedExpWriter extends ExpWriter implements CryptographyConstants { 29 38 private SecretKey key; 30 private String label; 39 40 private final String label; 41 42 private static final String ENCRYPTED_EXP_FLAG = "EncryptedExp"; 31 43 private static final String nl = "\n"; 32 33 public EncryptedExpWriter(String encryptionLabel) throws IOException { 34 if (encryptionLabel.compareTo("Profile") == 0) { 44 private static final String labelProfile = "Profile"; 45 private static final String labelNone = "None"; 46 protected static final String SURROGATE_TERMINATOR = TERMINATOR + "..."; 47 48 public EncryptedExpWriter(String encryptionLabel) { 49 label = encryptionLabel; 50 if (label.equals(labelNone)) { 51 return; 52 } 53 54 if (label.equals(labelProfile)) { 35 55 // obtain personal key 36 56 Text text = KeyList.PersonalKey.get(); … … 40 60 key = new SecretKeySpec(keyBytes, SymmetricAlgorithm); 41 61 } 42 label = "Profile";43 62 } else { 44 LabelResult res = Label.resolveKey( encryptionLabel);63 LabelResult res = Label.resolveKey(label); 45 64 if (res == LabelResult.SuccessResolveLabelToKey) { 46 65 byte[] keyBytes = res.key; 47 66 key = new SecretKeySpec(keyBytes, SymmetricAlgorithm); 48 label = encryptionLabel;49 67 } else if (res == LabelResult.ErrorUnableToFindLabel) { 50 68 MessageBay.errorMessage(res.toString() + encryptionLabel); 69 key = null; 51 70 } else { 52 71 MessageBay.errorMessage(res.toString()); … … 54 73 } 55 74 } 56 75 76 @Override 77 public void outputFrame(Frame frame) throws IOException { 78 if (_writer == null) { return; } 79 80 preOutputFrame(); 81 writeHeader(frame); 82 83 // write item 84 writeItemData(frame); 85 writeTerminator(); 86 87 // write lines and constraints 88 writeLineData(); 89 writeTerminator(); 90 writeConstraintData(); 91 writeTerminator(); 92 93 writeLine(SessionStats.getFrameEventList(frame)); 94 } 95 57 96 @Override 58 97 protected void preOutputFrame() { 59 98 try { 60 String line = E ncryptedExpReader.ENCRYPTED_EXP_FLAG + label + nl;99 String line = ENCRYPTED_EXP_FLAG + label + nl; 61 100 _writer.write(line); 62 101 _stringWriter.append(line); … … 67 106 68 107 @Override 69 public void outputFrame(Frame frame) throws IOException {70 individualItemEncryption(frame);71 super.outputFrame(frame);72 }73 74 @Override75 108 protected void writeLine(String line) throws IOException { 76 109 // do not write empty lines 77 110 if (line == null) { return; } 78 111 79 // prepare line to write out 80 byte[] encrypted = EncryptSymmetric(line.getBytes(), key); 81 String toWrite = Base64.getEncoder().encodeToString(encrypted) + nl; 82 112 String toWrite; 113 if (key == null && label.equals(labelNone)) { 114 toWrite = line + nl; 115 } else { 116 // prepare line to write out 117 byte[] encrypted = EncryptSymmetric(line.getBytes(), key); 118 toWrite = Base64.getEncoder().encodeToString(encrypted) + nl; 119 } 120 83 121 // output 84 122 _writer.write(toWrite); … … 86 124 } 87 125 88 private void individualItemEncryption(Frame frame) { 89 // Find items with their own encryption label. 90 Collection<Item> itemsWithEncryptionLabel = frame.getAllItems(); 91 itemsWithEncryptionLabel.removeIf(i -> i.getEncryptionLabel() == null || i.getEncryptionLabel().isEmpty()); 92 93 for (Item i: itemsWithEncryptionLabel) { 94 // Encrypt the Text Items 95 if (i instanceof Text) { 96 LabelResult res = Label.resolveKey(i.getEncryptionLabel()); 97 if (res == LabelResult.SuccessResolveLabelToKey) { 98 byte[] keyBytes = res.key; 99 if (keyBytes == null) { continue; } 100 SecretKeySpec key = new SecretKeySpec(keyBytes, SymmetricAlgorithm); 101 byte[] encryptedBytes = EncryptSymmetric(i.getText().getBytes(), key); 102 i.setText(Base64.getEncoder().encodeToString(encryptedBytes)); 126 @Override 127 protected void writeLineData() throws IOException { 128 super.writeLineData(); 129 130 writeSurrogateLineData(); 131 } 132 133 @Override 134 protected void writeClass(Item toWrite) throws IOException { 135 LinkedHashMap<Character,Method> itemTags = new LinkedHashMap<Character, Method>(getItemTags()); 136 LinkedHashMap<String,Method> itemTagsExt = new LinkedHashMap<String, Method>(getItemTagsExt()); 137 138 writeTag(toWrite, new Object[] {}, itemTags, 'S'); 139 writeTag(toWrite, new Object[] {}, itemTagsExt, "_el"); 140 141 itemTags.remove('S'); 142 itemTagsExt.remove("_el"); 143 144 writeTags(toWrite, new Object[] {}, itemTags); 145 writeTags(toWrite, new Object[] {}, itemTagsExt); 146 } 147 148 @Override 149 protected <T> void writeTag(Item toWrite, Object[] param, LinkedHashMap<T, Method> tags, T tag) { 150 EncryptionDetail encryptionDetail = toWrite.getEncryptionDetailForTag(tag + ""); 151 switch (encryptionDetail) { 152 case UnencryptedOnSave: 153 super.writeTag(toWrite, param, tags, tag); 154 break; 155 case ReencryptOnSave: 156 Method toRun = tags.get(tag); 157 Class<?> declarer = toRun.getDeclaringClass(); 158 LabelResult res = Label.resolveKey(toWrite.getEncryptionLabel()); 159 if (declarer.isAssignableFrom(toWrite.getClass()) && res == LabelResult.SuccessResolveLabelToKey) { 160 SecretKey key = new SecretKeySpec(res.key, SymmetricAlgorithm); 161 try { 162 Object o = toRun.invoke(toWrite, new Object[] {}); 163 o = Conversion.ConvertToExpeditee(toRun, o); 164 if (o != null) { 165 if (o instanceof List) { 166 for (Object line: (List<?>)o) { 167 byte[] encryptedBytes = EncryptSymmetric(line.toString().getBytes(), key); 168 writeLine(tag + "E", Base64.getEncoder().encodeToString(encryptedBytes)); 169 } 170 } else { 171 byte[] encryptedBytes = EncryptSymmetric(o.toString().getBytes(), key); 172 writeLine(tag + "E", Base64.getEncoder().encodeToString(encryptedBytes)); 173 } 174 } 175 } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { 176 e.printStackTrace(); 177 } catch (IOException e) { 178 e.printStackTrace(); 103 179 } 104 180 } 105 } 106 } 107 108 // protected static byte[] resolveKeyFromLabel(String label, String framename) { 109 // String credentialsFrameName = UserSettings.ProfileName.get() + AuthenticatorBrowser.CREDENTIALS_FRAME; 110 // Frame credentialsFrame = FrameIO.LoadFrame(credentialsFrameName); 111 // Collection<Text> textItems = credentialsFrame.getTextItems(); 112 // textItems.removeIf(t -> !t.getText().equals("Secrets")); 113 // textItems.removeIf(t -> !t.hasLink()); 114 // if (textItems.isEmpty()) { 115 // MessageBay.errorMessage("Unable to find label " + label + " to encrypt frame " + framename + "."); 116 // return null; 117 // } else { 118 // Text linkToSecretsFrame = textItems.iterator().next(); 119 // Frame secretsFrame = FrameIO.LoadFrame(linkToSecretsFrame.getParent().getFramesetName() + linkToSecretsFrame.getLink()); 120 // Collection<Text> labels = secretsFrame.getTextItems(); 121 // labels.removeIf(lbl -> !lbl.getText().equals(label)); 122 // labels.removeIf(lbl -> lbl.getData() == null || lbl.getData().size() == 0); 123 // if (labels.isEmpty()) { 124 // MessageBay.errorMessage("Unable to find label " + label + " to encrypt frame " + framename + "."); 125 // return null; 126 // } 127 // 128 // Text labelItem = labels.iterator().next(); 129 // String data = labelItem.getData().get(0); 130 // if (data.contains("{")) { 131 // MessageBay.errorMessage("You only have a fraction of the required key to access " + framename + "."); 132 // return null; 133 // } else { 134 // try { 135 // return Base64.getDecoder().decode(data); 136 // } catch (IllegalArgumentException e) { 137 // MessageBay.errorMessage("Unable to create key out of data stored in label " + label + "."); 138 // return null; 139 // } 140 // } 141 // } 142 // } 181 break; 182 case UseUndecipheredValueOnSave: 183 try { 184 writeLine(tag + "E", encryptionDetail.getUndecipheredValue()); 185 } catch (IOException e) { 186 e.printStackTrace(); 187 } 188 break; 189 } 190 } 191 192 private void writeItemData(Frame frame) throws IOException { 193 // write each item in the frame 194 for (Item i : frame.getItemsToSave()) { 195 assert (!(i instanceof Line)); 196 writeItem(i); 197 } 198 199 for (final Item i: frame.getBodyItemsWithInsufficientPermissions()) { 200 assert (!(i instanceof Line)); 201 writeItem(i); 202 } 203 204 writeSurrogateItemsData(frame); 205 } 206 207 private void writeSurrogateItemsData(Frame frame) throws IOException { 208 List<Surrogate> surrogates = new ArrayList<Surrogate>(); 209 210 Consumer<? super Item> store = item -> surrogates.addAll(item.getSurrogates()); 211 frame.getItemsToSave().forEach(store); 212 frame.getBodyItemsWithInsufficientPermissions().forEach(store); 213 214 if (!surrogates.isEmpty()) { 215 // If there is some surrogates, write the surrogate 216 // terminator, then write out the surrogates. 217 writeSurrogateTerminator(); 218 219 for (Surrogate surrogate: surrogates) { 220 String output = surrogate.toString(); 221 writeLine(output); 222 } 223 } 224 } 225 226 private void writeSurrogateLineData() throws IOException { 227 List<Surrogate> surrogateLines = new ArrayList<Surrogate>(); 228 229 for(Item lineEnd: _lineEnds) { 230 List<Line> lines = lineEnd.getLines(); 231 for (Line line: lines) { 232 surrogateLines.addAll(line.getSurrogates()); 233 } 234 } 235 236 if (!surrogateLines.isEmpty()) { 237 // If there is some surrogates, write the surrogate 238 // terminator, then write out the surrogates. 239 writeSurrogateTerminator(); 240 241 for (Surrogate surrogate: surrogateLines) { 242 String output = surrogate.toString(); 243 writeLine(output); 244 } 245 } 246 } 247 248 private void writeSurrogateTerminator() throws IOException { 249 writeLine(SURROGATE_TERMINATOR + nl); 250 } 143 251 144 252 private static byte[] EncryptSymmetric(byte[] toEncrypt, SecretKey key) { … … 156 264 return null; 157 265 } 158 } 266 } 159 267 } -
trunk/src/org/expeditee/gui/Frame.java
r1407 r1408 77 77 * 78 78 */ 79 public class Frame {79 public class Frame /*implements Cloneable*/ { 80 80 81 81 /** The frame number to indicate this is a virtual frame. */ … … 187 187 public Frame() { 188 188 } 189 190 // public Frame clone() { 191 // Frame clone = new Frame(); 192 // for (Item i: this._body) { 193 // Item copy = i.copy(); 194 // copy.setID(i.getID()); 195 // clone._body.add(copy); 196 // } 197 // for(Item i: this._bodyHiddenDueToPermissions) { 198 // Item copy = i.copy(); 199 // copy.setID(i.getID()); 200 // clone._bodyHiddenDueToPermissions.add(i); 201 // } 202 // if (this._frameName != null) { 203 // clone._frameName = this._frameName.copy(); 204 // } 205 // for(Overlay key: this._overlays.keySet()) { 206 // clone._overlays.put(key, this._overlays.get(key)); 207 // } 208 // for(Vector v: this._vectors) { 209 // clone._vectors.add(v); 210 // } 211 // for (Item i: this._interactableItems) { 212 // clone._interactableItems.add(i.copy()); 213 // } 214 // for (Item i: this._overlayItems) { 215 // clone._overlayItems.add(i.copy()); 216 // } 217 // for (Item i: this._vectorItems) { 218 // clone._vectorItems.add(i.copy()); 219 // } 220 // clone._frameData = this._frameData; 221 // clone._frameset = this._frameset; 222 // clone._number = this._number; 223 // clone._version = this._version; 224 // clone._permissionTriple = this._permissionTriple; 225 // clone._owner = this._owner; 226 // clone._creationDate = this._creationDate; 227 // clone._modifiedUser = this._modifiedUser; 228 // clone._modifiedDate = this._modifiedDate; 229 // clone._modifiedDatePrecise = this._modifiedDatePrecise; 230 // clone._frozenDate = this._frozenDate; 231 // clone._background = this._background; 232 // clone._foreground = this._foreground; 233 // clone.path = this.path; 234 // clone._isLocal = this._isLocal; 235 // clone._sorted = this._sorted; 236 // clone._change = this._change; 237 // clone._saved = this._saved; 238 // clone._lineCount = this._lineCount; 239 // clone._itemCount = this._itemCount; 240 // clone._buffer = this._buffer; 241 // clone._validBuffer = this._validBuffer; 242 // clone._activeTime = (Time) this._activeTime.clone(); 243 // clone._darkTime = (Time) this._darkTime.clone(); 244 // clone._dotTemplate = this._dotTemplate.copy(); 245 // 246 // return clone; 247 // } 189 248 190 249 public boolean isReadOnly() … … 233 292 return _validBuffer; 234 293 } 235 294 236 295 private void setBufferValid(boolean newValue) 237 296 { -
trunk/src/org/expeditee/io/DefaultFrameReader.java
r1405 r1408 181 181 _DelayedItemTags.add('/'); 182 182 183 _ItemTagsExt.put("_el", Item.class.getMethod("setEncryptionLabel ", pString));183 _ItemTagsExt.put("_el", Item.class.getMethod("setEncryptionLabelOnLoad", pString)); 184 184 185 185 _ItemTagsExt.put("_ph", Text.class.getMethod("setPlaceholder", pString)); -
trunk/src/org/expeditee/io/DefaultFrameWriter.java
r1405 r1408 55 55 56 56 // keep track of methods that are put on the same line 57 pr otectedstatic LinkedHashMap<Character, Method> _ItemTags = null;57 private static LinkedHashMap<Character, Method> _ItemTags = null; 58 58 // IMPORTANT: keys in _ItemTagsExt must start with underscore as their first character 59 pr otectedstatic LinkedHashMap<String, Method> _ItemTagsExt = null;59 private static LinkedHashMap<String, Method> _ItemTagsExt = null; 60 60 61 61 protected static LinkedHashMap<Character, Method> _FrameTags = null; … … 319 319 } 320 320 321 /** 322 * @return the _ItemTags 323 */ 324 public static LinkedHashMap<Character, Method> getItemTags() { 325 return _ItemTags; 326 } 327 328 /** 329 * @return the _ItemTagsExt 330 */ 331 public static LinkedHashMap<String, Method> getItemTagsExt() { 332 return _ItemTagsExt; 333 } 334 321 335 } -
trunk/src/org/expeditee/io/ExpReader.java
r1321 r1408 48 48 public static final String EXTENTION = ".exp"; 49 49 50 pr ivateBufferedReader _reader = null;51 52 pr ivateString _frameName;50 protected BufferedReader _reader = null; 51 52 protected String _frameName; 53 53 54 54 /** … … 128 128 newFrame.setName(_frameName); 129 129 130 finalList<DelayedAction> delayedActions = new LinkedList<DelayedAction>();130 List<DelayedAction> delayedActions = new LinkedList<DelayedAction>(); 131 131 132 132 // First read all the header lines 133 while (_reader.ready() && !(next = _reader.readLine()).equals("Z")) { 134 if (isValidLine(next)) { 135 processHeaderLine(newFrame, next); 136 } 137 } 133 next = readTheHeader(newFrame); 138 134 139 135 // Now read all the items 140 Item currentItem = null; 141 while (_reader.ready() && !(next = _reader.readLine()).equals("Z")) { 142 // if this is the start of a new item add a new item 143 if (isValidLine(next)) { 144 if (getTag(next) == 'S') { 145 String value = getValue(next); 146 int id = Integer.parseInt(value.substring(2)); 147 148 switch (value.charAt(0)) { 149 case 'P': // check if its a point 150 currentItem = new Dot(id); 151 break; 152 default: 153 currentItem = new Text(id); 154 break; 155 } 156 _linePoints.put(currentItem.getID(), currentItem); 157 newFrame.addItem(currentItem); 158 } else if (currentItem != null && actionShouldBeDelayed(getTag(next))) { 159 delayedActions.add(new DelayedAction(currentItem, next)); 160 } else if (currentItem != null) { 161 processBodyLine(currentItem, next); 162 // final boolean hasSpecifiedPermission = currentItem.getPermission() != null; 163 // final boolean hasSpecifiedOwner = currentItem.getOwner() != null; 164 // if (hasSpecifiedPermission && hasSpecifiedOwner && currentItem.getPermission().getPermission(currentItem.getOwner()) == UserAppliedPermission.denied) { 165 // newFrame.removeItem(currentItem); 166 // newFrame.addItemHidden(currentItem); 167 // continue; 168 // } 169 } else { 170 System.err.println("Error while reading in frame (ExpReader): Found body line but no current item to apply it to."); 171 } 172 } 173 } 136 next = readTheItems(newFrame, delayedActions); 174 137 175 138 // Read the lines 176 while (_reader.ready() && !(next = _reader.readLine()).equals("Z")) { 177 if (isValidLine(next)) { 178 Point idtype = separateValues(next.substring(2)); 179 // The next line must be the endpoints 180 if (!_reader.ready()) { 181 throw new Exception("Unexpected end of file"); 182 } 183 next = _reader.readLine(); 184 Point startend = separateValues(next.substring(2)); 185 int start = startend.getX(); 186 int end = startend.getY(); 187 188 if (_linePoints.get(start) != null 189 && _linePoints.get(end) != null) { 190 newFrame.addItem(new Line(_linePoints.get(start), 191 _linePoints.get(end), idtype.getX())); 192 } else { 193 System.out 194 .println("Error reading line with unknown end points"); 195 } 196 } 197 } 139 next = readTheLines(newFrame); 198 140 199 141 // Read the constraints 200 while (_reader.ready() && !(next = _reader.readLine()).equals("Z")) { 201 if (isValidLine(next)) { 202 Point idtype = separateValues(next.substring(2)); 203 // The next line must be the endpoints 204 if (!_reader.ready()) { 205 throw new Exception("Unexpected end of file"); 206 } 207 next = _reader.readLine(); 208 Point startend = separateValues(next.substring(2)); 209 210 Item a = _linePoints.get(startend.getX()); 211 Item b = _linePoints.get(startend.getY()); 212 213 new Constraint(a, b, idtype.getX(), idtype.getY()); 214 } 215 } 142 next = readTheConstraints(); 216 143 217 144 for(DelayedAction action: delayedActions) { … … 220 147 221 148 // Read the stats 222 while (_reader.ready() && ((next = _reader.readLine()) != null)) { 223 if (next.startsWith(SessionStats.ACTIVE_TIME_ATTRIBUTE)) { 224 try { 225 String value = next.substring(SessionStats.ACTIVE_TIME_ATTRIBUTE.length()).trim(); 226 newFrame.setActiveTime(value); 227 } catch (Exception e) { 228 } 229 230 } else if (next.startsWith(SessionStats.DARK_TIME_ATTRIBUTE)) { 231 try { 232 String value = next.substring(SessionStats.DARK_TIME_ATTRIBUTE.length()).trim(); 233 newFrame.setDarkTime(value); 234 } catch (Exception e) { 235 } 236 237 } 238 } 149 next = readTheStats(newFrame); 239 150 240 151 } catch (Exception e) { … … 252 163 } 253 164 254 private class DelayedAction { 165 protected String readTheStats(Frame newFrame) throws IOException { 166 String next = null; 167 while (_reader.ready() && ((next = _reader.readLine()) != null)) { 168 if (next.startsWith(SessionStats.ACTIVE_TIME_ATTRIBUTE)) { 169 try { 170 String value = next.substring(SessionStats.ACTIVE_TIME_ATTRIBUTE.length()).trim(); 171 newFrame.setActiveTime(value); 172 } catch (Exception e) { 173 } 174 175 } else if (next.startsWith(SessionStats.DARK_TIME_ATTRIBUTE)) { 176 try { 177 String value = next.substring(SessionStats.DARK_TIME_ATTRIBUTE.length()).trim(); 178 newFrame.setDarkTime(value); 179 } catch (Exception e) { 180 } 181 182 } 183 } 184 return next; 185 } 186 187 protected String readTheConstraints() throws IOException, Exception { 188 String next = null; 189 while (_reader.ready() && !(next = _reader.readLine()).equals("Z")) { 190 if (isValidLine(next)) { 191 Point idtype = separateValues(next.substring(2)); 192 // The next line must be the endpoints 193 if (!_reader.ready()) { 194 throw new Exception("Unexpected end of file"); 195 } 196 next = _reader.readLine(); 197 Point startend = separateValues(next.substring(2)); 198 199 Item a = _linePoints.get(startend.getX()); 200 Item b = _linePoints.get(startend.getY()); 201 202 new Constraint(a, b, idtype.getX(), idtype.getY()); 203 } 204 } 205 return next; 206 } 207 208 protected String readTheLines(Frame newFrame) throws IOException, Exception { 209 String next = null; 210 while (_reader.ready() && !(next = _reader.readLine()).equals("Z")) { 211 if (isValidLine(next)) { 212 Point idtype = separateValues(next.substring(2)); 213 // The next line must be the endpoints 214 if (!_reader.ready()) { 215 throw new Exception("Unexpected end of file"); 216 } 217 next = _reader.readLine(); 218 Point startend = separateValues(next.substring(2)); 219 int start = startend.getX(); 220 int end = startend.getY(); 221 222 if (_linePoints.get(start) != null 223 && _linePoints.get(end) != null) { 224 newFrame.addItem(new Line(_linePoints.get(start), 225 _linePoints.get(end), idtype.getX())); 226 } else { 227 System.out 228 .println("Error reading line with unknown end points"); 229 } 230 } 231 } 232 return next; 233 } 234 235 protected String readTheItems(Frame newFrame, final List<DelayedAction> delayedActions) throws IOException { 236 String next = null; 237 Item currentItem = null; 238 while (_reader.ready() && !(next = _reader.readLine()).equals("Z")) { 239 // if this is the start of a new item add a new item 240 if (isValidLine(next)) { 241 if (getTag(next) == 'S') { 242 currentItem = newItem(next); 243 _linePoints.put(currentItem.getID(), currentItem); 244 newFrame.addItem(currentItem); 245 } else if (currentItem != null && actionShouldBeDelayed(getTag(next))) { 246 delayedActions.add(new DelayedAction(currentItem, next)); 247 } else if (currentItem != null) { 248 processBodyLine(currentItem, next); 249 } else { 250 System.err.println("Error while reading in frame (ExpReader): Found body line but no current item to apply it to."); 251 } 252 } 253 } 254 return next; 255 } 256 257 protected Item newItem(String line) { 258 Item currentItem; 259 String value = getValue(line); 260 int id = Integer.parseInt(value.substring(2)); 261 262 switch (value.charAt(0)) { 263 case 'P': // check if its a point 264 currentItem = new Dot(id); 265 break; 266 default: 267 currentItem = new Text(id); 268 break; 269 } 270 return currentItem; 271 } 272 273 protected String readTheHeader(Frame newFrame) throws IOException { 274 String next = null; 275 while (_reader.ready() && !(next = _reader.readLine()).equals("Z")) { 276 if (isValidLine(next)) { 277 processHeaderLine(newFrame, next); 278 } 279 } 280 return next; 281 } 282 283 public class DelayedAction { 255 284 private Item theItem; 256 285 private String theLine; 257 286 258 DelayedAction(final Item theItem, final String theLine) {287 public DelayedAction(final Item theItem, final String theLine) { 259 288 this.theItem = theItem; 260 289 this.theLine = theLine; 261 290 } 262 291 263 void exec() {292 public void exec() { 264 293 processBodyLine(theItem, theLine); 265 294 } … … 267 296 268 297 // Stores points used when constructing lines 269 pr ivateHashMap<Integer, Item> _linePoints = new HashMap<Integer, Item>();298 protected HashMap<Integer, Item> _linePoints = new HashMap<Integer, Item>(); 270 299 271 300 /** -
trunk/src/org/expeditee/io/ExpWriter.java
r1326 r1408 28 28 import java.lang.reflect.Method; 29 29 import java.util.Iterator; 30 import java.util.LinkedHashMap; 30 31 import java.util.LinkedList; 31 32 import java.util.List; … … 53 54 protected String _framename; 54 55 55 pr ivatestatic final char TERMINATOR = 'Z';56 protected static final char TERMINATOR = 'Z'; 56 57 57 58 public ExpWriter() { … … 87 88 try { 88 89 _FrameTags.remove('A'); 89 _ItemTags.put('S', Item.class.getMethod("getTypeAndID",90 getItemTags().put('S', Item.class.getMethod("getTypeAndID", 90 91 new Class[] {})); 91 92 } catch (Exception e) { … … 130 131 writeTerminator(); 131 132 writeLine(SessionStats.getFrameEventList(frame)); 132 133 return;134 133 } 135 134 … … 137 136 } 138 137 139 pr ivatevoid writeHeader(Frame toWrite) throws IOException {138 protected void writeHeader(Frame toWrite) throws IOException { 140 139 Iterator<Character> it = _FrameTags.keySet().iterator(); 141 140 Object[] param = {}; … … 185 184 // } 186 185 187 pr ivatevoid writeLine(String tag, String line) throws IOException {186 protected void writeLine(String tag, String line) throws IOException { 188 187 writeLine(tag + " " + line); 189 188 } … … 227 226 } 228 227 229 pr ivateList<Item> _lineEnds = new LinkedList<Item>();228 protected List<Item> _lineEnds = new LinkedList<Item>(); 230 229 231 230 // Writes out a LineEnd to the file … … 306 305 } 307 306 308 private void writeClass(Object toWrite) throws IOException { 309 Iterator<Character> it = _ItemTags.keySet().iterator(); 310 Iterator<String> itExt = _ItemTagsExt.keySet().iterator(); 311 Object[] param = {}; 312 307 protected void writeClass(Item toWrite) throws IOException { 308 writeTags(toWrite, new Object[] {}, getItemTags()); 309 writeTags(toWrite, new Object[] {}, getItemTagsExt()); 310 } 311 312 protected <T> void writeTags(Item toWrite, Object[] param, LinkedHashMap<T, Method> tags) { 313 Iterator<T> it = tags.keySet().iterator(); 313 314 while (it.hasNext()) { 314 Character tag = it.next(); 315 Method toRun = _ItemTags.get(tag); 316 Class<?> declarer = toRun.getDeclaringClass(); 317 if (declarer.isAssignableFrom(toWrite.getClass())) { 318 try { 319 Object o = toRun.invoke(toWrite, param); 320 o = Conversion.ConvertToExpeditee(toRun, o); 321 if (o != null) { 322 if (o instanceof List) { 323 for (Object line : (List) o) { 324 writeLine(tag.toString(), line.toString()); 325 } 326 } else 327 writeLine(tag.toString(), o.toString()); 328 } 329 } catch (Exception e) { 330 e.printStackTrace(); 315 T tag = it.next(); 316 writeTag(toWrite, param, tags, tag); 317 } 318 } 319 320 protected <T> void writeTag(Item toWrite, Object[] param, LinkedHashMap<T, Method> tags, T tag) { 321 Method toRun = tags.get(tag); 322 Class<?> declarer = toRun.getDeclaringClass(); 323 if (declarer.isAssignableFrom(toWrite.getClass())) { 324 try { 325 Object o = toRun.invoke(toWrite, param); 326 o = Conversion.ConvertToExpeditee(toRun, o); 327 if (o != null) { 328 if (o instanceof List) { 329 for (Object line : (List) o) { 330 writeLine(tag.toString(), line.toString()); 331 } 332 } else 333 writeLine(tag.toString(), o.toString()); 331 334 } 332 } 333 } 334 335 while (itExt.hasNext()) { 336 String tag = itExt.next(); 337 Method toRun = _ItemTagsExt.get(tag); 338 Class<?> declarer = toRun.getDeclaringClass(); 339 if (declarer.isAssignableFrom(toWrite.getClass())) { 340 try { 341 Object o = toRun.invoke(toWrite, param); 342 o = Conversion.ConvertToExpeditee(toRun, o); 343 if (o != null) { 344 if (o instanceof List) { 345 for (Object line : (List) o) { 346 writeLine(tag.toString(), line.toString()); 347 } 348 } else 349 writeLine(tag.toString(), o.toString()); 350 } 351 } catch (Exception e) { 352 e.printStackTrace(); 353 } 335 } catch (Exception e) { 336 e.printStackTrace(); 354 337 } 355 338 } -
trunk/src/org/expeditee/io/KMSWriter.java
r919 r1408 81 81 try { 82 82 _FrameTags.put('A', Frame.class.getMethod("getName", new Class[] {})); 83 _ItemTags.put('S', Item.class.getMethod("getID", new Class[] {}));83 getItemTags().put('S', Item.class.getMethod("getID", new Class[] {})); 84 84 } catch (Exception e) { 85 85 … … 258 258 259 259 private void writeClass(Object toWrite) throws IOException { 260 Iterator<Character> it = _ItemTags.keySet().iterator();260 Iterator<Character> it = getItemTags().keySet().iterator(); 261 261 Object[] param = {}; 262 262 263 263 while (it.hasNext()) { 264 264 Character tag = it.next(); 265 Method toRun = _ItemTags.get(tag);265 Method toRun = getItemTags().get(tag); 266 266 Class<?> declarer = toRun.getDeclaringClass(); 267 267 if (declarer.isAssignableFrom(toWrite.getClass())) { -
trunk/src/org/expeditee/items/Item.java
r1407 r1408 22 22 import java.util.Collection; 23 23 import java.util.ConcurrentModificationException; 24 import java.util.HashMap; 24 25 import java.util.HashSet; 25 26 import java.util.LinkedHashSet; 26 27 import java.util.LinkedList; 27 28 import java.util.List; 29 import java.util.Map; 30 import java.util.Set; 28 31 29 32 import org.expeditee.actions.Actions; … … 44 47 import org.expeditee.core.bounds.EllipticalBounds; 45 48 import org.expeditee.core.bounds.PolygonBounds; 49 import org.expeditee.encryption.items.surrogates.EncryptionDetail; 50 import org.expeditee.encryption.items.surrogates.Surrogate; 46 51 import org.expeditee.gio.EcosystemManager; 47 52 import org.expeditee.gio.GraphicsManager; … … 57 62 import org.expeditee.gui.Vector; 58 63 import org.expeditee.io.Conversion; 64 import org.expeditee.io.DefaultFrameWriter; 59 65 import org.expeditee.settings.UserSettings; 60 66 import org.expeditee.settings.legacy.LegacyFeatures; … … 312 318 private Data _data = new Data(); 313 319 320 private Item surrogateFor = null; 321 private Set<Surrogate> surrogateItems = new HashSet<Surrogate>(); 322 private Map<String, EncryptionDetail> propertyEncryption = new HashMap<String, EncryptionDetail>(); 323 314 324 /** Just calls source.duplicateOnto(dest). */ 315 325 public static void DuplicateItem(Item source, Item dest) … … 3459 3469 public void setEncryptionLabel(String label) { 3460 3470 this._encryptionLabel = label; 3471 if (this.getSurrogates().isEmpty()) { 3472 Surrogate surrogate = Surrogate.forItem(this); 3473 this.addToSurrogates(surrogate); 3474 } 3475 3476 for (Character tag: DefaultFrameWriter.getItemTags().keySet()) { 3477 if (tag == 'S') { 3478 propertyEncryption.put(tag + "", EncryptionDetail.UnencryptedOnSave); 3479 } else { 3480 propertyEncryption.put(tag + "", EncryptionDetail.ReencryptOnSave); 3481 } 3482 } 3483 3484 for (String tag: DefaultFrameWriter.getItemTagsExt().keySet()) { 3485 if (tag == "_el") { 3486 propertyEncryption.put(tag, EncryptionDetail.UnencryptedOnSave); 3487 } else { 3488 propertyEncryption.put(tag, EncryptionDetail.ReencryptOnSave); 3489 } 3490 } 3491 } 3492 3493 public void setEncryptionLabelOnLoad(String label) { 3494 this._encryptionLabel = label; 3495 3496 for (Character tag: DefaultFrameWriter.getItemTags().keySet()) { 3497 if (tag == 'S') { 3498 propertyEncryption.put(tag + "", EncryptionDetail.UnencryptedOnSave); 3499 } else { 3500 propertyEncryption.put(tag + "", EncryptionDetail.ReencryptOnSave); 3501 } 3502 } 3503 3504 for (String tag: DefaultFrameWriter.getItemTagsExt().keySet()) { 3505 if (tag == "_el") { 3506 propertyEncryption.put(tag, EncryptionDetail.UnencryptedOnSave); 3507 } else { 3508 propertyEncryption.put(tag, EncryptionDetail.ReencryptOnSave); 3509 } 3510 } 3461 3511 } 3462 3512 … … 3511 3561 return dot; 3512 3562 } 3563 3564 public Set<Surrogate> getSurrogates() { 3565 return surrogateItems; 3566 } 3567 3568 public void setAsSurrogateFor(Item classic) { 3569 this.surrogateFor = classic; 3570 } 3571 3572 public void addToSurrogates(Surrogate surrogate) { 3573 this.surrogateItems.add(surrogate); 3574 } 3575 3576 public boolean isSurrogate() { 3577 return this.surrogateFor != null; 3578 } 3579 3580 public Item getClassic() { 3581 return this.surrogateFor; 3582 } 3583 3584 public EncryptionDetail getEncryptionDetailForTag(String tag) { 3585 EncryptionDetail encryptionDetail = propertyEncryption.get(tag); 3586 3587 if (encryptionDetail == null) { 3588 return EncryptionDetail.UnencryptedOnSave; 3589 } else { 3590 return encryptionDetail; 3591 } 3592 } 3593 3594 public void setEncryptionDetailForTag(String tag, EncryptionDetail encryptionDetail) { 3595 propertyEncryption.put(tag, encryptionDetail); 3596 } 3513 3597 }
Note:
See TracChangeset
for help on using the changeset viewer.