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