/src/poco/XML/include/Poco/SAX/InputSource.h
Line | Count | Source |
1 | | // |
2 | | // InputSource.h |
3 | | // |
4 | | // Library: XML |
5 | | // Package: SAX |
6 | | // Module: SAX |
7 | | // |
8 | | // SAX InputSource - A single input source for an XML entity. |
9 | | // |
10 | | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
11 | | // and Contributors. |
12 | | // |
13 | | // SPDX-License-Identifier: BSL-1.0 |
14 | | // |
15 | | |
16 | | |
17 | | #ifndef SAX_InputSource_INCLUDED |
18 | | #define SAX_InputSource_INCLUDED |
19 | | |
20 | | |
21 | | #include "Poco/XML/XML.h" |
22 | | #include "Poco/XML/XMLString.h" |
23 | | #include "Poco/XML/XMLStream.h" |
24 | | |
25 | | |
26 | | namespace Poco::XML { |
27 | | |
28 | | |
29 | | class XML_API InputSource |
30 | | /// This class allows a SAX application to encapsulate information about an input |
31 | | /// source in a single object, which may include a public identifier, a system |
32 | | /// identifier, a byte stream (possibly with a specified encoding), and/or a character |
33 | | /// stream. |
34 | | /// |
35 | | /// There are two places that the application can deliver an input source to the |
36 | | /// parser: as the argument to the Parser.parse method, or as the return value of the |
37 | | /// EntityResolver::resolveEntity() method. |
38 | | /// |
39 | | /// The SAX parser will use the InputSource object to determine how to read XML input. |
40 | | /// If there is a character stream available, the parser will read that stream directly, |
41 | | /// disregarding any text encoding declaration found in that stream. If there is no character |
42 | | /// stream, but there is a byte stream, the parser will use that byte stream, using the |
43 | | /// encoding specified in the InputSource or else (if no encoding is specified) autodetecting |
44 | | /// the character encoding using an algorithm such as the one in the XML specification. |
45 | | /// If neither a character stream nor a byte stream is available, the parser will attempt |
46 | | /// to open a URI connection to the resource identified by the system identifier. |
47 | | /// |
48 | | /// An InputSource object belongs to the application: the SAX parser shall never modify it in |
49 | | /// any way (it may modify a copy if necessary). However, standard processing of both byte and |
50 | | /// character streams is to close them on as part of end-of-parse cleanup, so applications should |
51 | | /// not attempt to re-use such streams after they have been handed to a parser. |
52 | | { |
53 | | public: |
54 | | InputSource(); |
55 | | /// Zero-argument default constructor. |
56 | | |
57 | | InputSource(const XMLString& systemId); |
58 | | /// Creates a new input source with a system identifier. |
59 | | /// Applications may use setPublicId to include a public identifier as well, |
60 | | /// or setEncoding to specify the character encoding, if known. |
61 | | /// |
62 | | /// If the system identifier is a URL, it must be fully resolved (it may not |
63 | | /// be a relative URL). |
64 | | |
65 | | InputSource(XMLByteInputStream& istr); |
66 | | /// Creates a new input source with a byte stream. |
67 | | /// |
68 | | /// Application writers should use setSystemId() to provide a base for resolving |
69 | | /// relative URIs, may use setPublicId to include a public identifier, and may use |
70 | | /// setEncoding to specify the object's character encoding. |
71 | | |
72 | | ~InputSource(); |
73 | | /// Destroys the InputSource. |
74 | | |
75 | | void setPublicId(const XMLString& publicId); |
76 | | /// Set the public identifier for this input source. |
77 | | /// |
78 | | /// The public identifier is always optional: if the application writer includes one, |
79 | | /// it will be provided as part of the location information. |
80 | | |
81 | | void setSystemId(const XMLString& systemId); |
82 | | /// Set the system identifier for this input source. |
83 | | /// |
84 | | /// The system identifier is optional if there is a byte stream or a character stream, |
85 | | /// but it is still useful to provide one, since the application can use it to resolve |
86 | | /// relative URIs and can include it in error messages and warnings (the parser will |
87 | | /// attempt to open a connection to the URI only if there is no byte stream or character |
88 | | /// stream specified). |
89 | | /// |
90 | | /// If the application knows the character encoding of the object pointed to by the system |
91 | | /// identifier, it can register the encoding using the setEncoding method. |
92 | | /// |
93 | | /// If the system identifier is a URL, it must be fully resolved (it may not be a relative URL). |
94 | | |
95 | | [[nodiscard]] const XMLString& getPublicId() const; |
96 | | /// Get the public identifier for this input source. |
97 | | |
98 | | [[nodiscard]] const XMLString& getSystemId() const; |
99 | | /// Get the system identifier for this input source. |
100 | | |
101 | | void setByteStream(XMLByteInputStream& istr); |
102 | | /// Set the byte stream for this input source. |
103 | | /// The SAX parser will ignore this if there is also a character stream specified, but it |
104 | | /// will use a byte stream in preference to opening a URI connection itself. |
105 | | |
106 | | [[nodiscard]] XMLByteInputStream* getByteStream() const; |
107 | | /// Get the byte stream for this input source. |
108 | | |
109 | | void setCharacterStream(XMLCharInputStream& istr); |
110 | | /// Set the character stream for this input source. |
111 | | |
112 | | [[nodiscard]] XMLCharInputStream* getCharacterStream() const; |
113 | | /// Get the character stream for this input source. |
114 | | |
115 | | void setEncoding(const XMLString& encoding); |
116 | | /// Set the character encoding, if known. |
117 | | /// The encoding must be a string acceptable for an XML encoding declaration |
118 | | /// (see section 4.3.3 of the XML 1.0 recommendation). |
119 | | |
120 | | [[nodiscard]] const XMLString& getEncoding() const; |
121 | | /// Get the character encoding for a byte stream or URI. |
122 | | |
123 | | private: |
124 | | XMLString _publicId; |
125 | | XMLString _systemId; |
126 | | XMLString _encoding; |
127 | | XMLByteInputStream* _bistr; |
128 | | XMLCharInputStream* _cistr; |
129 | | }; |
130 | | |
131 | | |
132 | | // |
133 | | // inlines |
134 | | // |
135 | | inline const XMLString& InputSource::getPublicId() const |
136 | 175k | { |
137 | 175k | return _publicId; |
138 | 175k | } |
139 | | |
140 | | |
141 | | inline const XMLString& InputSource::getSystemId() const |
142 | 175k | { |
143 | 175k | return _systemId; |
144 | 175k | } |
145 | | |
146 | | |
147 | | inline const XMLString& InputSource::getEncoding() const |
148 | 0 | { |
149 | 0 | return _encoding; |
150 | 0 | } |
151 | | |
152 | | |
153 | | inline XMLByteInputStream* InputSource::getByteStream() const |
154 | 352k | { |
155 | 352k | return _bistr; |
156 | 352k | } |
157 | | |
158 | | |
159 | | inline XMLCharInputStream* InputSource::getCharacterStream() const |
160 | 117k | { |
161 | 117k | return _cistr; |
162 | 117k | } |
163 | | |
164 | | |
165 | | } // namespace Poco::XML |
166 | | |
167 | | |
168 | | #endif // SAX_InputSource_INCLUDED |