Ignore:
Timestamp:
06/26/19 16:02:00 (5 years ago)
Author:
bln4
Message:

More amendments to the way surrogates are handled. Including renaming classic to primary.

File:
1 edited

Legend:

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

    r1413 r1414  
    1414import java.util.Base64;
    1515import java.util.List;
     16import java.util.function.BiConsumer;
    1617import java.util.function.Predicate;
    1718
     
    3031import org.expeditee.gui.FrameIO;
    3132import org.expeditee.io.Conversion;
     33import org.expeditee.io.DefaultFrameWriter;
    3234import org.expeditee.io.ExpReader;
    3335import org.expeditee.items.Item;
     
    122124        }
    123125       
    124         @Override
     126//      @Override
     127//      protected String readTheItems(Frame newFrame, List<DelayedAction> delayedActions) throws IOException {
     128//              String next = null;
     129//              Item currentItem = null;
     130//             
     131//              Predicate<String> endOfSection = s -> s.equals(EncryptedExpWriter.TERMINATOR + "") || s.equals(EncryptedExpWriter.TERMINATOR_WITH_CONTINUATION);
     132//              while (_reader.ready() && !endOfSection.test(next = _reader.readLine())) {
     133//                      if (!isValidLine(next)) {
     134//                              continue;
     135//                      }
     136//                     
     137//                      String tag = getTagEnc(next);
     138//                      if (next.startsWith(DefaultFrameWriter.TYPE_AND_ID_STR + " ")) {
     139//                              currentItem = newItem(next);
     140//                              _linePoints.put(currentItem.getID(), currentItem);
     141//                              newFrame.addItem(currentItem);
     142//                              currentItem.setEncryptionDetailForTag(tag + "", EncryptionDetail.UnencryptedOnSave);
     143//                      } else if (next.startsWith("SurrogateFor")) {
     144//                              int parentID = Integer.parseInt(next.split(" ")[1]);
     145//                              newFrame.getItemWithID(parentID).addToSurrogates(currentItem);
     146//                              newFrame.addToSurrogates(currentItem);
     147//                      } else if (currentItem != null && actionShouldBeDelayed(tag.charAt(0))) {
     148//                              delayedActions.add(new DelayedAction(currentItem, next));
     149//                      } else if (currentItem != null) {
     150//                              processBodyLine(currentItem, next);
     151//                      } else {
     152//                              System.err.println("Error while reading in frame (ExpReader): Found body line but no current item to apply it to.");
     153//                      }
     154//              }
     155//                             
     156//              if (next.equals(EncryptedExpWriter.TERMINATOR_WITH_CONTINUATION)) {
     157//                      next = readTheItems(newFrame, delayedActions);
     158//              }
     159//             
     160//              return next;
     161//      }
     162       
    125163        protected String readTheItems(Frame newFrame, List<DelayedAction> delayedActions) throws IOException {
     164                // Read the primary item.
     165                BiConsumer<Item, String> addToBody = (item, line) -> newFrame.addItem(item);
     166                String next = readLineAfterLine(false, addToBody, delayedActions);
     167               
     168                // Read the surrogate items.
     169                if (next.equals(EncryptedExpWriter.TERMINATOR_WITH_CONTINUATION)) {
     170                        BiConsumer<Item, String> addToSurrogates = (item, line) -> {
     171                                int parentID = Integer.parseInt(line.split(" ")[1]);
     172                                newFrame.getItemWithID(parentID).addToSurrogates(item);
     173                                newFrame.addToSurrogates(item);
     174                        };
     175                        next = readLineAfterLine(true, addToSurrogates, delayedActions);
     176                }
     177                return next;
     178        }
     179       
     180        private String readLineAfterLine(boolean isSurrogate, BiConsumer<Item, String> storeResult, List<DelayedAction> delayedActions) throws IOException {
    126181                String next = null;
    127182                Item currentItem = null;
    128183               
    129                 Predicate<String> endOfSection = s -> s.equals("Z") || s.equals("Z...");
     184                Predicate<String> endOfSection = s -> s.equals(EncryptedExpWriter.TERMINATOR + "") || s.equals(EncryptedExpWriter.TERMINATOR_WITH_CONTINUATION);
    130185                while (_reader.ready() && !endOfSection.test(next = _reader.readLine())) {
    131186                        if (!isValidLine(next)) {
     
    134189                       
    135190                        String tag = getTagEnc(next);
    136                         if (next.startsWith("S ")) {
     191                        if (next.startsWith(DefaultFrameWriter.TYPE_AND_ID_STR + " ")) {
    137192                                currentItem = newItem(next);
    138193                                _linePoints.put(currentItem.getID(), currentItem);
    139                                 newFrame.addItem(currentItem);
     194                                if (!isSurrogate) {
     195                                        storeResult.accept(currentItem, next);
     196                                }
    140197                                currentItem.setEncryptionDetailForTag(tag + "", EncryptionDetail.UnencryptedOnSave);
    141198                        } else if (next.startsWith("SurrogateFor")) {
    142                                 int parentID = Integer.parseInt(next.split(" ")[1]);
    143                                 newFrame.getItemWithID(parentID).addToSurrogates(currentItem);
     199                                if (isSurrogate) {
     200                                        storeResult.accept(currentItem, next);
     201                                }
    144202                        } else if (currentItem != null && actionShouldBeDelayed(tag.charAt(0))) {
    145203                                delayedActions.add(new DelayedAction(currentItem, next));
     
    151209                }
    152210               
    153                 if (next.equals(EncryptedExpWriter.SURROGATE_TERMINATOR)) {
    154                         next = readTheItems(newFrame, delayedActions);
    155                 }
    156                
    157211                return next;
    158212        }
     213       
     214//      private String readLineAfterLine(Consumer<Item> storeResult, List<DelayedAction> delayedActions) throws IOException {
     215//              String next = null;
     216//              Item currentItem = null;
     217//             
     218//              Predicate<String> endOfSection = s -> {
     219//                      return s.equals(EncryptedExpWriter.TERMINATOR + "") || s.equals(EncryptedExpWriter.TERMINATOR_WITH_CONTINUATION);
     220//              };
     221//              while (_reader.ready() && !endOfSection.test(next = _reader.readLine())) {
     222//                      if (!isValidLine(next)) {
     223//                              continue;
     224//                      }
     225//                     
     226//                      String tag = getTagEnc(next);
     227//                      if (next.startsWith(DefaultFrameWriter.TYPE_AND_ID_STR + " ")) {
     228//                              currentItem = newItem(next);
     229//                              _linePoints.put(currentItem.getID(), currentItem);
     230//                              currentItem.setEncryptionDetailForTag(tag + "", EncryptionDetail.UnencryptedOnSave);
     231//                              storeResult.accept(currentItem);
     232//                      } else if (next.startsWith("SurrogateFor")) {
     233//                              int parentID = Integer.parseInt(next.split(" ")[1]);
     234//                              newFrame.getItemWithID(parentID).addToSurrogates(currentItem);
     235//                              new Delayed
     236//                      }
     237//              }
     238//             
     239//              return next;
     240//      }
    159241       
    160242        @Override
Note: See TracChangeset for help on using the changeset viewer.