KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ziclix > python > sql > util > PyArgParser


1 /*
2  * Jython Database Specification API 2.0
3  *
4  * $Id: PyArgParser.java,v 1.2 2005/02/23 04:26:21 bzimmer Exp $
5  *
6  * Copyright (c) 2001 brian zimmer <bzimmer@ziclix.com>
7  *
8  */

9 package com.ziclix.python.sql.util;
10
11 import org.python.core.Py;
12 import org.python.core.PyObject;
13
14 import java.util.HashMap JavaDoc;
15 import java.util.Map JavaDoc;
16
17 /**
18  * Parse the args and kws for a method call.
19  *
20  * @author brian zimmer
21  * @version $Revision: 1.2 $
22  */

23 public class PyArgParser extends Object JavaDoc {
24
25     /**
26      * Field keywords
27      */

28     protected Map JavaDoc keywords;
29
30     /**
31      * Field arguments
32      */

33     protected PyObject[] arguments;
34
35     /**
36      * Construct a parser with the arguments and keywords.
37      */

38     public PyArgParser(PyObject[] args, String JavaDoc[] kws) {
39
40         this.keywords = new HashMap JavaDoc();
41         this.arguments = null;
42
43         parse(args, kws);
44     }
45
46     /**
47      * Method parse
48      *
49      * @param args
50      * @param kws
51      */

52     protected void parse(PyObject[] args, String JavaDoc[] kws) {
53
54         // walk backwards through the kws and build the map
55
int largs = args.length;
56
57         if (kws != null) {
58             for (int i = kws.length - 1; i >= 0; i--) {
59                 keywords.put(kws[i], args[--largs]);
60             }
61         }
62
63         this.arguments = new PyObject[largs];
64
65         System.arraycopy(args, 0, this.arguments, 0, largs);
66     }
67
68     /**
69      * How many keywords?
70      */

71     public int numKw() {
72         return this.keywords.keySet().size();
73     }
74
75     /**
76      * Does the keyword exist?
77      */

78     public boolean hasKw(String JavaDoc kw) {
79         return this.keywords.containsKey(kw);
80     }
81
82     /**
83      * Return the value for the keyword, raise a KeyError if the keyword does
84      * not exist.
85      */

86     public PyObject kw(String JavaDoc kw) {
87
88         if (!hasKw(kw)) {
89             throw Py.KeyError(kw);
90         }
91
92         return (PyObject) this.keywords.get(kw);
93     }
94
95     /**
96      * Return the value for the keyword, return the default if the keyword does
97      * not exist.
98      */

99     public PyObject kw(String JavaDoc kw, PyObject def) {
100
101         if (!hasKw(kw)) {
102             return def;
103         }
104
105         return (PyObject) this.keywords.get(kw);
106     }
107
108     /**
109      * Get the array of keywords.
110      */

111     public String JavaDoc[] kws() {
112         return (String JavaDoc[]) this.keywords.keySet().toArray(new String JavaDoc[0]);
113     }
114
115     /**
116      * Get the number of arguments.
117      */

118     public int numArg() {
119         return this.arguments.length;
120     }
121
122     /**
123      * Return the argument at the given index, raise an IndexError if out of range.
124      */

125     public PyObject arg(int index) {
126
127         if ((index >= 0) && (index <= this.arguments.length - 1)) {
128             return this.arguments[index];
129         }
130
131         throw Py.IndexError("index out of range");
132     }
133
134     /**
135      * Return the argument at the given index, or the default if the index is out of range.
136      */

137     public PyObject arg(int index, PyObject def) {
138
139         if ((index >= 0) && (index <= this.arguments.length - 1)) {
140             return this.arguments[index];
141         }
142
143         return def;
144     }
145 }
146
Popular Tags