source: icXML/icXML-devel/tests/src/NetAccessorTest/NetAccessorTest.cpp @ 2726

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

Add original Xerces tests and samples directories

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: NetAccessorTest.cpp 470088 2006-11-01 20:35:12Z amassari $
20 *
21 */
22
23
24// ---------------------------------------------------------------------------
25//  Includes
26// ---------------------------------------------------------------------------
27#include    <xercesc/util/PlatformUtils.hpp>
28#include    <xercesc/util/XMLString.hpp>
29#include    <xercesc/util/XMLURL.hpp>
30#include    <xercesc/util/XMLNetAccessor.hpp>
31#include    <xercesc/util/BinInputStream.hpp>
32
33#if defined(XERCES_NEW_IOSTREAMS)
34#include        <iostream>
35#else
36#include        <iostream.h>
37#endif
38
39XERCES_CPP_NAMESPACE_USE
40
41
42inline XERCES_STD_QUALIFIER ostream& operator<<(XERCES_STD_QUALIFIER ostream& os, const XMLCh* xmlStr)
43{
44        char* transcoded = XMLString::transcode(xmlStr);
45    os << transcoded;
46    XMLString::release(&transcoded);
47    return os;
48}
49
50
51void
52exercise(BinInputStream& stream)
53{
54        static float percents[] = { 1.0, 0.5, 0.25, 0.1, 0.15, 0.113, 0.333, 0.0015, 0.0013 };
55        int numPercents = sizeof(percents) / sizeof(float);
56       
57        const unsigned int bufferMax = 4096;
58        XMLByte buffer[bufferMax];
59
60        int iteration = 0;
61        unsigned int bytesRead = 0;
62        do {
63                // Calculate a percentage of our maximum buffer size, going through
64                // them round-robin
65                float percent = percents[iteration % numPercents];
66                unsigned int bufCnt = (unsigned int)(bufferMax * percent);
67               
68                // Check to make sure we didn't go out of bounds
69                if (bufCnt <= 0)
70                        bufCnt = 1;
71                if (bufCnt > bufferMax)
72                        bufCnt = bufferMax;
73               
74                // Read bytes into our buffer
75                bytesRead = stream.readBytes(buffer, bufCnt);
76                //XERCES_STD_QUALIFIER cerr << "Read " << bytesRead << " bytes into a " << bufCnt << " byte buffer\n";
77
78                if (bytesRead > 0)
79                {
80                        // Write the data to standard out
81                        XERCES_STD_QUALIFIER cout.write((char*)buffer, bytesRead);
82                }
83               
84                ++iteration;
85        } while (bytesRead > 0);
86}
87
88
89// ---------------------------------------------------------------------------
90//  Program entry point
91// ---------------------------------------------------------------------------
92int
93main(int argc, char** argv)
94{
95    // Init the XML platform
96    try
97    {
98        XMLPlatformUtils::Initialize();
99    }
100
101    catch(const XMLException& toCatch)
102    {
103        XERCES_STD_QUALIFIER cout << "Error during platform init! Message:\n"
104             << toCatch.getMessage() << XERCES_STD_QUALIFIER endl;
105        return 1;
106    }
107   
108    // Look for our one and only parameter
109    if (argc != 2)
110    {
111        XERCES_STD_QUALIFIER cerr << "Usage: NetAccessorTest url\n"
112                        "\n"
113                        "This test reads data from the given url and writes the result\n"
114                        "to standard output.\n"
115                        "\n"
116                        "A variety of buffer sizes is are used during the test.\n"
117                        "\n"
118                        ;
119        exit(1);
120    }
121   
122    // Get the URL
123    char* url = argv[1];
124   
125    // Do the test
126    try
127    {
128        XMLURL xmlURL(url);
129       
130                // Get the netaccessor
131                XMLNetAccessor* na = XMLPlatformUtils::fgNetAccessor;
132                if (na == 0)
133                {
134                        XERCES_STD_QUALIFIER cerr <<  "No netaccessor is available. Aborting.\n";
135                        exit(2);
136                }
137               
138                // Build a binary input stream
139                BinInputStream* is = na->makeNew(xmlURL);
140                if (is == 0)
141                {
142                        XERCES_STD_QUALIFIER cerr <<  "No binary input stream created. Aborting.\n";
143                        exit(3);
144                }
145               
146                // Exercise the inputstream
147                exercise(*is);
148               
149                // Delete the is
150                delete is;
151       
152    }
153    catch(const XMLException& toCatch)
154    {
155        XERCES_STD_QUALIFIER cout << "Exception during test:\n    "
156             << toCatch.getMessage()
157             << XERCES_STD_QUALIFIER endl;
158    }
159
160    // And call the termination method
161    XMLPlatformUtils::Terminate();
162
163    return 0;
164}
165
Note: See TracBrowser for help on using the repository browser.