📄 documentconverter.cpp
字号:
attrs.push_back(VXMLAttribute(L"label" , ATTRIBUTE_LABEL));
attrs.push_back(VXMLAttribute(L"maxage" , ATTRIBUTE_MAXAGE));
attrs.push_back(VXMLAttribute(L"maxstale" , ATTRIBUTE_MAXSTALE));
attrs.push_back(VXMLAttribute(L"maxtime" , ATTRIBUTE_MAXTIME));
attrs.push_back(VXMLAttribute(L"message" , ATTRIBUTE_MESSAGE));
attrs.push_back(VXMLAttribute(L"messageexpr" , ATTRIBUTE_MESSAGEEXPR));
attrs.push_back(VXMLAttribute(L"method" , ATTRIBUTE_METHOD));
attrs.push_back(VXMLAttribute(L"modal" , ATTRIBUTE_MODAL));
attrs.push_back(VXMLAttribute(L"mode" , ATTRIBUTE_MODE));
attrs.push_back(VXMLAttribute(L"name" , ATTRIBUTE_NAME));
attrs.push_back(VXMLAttribute(L"namelist" , ATTRIBUTE_NAMELIST));
attrs.push_back(VXMLAttribute(L"next" , ATTRIBUTE_NEXT));
attrs.push_back(VXMLAttribute(L"nextitem" , ATTRIBUTE_NEXTITEM));
attrs.push_back(VXMLAttribute(L"root" , ATTRIBUTE_ROOT));
attrs.push_back(VXMLAttribute(L"scope" , ATTRIBUTE_SCOPE));
attrs.push_back(VXMLAttribute(L"slot" , ATTRIBUTE_SLOT));
attrs.push_back(VXMLAttribute(L"src" , ATTRIBUTE_SRC));
attrs.push_back(VXMLAttribute(L"srcexpr" , ATTRIBUTE_SRCEXPR));
attrs.push_back(VXMLAttribute(L"tag-format" , ATTRIBUTE_TAGFORMAT));
attrs.push_back(VXMLAttribute(L"timeout" , ATTRIBUTE_TIMEOUT));
attrs.push_back(VXMLAttribute(L"transferaudio" , ATTRIBUTE_TRANSFERAUDIO));
attrs.push_back(VXMLAttribute(L"type" , ATTRIBUTE_TYPE));
attrs.push_back(VXMLAttribute(L"value" , ATTRIBUTE_VALUE));
attrs.push_back(VXMLAttribute(L"valuetype" , ATTRIBUTE_VALUETYPE));
attrs.push_back(VXMLAttribute(L"version" , ATTRIBUTE_VERSION));
attrs.push_back(VXMLAttribute(L"weight" , ATTRIBUTE_WEIGHT));
attrs.push_back(VXMLAttribute(L"xml:lang" , ATTRIBUTE_XMLLANG));
// (2.2) Internals attributes (these are converted to others)
attrs.push_back(VXMLAttribute(L"schemaLocation", PRIV_ATTRIB_SCHEMALOC));
// (3) Final stuff.
std::sort(attrs.begin(), attrs.end());
std::sort(elems.begin(), elems.end());
}
static void DeinitializeTables()
{
attrs.clear();
elems.clear();
}
static bool ConvertElement(const VXIchar * name, int & result)
{
if (name == NULL) return false;
TABLE_ELEMS::iterator i = std::lower_bound(elems.begin(), elems.end(),
VXMLElementInfo(name, NODE_IF));
if (wcscmp(name, (*i).key) != 0) return false;
result = (*i).value;
return true;
}
static bool ConvertAttribute(const VXIchar * name, int & result)
{
if (name == NULL) return false;
TABLE_ATTRS::iterator i = std::lower_bound(attrs.begin(), attrs.end(),
VXMLAttribute(name,ATTRIBUTE_ID));
if (wcscmp(name, (*i).key) != 0) return false;
result = (*i).value;
return true;
}
//#############################################################################
class DummyLocator : public Locator {
public:
DummyLocator() { }
virtual ~DummyLocator() { }
virtual const XMLCh* getPublicId() const { return NULL; }
virtual const XMLCh* getSystemId() const { return NULL; }
virtual XMLSSize_t getLineNumber() const { return -1; }
virtual XMLSSize_t getColumnNumber() const { return -1; }
};
static DummyLocator DUMMYLOCATOR;
//#############################################################################
bool DocumentConverter::Initialize()
{
InitializeTables();
return true;
}
void DocumentConverter::Deinitialize()
{
DeinitializeTables();
}
DocumentConverter::DocumentConverter()
: locator(&DUMMYLOCATOR), doc(NULL), strict(false),
copyDepth(0), pcdataImpliesPrompt(true)
{
}
DocumentConverter::~DocumentConverter()
{
ResetDocument();
}
void DocumentConverter::ParseException(const VXIchar * message) const
{
if (message == NULL) throw SAXParseException(NULL, *locator);
VXIcharToXMLCh text(message);
throw SAXParseException(text.c_str(), *locator);
}
void DocumentConverter::startDocument()
{
version = -1;
choiceNumber = 0;
inGrammar = false;
implicitPrompt = false;
explicitPrompt = false;
copyDepth = 0;
ignoreDepth = 0;
pcdataImpliesPrompt = true;
documentLang=L"";
if (doc != NULL) VXMLDocumentRep::Release(doc);
doc = new VXMLDocumentRep();
if (doc == NULL)
ParseException(L"unable to allocate memory for element");
}
void DocumentConverter::endDocument()
{
}
void DocumentConverter::ResetDocument()
{
if (doc != NULL) VXMLDocumentRep::Release(doc);
doc = NULL;
}
VXMLDocumentRep * DocumentConverter::GetDocument()
{
VXMLDocumentRep * temp = doc;
doc = NULL;
return temp;
}
void DocumentConverter::setDocumentLocator(const Locator* const loc)
{
if (loc == NULL)
locator = &DUMMYLOCATOR;
else
locator = loc;
}
void DocumentConverter::startElement(const XMLCh* const uri,
const XMLCh* const localname,
const XMLCh* const qname,
const Attributes & attrs)
{
// (0) Just ignore this?
if (ignoreDepth > 0) {
++ignoreDepth;
return;
}
// (1) Copy this element or look it up?
// (1.1) Inside a <grammar>, we can blindly copy this data.
if (inGrammar) {
CopyElementStart(localname, attrs);
return;
}
// (1.2) Convert name string to integer value.
XMLChToVXIchar elementName(localname);
int rawElemType;
bool conversionFailed = !ConvertElement(elementName.c_str(), rawElemType);
// (1.3) Copy almost anything inside a <prompt>.
if (explicitPrompt) {
if (conversionFailed) {
CopyElementStart(localname, attrs);
return;
}
switch (rawElemType) {
case NODE_AUDIO:
case NODE_VALUE:
case NODE_ENUMERATE:
break;
default:
CopyElementStart(localname, attrs);
return;
}
}
// (1.4) Print errors for all other conversion failures.
if (conversionFailed) {
vxistring temp(L"unrecognized element - ");
temp += elementName.c_str();
ParseException(temp.c_str());
}
// (2) Check for ignored nodes and do version number processing.
if (rawElemType == PRIV_ELEM_METADATA) {
ignoreDepth = 1;
return;
}
// (2.1) Catch illegal nodes and do version number processing. Prompt and
// grammar copying flags are set in the same pass.
switch (rawElemType) {
case DEFAULTS_ROOT:
version = 2.0f;
break;
case NODE_VXML:
{
for (unsigned int index = 0; index < attrs.getLength(); ++index) {
if (!Compare(attrs.getLocalName(index), L"version")) continue;
const XMLCh * attributeValue = attrs.getValue(index);
if (Compare(attributeValue, L"2.0")) version = 2.0f;
else ParseException(L"unsupported vxml version");
break;
}
}
break;
case NODE_GRAMMAR: // Copy grammar contents.
inGrammar = true;
copyDepth = 0;
break;
case NODE_PROMPT: // Copy prompt contents.
explicitPrompt = true;
copyDepth = 0;
break;
case NODE_SCRIPT:
case NODE_CHOICE: // These may have PCDATA which is not a prompt
case NODE_OPTION:
case NODE_LOG:
pcdataImpliesPrompt = false;
break;
default:
break;
}
// (2.2) Is this the beginning or end of an implicit prompt?
switch (rawElemType) {
case NODE_AUDIO:
case NODE_VALUE: // NOTE: A <value> may also appear in <log>.
case NODE_ENUMERATE:
if (pcdataImpliesPrompt && !implicitPrompt && !explicitPrompt)
StartImplicitPrompt();
break;
case PRIV_ELEM_BREAK: // These implicitly imply a prompt.
case PRIV_ELEM_EMPHASIS:
case PRIV_ELEM_MARK:
case PRIV_ELEM_PARAGRAPH:
case PRIV_ELEM_PHONEME:
case PRIV_ELEM_PROSODY:
case PRIV_ELEM_SAYAS:
case PRIV_ELEM_SENTENCE:
case PRIV_ELEM_VOICE:
if (!implicitPrompt)
StartImplicitPrompt();
CopyElementStart(localname, attrs);
return;
default:
if (implicitPrompt) {
// We require that implicit prompts contain only familiar SSML.
if (copyDepth == 0)
EndImplicitPrompt();
else
ParseException(L"illegal prompt content");
}
break;
}
// (2.4) Convert nodes as necessary.
VXMLElementType elemType = VXMLElementType(rawElemType);
switch (rawElemType) {
case PRIV_ELEM_ERROR:
case PRIV_ELEM_HELP:
case PRIV_ELEM_NOINPUT:
case PRIV_ELEM_NOMATCH:
elemType = NODE_CATCH;
break;
default:
break;
}
if (elemType > PRIV_ELEM_RangeStart) {
vxistring temp(L"internal error for element - ");
temp += elementName.c_str();
ParseException(temp.c_str());
}
// (3) Create new element.
try {
hasContent = (elemType != NODE_SCRIPT && elemType != NODE_GRAMMAR);
doc->StartElement(elemType);
}
catch (const VXMLDocumentModel::OutOfMemory &) {
ParseException(L"unable to allocate memory for element");
}
catch (const VXMLDocumentModel::InternalError &) {
ParseException(L"corrupted document tree; unable to add element");
}
// (4) Add attributes to element.
int attrGroup1 = 0;
int attrGroup2 = 0;
// (4.1) Do special handling for converted nodes.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -