Ignore:
Timestamp:
06/25/19 13:57:37 (5 years ago)
Author:
bln4
Message:

Changed surrogates to work the way discussed with David. EncryptedExpReader/Writer updated to work with this.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/expeditee/encryption/io/EncryptedExpReader.java

    r1409 r1413  
    2626import org.expeditee.encryption.items.surrogates.EncryptionDetail;
    2727import org.expeditee.encryption.items.surrogates.Label;
    28 import org.expeditee.encryption.items.surrogates.Surrogate;
    2928import org.expeditee.encryption.items.surrogates.Label.LabelResult;
    3029import org.expeditee.gui.Frame;
     
    3736
    3837public class EncryptedExpReader extends ExpReader implements CryptographyConstants {
     38       
    3939        private static final String ENCRYPTED_EXP_FLAG = "EncryptedExp";
    4040        private static final String labelProfile = "Profile";
     
    4343        private boolean accessDenied = false;
    4444
    45         public EncryptedExpReader(String frameName) {
    46                 super(frameName);
    47         }
    48 
    4945        public static boolean isEncryptedExpediteeFile(final String path) throws IOException {
    5046                final BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(path), "UTF-8"));
     
    5248                in.close();
    5349                return firstLine.startsWith(ENCRYPTED_EXP_FLAG);
     50        }
     51       
     52        public EncryptedExpReader(String frameName) {
     53                super(frameName);
    5454        }
    5555       
     
    132132                                continue;
    133133                        }
     134                       
    134135                        String tag = getTagEnc(next);
    135                         if (tag.equals("S")) {
     136                        if (next.startsWith("S ")) {
    136137                                currentItem = newItem(next);
    137138                                _linePoints.put(currentItem.getID(), currentItem);
    138139                                newFrame.addItem(currentItem);
    139140                                currentItem.setEncryptionDetailForTag(tag + "", EncryptionDetail.UnencryptedOnSave);
     141                        } else if (next.startsWith("SurrogateFor")) {
     142                                int parentID = Integer.parseInt(next.split(" ")[1]);
     143                                newFrame.getItemWithID(parentID).addToSurrogates(currentItem);
    140144                        } else if (currentItem != null && actionShouldBeDelayed(tag.charAt(0))) {
    141145                                delayedActions.add(new DelayedAction(currentItem, next));
     
    147151                }
    148152               
    149                 if (next.equals("Z...")) {
    150                         next = readTheSurrogateItems(newFrame, delayedActions);
     153                if (next.equals(EncryptedExpWriter.SURROGATE_TERMINATOR)) {
     154                        next = readTheItems(newFrame, delayedActions);
    151155                }
    152156               
     
    159163                String tag = getTagEnc(line);
    160164                String value = getValue(line);
     165               
     166                if (item.isSurrogate() && item.isTagInherited(tag)) {
     167                        return;
     168                }
    161169               
    162170                // Attempt to decrypt the line if necessary.
     
    192200                }
    193201        }
    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         }
    239202       
    240203        protected static String getValue(String line) {
     
    247210        }
    248211       
    249         protected static boolean isEncryptedLine(String line) {
     212        private static String getTagEnc(String line) {
     213                char charAtZero = line.charAt(0);
     214                if (charAtZero == '_') {
     215                        return line.split(" ")[0];
     216                } else {
     217                        return charAtZero + "";
     218                }
     219        }
     220               
     221        private static boolean isEncryptedLine(String line) {
    250222                if (line.startsWith("S") || line.startsWith("_el")) {
    251223                        return false;
     
    278250                }
    279251        }
    280        
     252
    281253        private class EncryptedLineReader extends BufferedReader {
    282254                private boolean noneMode = false;
Note: See TracChangeset for help on using the changeset viewer.