diagnostic.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2006-2008 The FLWOR Foundation.
00003  * 
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  * 
00008  * http://www.apache.org/licenses/LICENSE-2.0
00009  * 
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #ifndef ZORBA_DIAGNOSTIC_API_H
00018 #define ZORBA_DIAGNOSTIC_API_H
00019 
00020 #include <cstring>
00021 #include <iostream>
00022 
00023 #include <zorba/config.h>
00024 
00025 namespace zorba {
00026 namespace diagnostic {
00027 
00028 ///////////////////////////////////////////////////////////////////////////////
00029 
00030 /**
00031  * A %QName is the abstract base class for a QName.
00032  */
00033 class ZORBA_DLL_PUBLIC QName {
00034 public:
00035   virtual ~QName();
00036 
00037   /**
00038    * Gets this QName's namespace URI.
00039    *
00040    * @return Returns said URI.
00041    */
00042   virtual char const* ns() const = 0;
00043 
00044   /**
00045    * Gets this QName's prefix.
00046    *
00047    * @return Returns said prefix.
00048    */
00049   virtual char const* prefix() const = 0;
00050 
00051   /**
00052    * Gets this QName's local name.
00053    *
00054    * @return Returns said local name.
00055    */
00056   virtual char const* localname() const = 0;
00057 };
00058 
00059 /**
00060  * Emits a QName to an ostream.
00061  *
00062  * @param o The ostream to emit to.
00063  * @param qn The QName to emit.
00064  * @return Returns \a o.
00065  */
00066 ZORBA_DLL_PUBLIC
00067 std::ostream& operator<<( std::ostream &o, QName const &qn );
00068 
00069 /**
00070  * Compares two QNames for equality.
00071  *
00072  * @param q1 The first QName.
00073  * @param q2 The second QName.
00074  * @return Returns \c true only if the QNames' namespaces and local names are
00075  * equal.
00076  */
00077 ZORBA_DLL_PUBLIC
00078 bool operator==( QName const &q1, QName const &q2 );
00079 
00080 /**
00081  * Compares two QNames for equality.
00082  *
00083  * @param q1 The first QName.
00084  * @param q2 The second QName.  It can be in Clark notation,
00085  * <code>{</code><em>namespace</em><code>}</code><em>local-name</em>, in which
00086  * case the namespaces and local-names are compared; or as
00087  * <em>prefix</em><code>:</code><em>local-name</em> in which case the prefixes
00088  * and local-names are compared.
00089  * @return Returns \c true only if the QNames are equal.
00090  */
00091 ZORBA_DLL_PUBLIC
00092 bool operator==( QName const &q1, char const *q2 );
00093 
00094 /**
00095  * Compares two QNames for equality.
00096  *
00097  * @param q1 The first QName.  It can be in Clark notation,
00098  * <code>{</code><em>namespace</em><code>}</code><em>local-name</em>, in which
00099  * case the namespaces and local-names are compared; or as
00100  * <em>prefix</em><code>:</code><em>local-name</em> in which case the prefixes
00101  * and local-names are compared.
00102  * @param q2 The second QName.
00103  * @return Returns \c true only if the QNames are equal.
00104  */
00105 inline bool operator==( char const *q1, QName const &q2 ) {
00106   return q2 == q1;
00107 }
00108 
00109 /**
00110  * Compares two QNames for equality.
00111  *
00112  * @tparam StringType The string type of \a q2.
00113  * @param q1 The first QName.
00114  * @param q2 The second QName.  It can be in Clark notation,
00115  * <code>{</code><em>namespace</em><code>}</code><em>local-name</em>, in which
00116  * case the namespaces and local-names are compared; or as
00117  * <em>prefix</em><code>:</code><em>local-name</em> in which case the prefixes
00118  * and local-names are compared.
00119  * @return Returns \c true only if the QNames are equal.
00120  */
00121 template<class StringType> inline
00122 bool operator==( QName const &q1, StringType const &q2 ) {
00123   return q1 == q2.c_str();
00124 }
00125 
00126 /**
00127  * Compares two QNames for equality.
00128  *
00129  * @tparam StringType The string type of \a q1.
00130  * @param q1 The first QName.  It can be in Clark notation,
00131  * <code>{</code><em>namespace</em><code>}</code><em>local-name</em>, in which
00132  * case the namespaces and local-names are compared; or as
00133  * <em>prefix</em><code>:</code><em>local-name</em> in which case the prefixes
00134  * and local-names are compared.
00135  * @param q2 The second QName.
00136  * @return Returns \c true only if the QNames are equal.
00137  */
00138 template<class StringType> inline
00139 bool operator==( StringType const &q1, QName const &q2 ) {
00140   return q1.c_str() == q2;
00141 }
00142 
00143 /**
00144  * Compares two QNames for inequality.
00145  *
00146  * @param q1 The first QName.
00147  * @param q2 The second QName.
00148  * @return Returns \c true only if either the QNames' namespaces or local names
00149  * are not equal.
00150  */
00151 inline bool operator!=( QName const &q1, QName const &q2 ) {
00152   return !(q1 == q2);
00153 }
00154 
00155 /**
00156  * Compares two QNames for inequality.
00157  *
00158  * @param q1 The first QName.
00159  * @param q2 The second QName.  It can be in Clark notation,
00160  * <code>{</code><em>namespace</em><code>}</code><em>local-name</em>, in which
00161  * case the namespaces and local-names are compared; or as
00162  * <em>prefix</em><code>:</code><em>local-name</em> in which case the prefixes
00163  * and local-names are compared.
00164  * @return Returns \c true only if either the QNames' namespaces or local names
00165  * are not equal.
00166  */
00167 inline bool operator!=( QName const &q1, char const *q2 ) {
00168   return !(q1 == q2);
00169 }
00170 
00171 /**
00172  * Compares two QNames for inequality.
00173  *
00174  * @param q1 The first QName.  It can be in Clark notation,
00175  * <code>{</code><em>namespace</em><code>}</code><em>local-name</em>, in which
00176  * case the namespaces and local-names are compared; or as
00177  * <em>prefix</em><code>:</code><em>local-name</em> in which case the prefixes
00178  * and local-names are compared.
00179  * @param q2 The second QName.
00180  * @return Returns \c true only if either the QNames' namespaces or local names
00181  * are not equal.
00182  */
00183 inline bool operator!=( char const *q1, QName const &q2 ) {
00184   return !(q1 == q2);
00185 }
00186 
00187 /**
00188  * Compares two QNames for inequality.
00189  *
00190  * @tparam StringType The string type of \a q2.
00191  * @param q1 The first QName.
00192  * @param q2 The second QName.  It can be in Clark notation,
00193  * <code>{</code><em>namespace</em><code>}</code><em>local-name</em>, in which
00194  * case the namespaces and local-names are compared; or as
00195  * <em>prefix</em><code>:</code><em>local-name</em> in which case the prefixes
00196  * and local-names are compared.
00197  * @return Returns \c true only if the QNames are not equal.
00198  */
00199 template<class StringType> inline
00200 bool operator!=( QName const &q1, StringType const &q2 ) {
00201   return q1 != q2.c_str();
00202 }
00203 
00204 /**
00205  * Compares two QNames for inequality.
00206  *
00207  * @tparam StringType The string type of \a q1.
00208  * @param q1 The first QName.  It can be in Clark notation,
00209  * <code>{</code><em>namespace</em><code>}</code><em>local-name</em>, in which
00210  * case the namespaces and local-names are compared; or as
00211  * <em>prefix</em><code>:</code><em>local-name</em> in which case the prefixes
00212  * and local-names are compared.
00213  * @param q2 The second QName.
00214  * @return Returns \c true only if the QNames are not equal.
00215  */
00216 template<class StringType> inline
00217 bool operator!=( StringType const &q1, QName const &q2 ) {
00218   return q1.c_str() != q2;
00219 }
00220 
00221 ///////////////////////////////////////////////////////////////////////////////
00222 
00223 /**
00224  * An diagnostic::category is the category of error.
00225  */
00226 enum category {
00227   UNKNOWN_CATEGORY,                     // must have integer value of 0
00228 
00229   XQUERY_CORE,
00230   XQUERY_FULL_TEXT,
00231   XQUERY_SCRIPTING,
00232   XQUERY_SERIALIZATION,
00233   XQUERY_UPDATE,
00234   XQUERY_USER_DEFINED,                  // for fn:error()
00235 
00236   ZORBA_XQP,                            // Zorba XQuery Processor
00237   ZORBA_API,                            // Zorba API
00238   ZORBA_DDF,                            // Data Definition Facility
00239   ZORBA_DEBUGGER,                       // Zorba Debugger
00240   ZORBA_OS,                             // Operating System
00241   ZORBA_SERIALIZATION,
00242   ZORBA_STORE
00243 };
00244 
00245 /**
00246  * Emits the given diagnostic::category to the given ostream.
00247  *
00248  * @param o The ostream to emit to.
00249  * @param c The category to emit.
00250  * @return Returns \a o.
00251  */
00252 ZORBA_DLL_PUBLIC
00253 std::ostream& operator<<( std::ostream &o, category c );
00254 
00255 /**
00256  * An diagnostic::kind is the kind of error.
00257  * See: http://www.w3.org/TR/xquery-30/#id-kinds-of-errors
00258  */
00259 enum kind {
00260   UNKNOWN_KIND,                         // must have integer value of 0
00261 
00262   /**
00263    * A static error is an error that must be detected during the static
00264    * analysis phase. A syntax error is an example of a static error.
00265    */
00266   XQUERY_STATIC,
00267 
00268   /**
00269    * A dynamic error is an error that must be detected during the dynamic
00270    * evaluation phase and may be detected during the static analysis phase.
00271    * Numeric overflow is an example of a dynamic error.
00272    */
00273   XQUERY_DYNAMIC,
00274 
00275   /**
00276    * A type error may be raised during the static analysis phase or the dynamic
00277    * evaluation phase.
00278    * 
00279    * During the static analysis phase, a type error occurs when the static type
00280    * of an expression does not match the expected type of the context in which
00281    * the expression occurs.
00282    *
00283    * During the dynamic evaluation phase, a type error occurs when the dynamic
00284    * type of a value does not match the expected type of the context in which
00285    * the value occurs.
00286    */
00287   XQUERY_TYPE
00288 };
00289 
00290 /**
00291  * Emits the given diagnostic::kind to the given ostream.
00292  *
00293  * @param o The ostream to emit to.
00294  * @param k The kind to emit.
00295  * @return Returns \a o.
00296  */
00297 ZORBA_DLL_PUBLIC
00298 std::ostream& operator<<( std::ostream &o, kind k );
00299 
00300 ///////////////////////////////////////////////////////////////////////////////
00301 
00302 } // namespace diagnostic
00303 } // namespace zorba
00304 
00305 #include <zorba/internal/qname.h>
00306 
00307 namespace zorba {
00308 
00309 ///////////////////////////////////////////////////////////////////////////////
00310 
00311 /**
00312  * A %Diagnostic is the base class for all Zorba diagnostics (errors and
00313  * warnings).
00314  */
00315 class ZORBA_DLL_PUBLIC Diagnostic {
00316 public:
00317   /**
00318    * Gets the QName for this diagnostic.
00319    *
00320    * @return Returns said QName.
00321    */
00322   virtual diagnostic::QName const& qname() const = 0;
00323 
00324   /**
00325    * Gets the category of this diagnostic.
00326    *
00327    * @return Returns said category.
00328    */
00329   virtual diagnostic::category category() const;
00330 
00331   /**
00332    * Gets the kind of this diagnostic.
00333    *
00334    * @return Returns said kind.
00335    */
00336   virtual diagnostic::kind kind() const;
00337 
00338   /**
00339    * Gets the message of this diagnostic.
00340    *
00341    * @return Returns said message.
00342    */
00343   virtual char const* message() const;
00344 
00345 protected:
00346   virtual ~Diagnostic();
00347 
00348   virtual Diagnostic const* clone() const = 0;
00349 
00350   /**
00351    * Destroys a %Diagnostic.  This is the only way a %Diagnostic should be
00352    * destroyed.
00353    */
00354   virtual void destroy() const;
00355 
00356   // Only ZorbaException may call clone() and destroy().
00357   friend class ZorbaException;
00358 };
00359 
00360 /**
00361  * Compares two diagnostics for equality.
00362  *
00363  * @param d1 The first diagnostic.
00364  * @param d2 The second diagnostic.
00365  * @return Returns \c true only if the diagnostics' QNames are equal.
00366  */
00367 inline bool operator==( Diagnostic const &d1, Diagnostic const &d2 ) {
00368   return d1.qname() == d2.qname();
00369 }
00370 
00371 /**
00372  * Compares two diagnostics for inequality.
00373  *
00374  * @param d1 The first diagnostic.
00375  * @param d2 The second diagnostic.
00376  * @return Returns \c true only if the diagnostics' QNames are not equal.
00377  */
00378 inline bool operator!=( Diagnostic const &d1, Diagnostic const &d2 ) {
00379   return !(d1 == d2);
00380 }
00381 
00382 ///////////////////////////////////////////////////////////////////////////////
00383 
00384 } // namespace zorba
00385 
00386 #include <zorba/internal/diagnostic.h>
00387 
00388 #endif /* ZORBA_DIAGNOSTIC_API_H */
00389 /*
00390  * Local variables:
00391  * mode: c++
00392  * End:
00393  */
00394 /* vim:set et sw=2 ts=2: */
blog comments powered by Disqus