Debian Codegraph @

core/libs/dngwriter/extra/xmp_sdk/source/XMLParserAdapter.hpp

raw
1 #ifndef __XMLParserAdapter_hpp__
2 #define __XMLParserAdapter_hpp__
3
4 // =================================================================================================
5 // Copyright 2005 Adobe
6 // All Rights Reserved.
7 //
8 // NOTICE: Adobe permits you to use, modify, and distribute this file in accordance with the terms
9 // of the Adobe license agreement accompanying it. If you have received this file from a source other
10 // than Adobe, then your use, modification, or distribution of it requires the prior written permission
11 // of Adobe.
12 // =================================================================================================
13
14 #if AdobePrivate
15 // =================================================================================================
16 // Change history
17 // ==============
18 //
19 // Writers:
20 // AWL Alan Lillich
21 // ADC Amandeep Chawla
22 // IJS Inder Jeet Singh
23 //
24 // mm-dd-yy who Description of changes, most recent on top.
25 //
26 // 03-19-14 IJS 5.6-f098 [3709896] P2 Handler Modified to support spanned clip.
27 // 10-10-12 ADC 5.5-c012 Changed internal implementation of common error notification infrastructure.
28 // 08-08-12 AWL 5.5-c007 XMPCore error notifications for one case of XML parsing, no existing test failures.
29 //
30 // 01-16-08 AWL 4.2-c035 Add XML_Node::SetAttrValue.
31 // 12-18-07 AWL 4.2-c034 Add XML_Node::IsEmptyLeafNode and XML_Node::GetAttrValue.
32 // 12-10-07 AWL 4.2-c033 Save prefix length in XML_Node, change XML_Node::GetNamedElement to take URI and local name.
33 // 12-07-07 AWL 4.2-c032 Add serialization and tree dump for the exposed Expat parser.
34 // 11-30-07 AWL 4.2-c027 Expose XML_Node and ExpatAdapter so that XMPFiles can use them.
35 //
36 // 08-16-06 AWL 4.1-c015 [1312441,1340116] Change the parsing code to tolerate ISO Latin-1 input. At
37 // the same time, get rid of the ugly hackery that tries to deal with unescaped ASCII
38 // control characters. Also get rid of the check and complaint about parsing into an
39 // empty object. This is predominantly what people want, so clear the XMPMeta object.
40 //
41 // 03-24-06 AWL 4.0-c001 Adapt for move to ham-perforce, integrate XMPFiles, bump version to 4.
42 //
43 // 04-06-05 AWL 3.2-013 [0509601] Normalize "single value" alt-text arrays. Improve the way the root
44 // XML node is found and extract the previous toolkit version number.
45 // 03-31-05 AWL 3.2-007 Fix parsing for UTF-16 and UTF-32.
46 // 02-03-05 AWL Initial creation.
47 //
48 // =================================================================================================
49 #endif // AdobePrivate
50
51 #include "public/include/XMP_Environment.h" // ! Must be the first #include!
52 #include "public/include/XMP_Const.h"
53
54 #include "source/XMP_LibUtils.hpp"
55
56 #include <string>
57 #include <vector>
58
59 // =================================================================================================
60 // XML_Node details
61 //
62 // The XML_Nodes are used only during the XML/RDF parsing process. This presently uses an XML parser
63 // to create an XML tree, then a recursive descent RDF recognizer to build the corresponding XMP.
64 // This makes it easier to swap XML parsers and provides a clean separation of XML and RDF issues.
65 // The overall parsing would be faster and use less memory if the RDF recognition were done on the
66 // fly using a state machine. But it was much easier to write the recursive descent version. The
67 // current implementation is pretty fast in absolute terms, so being faster might not be crucial.
68 //
69 // Like the XMP tree, the XML tree contains vectors of pointers for down links, and offspring have
70 // a pointer to their parent. Unlike the XMP tree, this is an exact XML document tree. There are no
71 // introduced top level namespace nodes or rearrangement of the nodes..
72 //
73 // The exact state of namespaces can vary during the XML parsing, depending on the parser in use.
74 // By the time the RDF recognition is done though, the namespaces must be normalized. All of the
75 // used namespaces must be registered, this is done automatically if necessary. All of the "live"
76 // namespace prefixes will be unique. The ns field of an XML_Node is the namespace URI, the name
77 // field contains a qualified name (prefix:local). This includes default namespace mapping, the
78 // URI and prefix will be missing only for elements and attributes in no namespace.
79
80 class XML_Node;
81
82 typedef XML_Node * XML_NodePtr; // Handy for things like: XML_Node * a, b; - b is XML_Node, not XML_Node*!
83
84 enum { kRootNode = 0, kElemNode = 1, kAttrNode = 2, kCDataNode = 3, kPINode = 4 };
85
86 #define IsWhitespaceChar(ch) ( ((ch) == ' ') || ((ch) == 0x09) || ((ch) == 0x0A) || ((ch) == 0x0D) )
87
88 typedef std::vector<XML_NodePtr> XML_NodeVector;
89 typedef XML_NodeVector::iterator XML_NodePos;
90 typedef XML_NodeVector::const_iterator XML_cNodePos;
91
92 #if 0 // Pattern for iterating over the children or attributes:
93 for ( size_t xxNum = 0, xxLim = _node_->_offspring_.size(); xxNum < xxLim; ++xxNum ) {
94 const XML_NodePtr _curr_ = _node_->_offspring_[xxNum];
95 }
96 #endif
97
98 class XML_Node {
99 public:
100
101 // Intended for lightweight internal use. Clients are expected to use the data directly.
102
103 XMP_Uns8 kind;
104 std::string ns, name, value;
105 size_t nsPrefixLen;
106 XML_NodePtr parent;
107 XML_NodeVector attrs;
108 XML_NodeVector content;
109
110 bool IsWhitespaceNode() const;
111 bool IsLeafContentNode() const; // An empty element or one with a single character data child node.
112 bool IsEmptyLeafNode() const;
113
114 XMP_StringPtr GetAttrValue ( XMP_StringPtr attrName ) const;
115 void SetAttrValue ( XMP_StringPtr attrName, XMP_StringPtr attrValue );
116
117 XMP_StringPtr GetLeafContentValue() const;
118 std::string* GetLeafContentPtr() const;
119 void SetLeafContentValue ( XMP_StringPtr value );
120
121 size_t CountNamedElements ( XMP_StringPtr nsURI, XMP_StringPtr localName ) const; // Number of child elements with this name.
122 XML_NodePtr GetNamedElement ( XMP_StringPtr nsURI, XMP_StringPtr localName, size_t which = 0 );
123
124 void Dump ( std::string * buffer );
125 void Serialize ( std::string * buffer );
126
127 void RemoveAttrs();
128 void RemoveContent();
129 void ClearNode();
130
131 XML_Node ( XML_NodePtr _parent, XMP_StringPtr _name, XMP_Uns8 _kind )
132 : kind(_kind), name(_name), parent(_parent), nsPrefixLen(0) {};
133
134 XML_Node ( XML_NodePtr _parent, const std::string & _name, XMP_Uns8 _kind )
135 : kind(_kind), name(_name), parent(_parent), nsPrefixLen(0) {};
136
137 virtual ~XML_Node() { RemoveAttrs(); RemoveContent(); };
138
139 private:
140
141 XML_Node() : kind(0), parent(0) {}; // ! Hidden to make sure parent pointer is always set.
142
143 };
144
145 // =================================================================================================
146 // Abstract base class for XML parser adapters used by the XMP toolkit.
147
148 enum { kXMLPendingInputMax = 16 };
149
150 class XMLParserAdapter {
151 public:
152
153 XMLParserAdapter() : tree(0,"",kRootNode), rootNode(0), rootCount(0),
154 charEncoding(XMP_OptionBits(-1)), pendingCount(0),
155 errorCallback(0)
156 {
157 #if XMP_DebugBuild
158 parseLog = 0;
159 #endif
160 };
161
162 virtual ~XMLParserAdapter() {};
163
164 virtual void ParseBuffer ( const void * buffer, size_t length, bool last ) = 0;
165
166 virtual void SetErrorCallback ( GenericErrorCallback * ec )
167 { this->errorCallback = ec; };
168
169 virtual void NotifyClient ( XMP_ErrorSeverity severity, XMP_Error & error )
170 {
171 if (this->errorCallback)
172 this->errorCallback->NotifyClient( severity, error );
173 }
174
175 XML_Node tree;
176 XML_NodeVector parseStack;
177 XML_NodePtr rootNode;
178 size_t rootCount;
179
180 XMP_OptionBits charEncoding;
181 size_t pendingCount;
182 unsigned char pendingInput[kXMLPendingInputMax]; // Buffered input for character encoding checks.
183
184 GenericErrorCallback * errorCallback; // Set if the relevant XMPCore or XMPFiles object has one.
185
186 #if XMP_DebugBuild
187 FILE * parseLog;
188 #endif
189
190 };
191
192 // =================================================================================================
193
194 #endif // __XMLParserAdapter_hpp__