1package gov.nist.javax.sip.parser.extensions;
2
3import gov.nist.javax.sip.header.*;
4import gov.nist.javax.sip.header.extensions.*;
5import gov.nist.javax.sip.parser.*;
6
7import java.text.ParseException;
8
9// Parser for Replaces Header (RFC3891)
10// Extension by pmusgrave
11//
12// Replaces        = "Replaces" HCOLON callid *(SEMI replaces-param)
13// replaces-param  = to-tag / from-tag / early-flag / generic-param
14// to-tag          = "to-tag" EQUAL token
15// from-tag        = "from-tag" EQUAL token
16// early-flag      = "early-only"
17//
18// TODO Should run a test case on early-only
19//
20
21public class ReplacesParser extends ParametersParser {
22
23    /**
24     * Creates new CallIDParser
25     * @param callID message to parse
26     */
27    public ReplacesParser(String callID) {
28        super(callID);
29    }
30
31    /**
32     * Constructor
33     * @param lexer Lexer to set
34     */
35    protected ReplacesParser(Lexer lexer) {
36        super(lexer);
37    }
38
39    /**
40     * parse the String message
41     * @return SIPHeader (CallID object)
42     * @throws ParseException if the message does not respect the spec.
43     */
44    public SIPHeader parse() throws ParseException {
45        if (debug)
46            dbg_enter("parse");
47        try {
48            headerName(TokenTypes.REPLACES_TO);
49
50            Replaces replaces = new Replaces();
51            this.lexer.SPorHT();
52            String callId = lexer.byteStringNoSemicolon();
53            this.lexer.SPorHT();
54            super.parse(replaces);
55            replaces.setCallId(callId);
56            return replaces;
57        } finally {
58            if (debug)
59                dbg_leave("parse");
60        }
61    }
62
63    public static void main(String args[]) throws ParseException {
64        String to[] =
65            {   "Replaces: 12345th5z8z\n",
66                "Replaces: 12345th5z8z;to-tag=tozght6-45;from-tag=fromzght789-337-2\n",
67            };
68
69        for (int i = 0; i < to.length; i++) {
70            ReplacesParser tp = new ReplacesParser(to[i]);
71            Replaces t = (Replaces) tp.parse();
72            System.out.println("Parsing => " + to[i]);
73            System.out.print("encoded = " + t.encode() + "==> ");
74            System.out.println("callId " + t.getCallId() + " from-tag=" + t.getFromTag()
75                    + " to-tag=" + t.getToTag()) ;
76
77        }
78    }
79
80}
81