Debian Codegraph @

src/lib/AbiDocument.cpp

raw
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /*
3 * This file is part of the libabw project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
9
10 #include <libabw/libabw.h>
11 #include "ABWXMLHelper.h"
12 #include "ABWParser.h"
13 #include "ABWZlibStream.h"
14 #include "libabw_internal.h"
15
16 namespace libabw
17 {
18 // small function needed to call the xml BAD_CAST on a char const *
19 static xmlChar *call_BAD_CAST_OnConst(char const *str)
20 {
21 return BAD_CAST(const_cast<char *>(str));
22 }
23 }
24
25 /**
26 \mainpage libabw documentation
27 This document contains both the libabw API specification and the normal libabw
28 documentation.
29 \section api_docs libabw API documentation
30 The external libabw API is provided by the AbiDocument class. This class, combined
31 with the librevenge::RVNGTextInterface class, are the only two classes that will be of interest
32 for the application programmer using libabw.
33 \section lib_docs libabw documentation
34 If you are interested in the structure of libabw itself, this whole document
35 would be a good starting point for exploring the internals of libabw. Mind that
36 this document is a work-in-progress, and will most likely not cover libabw for
37 the full 100%.
38 */
39
40 /**
41 Analyzes the content of an input stream to see if it can be parsed
42 \param input The input stream
43 \return A confidence value which represents the likelihood that the content from
44 the input stream can be parsed
45 */
46 ABWAPI bool libabw::AbiDocument::isFileFormatSupported(librevenge::RVNGInputStream *input) try
47 {
48 ABW_DEBUG_MSG(("AbiDocument::isFileFormatSupported\n"));
49 if (!input)
50 return false;
51 input->seek(0, librevenge::RVNG_SEEK_SET);
52 libabw::ABWZlibStream stream(input);
53 stream.seek(0, librevenge::RVNG_SEEK_SET);
54 auto reader = libabw::xmlReaderForStream(&stream);
55 if (!reader)
56 return false;
57 int ret = xmlTextReaderRead(reader.get());
58 while (ret == 1 && XML_READER_TYPE_ELEMENT != xmlTextReaderNodeType(reader.get()))
59 ret = xmlTextReaderRead(reader.get());
60 if (ret != 1)
61 return false;
62 const xmlChar *name = xmlTextReaderConstName(reader.get());
63 if (!name)
64 return false;
65 if (!xmlStrEqual(name, call_BAD_CAST_OnConst("abiword")))
66 {
67 if (!xmlStrEqual(name, call_BAD_CAST_OnConst("awml")))
68 return false;
69 }
70
71 // Checking the namespace of AbiWord documents.
72 const xmlChar *nsname = xmlTextReaderConstNamespaceUri(reader.get());
73 if (!nsname)
74 #if 1
75 return true; // Have seen some abiword files without NS declaration
76 #else
77 return false;
78 #endif
79 if (!xmlStrEqual(nsname, call_BAD_CAST_OnConst("http://www.abisource.com/awml.dtd")))
80 return false;
81
82 return true;
83 }
84 catch (...)
85 {
86 return false;
87 }
88
89 /**
90 Parses the input stream content. It will make callbacks to the functions provided by a
91 librevenge::RVNGTextInterface class implementation when needed. This is often commonly called the
92 'main parsing routine'.
93 \param input The input stream
94 \param textInterface A librevenge::RVNGTextInterface implementation
95 \param password The password used to protect the document or NULL if the document
96 is not protected
97 \return A value that indicates whether the conversion was successful and in case it
98 was not, it indicates the reason of the error
99 */
100 ABWAPI bool libabw::AbiDocument::parse(librevenge::RVNGInputStream *input, librevenge::RVNGTextInterface *textInterface) try
101 {
102 ABW_DEBUG_MSG(("AbiDocument::parse\n"));
103 if (!input)
104 return false;
105 input->seek(0, librevenge::RVNG_SEEK_SET);
106 libabw::ABWZlibStream stream(input);
107 libabw::ABWParser parser(&stream, textInterface);
108 if (parser.parse())
109 return true;
110 return false;
111 }
112 catch (...)
113 {
114 return false;
115 }
116
117 /* vim:set shiftwidth=2 softtabstop=2 expandtab: */