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   JSON_PARSER,
00245   JSON_SERIALIZATION
00246 };
00247 
00248 /**
00249  * Emits the given diagnostic::category to the given ostream.
00250  *
00251  * @param o The ostream to emit to.
00252  * @param c The category to emit.
00253  * @return Returns \a o.
00254  */
00255 ZORBA_DLL_PUBLIC
00256 std::ostream& operator<<( std::ostream &o, category c );
00257 
00258 /**
00259  * An diagnostic::kind is the kind of error.
00260  * See: http://www.w3.org/TR/xquery-30/#id-kinds-of-errors
00261  */
00262 enum kind {
00263   UNKNOWN_KIND,                         // must have integer value of 0
00264 
00265   /**
00266    * A static error is an error that must be detected during the static
00267    * analysis phase. A syntax error is an example of a static error.
00268    */
00269   XQUERY_STATIC,
00270 
00271   /**
00272    * A dynamic error is an error that must be detected during the dynamic
00273    * evaluation phase and may be detected during the static analysis phase.
00274    * Numeric overflow is an example of a dynamic error.
00275    */
00276   XQUERY_DYNAMIC,
00277 
00278   /**
00279    * A type error may be raised during the static analysis phase or the dynamic
00280    * evaluation phase.
00281    * 
00282    * During the static analysis phase, a type error occurs when the static type
00283    * of an expression does not match the expected type of the context in which
00284    * the expression occurs.
00285    *
00286    * During the dynamic evaluation phase, a type error occurs when the dynamic
00287    * type of a value does not match the expected type of the context in which
00288    * the value occurs.
00289    */
00290   XQUERY_TYPE
00291 };
00292 
00293 /**
00294  * Emits the given diagnostic::kind to the given ostream.
00295  *
00296  * @param o The ostream to emit to.
00297  * @param k The kind to emit.
00298  * @return Returns \a o.
00299  */
00300 ZORBA_DLL_PUBLIC
00301 std::ostream& operator<<( std::ostream &o, kind k );
00302 
00303 ///////////////////////////////////////////////////////////////////////////////
00304 
00305 } // namespace diagnostic
00306 } // namespace zorba
00307 
00308 #include <zorba/internal/qname.h>
00309 
00310 namespace zorba {
00311 
00312 ///////////////////////////////////////////////////////////////////////////////
00313 
00314 /**
00315  * A %Diagnostic is the base class for all Zorba diagnostics (errors and
00316  * warnings).
00317  */
00318 class ZORBA_DLL_PUBLIC Diagnostic {
00319 public:
00320   /**
00321    * Gets the QName for this diagnostic.
00322    *
00323    * @return Returns said QName.
00324    */
00325   virtual diagnostic::QName const& qname() const = 0;
00326 
00327   /**
00328    * Gets the category of this diagnostic.
00329    *
00330    * @return Returns said category.
00331    */
00332   virtual diagnostic::category category() const;
00333 
00334   /**
00335    * Gets the kind of this diagnostic.
00336    *
00337    * @return Returns said kind.
00338    */
00339   virtual diagnostic::kind kind() const;
00340 
00341   /**
00342    * Gets the message of this diagnostic.
00343    *
00344    * @return Returns said message.
00345    */
00346   virtual char const* message() const;
00347 
00348 protected:
00349   virtual ~Diagnostic();
00350 
00351   virtual Diagnostic const* clone() const = 0;
00352 
00353   /**
00354    * Destroys a %Diagnostic.  This is the only way a %Diagnostic should be
00355    * destroyed.
00356    */
00357   virtual void destroy() const;
00358 
00359   // Only ZorbaException may call clone() and destroy().
00360   friend class ZorbaException;
00361 };
00362 
00363 /**
00364  * Compares two diagnostics for equality.
00365  *
00366  * @param d1 The first diagnostic.
00367  * @param d2 The second diagnostic.
00368  * @return Returns \c true only if the diagnostics' QNames are equal.
00369  */
00370 inline bool operator==( Diagnostic const &d1, Diagnostic const &d2 ) {
00371   return d1.qname() == d2.qname();
00372 }
00373 
00374 /**
00375  * Compares two diagnostics for inequality.
00376  *
00377  * @param d1 The first diagnostic.
00378  * @param d2 The second diagnostic.
00379  * @return Returns \c true only if the diagnostics' QNames are not equal.
00380  */
00381 inline bool operator!=( Diagnostic const &d1, Diagnostic const &d2 ) {
00382   return !(d1 == d2);
00383 }
00384 
00385 ///////////////////////////////////////////////////////////////////////////////
00386 
00387 } // namespace zorba
00388 
00389 #include <zorba/internal/diagnostic.h>
00390 
00391 #endif /* ZORBA_DIAGNOSTIC_API_H */
00392 /*
00393  * Local variables:
00394  * mode: c++
00395  * End:
00396  */
00397 /* vim:set et sw=2 ts=2: */
blog comments powered by Disqus