GME  13
XSDErrorReporter.cpp
Go to the documentation of this file.
00001 /*
00002  * Licensed to the Apache Software Foundation (ASF) under one or more
00003  * contributor license agreements.  See the NOTICE file distributed with
00004  * this work for additional information regarding copyright ownership.
00005  * The ASF licenses this file to You under the Apache License, Version 2.0
00006  * (the "License"); you may not use this file except in compliance with
00007  * the License.  You may obtain a copy of the License at
00008  *
00009  *      http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 // ---------------------------------------------------------------------------
00019 //  Includes
00020 // ---------------------------------------------------------------------------
00021 #include <xercesc/util/PlatformUtils.hpp>
00022 #include <xercesc/util/XMLString.hpp>
00023 #include <xercesc/framework/XMLErrorCodes.hpp>
00024 #include <xercesc/framework/XMLValidityCodes.hpp>
00025 #include <xercesc/framework/XMLErrorReporter.hpp>
00026 #include <xercesc/util/XMLMsgLoader.hpp>
00027 #include <xercesc/util/XMLInitializer.hpp>
00028 #include <xercesc/validators/schema/XSDErrorReporter.hpp>
00029 #include <xercesc/validators/schema/XSDLocator.hpp>
00030 
00031 XERCES_CPP_NAMESPACE_BEGIN
00032 
00033 // ---------------------------------------------------------------------------
00034 //  Local static data
00035 // ---------------------------------------------------------------------------
00036 static XMLMsgLoader*  gErrMsgLoader = 0;
00037 static XMLMsgLoader*  gValidMsgLoader = 0;
00038 
00039 void XMLInitializer::initializeXSDErrorReporter()
00040 {
00041     gErrMsgLoader = XMLPlatformUtils::loadMsgSet(XMLUni::fgXMLErrDomain);
00042 
00043     if (!gErrMsgLoader)
00044       XMLPlatformUtils::panic(PanicHandler::Panic_CantLoadMsgDomain);
00045 
00046     gValidMsgLoader = XMLPlatformUtils::loadMsgSet(XMLUni::fgValidityDomain);
00047 
00048     if (!gValidMsgLoader)
00049       XMLPlatformUtils::panic(PanicHandler::Panic_CantLoadMsgDomain);
00050 }
00051 
00052 void XMLInitializer::terminateXSDErrorReporter()
00053 {
00054     delete gErrMsgLoader;
00055     gErrMsgLoader = 0;
00056 
00057     delete gValidMsgLoader;
00058     gValidMsgLoader = 0;
00059 }
00060 
00061 // ---------------------------------------------------------------------------
00062 //  XSDErrorReporter: Constructors and Destructor
00063 // ---------------------------------------------------------------------------
00064 XSDErrorReporter::XSDErrorReporter(XMLErrorReporter* const errorReporter) :
00065     fExitOnFirstFatal(false)
00066     , fErrorReporter(errorReporter)
00067 {
00068 
00069 }
00070 
00071 
00072 // ---------------------------------------------------------------------------
00073 //  XSDErrorReporter: Error reporting
00074 // ---------------------------------------------------------------------------
00075 void XSDErrorReporter::emitError(const unsigned int toEmit,
00076                                  const XMLCh* const msgDomain,
00077                                  const Locator* const aLocator)
00078 {
00079     // Bump the error count if it is not a warning
00080 //    if (XMLErrs::errorType(toEmit) != XMLErrorReporter::ErrType_Warning)
00081 //        incrementErrorCount();
00082 
00083     //
00084     //  Load the message into alocal and replace any tokens found in
00085     //  the text.
00086     //
00087     const XMLSize_t msgSize = 1023;
00088     XMLCh errText[msgSize + 1];
00089     XMLMsgLoader* msgLoader = gErrMsgLoader;
00090     XMLErrorReporter::ErrTypes errType = XMLErrs::errorType((XMLErrs::Codes) toEmit);
00091 
00092     if (XMLString::equals(msgDomain, XMLUni::fgValidityDomain)) {
00093 
00094         errType = XMLValid::errorType((XMLValid::Codes) toEmit);
00095         msgLoader = gValidMsgLoader;
00096     }
00097 
00098     if (!msgLoader->loadMsg(toEmit, errText, msgSize))
00099     {
00100                 // <TBD> Should probably load a default message here
00101     }
00102 
00103     if (fErrorReporter)
00104         fErrorReporter->error(toEmit, msgDomain, errType, errText, aLocator->getSystemId(),
00105                               aLocator->getPublicId(), aLocator->getLineNumber(),
00106                               aLocator->getColumnNumber());
00107 
00108     // Bail out if its fatal an we are to give up on the first fatal error
00109     if (errType == XMLErrorReporter::ErrType_Fatal && fExitOnFirstFatal)
00110         throw (XMLErrs::Codes) toEmit;
00111 }
00112 
00113 void XSDErrorReporter::emitError(const unsigned int toEmit,
00114                                  const XMLCh* const msgDomain,
00115                                  const Locator* const aLocator,
00116                                  const XMLCh* const text1,
00117                                  const XMLCh* const text2,
00118                                  const XMLCh* const text3,
00119                                  const XMLCh* const text4,
00120                                  MemoryManager* const manager)
00121 {
00122     // Bump the error count if it is not a warning
00123 //    if (XMLErrs::errorType(toEmit) != XMLErrorReporter::ErrType_Warning)
00124 //        incrementErrorCount();
00125 
00126     //
00127     //  Load the message into alocal and replace any tokens found in
00128     //  the text.
00129     //
00130     const XMLSize_t maxChars = 2047;
00131     XMLCh errText[maxChars + 1];
00132     XMLMsgLoader* msgLoader = gErrMsgLoader;
00133     XMLErrorReporter::ErrTypes errType = XMLErrs::errorType((XMLErrs::Codes) toEmit);
00134 
00135     if (XMLString::equals(msgDomain, XMLUni::fgValidityDomain)) {
00136 
00137         errType = XMLValid::errorType((XMLValid::Codes) toEmit);
00138         msgLoader = gValidMsgLoader;
00139     }
00140 
00141     if (!msgLoader->loadMsg(toEmit, errText, maxChars, text1, text2, text3, text4, manager))
00142     {
00143                 // <TBD> Should probably load a default message here
00144     }
00145 
00146     if (fErrorReporter)
00147         fErrorReporter->error(toEmit, msgDomain, errType, errText, aLocator->getSystemId(),
00148                               aLocator->getPublicId(), aLocator->getLineNumber(),
00149                               aLocator->getColumnNumber());
00150 
00151     // Bail out if its fatal an we are to give up on the first fatal error
00152     if (errType == XMLErrorReporter::ErrType_Fatal && fExitOnFirstFatal)
00153         throw (XMLErrs::Codes) toEmit;
00154 }
00155 
00156 void XSDErrorReporter::emitError(const XMLException&  except,
00157                                  const Locator* const aLocator)
00158 {
00159     const XMLCh* const  errText = except.getMessage();
00160     const unsigned int  toEmit = except.getCode();
00161     //Before the code was modified to call this routine it used to use
00162     //the XMLErrs::DisplayErrorMessage error message, which is just {'0'}
00163     //and that error message has errType of Error.  So to be consistent
00164     //with previous behaviour set the errType to be Error instead of
00165     //getting the error type off of the exception.
00166     //XMLErrorReporter::ErrTypes errType = XMLErrs::errorType((XMLErrs::Codes) toEmit);
00167     XMLErrorReporter::ErrTypes errType = XMLErrorReporter::ErrType_Error;
00168 
00169     if (fErrorReporter)
00170         fErrorReporter->error(toEmit, XMLUni::fgExceptDomain, errType, errText, aLocator->getSystemId(),
00171                               aLocator->getPublicId(), aLocator->getLineNumber(),
00172                               aLocator->getColumnNumber());
00173 
00174     // Bail out if its fatal an we are to give up on the first fatal error
00175     //if (errType == XMLErrorReporter::ErrType_Fatal && fExitOnFirstFatal)
00176     //    throw (XMLErrs::Codes) toEmit;
00177 }
00178 
00179 XERCES_CPP_NAMESPACE_END