1 // Copyright (c) 2011 Marshall A. Greenblatt. All rights reserved. 2 // 3 // Redistribution and use in source and binary forms, with or without 4 // modification, are permitted provided that the following conditions are 5 // met: 6 // 7 // * Redistributions of source code must retain the above copyright 8 // notice, this list of conditions and the following disclaimer. 9 // * Redistributions in binary form must reproduce the above 10 // copyright notice, this list of conditions and the following disclaimer 11 // in the documentation and/or other materials provided with the 12 // distribution. 13 // * Neither the name of Google Inc. nor the name Chromium Embedded 14 // Framework nor the names of its contributors may be used to endorse 15 // or promote products derived from this software without specific prior 16 // written permission. 17 // 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 // 30 // --------------------------------------------------------------------------- 31 // 32 // The contents of this file are only available to applications that link 33 // against the libcef_dll_wrapper target. 34 // 35 36 #ifndef CEF_INCLUDE_WRAPPER_CEF_XML_OBJECT_H_ 37 #define CEF_INCLUDE_WRAPPER_CEF_XML_OBJECT_H_ 38 #pragma once 39 40 #include <map> 41 #include <vector> 42 43 #include "include/base/cef_lock.h" 44 #include "include/base/cef_ref_counted.h" 45 #include "include/cef_base.h" 46 #include "include/cef_xml_reader.h" 47 48 class CefStreamReader; 49 50 /// 51 // Thread safe class for representing XML data as a structured object. This 52 // class should not be used with large XML documents because all data will be 53 // resident in memory at the same time. This implementation supports a 54 // restricted set of XML features: 55 // <pre> 56 // (1) Processing instructions, whitespace and comments are ignored. 57 // (2) Elements and attributes must always be referenced using the fully 58 // qualified name (ie, namespace:localname). 59 // (3) Empty elements (<a/>) and elements with zero-length values (<a></a>) 60 // are considered the same. 61 // (4) Element nodes are considered part of a value if: 62 // (a) The element node follows a non-element node at the same depth 63 // (see 5), or 64 // (b) The element node does not have a namespace and the parent node does. 65 // (5) Mixed node types at the same depth are combined into a single element 66 // value as follows: 67 // (a) All node values are concatenated to form a single string value. 68 // (b) Entity reference nodes are resolved to the corresponding entity 69 // value. 70 // (c) Element nodes are represented by their outer XML string. 71 // </pre> 72 /// 73 class CefXmlObject : public base::RefCountedThreadSafe<CefXmlObject> { 74 public: 75 using ObjectVector = std::vector<CefRefPtr<CefXmlObject>>; 76 using AttributeMap = std::map<CefString, CefString>; 77 78 /// 79 // Create a new object with the specified name. An object name must always be 80 // at least one character long. 81 /// 82 explicit CefXmlObject(const CefString& name); 83 84 CefXmlObject(const CefXmlObject&) = delete; 85 CefXmlObject& operator=(const CefXmlObject&) = delete; 86 87 /// 88 // Load the contents of the specified XML stream into this object. The 89 // existing children and attributes, if any, will first be cleared. 90 /// 91 bool Load(CefRefPtr<CefStreamReader> stream, 92 CefXmlReader::EncodingType encodingType, 93 const CefString& URI, 94 CefString* loadError); 95 96 /// 97 // Set the name, children and attributes of this object to a duplicate of the 98 // specified object's contents. The existing children and attributes, if any, 99 // will first be cleared. 100 /// 101 void Set(CefRefPtr<CefXmlObject> object); 102 103 /// 104 // Append a duplicate of the children and attributes of the specified object 105 // to this object. If |overwriteAttributes| is true then any attributes in 106 // this object that also exist in the specified object will be overwritten 107 // with the new values. The name of this object is not changed. 108 /// 109 void Append(CefRefPtr<CefXmlObject> object, bool overwriteAttributes); 110 111 /// 112 // Return a new object with the same name, children and attributes as this 113 // object. The parent of the new object will be NULL. 114 /// 115 CefRefPtr<CefXmlObject> Duplicate(); 116 117 /// 118 // Clears this object's children and attributes. The name and parenting of 119 // this object are not changed. 120 /// 121 void Clear(); 122 123 /// 124 // Access the object's name. An object name must always be at least one 125 // character long. 126 /// 127 CefString GetName(); 128 bool SetName(const CefString& name); 129 130 /// 131 // Access the object's parent. The parent can be NULL if this object has not 132 // been added as the child on another object. 133 /// 134 bool HasParent(); 135 CefRefPtr<CefXmlObject> GetParent(); 136 137 /// 138 // Access the object's value. An object cannot have a value if it also has 139 // children. Attempting to set the value while children exist will fail. 140 /// 141 bool HasValue(); 142 CefString GetValue(); 143 bool SetValue(const CefString& value); 144 145 /// 146 // Access the object's attributes. Attributes must have unique names. 147 /// 148 bool HasAttributes(); 149 size_t GetAttributeCount(); 150 bool HasAttribute(const CefString& name); 151 CefString GetAttributeValue(const CefString& name); 152 bool SetAttributeValue(const CefString& name, const CefString& value); 153 size_t GetAttributes(AttributeMap& attributes); 154 void ClearAttributes(); 155 156 /// 157 // Access the object's children. Each object can only have one parent so 158 // attempting to add an object that already has a parent will fail. Removing a 159 // child will set the child's parent to NULL. Adding a child will set the 160 // child's parent to this object. This object's value, if any, will be cleared 161 // if a child is added. 162 /// 163 bool HasChildren(); 164 size_t GetChildCount(); 165 bool HasChild(CefRefPtr<CefXmlObject> child); 166 bool AddChild(CefRefPtr<CefXmlObject> child); 167 bool RemoveChild(CefRefPtr<CefXmlObject> child); 168 size_t GetChildren(ObjectVector& children); 169 void ClearChildren(); 170 171 /// 172 // Find the first child with the specified name. 173 /// 174 CefRefPtr<CefXmlObject> FindChild(const CefString& name); 175 176 /// 177 // Find all children with the specified name. 178 /// 179 size_t FindChildren(const CefString& name, ObjectVector& children); 180 181 private: 182 // Protect against accidental deletion of this object. 183 friend class base::RefCountedThreadSafe<CefXmlObject>; 184 ~CefXmlObject(); 185 186 void SetParent(CefXmlObject* parent); 187 188 CefString name_; 189 CefXmlObject* parent_; 190 CefString value_; 191 AttributeMap attributes_; 192 ObjectVector children_; 193 194 base::Lock lock_; 195 }; 196 197 #endif // CEF_INCLUDE_WRAPPER_CEF_XML_OBJECT_H_ 198