• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef IMAGE_IO_XML_XML_TERMINAL_H_  // NOLINT
2 #define IMAGE_IO_XML_XML_TERMINAL_H_  // NOLINT
3 
4 #include <string>
5 
6 #include "image_io/base/data_scanner.h"
7 #include "image_io/xml/xml_action.h"
8 #include "image_io/xml/xml_token_context.h"
9 
10 namespace photos_editing_formats {
11 namespace image_io {
12 
13 /// A terminal represents a part of a rule that uses a DataScanner to match
14 /// zero or more characters from a DataSource. A terminal can also have a name
15 /// that can be be used in error messages and also used to identify it in a
16 /// rule. A terminal can also have an action function associated with it that it
17 /// can use to validate the token produced by the terminal/scanner, and do
18 /// further processing with the token. Finally, the terminal's action function
19 /// can manipulate the DataMatchResult that was produced by the terminal's
20 /// scanner and accessible via the action function's XmlActionContext param.
21 class XmlTerminal {
22  public:
XmlTerminal(const DataScanner & scanner)23   explicit XmlTerminal(const DataScanner& scanner) : scanner_(scanner) {}
24 
25   /// Sets the name of the terminal. Looks best with an XmlRule::AddTerminal
26   /// function: AddWhitespaceTerminal().WithName("SomeName");
27   /// @param name The name to give to the terminal.
28   /// @return A reference to the terminal.
WithName(const std::string & name)29   XmlTerminal& WithName(const std::string& name) {
30     name_ = name;
31     return *this;
32   }
33 
34   /// Sets the description of the terminal's scanner used for errors.
35   /// Looks best with an XmlRule::AddTerminal function:
36   /// AddWhitespaceTerminal().WithDescription("intra element whitespace")
37   /// @param description The description to give to the terminal's scanner.
38   /// @return A reference to the terminal.
WithDescription(const std::string & description)39   XmlTerminal& WithDescription(const std::string& description) {
40     scanner_.SetDescription(description);
41     return *this;
42   }
43 
44   /// Sets the action of the terminal. Looks best with an XmlRule::AddTerminal
45   /// function: AddWhitespaceTerminal().WithAction(SomeAction);
46   /// @param action The action to give to the terminal.
47   /// @return A reference to the terminal.
WithAction(const XmlAction & action)48   XmlTerminal& WithAction(const XmlAction& action) {
49     action_ = action;
50     return *this;
51   }
52 
53   /// @return The terminal's scanner.
GetScanner()54   DataScanner* GetScanner() { return &scanner_; }
55 
56   /// @return The terminal's name.
GetName()57   const std::string& GetName() const { return name_; }
58 
59   /// @return The terminal's scanner's description.
GetDescription()60   std::string GetDescription() const { return scanner_.GetDescription(); }
61 
62   /// @return The terminal's action function.
GetAction()63   const XmlAction& GetAction() const { return action_; }
64 
65  private:
66   DataScanner scanner_;
67   XmlAction action_;
68   std::string name_;
69 };
70 
71 }  // namespace image_io
72 }  // namespace photos_editing_formats
73 
74 #endif  // IMAGE_IO_XML_XML_TERMINAL_H_  // NOLINT
75