source: icXML/icXML-devel/src/xercesc/framework/LocalFileInputSource.cpp @ 2722

Last change on this file since 2722 was 2722, checked in by cameron, 6 years ago

Original Xerces files with import mods for icxercesc

File size: 6.6 KB
Line 
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 *      http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18
19// ---------------------------------------------------------------------------
20//  Includes
21// ---------------------------------------------------------------------------
22#include <xercesc/framework/LocalFileInputSource.hpp>
23#include <xercesc/util/BinFileInputStream.hpp>
24#include <icxercesc/util/PlatformUtils.hpp>
25#include <icxercesc/util/XMLString.hpp>
26#include <xercesc/util/XMLUniDefs.hpp>
27
28XERCES_CPP_NAMESPACE_BEGIN
29
30/***
31 *
32 * Originated by Chris larsson
33 *
34 * Issue:
35 *
36 * There is an inconsistency in URI resolution in the case where the file itself is a
37 * symbolic link to another path (or the path has path segment which is a symbolic
38 * link to another path). So, is the base path the directory where the symbolic link resides
39 * or the directory where the real file resides? I'm sure one could argue either way,
40 * but I think that having the base path be the directory where the symbolic link resides
41 * is more intuitive.
42 *
43 * Defining it this way would then make the behavior consistent with using an absolute
44 * path as well as with the java behavior.
45 *
46 * Proposal:
47 *
48 * The URI is resolved within the parser code, and is somewhat independant of the OS.
49 *
50 * A relative path is resolved by querying the current directory and appending the
51 * relative part onto the returned current directory string to obtain the base URI.
52 * An absolute path is simply used as the base URI.
53 * Then remove all "./" and "../" path segments using an algorithm like weavepath to obtain
54 * the resolved base URI.
55 *
56 * When you need to access another file such as a dtd, use the resolved base URI and add on
57 * the relative URI of the dtd file. Then resolve it using the same weavepath algorithm.
58 *
59 * Note:
60 *
61 *   Java parser behaves differently for a path containning symbolic path segment. When
62 *   it is given an absolute path, it can locate the primary instance document, while given
63 *   relative path, it might not.
64 *
65 *   It is because Java parser uses URI solution where "/segment/../" is required to be removed
66 *   from the resultant path if a relative URI is merged to a baseURI. While this is NOT required
67 *   for an absolute URI.
68 *   
69 *   So if a path segment, which is symbolic link, happen to be followed by the '/../', it is
70 *   NOT removed from the path if it is given in absolute form, and the underlying file system
71 *   will locate the file, if in relative form, that symbolic link path segment together with
72 *   '../' is removed from the resultant path, and the file system may NOT be able to locate
73 *   the file, if there is a one, it is definitely not the one expected, in fact by accident.
74 *
75 *   Therefore, to keep consistent with Java parser, for now, we do not apply removeDotDotSlash()
76 *   for absolute path.
77 * 
78 ***/
79
80// ---------------------------------------------------------------------------
81//  LocalFileInputSource: Constructors and Destructor
82// ---------------------------------------------------------------------------
83LocalFileInputSource::LocalFileInputSource( const XMLCh* const basePath
84                                          , const XMLCh* const relativePath
85                                          , MemoryManager* const manager)
86    : InputSource(manager)
87{
88    //
89    //  If the relative part is really relative, then weave it together
90    //  with the base path. If not, just take the relative path as the
91    //  entire path.
92    //
93    if (XMLPlatformUtils::isRelative(relativePath, manager))
94    {
95        XMLCh* tmpBuf = XMLPlatformUtils::weavePaths(basePath, relativePath, manager);
96        setSystemId(tmpBuf);
97        manager->deallocate(tmpBuf); //delete [] tmpBuf;
98    }
99    else
100    {
101        XMLCh* tmpBuf = XMLString::replicate(relativePath, manager);
102        XMLPlatformUtils::removeDotSlash(tmpBuf, manager);
103        setSystemId(tmpBuf);
104        manager->deallocate(tmpBuf);//delete [] tmpBuf;
105    }
106
107}
108
109LocalFileInputSource::LocalFileInputSource(const XMLCh* const filePath,
110                                           MemoryManager* const manager)
111    : InputSource(manager)
112{
113
114    //
115    //  If the path is relative, then complete it acording to the current
116    //  working directory rules of the current platform. Else, just take
117    //  it as is.
118    //
119    if (XMLPlatformUtils::isRelative(filePath, manager))
120    {
121        XMLCh* curDir = XMLPlatformUtils::getCurrentDirectory(manager);
122
123        XMLSize_t curDirLen = XMLString::stringLen(curDir);
124        XMLSize_t filePathLen = XMLString::stringLen(filePath);
125        XMLCh* fullDir = (XMLCh*) manager->allocate
126        (
127            (curDirLen + filePathLen + 2) * sizeof(XMLCh)
128        );//new XMLCh [ curDirLen + filePathLen + 2];
129
130        XMLString::copyString(fullDir, curDir);
131        fullDir[curDirLen] = chForwardSlash;
132        XMLString::copyString(&fullDir[curDirLen+1], filePath);
133       
134        XMLPlatformUtils::removeDotSlash(fullDir, manager);
135        XMLPlatformUtils::removeDotDotSlash(fullDir, manager);
136
137        setSystemId(fullDir);
138
139        manager->deallocate(curDir);//delete [] curDir;
140        manager->deallocate(fullDir);//delete [] fullDir;
141    }
142     else
143    {
144        XMLCh* tmpBuf = XMLString::replicate(filePath, manager);
145        XMLPlatformUtils::removeDotSlash(tmpBuf, manager);
146        setSystemId(tmpBuf);
147        manager->deallocate(tmpBuf);//delete [] tmpBuf;
148    }
149
150}
151
152LocalFileInputSource::~LocalFileInputSource()
153{
154}
155
156
157// ---------------------------------------------------------------------------
158//  LocalFileInputSource: InputSource interface implementation
159// ---------------------------------------------------------------------------
160BinInputStream* LocalFileInputSource::makeStream() const
161{
162    BinFileInputStream* retStrm = new (getMemoryManager()) BinFileInputStream(getSystemId(), getMemoryManager());
163    if (!retStrm->getIsOpen())
164    {
165        delete retStrm;
166        return 0;
167    }
168    return retStrm;
169}
170
171XERCES_CPP_NAMESPACE_END
172
Note: See TracBrowser for help on using the repository browser.