source: icXML/icXML-devel/src/xercesc/util/NetAccessors/MacOSURLAccessCF/URLAccessCFBinInputStream.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: 5.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: URLAccessCFBinInputStream.cpp 936316 2010-04-21 14:19:58Z borisk $
20 */
21
22#include <cstdlib>
23#include <cstring>
24
25#include <xercesc/util/XMLNetAccessor.hpp>
26#include <xercesc/util/NetAccessors/MacOSURLAccessCF/URLAccessCFBinInputStream.hpp>
27#include <icxercesc/util/XMLString.hpp>
28#include <xercesc/util/XMLExceptMsgs.hpp>
29#include <xercesc/util/Janitor.hpp>
30
31XERCES_CPP_NAMESPACE_BEGIN
32
33URLAccessCFBinInputStream::URLAccessCFBinInputStream(const XMLURL& urlSource)
34      : mBytesProcessed(0),
35        mDataRef(NULL)
36{
37    //  Figure out what we're dealing with
38    const XMLCh* urlText = urlSource.getURLText();
39    unsigned int urlLength = XMLString::stringLen(urlText);
40
41    //  Create a CFString from the path
42    CFStringRef stringRef = NULL;
43    if (urlText)
44    {
45        stringRef = CFStringCreateWithCharacters(
46            kCFAllocatorDefault,
47            urlText,
48            urlLength
49            );
50    }
51
52    //  Create a URLRef from the CFString
53    CFURLRef urlRef = NULL;
54    if (stringRef)
55    {
56        urlRef = CFURLCreateWithString(
57            kCFAllocatorDefault,
58            stringRef,
59            NULL                                // CFURLRef baseURL
60            );
61    }
62
63        //      Fetch the data
64    mDataRef = NULL;
65    SInt32 errorCode = 0;
66    Boolean success = false;
67    if (stringRef)
68    {
69        success = CFURLCreateDataAndPropertiesFromResource(
70            kCFAllocatorDefault,
71            urlRef,
72            &mDataRef,
73            NULL,                               // CFDictionaryRef *properties,
74            NULL,                               // CFArrayRef desiredProperties,
75            &errorCode
76            );
77    }
78
79    //  Cleanup temporary stuff
80    if (stringRef)
81        CFRelease(stringRef);
82    if (urlRef)
83        CFRelease(urlRef);
84
85    //  Check for an error in fetching the data
86    if (!success || errorCode)
87    {
88        //      Dispose any potential dataRef
89        if (mDataRef)
90        {
91            CFRelease(mDataRef);
92            mDataRef = NULL;
93        }
94
95        //      Do a best attempt at mapping some errors
96        switch (errorCode)
97        {
98            case kCFURLUnknownSchemeError:
99                ThrowXML(MalformedURLException, XMLExcepts::URL_UnsupportedProto);
100                break;
101
102            case kCFURLRemoteHostUnavailableError:
103              {
104                if (urlSource.getHost())
105                  ThrowXML1(NetAccessorException, XMLExcepts::NetAcc_TargetResolution, urlSource.getHost());
106                else
107                  ThrowXML1(NetAccessorException, XMLExcepts::File_CouldNotOpenFile, urlText);
108                break;
109              }
110
111            case kCFURLUnknownError:
112                ThrowXML1(NetAccessorException, XMLExcepts::NetAcc_ReadSocket, urlText);
113                break;
114
115            case kCFURLResourceNotFoundError:
116            case kCFURLResourceAccessViolationError:
117            case kCFURLTimeoutError:
118                ThrowXML1(NetAccessorException, XMLExcepts::File_CouldNotOpenFile, urlText);
119                break;
120
121            case kCFURLImproperArgumentsError:
122            case kCFURLUnknownPropertyKeyError:
123            case kCFURLPropertyKeyUnavailableError:
124            default:
125                ThrowXML1(NetAccessorException, XMLExcepts::NetAcc_InternalError, urlText);
126                break;
127        }
128    }
129}
130
131
132URLAccessCFBinInputStream::~URLAccessCFBinInputStream()
133{
134    //  Release any dataRef
135    if (mDataRef)
136        CFRelease(mDataRef);
137}
138
139
140//
141//      We've already read the data into a dataRef.
142//      Just spoon it out to the caller as they ask for it.
143//
144XMLSize_t
145URLAccessCFBinInputStream::readBytes(XMLByte* const    toFill
146                                    , const XMLSize_t  maxToRead)
147{
148    //  If we don't have a dataRef, we can't return any data
149    if (!mDataRef)
150        return 0;
151
152    //  Get the length of the data we've fetched
153    CFIndex dataLength = CFDataGetLength(mDataRef);
154
155    //  Calculate how much to return based on how much
156    //  we've already returned, and how much the user wants
157    CFIndex n = dataLength - mBytesProcessed;                   // Amount remaining
158    CFIndex desired = maxToRead & 0x7fffffff;                   // CFIndex is signed
159    if (n > desired)                                                                    // Amount desired
160        n = desired;
161
162    //  Read the appropriate bytes into the user buffer
163    CFRange range = CFRangeMake(mBytesProcessed, n);
164    CFDataGetBytes(mDataRef, range, reinterpret_cast<UInt8*>(toFill));
165
166    //  Update bytes processed
167    mBytesProcessed += n;
168
169    //  Return the number of bytes delivered
170    return n;
171}
172
173const XMLCh* URLAccessCFBinInputStream::getContentType() const
174{
175    // TODO
176    //
177    return 0;
178}
179
180XERCES_CPP_NAMESPACE_END
Note: See TracBrowser for help on using the repository browser.