001: /*
002: * Jython Database Specification API 2.0
003: *
004: * $Id: PyArgParser.java 2414 2005-02-23 04:26:23Z bzimmer $
005: *
006: * Copyright (c) 2001 brian zimmer <bzimmer@ziclix.com>
007: *
008: */
009: package com.ziclix.python.sql.util;
010:
011: import org.python.core.Py;
012: import org.python.core.PyObject;
013:
014: import java.util.HashMap;
015: import java.util.Map;
016:
017: /**
018: * Parse the args and kws for a method call.
019: *
020: * @author brian zimmer
021: * @version $Revision: 2414 $
022: */
023: public class PyArgParser extends Object {
024:
025: /**
026: * Field keywords
027: */
028: protected Map keywords;
029:
030: /**
031: * Field arguments
032: */
033: protected PyObject[] arguments;
034:
035: /**
036: * Construct a parser with the arguments and keywords.
037: */
038: public PyArgParser(PyObject[] args, String[] kws) {
039:
040: this .keywords = new HashMap();
041: this .arguments = null;
042:
043: parse(args, kws);
044: }
045:
046: /**
047: * Method parse
048: *
049: * @param args
050: * @param kws
051: */
052: protected void parse(PyObject[] args, String[] kws) {
053:
054: // walk backwards through the kws and build the map
055: int largs = args.length;
056:
057: if (kws != null) {
058: for (int i = kws.length - 1; i >= 0; i--) {
059: keywords.put(kws[i], args[--largs]);
060: }
061: }
062:
063: this .arguments = new PyObject[largs];
064:
065: System.arraycopy(args, 0, this .arguments, 0, largs);
066: }
067:
068: /**
069: * How many keywords?
070: */
071: public int numKw() {
072: return this .keywords.keySet().size();
073: }
074:
075: /**
076: * Does the keyword exist?
077: */
078: public boolean hasKw(String kw) {
079: return this .keywords.containsKey(kw);
080: }
081:
082: /**
083: * Return the value for the keyword, raise a KeyError if the keyword does
084: * not exist.
085: */
086: public PyObject kw(String kw) {
087:
088: if (!hasKw(kw)) {
089: throw Py.KeyError(kw);
090: }
091:
092: return (PyObject) this .keywords.get(kw);
093: }
094:
095: /**
096: * Return the value for the keyword, return the default if the keyword does
097: * not exist.
098: */
099: public PyObject kw(String 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[] kws() {
112: return (String[]) this .keywords.keySet().toArray(new String[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: }
|