1 /*
2  * ===========================================================================
3  *
4  *                            PUBLIC DOMAIN NOTICE
5  *               National Center for Biotechnology Information
6  *
7  *  This software/database is a "United States Government Work" under the
8  *  terms of the United States Copyright Act.  It was written as part of
9  *  the author's official duties as a United States Government employee and
10  *  thus cannot be copyrighted.  This software/database is freely available
11  *  to the public for use. The National Library of Medicine and the U.S.
12  *  Government have not placed any restriction on its use or reproduction.
13  *
14  *  Although all reasonable efforts have been taken to ensure the accuracy
15  *  and reliability of the software and data, the NLM and the U.S.
16  *  Government do not and cannot warrant the performance or results that
17  *  may be obtained by using this software or data. The NLM and the U.S.
18  *  Government disclaim all warranties, express or implied, including
19  *  warranties of performance, merchantability or fitness for any particular
20  *  purpose.
21  *
22  *  Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Author:  Michael Kornbluh
27  *
28  * File Description:
29  *   Classes for listening to errors, progress, etc.
30  *
31  */
32 
33 #include <ncbi_pch.hpp>
34 
35 #include <objtools/readers/message_listener.hpp>
36 #include <objtools/readers/reader_error_codes.hpp>
37 
38 BEGIN_NCBI_SCOPE
BEGIN_SCOPE(objects)39 BEGIN_SCOPE(objects) // namespace ncbi::objects::
40 
41 void
42 CMessageListenerBase::PutProgress(
43     const string & sMessage,
44     const Uint8 iNumDone,
45     const Uint8 iNumTotal)
46 {
47     // NB: Some other classes rely on the message fitting in one line.
48 
49     // NB: New attributes or inner elements could be added to the resulting
50     //     message at any time, so make no assumptions.
51 
52     if( ! m_pProgressOstrm ) {
53         // no stream to write to
54         return;
55     }
56 
57     *m_pProgressOstrm << "<message severity=\"INFO\" ";
58 
59     if( iNumDone > 0 ) {
60         *m_pProgressOstrm << "num_done=\"" << iNumDone << "\" ";
61     }
62 
63     if( iNumTotal > 0 ) {
64         *m_pProgressOstrm << "num_total=\"" << iNumTotal << "\" ";
65     }
66 
67     if( sMessage.empty() ) {
68         *m_pProgressOstrm  << " />";
69     } else {
70         *m_pProgressOstrm  << " >";
71 
72         string sXMLEncodedMessage = NStr::XmlEncode(sMessage);
73 
74         // some functionality relies on progress messages fitting into
75         // one line, so we escape newlines (just in case) while
76         // we write it.
77         ITERATE( string, msg_it, sXMLEncodedMessage ) {
78             const char ch = *msg_it;
79             switch(ch) {
80             case '\r':
81                 *m_pProgressOstrm << "&#xD;";
82                 break;
83             case '\n':
84                 *m_pProgressOstrm << "&#xA;";
85                 break;
86             default:
87                 *m_pProgressOstrm << ch;
88                 break;
89             }
90         }
91 
92         *m_pProgressOstrm << "</message>" << NcbiEndl;
93     }
94 
95     m_pProgressOstrm->flush();
96 }
97 
98 
CGPipeMessageListener(bool ignoreBadModValue)99 CGPipeMessageListener::CGPipeMessageListener(bool ignoreBadModValue)
100     : m_IgnoreBadModValue(ignoreBadModValue) {}
101 
102 
PutError(const ILineError & error)103 bool CGPipeMessageListener::PutError(const ILineError& error) {
104 
105     const auto severity = error.GetSeverity();
106 
107     if (severity == eDiag_Info) {
108         return true;
109     }
110 
111     if (severity == eDiag_Warning) {
112         ERR_POST(Warning << error.Message());
113         return true;
114     }
115 
116     return ((error.GetCode() == EReaderCode::eReader_Mods) &&
117             (error.GetSubCode() != EModSubcode::eModSubcode_InvalidValue ||
118              m_IgnoreBadModValue));
119 }
120 
121 
122 
123 END_SCOPE(objects)
124 END_NCBI_SCOPE
125 
126