Ignore:
Timestamp:
06/21/19 12:12:55 (5 years ago)
Author:
bln4
Message:

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.

Location:
trunk/src/org/expeditee/io
Files:
5 edited

Legend:

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

    r1405 r1408  
    181181                        _DelayedItemTags.add('/');
    182182                       
    183                         _ItemTagsExt.put("_el", Item.class.getMethod("setEncryptionLabel", pString));
     183                        _ItemTagsExt.put("_el", Item.class.getMethod("setEncryptionLabelOnLoad", pString));
    184184                       
    185185                        _ItemTagsExt.put("_ph", Text.class.getMethod("setPlaceholder", pString));
  • trunk/src/org/expeditee/io/DefaultFrameWriter.java

    r1405 r1408  
    5555
    5656        // keep track of methods that are put on the same line
    57         protected static LinkedHashMap<Character, Method> _ItemTags = null;
     57        private static LinkedHashMap<Character, Method> _ItemTags = null;
    5858        // IMPORTANT: keys in _ItemTagsExt must start with underscore as their first character
    59         protected static LinkedHashMap<String, Method> _ItemTagsExt = null;
     59        private static LinkedHashMap<String, Method> _ItemTagsExt = null;
    6060
    6161        protected static LinkedHashMap<Character, Method> _FrameTags = null;
     
    319319        }
    320320
     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
    321335}
  • trunk/src/org/expeditee/io/ExpReader.java

    r1321 r1408  
    4848        public static final String EXTENTION = ".exp";
    4949
    50         private BufferedReader _reader = null;
    51 
    52         private String _frameName;
     50        protected BufferedReader _reader = null;
     51
     52        protected String _frameName;
    5353
    5454        /**
     
    128128                        newFrame.setName(_frameName);
    129129
    130                         final List<DelayedAction> delayedActions = new LinkedList<DelayedAction>();
     130                        List<DelayedAction> delayedActions = new LinkedList<DelayedAction>();
    131131                       
    132132                        // 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);
    138134
    139135                        // 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);
    174137
    175138                        // 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);
    198140
    199141                        // 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();
    216143                       
    217144                        for(DelayedAction action: delayedActions) {
     
    220147
    221148                        // 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);
    239150
    240151                } catch (Exception e) {
     
    252163        }
    253164
    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 {
    255284                private Item theItem;
    256285                private String theLine;
    257286
    258                 DelayedAction(final Item theItem, final String theLine) {
     287                public DelayedAction(final Item theItem, final String theLine) {
    259288                        this.theItem = theItem;
    260289                        this.theLine = theLine;
    261290                }
    262291               
    263                 void exec() {
     292                public void exec() {
    264293                        processBodyLine(theItem, theLine);
    265294                }
     
    267296       
    268297        // Stores points used when constructing lines
    269         private HashMap<Integer, Item> _linePoints = new HashMap<Integer, Item>();
     298        protected HashMap<Integer, Item> _linePoints = new HashMap<Integer, Item>();
    270299
    271300        /**
  • trunk/src/org/expeditee/io/ExpWriter.java

    r1326 r1408  
    2828import java.lang.reflect.Method;
    2929import java.util.Iterator;
     30import java.util.LinkedHashMap;
    3031import java.util.LinkedList;
    3132import java.util.List;
     
    5354        protected String _framename;
    5455
    55         private static final char TERMINATOR = 'Z';
     56        protected static final char TERMINATOR = 'Z';
    5657
    5758        public ExpWriter() {
     
    8788                try {
    8889                        _FrameTags.remove('A');
    89                         _ItemTags.put('S', Item.class.getMethod("getTypeAndID",
     90                        getItemTags().put('S', Item.class.getMethod("getTypeAndID",
    9091                                        new Class[] {}));
    9192                } catch (Exception e) {
     
    130131                writeTerminator();
    131132                writeLine(SessionStats.getFrameEventList(frame));
    132 
    133                 return;
    134133        }
    135134
     
    137136        }
    138137
    139         private void writeHeader(Frame toWrite) throws IOException {
     138        protected void writeHeader(Frame toWrite) throws IOException {
    140139                Iterator<Character> it = _FrameTags.keySet().iterator();
    141140                Object[] param = {};
     
    185184//      }
    186185
    187         private void writeLine(String tag, String line) throws IOException {
     186        protected void writeLine(String tag, String line) throws IOException {
    188187                writeLine(tag + " " + line);
    189188        }
     
    227226        }
    228227
    229         private List<Item> _lineEnds = new LinkedList<Item>();
     228        protected List<Item> _lineEnds = new LinkedList<Item>();
    230229
    231230        // Writes out a LineEnd to the file
     
    306305        }
    307306
    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();
    313314                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());
    331334                                }
    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();
    354337                        }
    355338                }
  • trunk/src/org/expeditee/io/KMSWriter.java

    r919 r1408  
    8181                try {
    8282                        _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[] {}));
    8484                } catch (Exception e) {
    8585
     
    258258
    259259        private void writeClass(Object toWrite) throws IOException {
    260                 Iterator<Character> it = _ItemTags.keySet().iterator();
     260                Iterator<Character> it = getItemTags().keySet().iterator();
    261261                Object[] param = {};
    262262
    263263                while (it.hasNext()) {
    264264                        Character tag = it.next();
    265                         Method toRun = _ItemTags.get(tag);
     265                        Method toRun = getItemTags().get(tag);
    266266                        Class<?> declarer = toRun.getDeclaringClass();
    267267                        if (declarer.isAssignableFrom(toWrite.getClass())) {
Note: See TracChangeset for help on using the changeset viewer.