The application clock.vxml prompts the user to ask for the time of day or today's date. The <object> element calls the applet clock.java, which returns the time or date. The grammar clock.grxml supports the application.
<?xml version="1.0" encoding="ISO-8859-1"?>
<grammar version="1.0" xmlns="http://www.w3.org/2001/06/grammar"
xml:lang="en-US" mode="voice" root="clock" tag-format="semantics/1.0">
<rule id="clock">
<one-of>
<item>
<item> what </item>
<ruleref uri="#mode"/>
<item repeat="0-1"> is it </item>
<item repeat="0-1"> please </item>
<tag> $.mode = $mode; </tag>
</item>
<item>
<item> what is todays </item>
<ruleref uri="#mode"/>
<tag> $.mode = $mode; </tag>
</item>
</one-of>
</rule>
<rule id="mode">
<one-of>
<item> month </item>
<item> date </item>
<item> day </item>
<item> time </item>
</one-of>
</rule>
</grammar>
<?xml version="1.0" encoding="iso-8859-1"?>
<vxml version="2.0" xmlns="http://www.w3.org/2001/vxml" xml:lang="en-US">
<form id="clock">
<grammar src="clock.grxml" type="application/srgs+xml"/>
<block>
<prompt> Welcome to the Clock Application </prompt>
</block>
<initial name = "start">
<prompt> What would you like to know? </prompt>
<help> Please say what time is it or what is todays date. </help>
<noinput count="1">
<reprompt/>
</noinput>
<noinput count="2">
<reprompt/>
<assign name="start" expr="true"/>
</noinput>
</initial>
<field name = "mode">
<prompt> What would you like to know? </prompt>
</field>
<object name="clock"
archive="http://www.example.com/java/Clock.jar"
classid="method://Clock/getCurrent"
codetype="javacode">
<param name="setLocale" value="en_US"/>
<param name="setMode" expr="mode"/>
<filled>
<prompt>
The current <value expr="mode"/> is <value expr="clock"/>.
</prompt>
</filled>
</object>
</form>
</vxml>
// package com.ibm.speech.test;
import java.util.Date;
import java.util.Locale;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.StringTokenizer;
/**
* Class Clock represents a clock maintaining both time of day and
* current date. Applications can call setMode to determine the mode
* to return the clock value in. The mode can be DAY, DATE, MONTH, TIME.
* The default mode is DATE. After setting the mode the application can
* call getCurrent to obtain the current clock value for that mode.
* If the application doesn't call setLocale the default local is used.
*
* @author bhm
* @version 1.0
* @see java.util.Calendar
* @see java.util.Date
*/
public class Clock {
public Clock () {
mode = MDATE;
locale = Locale.getDefault ();
} // <init>
/**
* Method to set the current clock mode. The clock mode determines the
* format value return by getCurrent.
* @param mode One of MDATE, MMONT, MDAY, MTIME
*/
public void setMode (String mode) {
if (FORMAT_TABLE.containsKey (mode))
this.mode = mode;
} // setMode ()
/**
* Method to set the locale for the clock presentation value.
* @param locName The locale name to set as the current locale.
*/
public void setLocale (String locName) {
String lang = null;
String country = null;
StringTokenizer st = new StringTokenizer (locName, "_");
if (st.hasMoreTokens ())
lang = st.nextToken ();
if (st.hasMoreTokens ()) country = st.nextToken ();
this.locale = new Locale (lang, country);
} // setLocale ()
/**
* Method to return the current clock value in the specified mode.
* @return The current clock value in the specified mode.
*/
public String getCurrent () {
return new SimpleDateFormat ((String) FORMAT_TABLE.get (mode), locale) .format (new Date ());
} // getCurrent ()
public static final String MTIME = "time";
public static final String MDATE = "date";
public static final String MDAY = "day";
public static final String MMONT = "month";
private static final HashMap FORMAT_TABLE = new HashMap (4);
static {
FORMAT_TABLE.put (MTIME, "K:mm a"); // time
FORMAT_TABLE.put (MDATE, "MMMM d, yyyy"); // date
FORMAT_TABLE.put (MDAY, "EEEE"); // day
FORMAT_TABLE.put (MMONT, "MMMM"); // month
} // <static init>
private String mode; // the clocks mode private
Locale locale; // the clocks locale
} // Clock