| 1 | /* |
|---|
| 2 | * Created on Aug 31, 2005 |
|---|
| 3 | * |
|---|
| 4 | * $Log$ |
|---|
| 5 | * Revision 1.1 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 | * |
|---|
| 9 | */ |
|---|
| 10 | package ws.fugue88.jpath; |
|---|
| 11 | |
|---|
| 12 | import java.nio.CharBuffer; |
|---|
| 13 | import java.text.ParseException; |
|---|
| 14 | import java.util.LinkedList; |
|---|
| 15 | import java.util.List; |
|---|
| 16 | import java.util.regex.Matcher; |
|---|
| 17 | import java.util.regex.Pattern; |
|---|
| 18 | |
|---|
| 19 | /** |
|---|
| 20 | * @author dsowen |
|---|
| 21 | */ |
|---|
| 22 | public class SimplePathParser extends PathParser { |
|---|
| 23 | |
|---|
| 24 | public Path parse(final String path) throws ParseException |
|---|
| 25 | { |
|---|
| 26 | final List parts = new LinkedList(); |
|---|
| 27 | |
|---|
| 28 | CharSequence buff = CharBuffer.wrap(path); |
|---|
| 29 | do { |
|---|
| 30 | final PathPart pp; |
|---|
| 31 | Matcher m; |
|---|
| 32 | if((m = IDENT.matcher(buff)).find()) { |
|---|
| 33 | pp = new Identifier(m.group(1)); |
|---|
| 34 | } else if((m = NUM_SEL.matcher(buff)).find()) { |
|---|
| 35 | pp = new NumberSelector(m.group(1)); |
|---|
| 36 | } else if((m = STR_SEL.matcher(buff)).find()) { |
|---|
| 37 | pp = new StringSelector(unquote(m.group(1))); |
|---|
| 38 | } else { |
|---|
| 39 | throw new ParseException( |
|---|
| 40 | "Expected identifier or selector, found '" |
|---|
| 41 | + (buff.length() > 40 ? buff.subSequence(0, 37) |
|---|
| 42 | + "..." : buff) + "'.", -1); |
|---|
| 43 | } |
|---|
| 44 | parts.add(pp); |
|---|
| 45 | buff = buff.subSequence(m.end(), buff.length()); |
|---|
| 46 | } while(buff.length() > 0); |
|---|
| 47 | |
|---|
| 48 | return new Path(parts); |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | private static final String IDENT_START = "A-Za-z_"; |
|---|
| 52 | private static final String IDENT_END = IDENT_START + "0-9"; |
|---|
| 53 | private static final Pattern IDENT = Pattern.compile("^/([" + IDENT_START |
|---|
| 54 | + "][" + IDENT_END + "]*)"); |
|---|
| 55 | |
|---|
| 56 | private static final String NUM_LIT = "-?[0-9]+"; |
|---|
| 57 | private static final Pattern NUM_SEL = Pattern.compile("^\\[(" + NUM_LIT |
|---|
| 58 | + ")\\]"); |
|---|
| 59 | |
|---|
| 60 | private static final String STR_LIT = "'((?:[^']|\\\\')*)'"; |
|---|
| 61 | private static final Pattern STR_SEL = Pattern.compile("^\\[" + STR_LIT |
|---|
| 62 | + "\\]"); |
|---|
| 63 | } |
|---|