1 /*
2  * Copyright 2009-2017 Alibaba Cloud All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 
18 #include <alibabacloud/oss/model/ListBucketsResult.h>
19 #include <external/tinyxml2/tinyxml2.h>
20 #include <alibabacloud/oss/model/Bucket.h>
21 #include <alibabacloud/oss/model/Owner.h>
22 #include "utils/Utils.h"
23 using namespace AlibabaCloud::OSS;
24 using namespace tinyxml2;
25 
ListBucketsResult()26 ListBucketsResult::ListBucketsResult():
27     OssResult(),
28     prefix_(),
29     marker_(),
30     nextMarker_(),
31     isTruncated_(false),
32     maxKeys_()
33 {
34 }
35 
ListBucketsResult(const std::string & result)36 ListBucketsResult::ListBucketsResult(const std::string& result):
37     ListBucketsResult()
38 {
39     *this = result;
40 }
41 
ListBucketsResult(const std::shared_ptr<std::iostream> & result)42 ListBucketsResult::ListBucketsResult(const std::shared_ptr<std::iostream>& result):
43     ListBucketsResult()
44 {
45     std::istreambuf_iterator<char> isb(*result.get()), end;
46     std::string str(isb, end);
47     *this = str;
48 }
49 
operator =(const std::string & result)50 ListBucketsResult& ListBucketsResult::operator =(const std::string& result)
51 {
52     XMLDocument doc;
53     XMLError xml_err;
54     if ((xml_err = doc.Parse(result.c_str(), result.size())) == XML_SUCCESS) {
55         XMLElement* root =doc.RootElement();
56         if (root && !std::strncmp("ListAllMyBucketsResult", root->Name(), 22)) {
57             XMLElement *node;
58             node = root->FirstChildElement("Prefix");
59             if (node && node->GetText()) prefix_ = node->GetText();
60 
61             node = root->FirstChildElement("Marker");
62             if (node && node->GetText()) marker_ = node->GetText();
63 
64             node = root->FirstChildElement("MaxKeys");
65             if (node && node->GetText()) maxKeys_ = atoi(node->GetText());
66 
67             node = root->FirstChildElement("IsTruncated");
68             if (node && node->GetText()) isTruncated_ = !std::strncmp("true", node->GetText(), 4);
69 
70             node = root->FirstChildElement("NextMarker");
71             if (node && node->GetText()) nextMarker_ = node->GetText();
72 
73             node = root->FirstChildElement("Owner");
74             std::string owner_ID, owner_DisplayName;
75             if (node) {
76                 XMLElement *sub_node;
77                 sub_node = node->FirstChildElement("ID");
78                 if (sub_node && sub_node->GetText()) owner_ID = sub_node->GetText();
79 
80                 sub_node = node->FirstChildElement("DisplayName");
81                 if (sub_node && sub_node->GetText()) owner_DisplayName = sub_node->GetText();
82             }
83 
84             Owner owner(owner_ID, owner_DisplayName);
85             //buckets
86             XMLElement *buckets_node = root->FirstChildElement("Buckets");
87             if (buckets_node) {
88                 XMLElement *bucket_node = buckets_node->FirstChildElement("Bucket");
89                 for (; bucket_node; bucket_node = bucket_node->NextSiblingElement()) {
90                     Bucket bucket;
91                     node = bucket_node->FirstChildElement("CreationDate");
92                     if (node && node->GetText()) bucket.creationDate_ = node->GetText();
93 
94                     node = bucket_node->FirstChildElement("ExtranetEndpoint");
95                     if (node && node->GetText()) bucket.extranetEndpoint_ = node->GetText();
96 
97                     node = bucket_node->FirstChildElement("IntranetEndpoint");
98                     if (node && node->GetText()) bucket.intranetEndpoint_ = node->GetText();
99 
100                     node = bucket_node->FirstChildElement("Location");
101                     if (node && node->GetText()) bucket.location_ = node->GetText();
102 
103                     node = bucket_node->FirstChildElement("Name");
104                     if (node && node->GetText()) bucket.name_ = node->GetText();
105 
106                     node = bucket_node->FirstChildElement("StorageClass");
107                     if (node && node->GetText()) bucket.storageClass_ = ToStorageClassType(node->GetText());
108 
109                     bucket.owner_ = owner;
110                     buckets_.push_back(bucket);
111                 }
112             }
113         }
114 
115         //TODO check the result and the parse flag;
116         parseDone_ = true;
117     }
118     return *this;
119 }
120 
121