source: XMLIO_V2/external/src/POCO/Foundation.save/URIStreamOpener.cpp @ 80

Last change on this file since 80 was 80, checked in by ymipsl, 14 years ago

ajout lib externe

  • Property svn:eol-style set to native
File size: 5.0 KB
Line 
1//
2// URIStreamOpener.cpp
3//
4// $Id: //poco/1.3/Foundation/src/URIStreamOpener.cpp#2 $
5//
6// Library: Foundation
7// Package: URI
8// Module:  URIStreamOpener
9//
10// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
11// and Contributors.
12//
13// Permission is hereby granted, free of charge, to any person or organization
14// obtaining a copy of the software and accompanying documentation covered by
15// this license (the "Software") to use, reproduce, display, distribute,
16// execute, and transmit the Software, and to prepare derivative works of the
17// Software, and to permit third-parties to whom the Software is furnished to
18// do so, all subject to the following:
19//
20// The copyright notices in the Software and this entire statement, including
21// the above license grant, this restriction and the following disclaimer,
22// must be included in all copies of the Software, in whole or in part, and
23// all derivative works of the Software, unless such copies or derivative
24// works are solely in the form of machine-executable object code generated by
25// a source language processor.
26//
27// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
30// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
31// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
32// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
33// DEALINGS IN THE SOFTWARE.
34//
35
36
37#include "Poco/URIStreamOpener.h"
38#include "Poco/URIStreamFactory.h"
39#include "Poco/FileStreamFactory.h"
40#include "Poco/URI.h"
41#include "Poco/Path.h"
42#include "Poco/SingletonHolder.h"
43#include "Poco/Exception.h"
44
45
46namespace Poco {
47
48
49URIStreamOpener::URIStreamOpener()
50{
51        registerStreamFactory("file", new FileStreamFactory);
52}
53
54
55URIStreamOpener::~URIStreamOpener()
56{
57        for (FactoryMap::iterator it = _map.begin(); it != _map.end(); ++it)
58                delete it->second;
59}
60
61
62std::istream* URIStreamOpener::open(const URI& uri) const
63{
64        FastMutex::ScopedLock lock(_mutex);
65
66        std::string scheme;
67        if (uri.isRelative())
68                scheme = "file";
69        else
70                scheme = uri.getScheme();
71        return openURI(scheme, uri);
72}
73
74
75std::istream* URIStreamOpener::open(const std::string& pathOrURI) const
76{
77        FastMutex::ScopedLock lock(_mutex);
78
79        try
80        {
81                URI uri(pathOrURI);
82                std::string scheme(uri.getScheme());
83                FactoryMap::const_iterator it = _map.find(scheme);
84                if (it != _map.end())
85                        return openURI(scheme, uri);
86        }
87        catch (Exception&)
88        {
89        }
90        Path path(pathOrURI, Path::PATH_GUESS);
91        return openFile(path);
92}
93
94
95std::istream* URIStreamOpener::open(const std::string& basePathOrURI, const std::string& pathOrURI) const
96{
97        FastMutex::ScopedLock lock(_mutex);
98
99        try
100        {
101                URI uri(basePathOrURI);
102                std::string scheme(uri.getScheme());
103                FactoryMap::const_iterator it = _map.find(scheme);
104                if (it != _map.end())
105                {
106                        uri.resolve(pathOrURI);
107                        return openURI(scheme, uri);
108                }
109        }
110        catch (Exception&)
111        {
112        }
113        Path base(basePathOrURI, Path::PATH_GUESS);
114        Path path(pathOrURI, Path::PATH_GUESS);
115        base.resolve(path);
116        return openFile(base);
117}
118
119       
120void URIStreamOpener::registerStreamFactory(const std::string& scheme, URIStreamFactory* pFactory)
121{
122        poco_check_ptr (pFactory);
123
124        FastMutex::ScopedLock lock(_mutex);
125        if (_map.find(scheme) == _map.end())
126        {
127                _map[scheme] = pFactory;
128        }
129        else throw ExistsException("An URIStreamFactory for the given scheme has already been registered", scheme);
130}
131
132
133void URIStreamOpener::unregisterStreamFactory(const std::string& scheme)
134{
135        FastMutex::ScopedLock lock(_mutex);
136       
137        FactoryMap::iterator it = _map.find(scheme);
138        if (it != _map.end())
139        {
140                URIStreamFactory* pFactory = it->second;
141                _map.erase(it);
142                delete pFactory;
143        }
144        else throw NotFoundException("No URIStreamFactory has been registered for the given scheme", scheme);
145}
146
147
148bool URIStreamOpener::supportsScheme(const std::string& scheme)
149{
150        FastMutex::ScopedLock lock(_mutex);
151        return _map.find(scheme) != _map.end();
152}
153
154
155URIStreamOpener& URIStreamOpener::defaultOpener()
156{
157        static SingletonHolder<URIStreamOpener> sh;
158        return *sh.get();
159}
160
161
162std::istream* URIStreamOpener::openFile(const Path& path) const
163{
164        FileStreamFactory factory;
165        return factory.open(path);
166}
167
168
169std::istream* URIStreamOpener::openURI(const std::string& scheme, const URI& uri) const
170{
171        std::string actualScheme(scheme);
172        URI actualURI(uri);
173        int redirects = 0;
174       
175        while (redirects < MAX_REDIRECTS)
176        {
177                try
178                {
179                        FactoryMap::const_iterator it = _map.find(actualScheme);
180                        if (it != _map.end())
181                                return it->second->open(actualURI);
182                        else if (redirects > 0)
183                                throw UnknownURISchemeException(actualURI.toString() + std::string("; redirected from ") + uri.toString());
184                        else
185                                throw UnknownURISchemeException(actualURI.toString());
186                }
187                catch (URIRedirection& redir)
188                {
189                        actualURI = redir.uri();
190                        actualScheme = actualURI.getScheme();
191                        ++redirects;
192                }
193        }
194        throw IOException("Too many redirects while opening URI", uri.toString());
195}
196
197
198} // namespace Poco
Note: See TracBrowser for help on using the repository browser.