| 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.vcloud.xml.ovf; |
| 20 | |
| 21 | import static org.jclouds.vcloud.util.Utils.newReferenceType; |
| 22 | |
| 23 | import java.util.Map; |
| 24 | |
| 25 | import org.jclouds.http.functions.ParseSax; |
| 26 | import org.jclouds.util.SaxUtils; |
| 27 | import org.jclouds.vcloud.domain.ReferenceType; |
| 28 | import org.jclouds.vcloud.domain.ovf.VCloudOperatingSystemSection; |
| 29 | import org.jclouds.vcloud.util.Utils; |
| 30 | import org.xml.sax.Attributes; |
| 31 | |
| 32 | /** |
| 33 | * @author Adrian Cole |
| 34 | */ |
| 35 | public class VCloudOperatingSystemHandler extends ParseSax.HandlerWithResult<VCloudOperatingSystemSection> { |
| 36 | private StringBuilder currentText = new StringBuilder(); |
| 37 | |
| 38 | protected ReferenceType os; |
| 39 | protected Integer id; |
| 40 | protected String info; |
| 41 | protected String vmwOsType; |
| 42 | protected String description; |
| 43 | protected ReferenceType edit; |
| 44 | |
| 45 | public VCloudOperatingSystemSection getResult() { |
| 46 | VCloudOperatingSystemSection system = new VCloudOperatingSystemSection(id, info, description, os.getType(), os.getHref(), |
| 47 | vmwOsType, edit); |
| 48 | os = null; |
| 49 | id = null; |
| 50 | info = null; |
| 51 | vmwOsType = null; |
| 52 | description = null; |
| 53 | edit = null; |
| 54 | return system; |
| 55 | } |
| 56 | |
| 57 | @Override |
| 58 | public void startElement(String uri, String localName, String qName, Attributes attrs) { |
| 59 | Map<String, String> attributes = SaxUtils.cleanseAttributes(attrs); |
| 60 | if (qName.endsWith("Link")) { |
| 61 | this.edit = Utils.newReferenceType(attributes); |
| 62 | } else if (qName.endsWith("OperatingSystemSection")) { |
| 63 | os = newReferenceType(attributes); |
| 64 | vmwOsType = attributes.get("osType"); |
| 65 | if (attributes.containsKey("id")) |
| 66 | this.id = Integer.parseInt(attributes.get("id")); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public void endElement(String uri, String localName, String qName) { |
| 72 | if (qName.endsWith("Info")) { |
| 73 | this.info = currentText.toString().trim(); |
| 74 | } else if (qName.endsWith("Description")) { |
| 75 | this.description = currentText.toString().trim(); |
| 76 | } |
| 77 | currentText = new StringBuilder(); |
| 78 | } |
| 79 | |
| 80 | public void characters(char ch[], int start, int length) { |
| 81 | currentText.append(ch, start, length); |
| 82 | } |
| 83 | } |