1 #include "image_io/xml/xml_cdata_and_comment_rules.h" 2 3 #include <utility> 4 5 #include "image_io/xml/xml_handler.h" 6 #include "image_io/xml/xml_token_context.h" 7 8 namespace photos_editing_formats { 9 namespace image_io { 10 XmlCdataRule()11XmlCdataRule::XmlCdataRule() : XmlCdataRule(kFirstStartPoint) {} 12 XmlCdataRule(StartPoint start_point)13XmlCdataRule::XmlCdataRule(StartPoint start_point) : XmlRule("CDATA") { 14 // <![CDATA[ ... ]]> 15 AddLiteralTerminal("<!"); 16 AddLiteralTerminal("[CDATA["); 17 AddThroughLiteralTerminal("]]>").WithAction( 18 [&](const XmlActionContext& context) { 19 return HandleCdataValue(context); 20 }); 21 if (start_point == kSecondStartPoint) { 22 SetTerminalIndex(1); 23 } 24 } 25 HandleCdataValue(const XmlActionContext & context)26DataMatchResult XmlCdataRule::HandleCdataValue( 27 const XmlActionContext& context) { 28 XmlTokenContext token_context(context); 29 return context.GetHandler()->Cdata(token_context); 30 } 31 XmlCommentRule()32XmlCommentRule::XmlCommentRule() : XmlCommentRule(kFirstStartPoint) {} 33 XmlCommentRule(StartPoint start_point)34XmlCommentRule::XmlCommentRule(StartPoint start_point) : XmlRule("Comment") { 35 // <!-- ... --> 36 AddLiteralTerminal("<!"); 37 AddLiteralTerminal("--"); 38 AddThroughLiteralTerminal("-->").WithAction( 39 [&](const XmlActionContext& context) { 40 return HandleCommentValue(context); 41 }); 42 if (start_point == kSecondStartPoint) { 43 SetTerminalIndex(1); 44 } 45 } 46 HandleCommentValue(const XmlActionContext & context)47DataMatchResult XmlCommentRule::HandleCommentValue( 48 const XmlActionContext& context) { 49 XmlTokenContext token_context(context); 50 return context.GetHandler()->Comment(token_context); 51 } 52 XmlCdataOrCommentRule()53XmlCdataOrCommentRule::XmlCdataOrCommentRule() 54 : XmlCdataOrCommentRule(kFirstStartPoint) {} 55 XmlCdataOrCommentRule(StartPoint start_point)56XmlCdataOrCommentRule::XmlCdataOrCommentRule(StartPoint start_point) 57 : XmlRule("CdataOrComment") { 58 // <![CDATA[ ... ]]> or <!-- ... --> 59 // So after the initial "<!" literal can come a "[" or a "-". 60 AddLiteralTerminal("<!"); 61 AddSentinelTerminal("[-").WithAction([&](const XmlActionContext& context) { 62 return HandlePostBangChar(context); 63 }); 64 if (start_point == kSecondStartPoint) { 65 SetTerminalIndex(1); 66 } 67 } 68 HandlePostBangChar(const XmlActionContext & context)69DataMatchResult XmlCdataOrCommentRule::HandlePostBangChar( 70 const XmlActionContext& context) { 71 char sentinel = context.GetTerminal()->GetScanner()->GetSentinel(); 72 if (sentinel == '[') { 73 std::unique_ptr<XmlRule> rule(new XmlCdataRule(kSecondStartPoint)); 74 SetNextRule(std::move(rule)); 75 } else if (sentinel == '-') { 76 std::unique_ptr<XmlRule> rule(new XmlCommentRule(kSecondStartPoint)); 77 SetNextRule(std::move(rule)); 78 } 79 return context.GetResultWithBytesConsumed(0); 80 } 81 82 } // namespace image_io 83 } // namespace photos_editing_formats 84