summaryrefslogtreecommitdiff
path: root/java/gov/nist/javax/sip/parser/URLParser.java
blob: 6f2423d8de899c35a722b4f94579c753f7cfed39 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
/*
* Conditions Of Use
*
* This software was developed by employees of the National Institute of
* Standards and Technology (NIST), an agency of the Federal Government.
* Pursuant to title 15 Untied States Code Section 105, works of NIST
* employees are not subject to copyright protection in the United States
* and are considered to be in the public domain.  As a result, a formal
* license is not needed to use the software.
*
* This software is provided by NIST as a service and is expressly
* provided "AS IS."  NIST MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED
* OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT
* AND DATA ACCURACY.  NIST does not warrant or make any representations
* regarding the use of the software or the results thereof, including but
* not limited to the correctness, accuracy, reliability or usefulness of
* the software.
*
* Permission to use this software is contingent upon your acceptance
* of the terms of this agreement
*
* .
*
*/
package gov.nist.javax.sip.parser;
import gov.nist.core.HostNameParser;
import gov.nist.core.HostPort;
import gov.nist.core.NameValue;
import gov.nist.core.NameValueList;
import gov.nist.core.Token;
import gov.nist.javax.sip.address.GenericURI;
import gov.nist.javax.sip.address.SipUri;
import gov.nist.javax.sip.address.TelURLImpl;
import gov.nist.javax.sip.address.TelephoneNumber;
import java.text.ParseException;

/**
 * Parser For SIP and Tel URLs. Other kinds of URL's are handled by the
 * J2SE 1.4 URL class.
 * @version 1.2 $Revision: 1.27 $ $Date: 2009/10/22 10:27:39 $
 *
 * @author M. Ranganathan   <br/>
 *
 *
 */
public class URLParser extends Parser {

    public URLParser(String url) {
        this.lexer = new Lexer("sip_urlLexer", url);
    }

    // public tag added - issued by Miguel Freitas
    public URLParser(Lexer lexer) {
        this.lexer = lexer;
        this.lexer.selectLexer("sip_urlLexer");
    }
    protected static boolean isMark(char next) {
        switch (next) {
            case '-':
            case '_':
            case '.':
            case '!':
            case '~':
            case '*':
            case '\'':
            case '(':
            case ')':
                return true;
            default:
                return false;
        }
    }

    protected static boolean isUnreserved(char next) {
        return Lexer.isAlphaDigit(next) || isMark(next);
    }

    protected static boolean isReservedNoSlash(char next) {
        switch (next) {
            case ';':
            case '?':
            case ':':
            case '@':
            case '&':
            case '+':
            case '$':
            case ',':
                return true;
            default:
                return false;
        }
    }

    // Missing '=' bug in character set - discovered by interop testing
    // at SIPIT 13 by Bob Johnson and Scott Holben.
    // change . to ; by Bruno Konik
    protected static boolean isUserUnreserved(char la) {
        switch (la) {
            case '&':
            case '?':
            case '+':
            case '$':
            case '#':
            case '/':
            case ',':
            case ';':
            case '=':
                return true;
            default:
                return false;
        }
    }

    protected String unreserved() throws ParseException {
        char next = lexer.lookAhead(0);
        if (isUnreserved(next)) {
            lexer.consume(1);
            return String.valueOf(next);
        } else
            throw createParseException("unreserved");

    }

    /** Name or value of a parameter.
     */
    protected String paramNameOrValue() throws ParseException {
        int startIdx = lexer.getPtr();
        while (lexer.hasMoreChars()) {
            char next = lexer.lookAhead(0);
            boolean isValidChar = false;
            switch (next) {
                case '[':
                case ']':// JvB: fixed this one
                case '/':
                case ':':
                case '&':
                case '+':
                case '$':
                    isValidChar = true;
            }
            if (isValidChar || isUnreserved(next)) {
                lexer.consume(1);
            } else if (isEscaped()) {
                lexer.consume(3);
            } else
                break;
        }
        return lexer.getBuffer().substring(startIdx, lexer.getPtr());
    }

    private NameValue uriParam() throws ParseException {
        if (debug)
            dbg_enter("uriParam");
        try {
            String pvalue = "";
            String pname = paramNameOrValue();
            char next = lexer.lookAhead(0);
            boolean isFlagParam = true;
            if (next == '=') {
                lexer.consume(1);
                pvalue = paramNameOrValue();
                isFlagParam = false;
            }
            if (pname.length() == 0 &&
                ( pvalue == null ||
                pvalue.length() == 0))
                return null;
            else return new NameValue(pname, pvalue, isFlagParam);
        } finally {
            if (debug)
                dbg_leave("uriParam");
        }
    }

    protected static boolean isReserved(char next) {
        switch (next) {
            case ';':
            case '/':
            case '?':
            case ':':
            case '=': // Bug fix by Bruno Konik
            case '@':
            case '&':
            case '+':
            case '$':
            case ',':
                return true;
            default:
                return false;
        }
    }

    protected String reserved() throws ParseException {
        char next = lexer.lookAhead(0);
        if (isReserved(next)) {
            lexer.consume(1);
            return new StringBuffer().append(next).toString();
        } else
            throw createParseException("reserved");
    }

    protected boolean isEscaped() {
        try {
            return lexer.lookAhead(0) == '%' &&
                Lexer.isHexDigit(lexer.lookAhead(1)) &&
                Lexer.isHexDigit(lexer.lookAhead(2));
        } catch (Exception ex) {
            return false;
        }
    }

    protected String escaped() throws ParseException {
        if (debug)
            dbg_enter("escaped");
        try {
            StringBuffer retval = new StringBuffer();
            char next = lexer.lookAhead(0);
            char next1 = lexer.lookAhead(1);
            char next2 = lexer.lookAhead(2);
            if (next == '%'
                && Lexer.isHexDigit(next1)
                && Lexer.isHexDigit(next2)) {
                lexer.consume(3);
                retval.append(next);
                retval.append(next1);
                retval.append(next2);
            } else
                throw createParseException("escaped");
            return retval.toString();
        } finally {
            if (debug)
                dbg_leave("escaped");
        }
    }

    protected String mark() throws ParseException {
        if (debug)
            dbg_enter("mark");
        try {
            char next = lexer.lookAhead(0);
            if (isMark(next)) {
                lexer.consume(1);
                return new String( new char[]{next} );
            } else
                throw createParseException("mark");
        } finally {
            if (debug)
                dbg_leave("mark");
        }
    }

    protected String uric() {
        if (debug)
            dbg_enter("uric");
        try {
            try {
                char la = lexer.lookAhead(0);
                if (isUnreserved(la)) {
                    lexer.consume(1);
                    return Lexer.charAsString(la);
                } else if (isReserved(la)) {
                    lexer.consume(1);
                    return Lexer.charAsString(la);
                } else if (isEscaped()) {
                    String retval = lexer.charAsString(3);
                    lexer.consume(3);
                    return retval;
                } else
                    return null;
            } catch (Exception ex) {
                return null;
            }
        } finally {
            if (debug)
                dbg_leave("uric");
        }

    }

    protected String uricNoSlash() {
        if (debug)
            dbg_enter("uricNoSlash");
        try {
            try {
                char la = lexer.lookAhead(0);
                if (isEscaped()) {
                    String retval = lexer.charAsString(3);
                    lexer.consume(3);
                    return retval;
                } else if (isUnreserved(la)) {
                    lexer.consume(1);
                    return Lexer.charAsString(la);
                } else if (isReservedNoSlash(la)) {
                    lexer.consume(1);
                    return Lexer.charAsString(la);
                } else
                    return null;
            } catch (ParseException ex) {
                return null;
            }
        } finally {
            if (debug)
                dbg_leave("uricNoSlash");
        }
    }

    protected String uricString() throws ParseException {
        StringBuffer retval = new StringBuffer();
        while (true) {
            String next = uric();
            if (next == null) {
                char la = lexer.lookAhead(0);
                // JvB: allow IPv6 addresses in generic URI strings
                // e.g. http://[::1]
                if ( la == '[' ) {
                    HostNameParser hnp = new HostNameParser(this.getLexer());
                    HostPort hp = hnp.hostPort( false );
                    retval.append(hp.toString());
                    continue;
                }
                break;
            }
            retval.append(next);
        }
        return retval.toString();
    }

    /**
     * Parse and return a structure for a generic URL.
     * Note that non SIP URLs are just stored as a string (not parsed).
     * @return URI is a URL structure for a SIP url.
     * @throws ParseException if there was a problem parsing.
     */
    public GenericURI uriReference( boolean inBrackets ) throws ParseException {
        if (debug)
            dbg_enter("uriReference");
        GenericURI retval = null;
        Token[] tokens = lexer.peekNextToken(2);
        Token t1 = (Token) tokens[0];
        Token t2 = (Token) tokens[1];
        try {

            if (t1.getTokenType() == TokenTypes.SIP ||
                    t1.getTokenType() == TokenTypes.SIPS) {
                if (t2.getTokenType() == ':')
                    retval = sipURL( inBrackets );
                else
                    throw createParseException("Expecting \':\'");
            } else if (t1.getTokenType() == TokenTypes.TEL) {
                if (t2.getTokenType() == ':') {
                    retval = telURL( inBrackets );
                } else
                    throw createParseException("Expecting \':\'");
            } else {
                String urlString = uricString();
                try {
                    retval = new GenericURI(urlString);
                } catch (ParseException ex) {
                    throw createParseException(ex.getMessage());
                }
            }
        } finally {
            if (debug)
                dbg_leave("uriReference");
        }
        return retval;
    }

    /**
     * Parser for the base phone number.
     */
    private String base_phone_number() throws ParseException {
        StringBuffer s = new StringBuffer();

        if (debug)
            dbg_enter("base_phone_number");
        try {
            int lc = 0;
            while (lexer.hasMoreChars()) {
                char w = lexer.lookAhead(0);
                if (Lexer.isDigit(w)
                    || w == '-'
                    || w == '.'
                    || w == '('
                    || w == ')') {
                    lexer.consume(1);
                    s.append(w);
                    lc++;
                } else if (lc > 0)
                    break;
                else
                    throw createParseException("unexpected " + w);
            }
            return s.toString();
        } finally {
            if (debug)
                dbg_leave("base_phone_number");
        }

    }

    /**
     * Parser for the local phone #.
     */
    private String local_number() throws ParseException {
        StringBuffer s = new StringBuffer();
        if (debug)
            dbg_enter("local_number");
        try {
            int lc = 0;
            while (lexer.hasMoreChars()) {
                char la = lexer.lookAhead(0);
                if (la == '*'
                    || la == '#'
                    || la == '-'
                    || la == '.'
                    || la == '('
                    || la == ')'
                        // JvB: allow 'A'..'F', should be uppercase
                    || Lexer.isHexDigit(la)) {
                    lexer.consume(1);
                    s.append(la);
                    lc++;
                } else if (lc > 0)
                    break;
                else
                    throw createParseException("unexepcted " + la);
            }
            return s.toString();
        } finally {
            if (debug)
                dbg_leave("local_number");
        }

    }

    /**
     * Parser for telephone subscriber.
     *
     * @return the parsed telephone number.
     */
    public final TelephoneNumber parseTelephoneNumber( boolean inBrackets ) 
    	throws ParseException {
        TelephoneNumber tn;

        if (debug)
            dbg_enter("telephone_subscriber");
        lexer.selectLexer("charLexer");
        try {
            char c = lexer.lookAhead(0);
            if (c == '+')
                tn = global_phone_number( inBrackets );
            else if (
                Lexer.isHexDigit(c)// see RFC3966
                    || c == '#'
                    || c == '*'
                    || c == '-'
                    || c == '.'
                    || c == '('
                    || c == ')' ) {
                tn = local_phone_number( inBrackets );
            } else
                throw createParseException("unexpected char " + c);
            return tn;
        } finally {
            if (debug)
                dbg_leave("telephone_subscriber");
        }

    }

    private final TelephoneNumber global_phone_number( boolean inBrackets ) throws ParseException {
        if (debug)
            dbg_enter("global_phone_number");
        try {
            TelephoneNumber tn = new TelephoneNumber();
            tn.setGlobal(true);
            NameValueList nv = null;
            this.lexer.match(PLUS);
            String b = base_phone_number();
            tn.setPhoneNumber(b);
            if (lexer.hasMoreChars()) {
                char tok = lexer.lookAhead(0);
                if (tok == ';' && inBrackets) {
                    this.lexer.consume(1);
                    nv = tel_parameters();
                    tn.setParameters(nv);
                }
            }
            return tn;
        } finally {
            if (debug)
                dbg_leave("global_phone_number");
        }
    }

    private TelephoneNumber local_phone_number( boolean inBrackets ) throws ParseException {
        if (debug)
            dbg_enter("local_phone_number");
        TelephoneNumber tn = new TelephoneNumber();
        tn.setGlobal(false);
        NameValueList nv = null;
        String b = null;
        try {
            b = local_number();
            tn.setPhoneNumber(b);
            if (lexer.hasMoreChars()) {
                Token tok = this.lexer.peekNextToken();
                switch (tok.getTokenType()) {
                    case SEMICOLON:
                        {
                        	if (inBrackets) {
                        		this.lexer.consume(1);
                        		nv = tel_parameters();
                        		tn.setParameters(nv);
                        	}
                            break;
                        }
                    default :
                        {
                            break;
                        }
                }
            }
        } finally {
            if (debug)
                dbg_leave("local_phone_number");
        }
        return tn;
    }

    private NameValueList tel_parameters() throws ParseException {
        NameValueList nvList = new NameValueList();

        // JvB: Need to handle 'phone-context' specially
        // 'isub' (or 'ext') MUST appear first, but we accept any order here
        NameValue nv;
        while ( true ) {
            String pname = paramNameOrValue();

            // Handle 'phone-context' specially, it may start with '+'
            if ( pname.equalsIgnoreCase("phone-context")) {
                nv = phone_context();
            } else {
                if (lexer.lookAhead(0) == '=') {
                    lexer.consume(1);
                    String value = paramNameOrValue();
                    nv = new NameValue( pname, value, false );
                } else {
                    nv = new NameValue( pname, "", true );// flag param
                }
            }
            nvList.set( nv );

            if ( lexer.lookAhead(0) == ';' ) {
                lexer.consume(1);
            } else {
                return nvList;
            }
        }

    }

    /**
     * Parses the 'phone-context' parameter in tel: URLs
     * @throws ParseException
     */
    private NameValue phone_context() throws ParseException {
        lexer.match('=');

        char la = lexer.lookAhead(0);
        Object value;
        if (la=='+') {// global-number-digits
            lexer.consume(1);// skip '+'
            value = "+" + base_phone_number();
        } else if ( Lexer.isAlphaDigit(la) ) {
            Token t = lexer.match( Lexer.ID );// more broad than allowed
            value = t.getTokenValue();
        } else {
            throw new ParseException( "Invalid phone-context:" + la , -1 );
        }
        return new NameValue( "phone-context", value, false );
    }

    /**
     * Parse and return a structure for a Tel URL.
     * @return a parsed tel url structure.
     */
    public TelURLImpl telURL( boolean inBrackets ) throws ParseException {
        lexer.match(TokenTypes.TEL);
        lexer.match(':');
        TelephoneNumber tn = this.parseTelephoneNumber(inBrackets);
        TelURLImpl telUrl = new TelURLImpl();
        telUrl.setTelephoneNumber(tn);
        return telUrl;

    }

    /**
     * Parse and return a structure for a SIP URL.
     * @return a URL structure for a SIP url.
     * @throws ParseException if there was a problem parsing.
     */
    public SipUri sipURL( boolean inBrackets ) throws ParseException {
        if (debug)
            dbg_enter("sipURL");
        SipUri retval = new SipUri();
        // pmusgrave - handle sips case
        Token nextToken = lexer.peekNextToken();
        int sipOrSips = TokenTypes.SIP;
        String scheme = TokenNames.SIP;
        if ( nextToken.getTokenType() == TokenTypes.SIPS)
        {
            sipOrSips = TokenTypes.SIPS;
            scheme = TokenNames.SIPS;
        }

        try {
            lexer.match(sipOrSips);
            lexer.match(':');
            retval.setScheme(scheme);
            int startOfUser = lexer.markInputPosition();
            String userOrHost = user();// Note: user may contain ';', host may not...
            String passOrPort = null;

            // name:password or host:port
            if ( lexer.lookAhead() == ':' ) {
                lexer.consume(1);
                passOrPort = password();
            }

            // name@hostPort
            if ( lexer.lookAhead() == '@' ) {
                lexer.consume(1);
                retval.setUser( userOrHost );
                if (passOrPort!=null) retval.setUserPassword( passOrPort );
            } else {
                // then userOrHost was a host, backtrack just in case a ';' was eaten...
                lexer.rewindInputPosition( startOfUser );
            }

            HostNameParser hnp = new HostNameParser(this.getLexer());
            HostPort hp = hnp.hostPort( false );
            retval.setHostPort(hp);

            lexer.selectLexer("charLexer");
            while (lexer.hasMoreChars()) {
            	// If the URI is not enclosed in brackets, parameters belong to header
                if (lexer.lookAhead(0) != ';' || !inBrackets)
                    break;
                lexer.consume(1);
                NameValue parms = uriParam();
                if (parms != null) retval.setUriParameter(parms);
            }

            if (lexer.hasMoreChars() && lexer.lookAhead(0) == '?') {
                lexer.consume(1);
                while (lexer.hasMoreChars()) {
                    NameValue parms = qheader();
                    retval.setQHeader(parms);
                    if (lexer.hasMoreChars() && lexer.lookAhead(0) != '&')
                        break;
                    else
                        lexer.consume(1);
                }
            }
            return retval;
        // BEGIN android-added
        } catch (RuntimeException e) {
            throw new ParseException("Invalid URL: " + lexer.getBuffer(), -1);
        // END android-added
        } finally {
            if (debug)
                dbg_leave("sipURL");
        }
    }

    public String peekScheme() throws ParseException {
        Token[] tokens = lexer.peekNextToken(1);
        if (tokens.length == 0)
            return null;
        String scheme = ((Token) tokens[0]).getTokenValue();
        return scheme;
    }

    /**
     * Get a name value for a given query header (ie one that comes
     * after the ?).
     */
    protected NameValue qheader() throws ParseException {
        String name = lexer.getNextToken('=');
        lexer.consume(1);
        String value = hvalue();
        return new NameValue(name, value, false);

    }

    protected String hvalue() throws ParseException {
        StringBuffer retval = new StringBuffer();
        while (lexer.hasMoreChars()) {
            char la = lexer.lookAhead(0);
            // Look for a character that can terminate a URL.
            boolean isValidChar = false;
            switch (la) {
                case '+':
                case '?':
                case ':':
                case '[':
                case ']':
                case '/':
                case '$':
                case '_':
                case '-':
                case '"':
                case '!':
                case '~':
                case '*':
                case '.':
                case '(':
                case ')':
                    isValidChar = true;
            }
            if (isValidChar || Lexer.isAlphaDigit(la)) {
                lexer.consume(1);
                retval.append(la);
            } else if (la == '%') {
                retval.append(escaped());
            } else
                break;
        }
        return retval.toString();
    }

    /**
     * Scan forward until you hit a terminating character for a URL.
     * We do not handle non sip urls in this implementation.
     * @return the string that takes us to the end of this URL (i.e. to
     * the next delimiter).
     */
    protected String urlString() throws ParseException {
        StringBuffer retval = new StringBuffer();
        lexer.selectLexer("charLexer");

        while (lexer.hasMoreChars()) {
            char la = lexer.lookAhead(0);
            // Look for a character that can terminate a URL.
            if (la == ' '
                || la == '\t'
                || la == '\n'
                || la == '>'
                || la == '<')
                break;
            lexer.consume(0);
            retval.append(la);
        }
        return retval.toString();
    }

    protected String user() throws ParseException {
        if (debug)
            dbg_enter("user");
        try {
            int startIdx = lexer.getPtr();
            while (lexer.hasMoreChars()) {
                char la = lexer.lookAhead(0);
                if (isUnreserved(la) || isUserUnreserved(la)) {
                    lexer.consume(1);
                } else if (isEscaped()) {
                    lexer.consume(3);
                } else
                    break;
            }
            return lexer.getBuffer().substring(startIdx, lexer.getPtr());
        } finally {
            if (debug)
                dbg_leave("user");
        }

    }

    protected String password() throws ParseException {
        int startIdx = lexer.getPtr();
        while (true) {
            char la = lexer.lookAhead(0);
            boolean isValidChar = false;
            switch (la) {
                case '&':
                case '=':
                case '+':
                case '$':
                case ',':
                    isValidChar = true;
            }
            if (isValidChar || isUnreserved(la)) {
                lexer.consume(1);
            } else if (isEscaped()) {
                lexer.consume(3); // bug reported by
                                // Jeff Haynie
            } else
                break;

        }
        return lexer.getBuffer().substring(startIdx, lexer.getPtr());
    }

    /**
     * Default parse method. This method just calls uriReference.
     */
    public GenericURI parse() throws ParseException {
        return uriReference( true );
    }

    // quick test routine for debugging type assignment
    public static void main(String[] args) throws ParseException
    {
        // quick test for sips parsing
        String[] test = { "sip:alice@example.com",
                    "sips:alice@examples.com" ,
                    "sip:3Zqkv5dajqaaas0tCjCxT0xH2ZEuEMsFl0xoasip%3A%2B3519116786244%40siplab.domain.com@213.0.115.163:7070"};

        for ( int i = 0; i < test.length; i++)
        {
            URLParser p  = new URLParser(test[i]);

                GenericURI uri = p.parse();
                System.out.println("uri type returned " + uri.getClass().getName());
                System.out.println(test[i] + " is SipUri? " + uri.isSipURI()
                        + ">" + uri.encode());
        }
    }

    /**

    **/
}