GME  13
XMLValidator.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  * $Id: XMLValidator.cpp 635560 2008-03-10 14:10:09Z borisk $
00020  */
00021 
00022 // ---------------------------------------------------------------------------
00023 //  Includes
00024 // ---------------------------------------------------------------------------
00025 #include <xercesc/framework/XMLValidator.hpp>
00026 #include <xercesc/util/PlatformUtils.hpp>
00027 #include <xercesc/util/XMLInitializer.hpp>
00028 #include <xercesc/util/XMLMsgLoader.hpp>
00029 #include <xercesc/internal/XMLScanner.hpp>
00030 
00031 XERCES_CPP_NAMESPACE_BEGIN
00032 
00033 static XMLMsgLoader* sMsgLoader = 0;
00034 
00035 void XMLInitializer::initializeXMLValidator()
00036 {
00037     sMsgLoader = XMLPlatformUtils::loadMsgSet(XMLUni::fgValidityDomain);
00038 
00039     if (!sMsgLoader)
00040       XMLPlatformUtils::panic(PanicHandler::Panic_CantLoadMsgDomain);
00041 }
00042 
00043 void XMLInitializer::terminateXMLValidator()
00044 {
00045     delete sMsgLoader;
00046     sMsgLoader = 0;
00047 }
00048 
00049 // ---------------------------------------------------------------------------
00050 //  XMLValidator: Error emitting methods
00051 // ---------------------------------------------------------------------------
00052 
00053 //
00054 //  These methods are called whenever the scanner wants to emit an error.
00055 //  It handles getting the message loaded, doing token replacement, etc...
00056 //  and then calling the error handler, if its installed.
00057 //
00058 void XMLValidator::emitError(const XMLValid::Codes toEmit)
00059 {
00060     // Bump the error count if it is not a warning
00061     if (XMLValid::errorType(toEmit) != XMLErrorReporter::ErrType_Warning)
00062         fScanner->incrementErrorCount();
00063 
00064     //  Call error reporter if we have one
00065     if (fErrorReporter)
00066     {
00067         // Load the message into a local for display
00068         const XMLSize_t msgSize = 1023;
00069         XMLCh errText[msgSize + 1];
00070 
00071         // load the text
00072         if (!sMsgLoader->loadMsg(toEmit, errText, msgSize))
00073         {
00074           // <TBD> Probably should load a default msg here
00075         }
00076 
00077         //
00078         //  Create a LastExtEntityInfo structure and get the reader manager
00079         //  to fill it in for us. This will give us the information about
00080         //  the last reader on the stack that was an external entity of some
00081         //  sort (i.e. it will ignore internal entities.
00082         //
00083         ReaderMgr::LastExtEntityInfo lastInfo;
00084         fReaderMgr->getLastExtEntityInfo(lastInfo);
00085 
00086         fErrorReporter->error
00087         (
00088             toEmit
00089             , XMLUni::fgValidityDomain
00090             , XMLValid::errorType(toEmit)
00091             , errText
00092             , lastInfo.systemId
00093             , lastInfo.publicId
00094             , lastInfo.lineNumber
00095             , lastInfo.colNumber
00096         );
00097     }
00098 
00099     // Bail out if its fatal an we are to give up on the first fatal error
00100     if (((XMLValid::isError(toEmit) && fScanner->getValidationConstraintFatal())
00101          || XMLValid::isFatal(toEmit))
00102     &&  fScanner->getExitOnFirstFatal()
00103     &&  !fScanner->getInException())
00104     {
00105         throw toEmit;
00106     }
00107 }
00108 
00109 void XMLValidator::emitError(const  XMLValid::Codes toEmit
00110                             , const XMLCh* const    text1
00111                             , const XMLCh* const    text2
00112                             , const XMLCh* const    text3
00113                             , const XMLCh* const    text4)
00114 {
00115     // Bump the error count if it is not a warning
00116     if (XMLValid::errorType(toEmit) != XMLErrorReporter::ErrType_Warning)
00117         fScanner->incrementErrorCount();
00118 
00119     //  Call error reporter if we have one
00120     if (fErrorReporter)
00121     {
00122         //
00123         //  Load the message into alocal and replace any tokens found in
00124         //  the text.
00125         //
00126         const XMLSize_t maxChars = 2047;
00127         XMLCh errText[maxChars + 1];
00128 
00129         // load the text
00130         if (!sMsgLoader->loadMsg(toEmit, errText, maxChars, text1, text2, text3, text4, fScanner->getMemoryManager()))
00131         {
00132           // <TBD> Should probably load a default message here
00133         }
00134 
00135         //
00136         //  Create a LastExtEntityInfo structure and get the reader manager
00137         //  to fill it in for us. This will give us the information about
00138         //  the last reader on the stack that was an external entity of some
00139         //  sort (i.e. it will ignore internal entities.
00140         //
00141         ReaderMgr::LastExtEntityInfo lastInfo;
00142         fReaderMgr->getLastExtEntityInfo(lastInfo);
00143 
00144         fErrorReporter->error
00145         (
00146             toEmit
00147             , XMLUni::fgValidityDomain
00148             , XMLValid::errorType(toEmit)
00149             , errText
00150             , lastInfo.systemId
00151             , lastInfo.publicId
00152             , lastInfo.lineNumber
00153             , lastInfo.colNumber
00154         );
00155     }
00156 
00157     // Bail out if its fatal an we are to give up on the first fatal error
00158     if (((XMLValid::isError(toEmit) && fScanner->getValidationConstraintFatal())
00159          || XMLValid::isFatal(toEmit))
00160     &&  fScanner->getExitOnFirstFatal()
00161     &&  !fScanner->getInException())
00162     {
00163         throw toEmit;
00164     }
00165 }
00166 
00167 void XMLValidator::emitError(const  XMLValid::Codes toEmit
00168                             , const char* const     text1
00169                             , const char* const     text2
00170                             , const char* const     text3
00171                             , const char* const     text4)
00172 {
00173     // Bump the error count if it is not a warning
00174     if (XMLValid::errorType(toEmit) != XMLErrorReporter::ErrType_Warning)
00175         fScanner->incrementErrorCount();
00176 
00177     //  Call error reporter if we have one
00178     if (fErrorReporter)
00179     {
00180         //
00181         //  Load the message into alocal and replace any tokens found in
00182         //  the text.
00183         //
00184         const XMLSize_t maxChars = 2047;
00185         XMLCh errText[maxChars + 1];
00186 
00187         // load the text
00188         if (!sMsgLoader->loadMsg(toEmit, errText, maxChars, text1, text2, text3, text4, fScanner->getMemoryManager()))
00189         {
00190           // <TBD> Should probably load a default message here
00191         }
00192 
00193         //
00194         //  Create a LastExtEntityInfo structure and get the reader manager
00195         //  to fill it in for us. This will give us the information about
00196         //  the last reader on the stack that was an external entity of some
00197         //  sort (i.e. it will ignore internal entities.
00198         //
00199         ReaderMgr::LastExtEntityInfo lastInfo;
00200         fReaderMgr->getLastExtEntityInfo(lastInfo);
00201 
00202         fErrorReporter->error
00203         (
00204             toEmit
00205             , XMLUni::fgValidityDomain
00206             , XMLValid::errorType(toEmit)
00207             , errText
00208             , lastInfo.systemId
00209             , lastInfo.publicId
00210             , lastInfo.lineNumber
00211             , lastInfo.colNumber
00212         );
00213     }
00214 
00215     // Bail out if its fatal an we are to give up on the first fatal error
00216     if (((XMLValid::isError(toEmit) && fScanner->getValidationConstraintFatal())
00217          || XMLValid::isFatal(toEmit))
00218     &&  fScanner->getExitOnFirstFatal()
00219     &&  !fScanner->getInException())
00220     {
00221         throw toEmit;
00222     }
00223 }
00224 
00225 void XMLValidator::emitError(const  XMLValid::Codes toEmit
00226                             , const XMLExcepts::Codes   originalExceptCode
00227                             , const XMLCh* const    text1
00228                             , const XMLCh* const    text2
00229                             , const XMLCh* const    text3
00230                             , const XMLCh* const    text4)
00231 {
00232     // Bump the error count if it is not a warning
00233     if (XMLValid::errorType(toEmit) != XMLErrorReporter::ErrType_Warning)
00234         fScanner->incrementErrorCount();
00235 
00236     //  Call error reporter if we have one
00237     if (fErrorReporter)
00238     {
00239         //
00240         //  Load the message into alocal and replace any tokens found in
00241         //  the text.
00242         //
00243         const XMLSize_t maxChars = 2047;
00244         XMLCh errText[maxChars + 1];
00245 
00246         // load the text
00247         if (!sMsgLoader->loadMsg(toEmit, errText, maxChars, text1, text2, text3, text4, fScanner->getMemoryManager()))
00248         {
00249           // <TBD> Should probably load a default message here
00250         }
00251 
00252         //
00253         //  Create a LastExtEntityInfo structure and get the reader manager
00254         //  to fill it in for us. This will give us the information about
00255         //  the last reader on the stack that was an external entity of some
00256         //  sort (i.e. it will ignore internal entities.
00257         //
00258         ReaderMgr::LastExtEntityInfo lastInfo;
00259         fReaderMgr->getLastExtEntityInfo(lastInfo);
00260 
00261         fErrorReporter->error
00262         (
00263             originalExceptCode
00264             , XMLUni::fgExceptDomain    //XMLUni::fgValidityDomain
00265             , XMLValid::errorType(toEmit)
00266             , errText
00267             , lastInfo.systemId
00268             , lastInfo.publicId
00269             , lastInfo.lineNumber
00270             , lastInfo.colNumber
00271         );
00272     }
00273 
00274     // Bail out if its fatal an we are to give up on the first fatal error
00275     if (((XMLValid::isError(toEmit) && fScanner->getValidationConstraintFatal())
00276          || XMLValid::isFatal(toEmit))
00277     &&  fScanner->getExitOnFirstFatal()
00278     &&  !fScanner->getInException())
00279     {
00280         throw toEmit;
00281     }
00282 }
00283 
00284 // ---------------------------------------------------------------------------
00285 //  XMLValidator: Hidden Constructors
00286 // ---------------------------------------------------------------------------
00287 XMLValidator::XMLValidator(XMLErrorReporter* const errReporter) :
00288 
00289     fBufMgr(0)
00290     , fErrorReporter(errReporter)
00291     , fReaderMgr(0)
00292     , fScanner(0)
00293 {
00294 }
00295 
00296 XERCES_CPP_NAMESPACE_END