MezzanineEngine 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
xpathquery.h
Go to the documentation of this file.
1 // © Copyright 2010 - 2014 BlackTopp Studios Inc.
2 /* This file is part of The Mezzanine Engine.
3 
4  The Mezzanine Engine is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  The Mezzanine Engine is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with The Mezzanine Engine. If not, see <http://www.gnu.org/licenses/>.
16 */
17 /* The original authors have included a copy of the license specified above in the
18  'Docs' folder. See 'gpl.txt'
19 */
20 /* We welcome the use of the Mezzanine engine to anyone, including companies who wish to
21  Build professional software and charge for their product.
22 
23  However there are some practical restrictions, so if your project involves
24  any of the following you should contact us and we will try to work something
25  out:
26  - DRM or Copy Protection of any kind(except Copyrights)
27  - Software Patents You Do Not Wish to Freely License
28  - Any Kind of Linking to Non-GPL licensed Works
29  - Are Currently In Violation of Another Copyright Holder's GPL License
30  - If You want to change our code and not add a few hundred MB of stuff to
31  your distribution
32 
33  These and other limitations could cause serious legal problems if you ignore
34  them, so it is best to simply contact us or the Free Software Foundation, if
35  you have any questions.
36 
37  Joseph Toppi - toppij@gmail.com
38  John Blackwood - makoenergy02@gmail.com
39 */
40 /*
41  *
42  * Software, Files, Libraries and all other items referenced in this clause refers only
43  * to the contents of this file and associated documentation.
44  *
45  * Pugixml parser - version 1.0
46  * --------------------------------------------------------
47  * Copyright © 2006-2012, by Arseny Kapoulkine (arseny.kapoulkine@gmail.com)
48  * Report bugs and download new versions at http://pugixml.org/
49  *
50  * This library is distributed under the MIT License. See notice at the end
51  * of this file.
52  *
53  * This work is based on the pugxml parser, which is:
54  * Copyright © 2003, by Kristen Wegner (kristen@tima.net)
55  */
56 #ifndef _xmlxpathquery_h
57 #define _xmlxpathquery_h
58 
59 /// @file
60 /// @brief The definition of the @ref Mezzanine::XML::XPathQuery .
61 
62 #include "datatypes.h"
63 #include "XML/xmlenumerations.h"
64 #include "XML/xpathparseresult.h"
65 
66 
67 
68 namespace Mezzanine
69 {
70  namespace XML
71  {
72  // A compiled XPath query object
73  class XPathNode;
74  class XPathNodeSet;
75  class XPathVariableSet;
76 
77 
78  /// @brief A compiled XPath query object
79  /// @details When you call @ref XML::Node::FindNodes with an expression string as an argument, a query object is created behind the scenes.
80  /// A query object represents a compiled XPath expression. Query objects can be needed in the following circumstances: \n
81  /// - You can precompile expressions to query objects to save compilation time if it becomes an issue; \n
82  /// - You can use query objects to evaluate XPath expressions which result in booleans, numbers or strings; \n
83  /// - You can get the type of expression value via query object. \n \n
84  /// Query objects correspond to @ref XML::XPathQuery type. They are immutable and non-copyable: they are bound to the expression at creation
85  /// time and can not be cloned. If you want to put query objects in a container, allocate them on heap via new operator and store pointers
86  /// to XML::XPathQuery in the container. \n \n
87  /// To evaluate an XPath expression there are a few EvaluatedType functions. According to XPath specification, value of any type can be
88  /// converted to boolean, number or string value, but no type other than node set can be converted to node set. Because of this,
89  /// @ref XPathQuery::EvaluateBoolean(), @ref XPathQuery::EvaluateNumber() and @ref XPathQuery::EvaluateString() always return a result,
90  /// but EvaluateNodeSet results in an error if the return type is not node set.
92  {
93  private:
94  /// @internal
95  /// @brief Used to keep much of the implementation hidden from item including this.
96  void* QueryImplementation;
97 
98  /// @internal
99  /// @brief A cache for the result of the query
100  XPathParseResult ResultCache ;
101 
102  #ifndef SWIG
103  /// @internal
104  /// @brief Used to convert this to a boolean value in a safe way
105  typedef void (*unspecified_bool_type)(XPathQuery***);
106  #endif
107 
108  /// @brief Private copy constructor to ensure non-copyable semantics.
109  XPathQuery(const XPathQuery&);
110 
111  /// @brief Private assignment operator to ensure non-assignable semantics.
112  XPathQuery& operator=(const XPathQuery&);
113 
114  public:
115  /// @brief Construct a compiled object from XPath expression.
116  /// @param query The query in the form of a c-string style char_t array.
117  /// @param variables Any extra data the query might need, passing a null pointer simply omits passing any arguments.
118  /// @throw Throws XPathException on compilation errors.
119  explicit XPathQuery(const Char8* query, XPathVariableSet* variables = 0);
120 
121  // Destructor
122  /// @brief Destructor
123  ~XPathQuery();
124 
125  /// @brief Get query expression return Type.
126  /// @return A XPathValueType.
127  XPathValueType ReturnType() const;
128 
129  /// @brief Evaluate expression as boolean value in the specified context; performs Type conversion if necessary.
130  /// @return A bool result of evaluating the expression.
131  /// @throw If XML_NO_EXCEPTIONS is not defined (by default it is not defined), throws std::bad_alloc on out of memory errors.
132  /// @param n The XPathNode that will serve as the context for the query.
133  bool EvaluateBoolean(const XPathNode& n) const;
134 
135  // Evaluate expression as double Value in the specified context; performs Type conversion if necessary.
136  /// @brief Evaluate expression as double value in the specified context; performs Type conversion if necessary.
137  /// @param n The XPathNode that will serve as the context for the query.
138  /// @throw Throws std::bad_alloc on out of memory errors.
139  /// @return A result as a double from evaluating the expression.
140  double EvaluateNumber(const XPathNode& n) const;
141 
142  // Evaluate expression as string Value in the specified context; performs Type conversion if necessary.
143  /// @brief Evaluate expression as string value in the specified context; performs Type conversion if necessary.
144  /// @param n The XPathNode that will serve as the context for the query.
145  /// @throw Throws std::bad_alloc on out of memory errors.
146  /// @return A result as a String from evaluating the expression.
147  String EvaluateString(const XPathNode& n) const;
148 
149  // Evaluate expression as string Value in the specified context; performs Type conversion if necessary.
150  // At most capacity characters are written to the destination buffer, full Result size is returned (includes terminating zero).
151  /// @brief Evaluate expression as string value in the specified context; performs Type conversion if necessary.
152  /// @param buffer The place to store the c-style Character array
153  /// @param capacity At most capacity characters are written to the destination buffer.
154  /// @param n The XPathNode that with serve as the context for the query.
155  /// @throw std::bad_alloc on out of memory errors.
156  /// @return Full result size is returned (includes terminating zero).
157  size_t EvaluateString(Char8* buffer, size_t capacity, const XPathNode& n) const;
158 
159  // Evaluate expression as node set in the specified context.
160  // throws XPathException on Type mismatch and std::bad_alloc on out of memory errors.
161 
162  /// @brief Evaluate expression as node set in the specified context.
163  /// @param n The XPathNode that with serve as the context for the query.
164  /// @return An XPathNodeSet.
165  XPathNodeSet EvaluateNodeSet(const XPathNode& n) const;
166 
167  /// @brief Get parsing Result (used to get compilation errors when XML_NO_EXCEPTIONS is enabled)
168  /// @return A const reference to an XPathParseResult.
169  const XPathParseResult& Result() const;
170 
171  #ifndef SWIG
172  /// @brief Used to convert this to a boolean value in a safe way
173  /// @return Returns true if the internal data is set and false otherwise.
174  operator unspecified_bool_type() const;
175  #endif
176 
177  /// @brief Logical not operator, used a workaround for borland compiler.
178  /// @return A bool that is the opposite of evaluatig this as a bool normally.
179  bool operator!() const;
180  };
181  }
182 } // /namespace Mezzanine
183 
184 
185 
186 #endif // Include guard
187 
188 
189 /*
190  *
191  * Software, Files, Libraries and all other items referenced in this clause refers only
192  * to the contents of this file and associated documentation.
193  *
194  * Copyright © 2006-2012 Arseny Kapoulkine
195  *
196  * Permission is hereby granted, free of charge, to any person
197  * obtaining a copy of this software and associated documentation
198  * files (the "Software"), to deal in the Software without
199  * restriction, including without limitation the rights to use,
200  * copy, modify, merge, publish, distribute, sublicense, and/or sell
201  * copies of the Software, and to permit persons to whom the
202  * Software is furnished to do so, subject to the following
203  * conditions:
204  *
205  * The above copyright notice and this permission notice shall be
206  * included in all copies or substantial portions of the Software.
207  *
208  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
209  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
210  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
211  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
212  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
213  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
214  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
215  * OTHER DEALINGS IN THE SOFTWARE.
216  */