summaryrefslogtreecommitdiff
path: root/src/core/resource/Resource.hpp
blob: 19340298da802650c4c18b53ef1d897ea8b3f2d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
    Ousía
    Copyright (C) 2014, 2015  Benjamin Paaßen, Andreas Stöckel

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/**
 * @file Resource.hpp
 *
 * Contains the Resource class, representing an external resource as well as
 * further types used for describing resources.
 *
 * @author Benjamin Paaßen (bpassen@techfak.uni-bielefeld.de)
 */

#ifndef _OUSIA_RESOURCE_HPP_
#define _OUSIA_RESOURCE_HPP_

#include <map>
#include <memory>
#include <string>

namespace ousia {

// Forward declaration
class ResourceLocator;

/**
 * This enum contains all possible types of includable resources in Ousía.
 */
enum class ResourceType {
	/**
     * Unknown type.
     */
	UNKNOWN,

	/**
     * The resource contains a domain description.
     */
	DOMAIN_DESC,

	/**
     * The resource contains a typesystem description.
     */
	TYPESYSTEM,

	/**
     * The resource contains a simple document.
     */
	DOCUMENT,

	/**
     * The resource contains style attributes.
     */
	ATTRIBUTES,

	/**
     * The resource is a stylesheet.
     */
	STYLESHEET,

	/**
     * The resource contains script (note that the actual scripting language is
     * not specified by the resource type).
     */
	SCRIPT,

	/**
     * Generic data, such as e.g. images.
     */
	DATA
};

/**
 * A Location contains the location of a Resource, e.g. a file path
 * on a hard drive. Note that the 'found' flag might be set to false
 * indicating that a resource was not found.
 */
class Resource {
private:
	/**
	 * Specifies whether the resource points at a valid location or not.
	 */
	bool valid;

	/**
	 * Reference pointing at the location
	 */
	ResourceLocator const *locator;

	/**
	 * Requested type of the resource.
	 */
	ResourceType type;

	/**
	 * This is a fully qualified/canonical path to the resource found or
	 * in an undefined state (possibly empty) if the 'valid' flag is set
	 * to 'false'.
	 */
	std::string location;

public:
	/**
	 * Default constructor of the Resource class, represents an invalid
	 * resource.
	 */
	Resource();

	/**
	 * Constructor of the Resource class.
	 *
	 * @param valid specifies whether the Resource is valid or not.
	 * @param locator specifies the resource locator that was used to locate the
	 * resource.
	 */
	Resource(bool valid, const ResourceLocator &locator,
	         ResourceType type, const std::string &location);

	/**
	 * This calls the 'stream' method of the underlying ResourceLocator that
	 * found this location and returns a stream containing the data of the
	 * Resource at this location.
	 *
	 * @return a stream containing the data of the Resource at this
	 *         location. This has to be a unique_pointer because the current
	 *         C++11 compiler does not yet support move semantics for
	 *         streams.
	 */
	std::unique_ptr<std::istream> stream() const;

	/**
	 * Returns whether this resource is valid or not.
	 *
	 * @return true if the resource is valid, false otherwise.
	 */
	bool isValid() const { return valid; }

	/**
	 * Returns a reference pointing at the locator that was used to locate this
	 * resource.
	 *
	 * @return the locator used for locating this resource.
	 */
	const ResourceLocator &getLocator() const { return *locator; }

	/**
	 * Returns the type of the resource that was requested when the resource was
	 * located.
	 *
	 * @return the type of the resource.
	 */
	ResourceType getType() const { return type; }

	/**
	 * Returns a canonical location that can be used in a hash map to identify
	 * a resource.
	 */
	const std::string &getLocation() const { return location; }
};

/**
 * Invalid resource instance.
 */
extern const Resource NullResource;
}

#endif /* _OUSIA_RESOURCE_HPP_ */