/** * Creates the <a href="http://wiki.mindmakers.org/projects:BML:main">Behavior Markup Language</a> * string for Avatar. * * @param utterance the utterance to speak <code>null</code> * @param ssml SSML with BML annotations * @return created XML string * @throws XMLStreamException if the stream could not be created. */ private String createBML(final String utterance, final SsmlDocument ssml) throws XMLStreamException { final ByteArrayOutputStream out = new ByteArrayOutputStream(); final XMLOutputFactory factory = XMLOutputFactory.newInstance(); final XMLStreamWriter writer = factory.createXMLStreamWriter(out, ENCODING); writer.writeStartDocument(ENCODING, "1.0"); writer.writeStartElement("bml"); writer.writeAttribute("id", "bml1"); writer.writeNamespace("ns1", BML_NAMESPACE_URI); if (ssml != null) { final Speak speak = ssml.getSpeak(); final NodeList children = speak.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { final Node child = children.item(i); final String namespace = child.getNamespaceURI(); if (namespace != null) { writeBMLNode(writer, child, utterance); } } } writer.writeEndElement(); writer.writeEndDocument(); writer.flush(); // lastGestureEndTime = 0; writer.close(); try { String output = out.toString(ENCODING); return output; } catch (UnsupportedEncodingException e) { LOGGER.warn(e.getMessage(), e); return out.toString(); } }
/** Sends the next speakable to Avatar. */ private void sendNextSpeakable() { final QueuedSpeakable next = speakables.peek(); final SpeakableText speakable = next.getSpeakable(); final String utterance; final SsmlDocument document; if (speakable instanceof SpeakableSsmlText) { final SpeakableSsmlText ssml = (SpeakableSsmlText) speakable; document = ssml.getDocument(); final Speak speak = document.getSpeak(); utterance = speak.getTextContent(); } else { utterance = speakable.getSpeakableText(); document = null; } ErrorEvent error = null; try { final String bml = createBML(utterance, document); sendToAvatar(bml); } catch (SocketException e) { error = new BadFetchError(e.getMessage(), e); } catch (UnknownHostException e) { error = new BadFetchError(e.getMessage(), e); } catch (IOException e) { error = new BadFetchError(e.getMessage(), e); } catch (XMLStreamException e) { error = new BadFetchError(e.getMessage(), e); } if (error != null) { synchronized (listeners) { for (SynthesizedOutputListener listener : listeners) { listener.outputError(error); } } } }
/** * Test method for {@link * VarStrategy#execute(org.jvoicexml.interpreter.VoiceXmlInterpreterContext, * org.jvoicexml.interpreter.VoiceXmlInterpreter, * org.jvoicexml.interpreter.FormInterpretationAlgorithm, org.jvoicexml.interpreter.FormItem, * org.jvoicexml.xml.VoiceXmlNode)} . * * @exception Exception Test failed. * @exception JVoiceXMLEvent test failed */ @Test public void testExecuteExpr() throws Exception, JVoiceXMLEvent { final String name = "test"; final String val = "this is a test"; final Block block = createBlock(); final Value value = block.appendChild(Value.class); value.setExpr(name); final DataModel model = getDataModel(); Mockito.when(model.evaluateExpression(name, Object.class)).thenReturn(val); final ValueStrategy strategy = new ValueStrategy(); executeTagStrategy(value, strategy); final SsmlDocument ssml = new SsmlDocument(); final Speak speak = ssml.getSpeak(); speak.addText("this is a test"); final SpeakableText speakable = new SpeakableSsmlText(ssml); final ImplementationPlatform platform = getContext().getImplementationPlatform(); Mockito.verify(platform).queuePrompt(speakable); }