| 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.Realm; |
| 25 | import org.jclouds.deltacloud.domain.Realm.State; |
| 26 | import org.jclouds.http.functions.ParseSax; |
| 27 | import org.jclouds.util.SaxUtils; |
| 28 | import org.xml.sax.Attributes; |
| 29 | import org.xml.sax.SAXException; |
| 30 | |
| 31 | /** |
| 32 | * @author Adrian Cole |
| 33 | */ |
| 34 | public class RealmHandler extends ParseSax.HandlerWithResult<Realm> { |
| 35 | private StringBuilder currentText = new StringBuilder(); |
| 36 | |
| 37 | private URI href; |
| 38 | private String id; |
| 39 | private String name; |
| 40 | private String limit; |
| 41 | private State state; |
| 42 | |
| 43 | private Realm realm; |
| 44 | |
| 45 | public Realm getResult() { |
| 46 | return realm; |
| 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 (attributes.containsKey("href")) { |
| 53 | this.href = URI.create(attributes.get("href")); |
| 54 | this.id = attributes.get("id"); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public void endElement(String uri, String localName, String qName) throws SAXException { |
| 60 | if (qName.equalsIgnoreCase("limit")) { |
| 61 | this.limit = currentText.toString().trim(); |
| 62 | if ("".equals(limit)) |
| 63 | limit = null; |
| 64 | } else if (qName.equalsIgnoreCase("name")) { |
| 65 | this.name = currentText.toString().trim(); |
| 66 | } else if (qName.equalsIgnoreCase("state")) { |
| 67 | this.state = State.fromValue(currentText.toString().trim()); |
| 68 | } else if (qName.equalsIgnoreCase("realm")) { |
| 69 | this.realm = new Realm(href, id, name, limit, state); |
| 70 | this.href = null; |
| 71 | this.id = null; |
| 72 | this.name = null; |
| 73 | this.limit = null; |
| 74 | this.state = null; |
| 75 | } |
| 76 | currentText = new StringBuilder(); |
| 77 | } |
| 78 | |
| 79 | public void characters(char ch[], int start, int length) { |
| 80 | currentText.append(ch, start, length); |
| 81 | } |
| 82 | } |