source: icXML/icXML-devel/src/xercesc/xinclude/XIncludeLocation.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: 4.3 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 * $Id: XIncludeLocation.cpp 932949 2010-04-11 17:40:33Z borisk $
20 */
21
22
23// ---------------------------------------------------------------------------
24//  Includes
25// ---------------------------------------------------------------------------
26#include <xercesc/xinclude/XIncludeLocation.hpp>
27#include <xercesc/dom/DOM.hpp>
28#include <xercesc/util/XMLUniDefs.hpp>
29
30XERCES_CPP_NAMESPACE_BEGIN
31
32const XMLCh *allocate(const XMLCh *href){
33    XMLCh *allocated;
34    XMLSize_t length = XMLString::stringLen(href);
35    allocated = (XMLCh*)XMLPlatformUtils::fgMemoryManager->allocate((length+1) * sizeof(XMLCh));
36    XMLString::copyString(allocated, href);
37    XMLPlatformUtils::removeDotDotSlash(allocated);
38
39    return allocated;
40}
41
42void deallocate(void *ptr){
43    if (ptr)
44        XMLPlatformUtils::fgMemoryManager->deallocate((void *)ptr);
45}
46
47// ---------------------------------------------------------------------------
48//  Destructor and Constructor
49// ---------------------------------------------------------------------------
50XIncludeLocation::XIncludeLocation(const XMLCh *href){
51    fHref = allocate(href);
52}
53
54XIncludeLocation::~XIncludeLocation(){
55    deallocate((void *)fHref);
56}
57
58const XMLCh *
59XIncludeLocation::prependPath(const XMLCh *baseToAdd){
60    XMLCh *relativeHref = NULL;
61    if (fHref == NULL){
62        return NULL;
63    }
64
65    if (baseToAdd == NULL){
66        return fHref;
67    }
68
69    XMLPlatformUtils::removeDotDotSlash((XMLCh*)baseToAdd);
70    XMLSize_t baseLength = XMLString::stringLen(baseToAdd);
71
72    int lastSlash = XMLString::lastIndexOf(baseToAdd, chForwardSlash);
73    if (lastSlash == -1){
74        /* not found, try another platform */
75        lastSlash = XMLString::lastIndexOf(baseToAdd, chBackSlash);
76    }
77
78    // Skip the scheme (e.g., file://) if fHref has one. Ideally we
79    // should detect also if the URI is absolute.
80    //
81    const XMLCh* hrefPath = findEndOfProtocol (fHref);
82    XMLSize_t hrefPathLength = XMLString::stringLen(hrefPath);
83
84    relativeHref = (XMLCh *)XMLPlatformUtils::fgMemoryManager->allocate((hrefPathLength + baseLength + 2) * sizeof(XMLCh));
85    if (relativeHref == NULL){
86        return NULL;
87    }
88    XMLString::copyNString(relativeHref, baseToAdd, lastSlash + 1);
89    relativeHref[lastSlash + 1] = chNull;
90    XMLString::catString(relativeHref, hrefPath);
91
92    /* free the old reference */
93    deallocate((void *)fHref);
94
95    fHref = relativeHref;
96    return fHref;
97}
98
99const XMLCh *
100XIncludeLocation::findEndOfProtocol(const XMLCh *URI){
101    if ( URI[0] == chLatin_f &&
102        URI[1] == chLatin_i &&
103        URI[2] == chLatin_l &&
104        URI[3] == chLatin_e &&
105        URI[4] == chColon &&
106        URI[5] == chForwardSlash &&
107        URI[6] == chForwardSlash &&
108        URI[7] == chForwardSlash )
109    {
110        return URI + 8;
111    }
112
113    if ( URI[0] == chLatin_f &&
114        URI[1] == chLatin_t &&
115        URI[2] == chLatin_p &&
116        URI[3] == chColon &&
117        URI[4] == chForwardSlash &&
118        URI[5] == chForwardSlash &&
119        URI[6] == chForwardSlash )
120    {
121        return URI + 7;
122    }
123
124    if ( URI[0] == chLatin_h &&
125        URI[1] == chLatin_t &&
126        URI[2] == chLatin_t &&
127        URI[3] == chLatin_p &&
128        URI[4] == chColon &&
129        URI[5] == chForwardSlash &&
130        URI[6] == chForwardSlash &&
131        URI[7] == chForwardSlash )
132    {
133        return URI + 8;
134    }
135
136    /* if method fails, simply return the URI and let the problem be detected
137     * and reported down the line (it may not have a protocol of course) */
138    return URI;
139}
140
141XERCES_CPP_NAMESPACE_END
Note: See TracBrowser for help on using the repository browser.