Changeset 6 for trunk/src/ws


Ignore:
Timestamp:
09/21/2005 10:00:46 PM (21 years ago)
Author:
dsowen
Message:

Split out the access stuff into accesslib.
New Creator interface off-loads object creation to user.

Location:
trunk/src/ws/fugue88/jpath
Files:
7 added
3 deleted
9 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/ws/fugue88/jpath/BrokenGraphException.java

    r4 r6  
    33 *
    44 * $Log$
     5 * Revision 1.2  2005/09/21 22:00:45  dsowen
     6 * Split out the access stuff into accesslib.
     7 * New Creator interface off-loads object creation to user.
     8 *
    59 * Revision 1.1  2005/08/12 19:01:46  dsowen
    610 * Feature: can automatically fill missing parts of a graph.
     
    1418public class BrokenGraphException extends Exception {
    1519
     20        public BrokenGraphException(final Context context, final Path path,
     21                        final Object parent, final PathPart child)
     22        {
     23                super("Graph broken for path '" + child + "' on object {" + parent
     24                                + "}, accessed from context " + context + " at '" + path + "'.");
     25
     26                _context = context;
     27                _path = path;
     28                _parent = parent;
     29                _child = child;
     30        }
     31
     32        private final Context _context;
     33        private final Path _path;
     34        private final Object _parent;
     35        private final PathPart _child;
    1636}
  • trunk/src/ws/fugue88/jpath/Context.java

    r5 r6  
    33 *
    44 * $Log$
     5 * Revision 1.4  2005/09/21 22:00:45  dsowen
     6 * Split out the access stuff into accesslib.
     7 * New Creator interface off-loads object creation to user.
     8 *
    59 * Revision 1.3  2005/08/19 17:51:17  dsowen
    610 * Fixed: NPEs when using accessors from paths, &c.  Included tests.
     
    2832import java.util.Map;
    2933
     34import ws.fugue88.access.Accessor;
     35import ws.fugue88.access.ListAccessor;
     36import ws.fugue88.access.MapAccessor;
     37
    3038/**
    3139 * @author dsowen
     
    4048                Context current = this;
    4149                for(Iterator i = path.parents().iterator(); i.hasNext();) {
    42                         Accessor accessor = current.getTargetAccessor((PathPart)i.next());
     50                        final PathPart part = (PathPart)i.next();
     51                        Accessor accessor = current.getTargetAccessor(part);
    4352                        Object obj = accessor.getValue();
    44                         if(obj == null) {
    45                                 if(_modifying) {
    46                                         obj = createBlank(accessor.getType());
    47                                         accessor.setValue(obj);
    48                                 } else {
    49                                         throw new BrokenGraphException();
    50                                 }
    51                         }
    52                         current = new Context(obj, _binder, _modifying);
     53                        if(obj == null)
     54                                        obj = _creator.create(accessor.getType(), current._root,
     55                                                        part);
     56                        if(obj == null)
     57                                        throw new BrokenGraphException(this, path, current._root,
     58                                                        part);
     59
     60                        accessor.setValue(obj);
     61                        current = new Context(obj, _binder, _creator);
    5362                }
    5463                return current.getTargetAccessor(path.terminal());
     
    7180        {
    7281                Object child = getTarget(path);
    73                 return new Context(child, _binder, _modifying);
     82                return new Context(child, _binder, _creator);
    7483        }
    7584
     
    7988        }
    8089
    81         Context(final Object root, final Binder binder, final boolean modifying)
     90        Context(final Object root, final Binder binder, final GraphCreator creator)
    8291        {
    8392                // NPEs
    8493                root.getClass();
    8594                binder.getClass();
     95                creator.getClass();
    8696
    8797                _root = root;
    8898                _binder = binder;
    89                 _modifying = modifying;
     99                _creator = creator;
    90100        }
    91101
     
    127137        private final Object _root;
    128138        private final Binder _binder;
    129         private final boolean _modifying;
     139        private final GraphCreator _creator;
    130140}
  • trunk/src/ws/fugue88/jpath/ContextFactory.java

    r4 r6  
    33 *
    44 * $Log$
     5 * Revision 1.3  2005/09/21 22:00:45  dsowen
     6 * Split out the access stuff into accesslib.
     7 * New Creator interface off-loads object creation to user.
     8 *
    59 * Revision 1.2  2005/08/12 19:01:46  dsowen
    610 * Feature: can automatically fill missing parts of a graph.
     
    1721public class ContextFactory {
    1822
    19         public Context createContext(final Object root)
     23        public Context createContext(final Object root, final GraphCreator creator)
    2024        {
    21                 return new Context(root, _binder, false);
    22         }
    23 
    24         public Context createModifyingContext(final Object root)
    25         {
    26                 return new Context(root, _binder, true);
     25                return new Context(root, _binder, creator);
    2726        }
    2827
  • trunk/src/ws/fugue88/jpath/Identifier.java

    r2 r6  
    33 *
    44 * $Log$
     5 * Revision 1.2  2005/09/21 22:00:46  dsowen
     6 * Split out the access stuff into accesslib.
     7 * New Creator interface off-loads object creation to user.
     8 *
    59 * Revision 1.1  2005/08/03 00:35:29  dsowen
    610 * Initial commit.
     
    2428        }
    2529
     30        public String toString()
     31        {
     32                return "/" + _name;
     33        }
     34
    2635        private final String _name;
    2736}
  • trunk/src/ws/fugue88/jpath/NumberSelector.java

    r2 r6  
    33 *
    44 * $Log$
     5 * Revision 1.2  2005/09/21 22:00:45  dsowen
     6 * Split out the access stuff into accesslib.
     7 * New Creator interface off-loads object creation to user.
     8 *
    59 * Revision 1.1  2005/08/03 00:35:29  dsowen
    610 * Initial commit.
     
    2933        }
    3034
     35        public String toString()
     36        {
     37                return "[" + _number + "]";
     38        }
     39
    3140        private final Integer _number;
    3241}
  • trunk/src/ws/fugue88/jpath/Path.java

    r5 r6  
    33 *
    44 * $Log$
     5 * Revision 1.3  2005/09/21 22:00:45  dsowen
     6 * Split out the access stuff into accesslib.
     7 * New Creator interface off-loads object creation to user.
     8 *
    59 * Revision 1.2  2005/08/19 17:51:17  dsowen
    610 * Fixed: NPEs when using accessors from paths, &c.  Included tests.
     
    1620package ws.fugue88.jpath;
    1721
    18 import java.nio.CharBuffer;
    19 import java.text.ParseException;
    2022import java.util.Collections;
    2123import java.util.Iterator;
    22 import java.util.LinkedList;
    2324import java.util.List;
    24 import java.util.regex.Matcher;
    25 import java.util.regex.Pattern;
    2625
    2726/**
     
    3029public class Path {
    3130
    32         public static String quote(final String s)
     31        Path(final List parts)
    3332        {
    34                 return s.replaceAll("'", "\\\\'");
    35         }
    36 
    37         public static String unquote(final String s)
    38         {
    39                 return s.replaceAll("\\\\'", "'");
    40         }
    41 
    42         public Path(final String path) throws ParseException
    43         {
    44                 _parts = new LinkedList();
    45 
    46                 CharSequence buff = CharBuffer.wrap(path);
    47                 do {
    48                         Matcher m;
    49                         final PathPart pp;
    50                         if((m = IDENT.matcher(buff)).find()) {
    51                                 pp = new Identifier(m.group(1));
    52                         } else if((m = NUM_SEL.matcher(buff)).find()) {
    53                                 pp = new NumberSelector(m.group(1));
    54                         } else if((m = STR_SEL.matcher(buff)).find()) {
    55                                 pp = new StringSelector(unquote(m.group(1)));
    56                         } else {
    57                                 throw new ParseException(
    58                                                 "Expected identifier or selector, found '"
    59                                                                 + (buff.length() > 40 ? buff.subSequence(0, 37)
    60                                                                                 + "..." : buff) + "'.", -1);
    61                         }
    62                         _parts.add(pp);
    63                         buff = buff.subSequence(m.end(), buff.length());
    64                 } while(buff.length() > 0);
     33                _parts = parts;
    6534        }
    6635
     
    6837        {
    6938                return new Path(_parts.subList(0, _parts.size() - 1));
     39        }
     40
     41        public Path children()
     42        {
     43                return new Path(_parts.subList(1, _parts.size()));
     44        }
     45
     46        public PathPart head()
     47        {
     48                return (PathPart)_parts.get(0);
    7049        }
    7150
     
    8059        }
    8160
    82         protected Path(final List list)
     61        public boolean isEmpty()
    8362        {
    84                 _parts = list;
     63                return _parts.isEmpty();
     64        }
     65
     66        public String toString()
     67        {
     68                final StringBuffer buff = new StringBuffer();
     69                for(final Iterator i = _parts.iterator(); i.hasNext();) {
     70                        buff.append(i.next());
     71                }
     72                return buff.toString();
    8573        }
    8674
    8775        private final List _parts;
    88 
    89         private static final String IDENT_START = "A-Za-z_";
    90         private static final String IDENT_END = IDENT_START + "0-9";
    91         private static final Pattern IDENT = Pattern.compile("^/([" + IDENT_START
    92                         + "][" + IDENT_END + "]*)");
    93 
    94         private static final String NUM_LIT = "-?[0-9]+";
    95         private static final Pattern NUM_SEL = Pattern.compile("^\\[(" + NUM_LIT
    96                         + ")\\]");
    97 
    98         private static final String STR_LIT = "'((?:[^']|\\\\')*)'";
    99         private static final Pattern STR_SEL = Pattern.compile("^\\[" + STR_LIT
    100                         + "\\]");
    10176}
  • trunk/src/ws/fugue88/jpath/PathPart.java

    r2 r6  
    33 *
    44 * $Log$
     5 * Revision 1.2  2005/09/21 22:00:45  dsowen
     6 * Split out the access stuff into accesslib.
     7 * New Creator interface off-loads object creation to user.
     8 *
    59 * Revision 1.1  2005/08/03 00:35:29  dsowen
    610 * Initial commit.
     
    1216 * @author dsowen
    1317 */
    14 public class PathPart {
     18public abstract class PathPart {
     19
     20        public abstract String toString();
    1521}
  • trunk/src/ws/fugue88/jpath/PropertyAccessor.java

    r2 r6  
    33 *
    44 * $Log$
     5 * Revision 1.2  2005/09/21 22:00:45  dsowen
     6 * Split out the access stuff into accesslib.
     7 * New Creator interface off-loads object creation to user.
     8 *
    59 * Revision 1.1  2005/08/03 00:35:29  dsowen
    610 * Initial commit.
     
    1014
    1115import java.lang.reflect.InvocationTargetException;
     16
     17import ws.fugue88.access.Accessor;
    1218
    1319/**
     
    1925        {
    2026                return _prop.getType();
     27        }
     28
     29        public void clearValue() throws InvocationTargetException
     30        {
     31                _prop.setValue(_that, null);
    2132        }
    2233
  • trunk/src/ws/fugue88/jpath/StringSelector.java

    r2 r6  
    33 *
    44 * $Log$
     5 * Revision 1.2  2005/09/21 22:00:45  dsowen
     6 * Split out the access stuff into accesslib.
     7 * New Creator interface off-loads object creation to user.
     8 *
    59 * Revision 1.1  2005/08/03 00:35:29  dsowen
    610 * Initial commit.
     
    2428        }
    2529
     30        public String toString()
     31        {
     32                return "['" + PathParser.quote(_str) + "']";
     33        }
     34
    2635        private final String _str;
    2736}
Note: See TracChangeset for help on using the changeset viewer.