00001 #include "LinguisticDefinition/XmlWordPatternFormatter.h" 00002 00003 #include <iostream> 00004 00005 using namespace std; 00006 using namespace LinguisticDefinition; 00007 00011 XmlWordPatternFormatter::XmlWordPatternFormatter(const LingDef &lingDef) : 00012 WordPatternFormatter(lingDef) { 00013 } 00014 00018 WordPattern XmlWordPatternFormatter::createWordPattern(xmlNodePtr node) { 00019 WordPattern expression; 00020 populateWordPattern(node, expression); 00021 return expression; 00022 } 00023 00027 bool XmlWordPatternFormatter::populateWordPattern(xmlNodePtr node, 00028 WordPattern &wordPattern) { 00029 00030 const LingDef::Pos *posDef = NULL; 00031 go(node, wordPattern.getModifiableRootElement(), posDef); 00032 00033 return true; 00034 } 00035 00039 bool XmlWordPatternFormatter::go(xmlNodePtr node, 00040 WordPattern::GroupElement &element, 00041 const LingDef::Pos *&posDef) { 00042 xmlXPathContextPtr xpContext = xmlXPathNewContext(node->doc); 00043 xpContext->node = node; 00044 00045 xmlXPathObjectPtr xpObj = 00046 xmlXPathEval((xmlChar *) "And|Or|Not|Pos|Feature|Form|Lemma", 00047 xpContext); 00048 if (xpObj != NULL) { 00049 xmlNodeSetPtr nodeSet = xpObj->nodesetval; 00050 if (nodeSet != NULL) { 00051 {for (int i = 0; i < nodeSet->nodeNr; i++) { 00052 xmlNodePtr subNode = nodeSet->nodeTab[i]; 00053 00054 if (xmlStrcmp(subNode->name, (xmlChar *) "And") == 0) { 00055 go(subNode, element.createAnd(), posDef); 00056 00057 } else if (xmlStrcmp(subNode->name, (xmlChar *) "Or") == 0) { 00058 go(subNode, element.createOr(), posDef); 00059 00060 } else if (xmlStrcmp(subNode->name, (xmlChar *) "Not") == 0) { 00061 WordPattern::GroupElement ¬Element = element.createAnd(); 00062 notElement.setNegative(); 00063 go(subNode, notElement, posDef); 00064 00065 } else if (xmlStrcmp(subNode->name, (xmlChar *) "Pos") == 0) { 00066 xmlChar *tmp = xmlGetProp(subNode, (xmlChar *) "name"); 00067 if (tmp != NULL) { 00068 const LingDef::Pos *pos = getLingDef().getPos((char *) tmp); 00069 if (pos == NULL) { 00070 cerr << "Feature '" << (char *) tmp << "' not defined" << endl; 00071 } else { 00072 posDef = pos; 00073 element.createLeaf().setPosDef(*pos); 00074 } 00075 xmlFree(tmp); 00076 } 00077 00078 } else if (xmlStrcmp(subNode->name, (xmlChar *) "Feature") == 0) { 00079 if (posDef == NULL) { 00080 cerr << "Must have a pos before feature" << endl; 00081 } else { 00082 xmlChar *tmp = xmlGetProp(subNode, (xmlChar *) "name"); 00083 if (tmp != NULL) { 00084 const LingDef::Feature *feature = 00085 posDef->getFeature((char *) tmp); 00086 if (feature == NULL) { 00087 cerr << "Feature '" << (char *) tmp << "' not defined" << endl; 00088 } else { 00089 element.createLeaf().setFeature(*feature); 00090 } 00091 xmlFree(tmp); 00092 } 00093 } 00094 00095 } else if (xmlStrcmp(subNode->name, (xmlChar *) "Form") == 0) { 00096 xmlChar *tmp = xmlNodeGetContent(subNode); 00097 if (tmp != NULL) { 00098 element.createLeaf().setForm((char *) tmp); 00099 xmlFree(tmp); 00100 } 00101 00102 } else if (xmlStrcmp(subNode->name, (xmlChar *) "Lemma") == 0) { 00103 xmlChar *tmp = xmlNodeGetContent(subNode); 00104 if (tmp != NULL) { 00105 element.createLeaf().setLemma((char *) tmp); 00106 xmlFree(tmp); 00107 } 00108 } 00109 00110 }} 00111 } 00112 xmlXPathFreeObject(xpObj); 00113 } 00114 00115 xmlXPathFreeContext(xpContext); 00116 00117 return true; 00118 }