ISEResultXmlParser.m 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. //
  2. // ISEResultXmlParser.m
  3. // IFlyMSCDemo
  4. //
  5. // Created by 张剑 on 15/3/6.
  6. //
  7. //
  8. #import "ISEResultXmlParser.h"
  9. #import "ISEResult.h"
  10. #import "ISEResultPhone.h"
  11. #import "ISEResultSyll.h"
  12. #import "ISEResultWord.h"
  13. #import "ISEResultSentence.h"
  14. #import "ISEResultFinal.h"
  15. #import "ISEResultReadSyllable.h"
  16. #import "ISEResultReadWord.h"
  17. #import "ISEResultReadSentence.h"
  18. @interface ISEResultXmlParser ()
  19. @property(nonatomic,retain)ISEResult* xmlResult;
  20. @property(nonatomic,assign)BOOL isPlainResult;
  21. @property(nonatomic,assign)BOOL isRecPaperPassed;
  22. @property(nonatomic,retain)ISEResultPhone* phone;
  23. @property(nonatomic,retain)ISEResultSyll* syll;
  24. @property(nonatomic,retain)ISEResultWord* word;
  25. @property(nonatomic,retain)ISEResultSentence* sentence;
  26. @end
  27. @implementation ISEResultXmlParser
  28. void readTotalResult(ISEResult* result, NSDictionary* attrDic);
  29. ISEResultPhone* createPhone(NSDictionary* attrDic);
  30. ISEResultSyll* createSyll(NSDictionary* attrDic);
  31. ISEResultWord* createWord(NSDictionary* attrDic);
  32. ISEResultSentence* createSentence(NSDictionary* attrDic);
  33. - (void)clearAllProperty{
  34. self.isPlainResult=NO;
  35. self.isRecPaperPassed=NO;
  36. self.phone=nil;
  37. self.syll=nil;
  38. self.word=nil;
  39. self.sentence=nil;
  40. }
  41. - (void)parserXml:(NSString*) xml{
  42. [self clearAllProperty];
  43. if(xml){
  44. self.xmlResult = nil;
  45. NSData* xmlData=[xml dataUsingEncoding:NSUTF8StringEncoding];
  46. NSXMLParser *parser = [[NSXMLParser alloc] initWithData:xmlData];
  47. [parser setShouldProcessNamespaces:NO];
  48. [parser setShouldReportNamespacePrefixes:NO];
  49. [parser setShouldResolveExternalEntities:NO];
  50. [parser setDelegate:self];
  51. [parser parse];
  52. }
  53. else{
  54. if (self.delegate && [self.delegate respondsToSelector:@selector(onISEResultXmlParserResult:)]) {
  55. [self.delegate onISEResultXmlParserResult:self.xmlResult];
  56. }
  57. }
  58. }
  59. #pragma mark - tools
  60. void readTotalResult(ISEResult* result, NSDictionary* attrDic) {
  61. result.beg_pos = [[attrDic objectForKey:@"beg_pos"] intValue];
  62. result.end_pos = [[attrDic objectForKey:@"end_pos"] intValue];
  63. result.content = [attrDic objectForKey:@"content"];
  64. result.total_score = [[attrDic objectForKey:@"total_score"] floatValue];
  65. result.accuracy_score = [[attrDic objectForKey:@"accuracy_score"] floatValue];
  66. result.fluency_score = [[attrDic objectForKey:@"fluency_score"] floatValue];
  67. result.integrity_score = [[attrDic objectForKey:@"integrity_score"] floatValue];
  68. result.time_len = [[attrDic objectForKey:@"time_len"] intValue];
  69. result.except_info = [attrDic objectForKey:@"except_info"];
  70. result.is_rejected = [[attrDic objectForKey:@"is_rejected"] boolValue];
  71. }
  72. ISEResultPhone* createPhone(NSDictionary* attrDic) {
  73. ISEResultPhone* phone=[[ISEResultPhone alloc] init];
  74. phone.beg_pos = [[attrDic objectForKey:@"beg_pos"] intValue];
  75. phone.end_pos = [[attrDic objectForKey:@"end_pos"] intValue];
  76. phone.content = [attrDic objectForKey:@"content"];
  77. phone.dp_message = [[attrDic objectForKey:@"dp_message"] intValue];
  78. phone.time_len = [[attrDic objectForKey:@"time_len"] intValue];
  79. return phone;
  80. }
  81. ISEResultSyll* createSyll(NSDictionary* attrDic) {
  82. ISEResultSyll* syll=[[ISEResultSyll alloc] init];
  83. syll.beg_pos = [[attrDic objectForKey:@"beg_pos"] intValue];
  84. syll.end_pos = [[attrDic objectForKey:@"end_pos"] intValue];
  85. syll.content = [attrDic objectForKey:@"content"];
  86. syll.symbol = [attrDic objectForKey:@"symbol"];
  87. syll.dp_message = [[attrDic objectForKey:@"dp_message"] intValue];
  88. syll.time_len = [[attrDic objectForKey:@"time_len"] intValue];
  89. return syll;
  90. }
  91. ISEResultWord* createWord(NSDictionary* attrDic) {
  92. ISEResultWord* word=[[ISEResultWord alloc] init];
  93. word.beg_pos = [[attrDic objectForKey:@"beg_pos"] intValue];
  94. word.end_pos = [[attrDic objectForKey:@"end_pos"] intValue];
  95. word.content = [attrDic objectForKey:@"content"];
  96. word.symbol = [attrDic objectForKey:@"symbol"];
  97. word.dp_message = [[attrDic objectForKey:@"dp_message"] intValue];
  98. word.time_len = [[attrDic objectForKey:@"time_len"] intValue];
  99. word.total_score = [[attrDic objectForKey:@"total_score"] floatValue];
  100. word.global_index = [[attrDic objectForKey:@"global_index"] intValue];
  101. word.index = [[attrDic objectForKey:@"index"] intValue];
  102. return word;
  103. }
  104. ISEResultSentence* createSentence(NSDictionary* attrDic) {
  105. ISEResultSentence* sentence=[[ISEResultSentence alloc] init];;
  106. sentence.beg_pos = [[attrDic objectForKey:@"beg_pos"] intValue];
  107. sentence.end_pos = [[attrDic objectForKey:@"end_pos"] intValue];
  108. sentence.content = [attrDic objectForKey:@"content"];
  109. sentence.time_len = [[attrDic objectForKey:@"time_len"] intValue];
  110. sentence.index = [[attrDic objectForKey:@"index"] intValue];
  111. sentence.word_count = [[attrDic objectForKey:@"word_count"] intValue];
  112. return sentence;
  113. }
  114. #pragma mark - NSXMLParser delegate
  115. - (void) parserDidStartDocument:(NSXMLParser *)parser{
  116. }
  117. - (void) parserDidEndDocument:(NSXMLParser *)parser{
  118. }
  119. - (void) parser:(NSXMLParser *)parser
  120. didStartElement:(NSString *)elementName
  121. namespaceURI:(NSString *)namespaceURI
  122. qualifiedName:(NSString *)qualifiedName
  123. attributes:(NSDictionary *)attributeDict{
  124. //complete
  125. if([@"rec_paper" isEqualToString:elementName]){
  126. _isRecPaperPassed=YES;
  127. }
  128. else if([@"read_syllable" isEqualToString:elementName]){
  129. if(!_isRecPaperPassed){
  130. _xmlResult=[[ISEResultReadSyllable alloc] init];
  131. }
  132. else{
  133. readTotalResult(self.xmlResult, attributeDict);
  134. }
  135. }
  136. else if([@"read_word" isEqualToString:elementName]){
  137. if(!_isRecPaperPassed){
  138. _xmlResult=[[ISEResultReadWord alloc] init];
  139. NSString* lan=[attributeDict objectForKey:@"lan"];
  140. _xmlResult.language=lan?lan:@"cn";
  141. }
  142. else{
  143. readTotalResult(self.xmlResult, attributeDict);
  144. }
  145. }
  146. else if([@"read_sentence" isEqualToString:elementName]||[@"read_chapter" isEqualToString:elementName]){
  147. if(!_isRecPaperPassed){
  148. _xmlResult=[[ISEResultReadSentence alloc] init];
  149. NSString* lan=[attributeDict objectForKey:@"lan"];
  150. _xmlResult.language=lan?lan:@"cn";
  151. }
  152. else{
  153. readTotalResult(self.xmlResult, attributeDict);
  154. }
  155. }
  156. else if([@"sentence" isEqualToString:elementName]){
  157. if(_xmlResult&&!_xmlResult.sentences){
  158. _xmlResult.sentences=[[NSMutableArray alloc] init];
  159. }
  160. _sentence=createSentence(attributeDict);
  161. }
  162. else if([@"word" isEqualToString:elementName]){
  163. if(_sentence && !_sentence.words){
  164. _sentence.words=[[NSMutableArray alloc] init];
  165. }
  166. _word=createWord(attributeDict);
  167. }
  168. else if([@"syll" isEqualToString:elementName]){
  169. if(_word && !_word.sylls){
  170. _word.sylls=[[NSMutableArray alloc] init];
  171. }
  172. _syll=createSyll(attributeDict);
  173. }
  174. else if([@"phone" isEqualToString:elementName]){
  175. if(_syll && !_syll.phones){
  176. _syll.phones=[[NSMutableArray alloc] init];
  177. }
  178. _phone=createPhone(attributeDict);
  179. }
  180. //plain
  181. if([@"FinalResult" isEqualToString:elementName]){
  182. self.isPlainResult=YES;
  183. _xmlResult = [[ISEResultFinal alloc] init];
  184. }
  185. else if([@"ret" isEqualToString:elementName]){
  186. [(ISEResultFinal *)_xmlResult setRet:[[attributeDict objectForKey:@"value"] intValue]];
  187. }
  188. else if([@"total_score" isEqualToString:elementName]){
  189. [(ISEResultFinal *)_xmlResult setTotal_score:[[attributeDict objectForKey:@"value"] floatValue]];
  190. }
  191. else if([@"xml_result" isEqualToString:elementName]){
  192. self.isPlainResult=NO;
  193. }
  194. }
  195. - (void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
  196. }
  197. - (void) parser:(NSXMLParser *)parser foundIgnorableWhitespace:(NSString *)whitespaceString{
  198. }
  199. - (void) parser:(NSXMLParser *) parser
  200. didEndElement:(NSString *) elementName
  201. namespaceURI:(NSString *) namespaceURI
  202. qualifiedName:(NSString *) qualifiedName{
  203. if([@"phone" isEqualToString:elementName]){
  204. [_syll.phones addObject:_phone];
  205. _phone=nil;
  206. }
  207. else if([@"syll" isEqualToString:elementName]){
  208. [_word.sylls addObject:_syll];
  209. _syll=nil;
  210. }
  211. else if([@"word" isEqualToString:elementName]){
  212. [_sentence.words addObject:_word];
  213. _word=nil;
  214. }
  215. else if([@"sentence" isEqualToString:elementName]){
  216. [_xmlResult.sentences addObject:_sentence];
  217. _sentence=nil;
  218. }
  219. else if([@"read_syllable" isEqualToString:elementName] ||
  220. [@"read_word" isEqualToString:elementName] ||
  221. [@"read_sentence" isEqualToString:elementName] ||
  222. [@"read_chapter" isEqualToString:elementName] ||
  223. [@"FinalResult" isEqualToString:elementName] ){
  224. [parser abortParsing];
  225. if (self.delegate && [self.delegate respondsToSelector:@selector(onISEResultXmlParserResult:)]) {
  226. [self.delegate onISEResultXmlParserResult:self.xmlResult];
  227. }
  228. }
  229. }
  230. - (void) parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError{
  231. //??????????
  232. // if (self.delegate && [self.delegate respondsToSelector:@selector(onISEResultXmlParser:Error:)]) {
  233. // [self.delegate onISEResultXmlParser:parser Error:parseError];
  234. // }
  235. }
  236. @end