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_macros.h" 45 #include "include/base/cef_ref_counted.h" 46 #include "include/cef_base.h" 47 #include "include/cef_xml_reader.h" 48 49 class CefStreamReader; 50 51 /// 52 // Thread safe class for representing XML data as a structured object. This 53 // class should not be used with large XML documents because all data will be 54 // resident in memory at the same time. This implementation supports a 55 // restricted set of XML features: 56 // <pre> 57 // (1) Processing instructions, whitespace and comments are ignored. 58 // (2) Elements and attributes must always be referenced using the fully 59 // qualified name (ie, namespace:localname). 60 // (3) Empty elements (<a/>) and elements with zero-length values (<a></a>) 61 // are considered the same. 62 // (4) Element nodes are considered part of a value if: 63 // (a) The element node follows a non-element node at the same depth 64 // (see 5), or 65 // (b) The element node does not have a namespace and the parent node does. 66 // (5) Mixed node types at the same depth are combined into a single element 67 // value as follows: 68 // (a) All node values are concatenated to form a single string value. 69 // (b) Entity reference nodes are resolved to the corresponding entity 70 // value. 71 // (c) Element nodes are represented by their outer XML string. 72 // </pre> 73 /// 74 class CefXmlObject : public base::RefCountedThreadSafe<CefXmlObject> { 75 public: 76 typedef std::vector<CefRefPtr<CefXmlObject>> ObjectVector; 77 typedef std::map<CefString, CefString> AttributeMap; 78 79 /// 80 // Create a new object with the specified name. An object name must always be 81 // at least one character long. 82 /// 83 explicit CefXmlObject(const CefString& name); 84 85 /// 86 // Load the contents of the specified XML stream into this object. The 87 // existing children and attributes, if any, will first be cleared. 88 /// 89 bool Load(CefRefPtr<CefStreamReader> stream, 90 CefXmlReader::EncodingType encodingType, 91 const CefString& URI, 92 CefString* loadError); 93 94 /// 95 // Set the name, children and attributes of this object to a duplicate of the 96 // specified object's contents. The existing children and attributes, if any, 97 // will first be cleared. 98 /// 99 void Set(CefRefPtr<CefXmlObject> object); 100 101 /// 102 // Append a duplicate of the children and attributes of the specified object 103 // to this object. If |overwriteAttributes| is true then any attributes in 104 // this object that also exist in the specified object will be overwritten 105 // with the new values. The name of this object is not changed. 106 /// 107 void Append(CefRefPtr<CefXmlObject> object, bool overwriteAttributes); 108 109 /// 110 // Return a new object with the same name, children and attributes as this 111 // object. The parent of the new object will be NULL. 112 /// 113 CefRefPtr<CefXmlObject> Duplicate(); 114 115 /// 116 // Clears this object's children and attributes. The name and parenting of 117 // this object are not changed. 118 /// 119 void Clear(); 120 121 /// 122 // Access the object's name. An object name must always be at least one 123 // character long. 124 /// 125 CefString GetName(); 126 bool SetName(const CefString& name); 127 128 /// 129 // Access the object's parent. The parent can be NULL if this object has not 130 // been added as the child on another object. 131 /// 132 bool HasParent(); 133 CefRefPtr<CefXmlObject> GetParent(); 134 135 /// 136 // Access the object's value. An object cannot have a value if it also has 137 // children. Attempting to set the value while children exist will fail. 138 /// 139 bool HasValue(); 140 CefString GetValue(); 141 bool SetValue(const CefString& value); 142 143 /// 144 // Access the object's attributes. Attributes must have unique names. 145 /// 146 bool HasAttributes(); 147 size_t GetAttributeCount(); 148 bool HasAttribute(const CefString& name); 149 CefString GetAttributeValue(const CefString& name); 150 bool SetAttributeValue(const CefString& name, const CefString& value); 151 size_t GetAttributes(AttributeMap& attributes); 152 void ClearAttributes(); 153 154 /// 155 // Access the object's children. Each object can only have one parent so 156 // attempting to add an object that already has a parent will fail. Removing a 157 // child will set the child's parent to NULL. Adding a child will set the 158 // child's parent to this object. This object's value, if any, will be cleared 159 // if a child is added. 160 /// 161 bool HasChildren(); 162 size_t GetChildCount(); 163 bool HasChild(CefRefPtr<CefXmlObject> child); 164 bool AddChild(CefRefPtr<CefXmlObject> child); 165 bool RemoveChild(CefRefPtr<CefXmlObject> child); 166 size_t GetChildren(ObjectVector& children); 167 void ClearChildren(); 168 169 /// 170 // Find the first child with the specified name. 171 /// 172 CefRefPtr<CefXmlObject> FindChild(const CefString& name); 173 174 /// 175 // Find all children with the specified name. 176 /// 177 size_t FindChildren(const CefString& name, ObjectVector& children); 178 179 private: 180 // Protect against accidental deletion of this object. 181 friend class base::RefCountedThreadSafe<CefXmlObject>; 182 ~CefXmlObject(); 183 184 void SetParent(CefXmlObject* parent); 185 186 CefString name_; 187 CefXmlObject* parent_; 188 CefString value_; 189 AttributeMap attributes_; 190 ObjectVector children_; 191 192 base::Lock lock_; 193 194 DISALLOW_COPY_AND_ASSIGN(CefXmlObject); 195 }; 196 197 #endif // CEF_INCLUDE_WRAPPER_CEF_XML_OBJECT_H_ 198