1 #ifndef IMAGE_IO_XML_XML_TOKEN_CONTEXT_H_ // NOLINT 2 #define IMAGE_IO_XML_XML_TOKEN_CONTEXT_H_ // NOLINT 3 4 #include <string> 5 #include <vector> 6 7 #include "image_io/base/data_context.h" 8 #include "image_io/base/data_match_result.h" 9 #include "image_io/base/data_range.h" 10 #include "image_io/xml/xml_portion.h" 11 12 namespace photos_editing_formats { 13 namespace image_io { 14 15 class XmlActionContext; 16 17 /// A token context is passed from the action of an XmlTerminal to an XmlHandler 18 /// associated with the XmlActionContext used to call the action function. 19 class XmlTokenContext : public DataContext { 20 public: 21 explicit XmlTokenContext(const XmlActionContext& context); 22 XmlTokenContext(size_t location, const DataRange& range, 23 const DataSegment& segment, const DataLineMap& data_line_map, 24 const DataMatchResult& result, const DataRange& token_range, 25 const XmlPortion& token_portion); 26 27 /// @return The result associated with the context. GetResult()28 const DataMatchResult& GetResult() const { return result_; } 29 30 /// @return The token range for the token. Note that the token range may not 31 /// be a subrange of the context's GetRange() or even the context's segment's 32 /// data range. Such would be the case when a token's value is split across 33 /// two or more data segments. GetTokenRange()34 const DataRange& GetTokenRange() const { return token_range_; } 35 36 /// @return The portion of the token that this context represents. This 37 /// portion value can be the bitwise or of any of the XmlPortion bit values. GetTokenPortion()38 const XmlPortion& GetTokenPortion() const { return token_portion_; } 39 40 /// Builds the string value of the token. If the context's token portion has 41 /// the XmlPortion::kBegin bit set, the string value is first cleared. Then 42 /// the string is extracted from the context's data source and appended onto 43 /// the value. Remember that some token values (especially attribute values) 44 /// can be quite long so care should be excercised when obtaining values with 45 /// this function. 46 /// @param value The value of the token being built. 47 /// @param trim_first_and_last_chars Whether to remove the first and last 48 /// characters of the token. This is nice to use when the token value is a 49 /// quoted string and the value itself is wanted without the quote marks. 50 /// @return Whether the token value is complete (i.e., the context's portion 51 /// had the XmlPortion::kEnd bit set). 52 bool BuildTokenValue(std::string* value, 53 bool trim_first_and_last_chars = false) const; 54 55 /// Builds the complete range of the token, which may need to be represented 56 /// by multiple disjoint ranges. If the token portion indicates all portions 57 /// of the token are present, then this simply clears the vector and pushes 58 /// copies the value returned by the GetTokenRange() into it. Otherwise, it 59 /// does the heavy lifting to build the vector of ranges. 60 /// @param value_ranges The vector of ranges of the token being built. 61 /// @param trim_first_and_last_chars Whether to remove the first and last 62 /// characters of the token. This is nice to use when the token value is a 63 /// If this parameter is true, the effect will be to increase the begin value 64 /// of the first range by 1 and decrease the last range's end by 1. 65 /// @return Whether the token range value is complete (i.e., the context's 66 /// portion had the XmlPortion::kEnd bit set). 67 bool BuildTokenValueRanges(std::vector<DataRange>* value_ranges, 68 bool trim_first_and_last_chars = false) const; 69 70 static XmlPortion ComputeTokenPortion(size_t token_scan_count, 71 DataMatchResult::Type result_type); 72 73 private: 74 DataMatchResult result_; 75 DataRange token_range_; 76 XmlPortion token_portion_; 77 }; 78 79 } // namespace image_io 80 } // namespace photos_editing_formats 81 82 #endif // IMAGE_IO_XML_XML_TOKEN_CONTEXT_H_ // NOLINT 83