| 1 | /** |
| 2 | * Licensed to jclouds, Inc. (jclouds) under one or more |
| 3 | * contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. jclouds licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. 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, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | package org.jclouds.deltacloud.xml; |
| 20 | |
| 21 | import java.net.URI; |
| 22 | import java.util.Map; |
| 23 | |
| 24 | import org.jclouds.deltacloud.domain.Image; |
| 25 | import org.jclouds.http.functions.ParseSax; |
| 26 | import org.jclouds.util.SaxUtils; |
| 27 | import org.xml.sax.Attributes; |
| 28 | import org.xml.sax.SAXException; |
| 29 | |
| 30 | /** |
| 31 | * @author Adrian Cole |
| 32 | */ |
| 33 | public class ImageHandler extends ParseSax.HandlerWithResult<Image> { |
| 34 | private StringBuilder currentText = new StringBuilder(); |
| 35 | |
| 36 | private URI href; |
| 37 | private String id; |
| 38 | private String ownerId; |
| 39 | private String name; |
| 40 | private String description; |
| 41 | private String architecture; |
| 42 | |
| 43 | private Image image; |
| 44 | |
| 45 | public Image getResult() { |
| 46 | return image; |
| 47 | } |
| 48 | |
| 49 | @Override |
| 50 | public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException { |
| 51 | Map<String, String> attributes = SaxUtils.cleanseAttributes(attrs); |
| 52 | if (qName.equals("image")) { |
| 53 | String href = attributes.get("href"); |
| 54 | if (href != null) { |
| 55 | this.href = URI.create(href); |
| 56 | } |
| 57 | this.id = attributes.get("id"); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public void endElement(String uri, String localName, String qName) throws SAXException { |
| 63 | if (qName.equalsIgnoreCase("owner_id")) { |
| 64 | this.ownerId = currentText.toString().trim(); |
| 65 | } else if (qName.equalsIgnoreCase("name")) { |
| 66 | this.name = currentText.toString().trim(); |
| 67 | } else if (qName.equalsIgnoreCase("description")) { |
| 68 | this.description = currentText.toString().trim(); |
| 69 | } else if (qName.equalsIgnoreCase("architecture")) { |
| 70 | this.architecture = currentText.toString().trim(); |
| 71 | } else if (qName.equalsIgnoreCase("image")) { |
| 72 | this.image = new Image(href, id, ownerId, name, description, architecture); |
| 73 | this.href = null; |
| 74 | this.id = null; |
| 75 | this.ownerId = null; |
| 76 | this.name = null; |
| 77 | this.description = null; |
| 78 | this.architecture = null; |
| 79 | } |
| 80 | currentText = new StringBuilder(); |
| 81 | } |
| 82 | |
| 83 | public void characters(char ch[], int start, int length) { |
| 84 | currentText.append(ch, start, length); |
| 85 | } |
| 86 | } |