libdigidocpp
WDoc.cpp
Go to the documentation of this file.
1 /*
2  * libdigidocpp
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  *
18  */
19 
20 #include "WDoc.h"
21 
22 #include "BDoc.h"
23 #include "DDoc.h"
24 #include "Document.h"
25 #include "io/ISerialize.h"
26 
27 using namespace digidoc;
28 
33 WDoc::WDoc(): m_doc(NULL) { setType( DDocType ); }
34 
39 WDoc::WDoc( DocumentType type ): m_doc(NULL) { setType( type ); }
40 
45 WDoc::WDoc( ADoc *doc ) { m_doc = doc; }
46 
51 {
52  if( m_doc )
53  delete m_doc;
54 }
55 
59 WDoc::WDoc( const std::string &path ) throw(IOException, BDocException)
60 {
61  std::string ext = path.substr( path.size() - 4 );
62  transform( ext.begin(), ext.end(), ext.begin(), tolower );
63 
64  if( ext == "bdoc" )
65  m_doc = new BDoc( path );
66  else if( ext == "ddoc" )
67  m_doc = new DDoc( path );
68  else
69  throw IOException( __FILE__, __LINE__, "Unknown document format" );
70 }
71 
81 void WDoc::addDocument(const Document& document) throw(BDocException)
82 {
83  if( !m_doc )
84  throw BDocException( __FILE__, __LINE__, "Document not open" );
85 
86  m_doc->addDocument( document );
87 }
88 
95 void WDoc::addSignature(const std::vector<unsigned char> &signature) throw(BDocException)
96 {
97  if( !m_doc )
98  throw BDocException( __FILE__, __LINE__, "Document not open" );
99 
100  m_doc->addSignature( signature );
101 }
102 
106 unsigned int WDoc::documentCount() const
107 {
108  if( !m_doc )
109  throw BDocException( __FILE__, __LINE__, "Document not open" );
110 
111  return m_doc->documentCount();
112 }
113 
121 Document WDoc::getDocument( unsigned int id ) const throw(BDocException)
122 {
123  if( !m_doc )
124  throw BDocException( __FILE__, __LINE__, "Document not open" );
125 
126  return m_doc->getDocument( id );
127 }
128 
136 const Signature* WDoc::getSignature( unsigned int id ) const throw(BDocException)
137 {
138  if( !m_doc )
139  throw BDocException( __FILE__, __LINE__, "Document not open" );
140 
141  return m_doc->getSignature( id );
142 }
143 
152 void WDoc::removeDocument( unsigned int id ) throw(BDocException)
153 {
154  if( !m_doc )
155  throw BDocException( __FILE__, __LINE__, "Document not open" );
156 
157  m_doc->removeDocument( id );
158 }
159 
166 void WDoc::removeSignature( unsigned int id ) throw(BDocException)
167 {
168  if( !m_doc )
169  throw BDocException( __FILE__, __LINE__, "Document not open" );
170 
171  m_doc->removeSignature( id );
172 }
173 
183 {
184  if( !m_doc )
185  throw BDocException( __FILE__, __LINE__, "Document not open" );
186 
187  m_doc->save();
188 }
189 
199 void WDoc::saveTo( const std::string &path ) throw(IOException, BDocException)
200 {
201  if( !m_doc )
202  throw BDocException( __FILE__, __LINE__, "Document not open" );
203 
204  m_doc->saveTo( path );
205 }
206 
212 {
213  delete m_doc;
214  switch( type )
215  {
216  case BDocType: m_doc = new BDoc(); break;
217  case DDocType: m_doc = new DDoc(); break;
218  default: m_doc = 0;
219  }
220 }
221 
229 void WDoc::sign( Signer *signer ) throw(BDocException)
230 {
231  if( !m_doc )
232  throw BDocException( __FILE__, __LINE__, "Document not open" );
233 
234  m_doc->sign( signer );
235 }
236 
240 unsigned int WDoc::signatureCount() const
241 {
242  if( !m_doc )
243  throw BDocException( __FILE__, __LINE__, "Document not open" );
244 
245  return m_doc->signatureCount();
246 }
247 
252 {
253  if( !m_doc )
254  throw BDocException( __FILE__, __LINE__, "Document not open" );
255 
256  return m_doc->documentType();
257 }
258 
262 std::vector<unsigned char> WDoc::getFileDigest( unsigned int id ) throw(BDocException)
263 {
264  if( !m_doc )
265  throw BDocException( __FILE__, __LINE__, "Document not open" );
266 
267  return m_doc->getFileDigest( id );
268 }