1 // Copyright (c) 2011 Marshall A. Greenblatt. All rights reserved.
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are
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
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.
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.
30 // ---------------------------------------------------------------------------
32 // The contents of this file are only available to applications that link
33 // against the libcef_dll_wrapper target.
36 #ifndef CEF_INCLUDE_WRAPPER_CEF_XML_OBJECT_H_
37 #define CEF_INCLUDE_WRAPPER_CEF_XML_OBJECT_H_
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"
49 class CefStreamReader;
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:
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
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
68 // (a) All node values are concatenated to form a single string value.
69 // (b) Entity reference nodes are resolved to the corresponding entity
71 // (c) Element nodes are represented by their outer XML string.
74 class CefXmlObject : public base::RefCountedThreadSafe<CefXmlObject> {
76 typedef std::vector<CefRefPtr<CefXmlObject> > ObjectVector;
77 typedef std::map<CefString, CefString > AttributeMap;
80 // Create a new object with the specified name. An object name must always be
81 // at least one character long.
83 explicit CefXmlObject(const CefString& name);
86 // Load the contents of the specified XML stream into this object. The
87 // existing children and attributes, if any, will first be cleared.
89 bool Load(CefRefPtr<CefStreamReader> stream,
90 CefXmlReader::EncodingType encodingType,
91 const CefString& URI, CefString* loadError);
94 // Set the name, children and attributes of this object to a duplicate of the
95 // specified object's contents. The existing children and attributes, if any,
96 // will first be cleared.
98 void Set(CefRefPtr<CefXmlObject> object);
101 // Append a duplicate of the children and attributes of the specified object
102 // to this object. If |overwriteAttributes| is true then any attributes in
103 // this object that also exist in the specified object will be overwritten
104 // with the new values. The name of this object is not changed.
106 void Append(CefRefPtr<CefXmlObject> object, bool overwriteAttributes);
109 // Return a new object with the same name, children and attributes as this
110 // object. The parent of the new object will be NULL.
112 CefRefPtr<CefXmlObject> Duplicate();
115 // Clears this object's children and attributes. The name and parenting of
116 // this object are not changed.
121 // Access the object's name. An object name must always be at least one
125 bool SetName(const CefString& name);
128 // Access the object's parent. The parent can be NULL if this object has not
129 // been added as the child on another object.
132 CefRefPtr<CefXmlObject> GetParent();
135 // Access the object's value. An object cannot have a value if it also has
136 // children. Attempting to set the value while children exist will fail.
139 CefString GetValue();
140 bool SetValue(const CefString& value);
143 // Access the object's attributes. Attributes must have unique names.
145 bool HasAttributes();
146 size_t GetAttributeCount();
147 bool HasAttribute(const CefString& name);
148 CefString GetAttributeValue(const CefString& name);
149 bool SetAttributeValue(const CefString& name, const CefString& value);
150 size_t GetAttributes(AttributeMap& attributes);
151 void ClearAttributes();
154 // Access the object's children. Each object can only have one parent so
155 // attempting to add an object that already has a parent will fail. Removing a
156 // child will set the child's parent to NULL. Adding a child will set the
157 // child's parent to this object. This object's value, if any, will be cleared
158 // if a child is added.
161 size_t GetChildCount();
162 bool HasChild(CefRefPtr<CefXmlObject> child);
163 bool AddChild(CefRefPtr<CefXmlObject> child);
164 bool RemoveChild(CefRefPtr<CefXmlObject> child);
165 size_t GetChildren(ObjectVector& children);
166 void ClearChildren();
169 // Find the first child with the specified name.
171 CefRefPtr<CefXmlObject> FindChild(const CefString& name);
174 // Find all children with the specified name.
176 size_t FindChildren(const CefString& name, ObjectVector& children);
179 // Protect against accidental deletion of this object.
180 friend class base::RefCountedThreadSafe<CefXmlObject>;
183 void SetParent(CefXmlObject* parent);
186 CefXmlObject* parent_;
188 AttributeMap attributes_;
189 ObjectVector children_;
193 DISALLOW_COPY_AND_ASSIGN(CefXmlObject);
196 #endif // CEF_INCLUDE_WRAPPER_CEF_XML_OBJECT_H_