1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 
6 #ifndef TRANSFRMX_XML_EVENT_HANDLER_H
7 #define TRANSFRMX_XML_EVENT_HANDLER_H
8 
9 #include "txCore.h"
10 #include "nsAtom.h"
11 
12 #define kTXNameSpaceURI u"http://www.mozilla.org/TransforMiix"
13 #define kTXWrapper "transformiix:result"
14 
15 class txOutputFormat;
16 namespace mozilla {
17 namespace dom {
18 class Document;
19 }
20 }  // namespace mozilla
21 
22 /**
23  * An interface for handling XML documents, loosely modeled
24  * after Dave Megginson's SAX 1.0 API.
25  */
26 
27 class txAXMLEventHandler {
28  public:
29   virtual ~txAXMLEventHandler() = default;
30 
31   /**
32    * Signals to receive the start of an attribute.
33    *
34    * @param aPrefix the prefix of the attribute
35    * @param aLocalName the localname of the attribute
36    * @param aLowercaseName the localname of the attribute in lower case
37    * @param aNsID the namespace ID of the attribute
38    * @param aValue the value of the attribute
39    */
40   virtual nsresult attribute(nsAtom* aPrefix, nsAtom* aLocalName,
41                              nsAtom* aLowercaseLocalName, int32_t aNsID,
42                              const nsString& aValue) = 0;
43 
44   /**
45    * Signals to receive the start of an attribute.
46    *
47    * @param aPrefix the prefix of the attribute
48    * @param aLocalName the localname of the attribute
49    * @param aNsID the namespace ID of the attribute
50    * @param aValue the value of the attribute
51    */
52   virtual nsresult attribute(nsAtom* aPrefix, const nsAString& aLocalName,
53                              const int32_t aNsID, const nsString& aValue) = 0;
54 
55   /**
56    * Signals to receive characters.
57    *
58    * @param aData the characters to receive
59    * @param aDOE disable output escaping for these characters
60    */
61   virtual nsresult characters(const nsAString& aData, bool aDOE) = 0;
62 
63   /**
64    * Signals to receive data that should be treated as a comment.
65    *
66    * @param data the comment data to receive
67    */
68   virtual nsresult comment(const nsString& aData) = 0;
69 
70   /**
71    * Signals the end of a document. It is an error to call
72    * this method more than once.
73    */
74   virtual nsresult endDocument(nsresult aResult) = 0;
75 
76   /**
77    * Signals to receive the end of an element.
78    */
79   virtual nsresult endElement() = 0;
80 
81   /**
82    * Signals to receive a processing instruction.
83    *
84    * @param aTarget the target of the processing instruction
85    * @param aData the data of the processing instruction
86    */
87   virtual nsresult processingInstruction(const nsString& aTarget,
88                                          const nsString& aData) = 0;
89 
90   /**
91    * Signals the start of a document.
92    */
93   virtual nsresult startDocument() = 0;
94 
95   /**
96    * Signals to receive the start of an element.
97    *
98    * @param aPrefix the prefix of the element
99    * @param aLocalName the localname of the element
100    * @param aLowercaseName the localname of the element in lower case
101    * @param aNsID the namespace ID of the element
102    */
103   virtual nsresult startElement(nsAtom* aPrefix, nsAtom* aLocalName,
104                                 nsAtom* aLowercaseLocalName, int32_t aNsID) = 0;
105 
106   /**
107    * Signals to receive the start of an element. Can throw
108    * NS_ERROR_XSLT_BAD_NODE_NAME if the name is invalid
109    *
110    * @param aPrefix the prefix of the element
111    * @param aLocalName the localname of the element
112    * @param aNsID the namespace ID of the element
113    */
114   virtual nsresult startElement(nsAtom* aPrefix, const nsAString& aLocalName,
115                                 const int32_t aNsID) = 0;
116 };
117 
118 #define TX_DECL_TXAXMLEVENTHANDLER                                          \
119   virtual nsresult attribute(nsAtom* aPrefix, nsAtom* aLocalName,           \
120                              nsAtom* aLowercaseLocalName, int32_t aNsID,    \
121                              const nsString& aValue) override;              \
122   virtual nsresult attribute(nsAtom* aPrefix, const nsAString& aLocalName,  \
123                              const int32_t aNsID, const nsString& aValue)   \
124       override;                                                             \
125   virtual nsresult characters(const nsAString& aData, bool aDOE) override;  \
126   virtual nsresult comment(const nsString& aData) override;                 \
127   virtual nsresult endDocument(nsresult aResult = NS_OK) override;          \
128   virtual nsresult endElement() override;                                   \
129   virtual nsresult processingInstruction(const nsString& aTarget,           \
130                                          const nsString& aData) override;   \
131   virtual nsresult startDocument() override;                                \
132   virtual nsresult startElement(nsAtom* aPrefix, nsAtom* aLocalName,        \
133                                 nsAtom* aLowercaseLocalName, int32_t aNsID) \
134       override;                                                             \
135   virtual nsresult startElement(nsAtom* aPrefix, const nsAString& aName,    \
136                                 const int32_t aNsID) override;
137 
138 class txAOutputXMLEventHandler : public txAXMLEventHandler {
139  public:
140   /**
141    * Gets the Mozilla output document
142    *
143    * @param aDocument the Mozilla output document
144    */
145   virtual void getOutputDocument(mozilla::dom::Document** aDocument) = 0;
146 };
147 
148 #define TX_DECL_TXAOUTPUTXMLEVENTHANDLER \
149   virtual void getOutputDocument(mozilla::dom::Document** aDocument) override;
150 
151 /**
152  * Interface used to create the appropriate outputhandler
153  */
154 class txAOutputHandlerFactory {
155  public:
156   virtual ~txAOutputHandlerFactory() = default;
157 
158   /**
159    * Creates an outputhandler for the specified format.
160    * @param aFromat  format to get handler for
161    * @param aHandler outparam. The created handler
162    */
163   virtual nsresult createHandlerWith(txOutputFormat* aFormat,
164                                      txAXMLEventHandler** aHandler) = 0;
165 
166   /**
167    * Creates an outputhandler for the specified format, with the specified
168    * name and namespace for the root element.
169    * @param aFromat  format to get handler for
170    * @param aName    name of the root element
171    * @param aNsID    namespace-id of the root element
172    * @param aHandler outparam. The created handler
173    */
174   virtual nsresult createHandlerWith(txOutputFormat* aFormat,
175                                      const nsAString& aName, int32_t aNsID,
176                                      txAXMLEventHandler** aHandler) = 0;
177 };
178 
179 #define TX_DECL_TXAOUTPUTHANDLERFACTORY                                       \
180   nsresult createHandlerWith(txOutputFormat* aFormat,                         \
181                              txAXMLEventHandler** aHandler) override;         \
182   nsresult createHandlerWith(txOutputFormat* aFormat, const nsAString& aName, \
183                              int32_t aNsID, txAXMLEventHandler** aHandler)    \
184       override;
185 
186 #endif
187