View Javadoc

1   /**
2    *
3    * Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com>
4    *
5    * ====================================================================
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   * ====================================================================
18   */
19  package org.jclouds.azureblob.xml;
20  
21  import java.net.URI;
22  import java.util.Date;
23  import java.util.Map;
24  import java.util.SortedSet;
25  
26  import javax.inject.Inject;
27  
28  import org.jclouds.azureblob.domain.ContainerProperties;
29  import org.jclouds.azureblob.domain.internal.ContainerPropertiesImpl;
30  import org.jclouds.azure.storage.domain.BoundedSet;
31  import org.jclouds.azure.storage.domain.internal.BoundedHashSet;
32  import org.jclouds.date.DateService;
33  import org.jclouds.http.functions.ParseSax;
34  import org.xml.sax.Attributes;
35  import org.xml.sax.SAXException;
36  
37  import com.google.common.collect.Maps;
38  import com.google.common.collect.Sets;
39  
40  /**
41   * Parses the following XML document:
42   * <p/>
43   * EnumerationResults AccountName="http://myaccount.blob.core.windows.net"
44   * 
45   * @see <a href="http://msdn.microsoft.com/en-us/library/dd179352.aspx" />
46   * @author Adrian Cole
47   */
48  public class AccountNameEnumerationResultsHandler extends
49           ParseSax.HandlerWithResult<BoundedSet<ContainerProperties>> {
50  
51     private SortedSet<ContainerProperties> containerMetadata = Sets.newTreeSet();
52     private String prefix;
53     private String marker;
54     private int maxResults;
55     private String nextMarker;
56     private URI currentUrl;
57     private Date currentLastModified;
58     private String currentETag;
59     private boolean inMetadata;
60  
61     private Map<String, String> currentMetadata = Maps.newHashMap();
62  
63     private StringBuilder currentText = new StringBuilder();
64  
65     private final DateService dateParser;
66     private URI accountUrl;
67  
68     @Inject
69     public AccountNameEnumerationResultsHandler(DateService dateParser) {
70        this.dateParser = dateParser;
71     }
72  
73     @Override
74     public void startElement(String uri, String localName, String qName, Attributes attributes)
75              throws SAXException {
76        if (qName.equals("Container")) {
77           inMetadata = false;
78        } else if (qName.equals("Metadata")) {
79           inMetadata = true;
80        } else if (qName.equals("EnumerationResults")) {
81           accountUrl = URI.create(attributes.getValue("AccountName").toString().trim());
82        }
83     }
84  
85     public BoundedSet<ContainerProperties> getResult() {
86        return new BoundedHashSet<ContainerProperties>(containerMetadata, accountUrl, prefix, marker,
87                 maxResults, nextMarker);
88     }
89  
90     public void endElement(String uri, String name, String qName) {
91        if (inMetadata && !qName.equals("Metadata")) {
92           currentMetadata.put(qName, currentText.toString().trim());
93        } else if (qName.equals("Metadata")) {
94           inMetadata = false;
95        } else if (qName.equals("MaxResults")) {
96           maxResults = Integer.parseInt(currentText.toString().trim());
97        } else if (qName.equals("Marker")) {
98           marker = currentText.toString().trim();
99           marker = (marker.equals("")) ? null : marker;
100       } else if (qName.equals("Prefix")) {
101          prefix = currentText.toString().trim();
102          prefix = (prefix.equals("")) ? null : prefix;
103       } else if (qName.equals("NextMarker")) {
104          nextMarker = currentText.toString().trim();
105          nextMarker = (nextMarker.equals("")) ? null : nextMarker;
106       } else if (qName.equals("Container")) {
107          containerMetadata.add(new ContainerPropertiesImpl(currentUrl, currentLastModified,
108                   currentETag, currentMetadata));
109          currentUrl = null;
110          currentLastModified = null;
111          currentETag = null;
112          currentMetadata = Maps.newHashMap();
113       } else if (qName.equals("Url")) {
114          currentUrl = URI.create(currentText.toString().trim());
115       } else if (qName.equals("Last-Modified")) {
116          currentLastModified = dateParser.rfc822DateParse(currentText.toString().trim());
117       } else if (qName.equals("Etag")) {
118          currentETag = currentText.toString().trim();
119       }
120       currentText = new StringBuilder();
121    }
122 
123    public void characters(char ch[], int start, int length) {
124       currentText.append(ch, start, length);
125    }
126 }