diff --git a/vcloud/core/src/main/java/org/jclouds/vcloud/util/Utils.java b/vcloud/core/src/main/java/org/jclouds/vcloud/util/Utils.java index 99cd97dc25..aa07a06f9a 100644 --- a/vcloud/core/src/main/java/org/jclouds/vcloud/util/Utils.java +++ b/vcloud/core/src/main/java/org/jclouds/vcloud/util/Utils.java @@ -28,42 +28,50 @@ import org.jclouds.vcloud.domain.internal.ReferenceTypeImpl; import org.jclouds.vcloud.domain.internal.TaskImpl.ErrorImpl; import org.xml.sax.Attributes; +import com.google.common.collect.Maps; + /** * * @author Adrian Cole */ public class Utils { - public static ReferenceType newNamedResource(Attributes attributes, String defaultType) { - String uri = attributes.getValue(attributes.getIndex("href")); - String type = attributes.getValue(attributes.getIndex("type")); - return new ReferenceTypeImpl(attributes.getValue(attributes.getIndex("name")), type != null ? type : defaultType, - URI.create(uri)); + public static ReferenceType newReferenceType(Map attributes, String defaultType) { + String uri = attributes.get("href"); + String type = attributes.get("type"); + return new ReferenceTypeImpl(attributes.get("name"), type != null ? type : defaultType, URI.create(uri)); } - public static ReferenceType newNamedResource(Attributes attributes) { - return newNamedResource(attributes, null); + public static Map cleanseAttributes(Attributes in) { + Map attrs = Maps.newLinkedHashMap(); + for (int i = 0; i < in.getLength(); i++) { + String name = in.getQName(i); + if (name.indexOf(':') != -1) + name = name.substring(name.indexOf(':') + 1); + attrs.put(name, in.getValue(i)); + } + return attrs; } - public static Task.Error newError(Attributes attributes) { - String minorErrorCode = attrOrNull(attributes, "minorErrorCode"); - String vendorSpecificErrorCode = attrOrNull(attributes, "vendorSpecificErrorCode"); + public static ReferenceType newReferenceType(Map attributes) { + return newReferenceType(attributes, null); + } + + public static Task.Error newError(Map attributes) { + String minorErrorCode = attributes.get("minorErrorCode"); + String vendorSpecificErrorCode = attributes.get("vendorSpecificErrorCode"); int errorCode; // remove this logic when vcloud 0.8 is gone try { - errorCode = Integer.parseInt(attrOrNull(attributes, "majorErrorCode")); + errorCode = Integer.parseInt(attributes.get("majorErrorCode")); } catch (NumberFormatException e) { errorCode = 500; - vendorSpecificErrorCode = attrOrNull(attributes, "majorErrorCode"); + vendorSpecificErrorCode = attributes.get("majorErrorCode"); } - return new ErrorImpl(attrOrNull(attributes, "message"), errorCode, minorErrorCode, vendorSpecificErrorCode, - attrOrNull(attributes, "stackTrace")); + return new ErrorImpl(attributes.get("message"), errorCode, minorErrorCode, vendorSpecificErrorCode, attributes + .get("stackTrace")); } - public static String attrOrNull(Attributes attributes, String attr) { - return attributes.getIndex(attr) >= 0 ? attributes.getValue(attributes.getIndex(attr)) : null; - } - - public static void putNamedResource(Map map, Attributes attributes) { - map.put(attributes.getValue(attributes.getIndex("name")), newNamedResource(attributes)); + public static void putReferenceType(Map map, Map attributes) { + map.put(attributes.get("name"), newReferenceType(attributes)); } } diff --git a/vcloud/core/src/main/java/org/jclouds/vcloud/xml/CatalogHandler.java b/vcloud/core/src/main/java/org/jclouds/vcloud/xml/CatalogHandler.java index a4822604ea..aa478c5b73 100644 --- a/vcloud/core/src/main/java/org/jclouds/vcloud/xml/CatalogHandler.java +++ b/vcloud/core/src/main/java/org/jclouds/vcloud/xml/CatalogHandler.java @@ -19,9 +19,12 @@ package org.jclouds.vcloud.xml; -import static org.jclouds.vcloud.util.Utils.newNamedResource; +import static org.jclouds.vcloud.util.Utils.cleanseAttributes; +import static org.jclouds.vcloud.util.Utils.newReferenceType; +import static org.jclouds.vcloud.util.Utils.putReferenceType; import java.util.List; +import java.util.Map; import java.util.SortedMap; import javax.inject.Inject; @@ -32,7 +35,6 @@ import org.jclouds.vcloud.domain.Catalog; import org.jclouds.vcloud.domain.ReferenceType; import org.jclouds.vcloud.domain.Task; import org.jclouds.vcloud.domain.internal.CatalogImpl; -import org.jclouds.vcloud.util.Utils; import org.xml.sax.Attributes; import org.xml.sax.SAXException; @@ -62,20 +64,21 @@ public class CatalogHandler extends ParseSax.HandlerWithResult { private boolean published = true; public Catalog getResult() { - return new CatalogImpl(catalog.getName(), catalog.getType(), catalog.getHref(), org, description, contents, tasks, - published); + return new CatalogImpl(catalog.getName(), catalog.getType(), catalog.getHref(), org, description, contents, + tasks, published); } @Override - public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { + public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException { + Map attributes = cleanseAttributes(attrs); if (qName.equals("Catalog")) { - catalog = Utils.newNamedResource(attributes, VCloudMediaType.CATALOG_XML); + catalog = newReferenceType(attributes, VCloudMediaType.CATALOG_XML); } else if (qName.equals("CatalogItem")) { - Utils.putNamedResource(contents, attributes); - } else if (qName.equals("Link") && "up".equals(Utils.attrOrNull(attributes, "rel"))) { - org = newNamedResource(attributes); + putReferenceType(contents, attributes); + } else if (qName.equals("Link") && "up".equals(attributes.get("rel"))) { + org = newReferenceType(attributes); } else { - taskHandler.startElement(uri, localName, qName, attributes); + taskHandler.startElement(uri, localName, qName, attrs); } } diff --git a/vcloud/core/src/main/java/org/jclouds/vcloud/xml/CatalogItemHandler.java b/vcloud/core/src/main/java/org/jclouds/vcloud/xml/CatalogItemHandler.java index 5cc470e6ee..4b077a8127 100644 --- a/vcloud/core/src/main/java/org/jclouds/vcloud/xml/CatalogItemHandler.java +++ b/vcloud/core/src/main/java/org/jclouds/vcloud/xml/CatalogItemHandler.java @@ -19,13 +19,16 @@ package org.jclouds.vcloud.xml; +import static org.jclouds.vcloud.util.Utils.cleanseAttributes; +import static org.jclouds.vcloud.util.Utils.newReferenceType; + +import java.util.Map; import java.util.SortedMap; import org.jclouds.http.functions.ParseSax; import org.jclouds.vcloud.domain.CatalogItem; import org.jclouds.vcloud.domain.ReferenceType; import org.jclouds.vcloud.domain.internal.CatalogItemImpl; -import org.jclouds.vcloud.util.Utils; import org.xml.sax.Attributes; import org.xml.sax.SAXException; @@ -49,13 +52,14 @@ public class CatalogItemHandler extends ParseSax.HandlerWithResult } @Override - public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { + public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException { + Map attributes = cleanseAttributes(attrs); if (qName.equals("CatalogItem")) { - catalogItem = Utils.newNamedResource(attributes); + catalogItem = newReferenceType(attributes); } else if (qName.equals("Entity")) { - entity = Utils.newNamedResource(attributes); + entity = newReferenceType(attributes); } else if (qName.equals("Property")) { - key = attributes.getValue(attributes.getIndex("key")); + key = attributes.get("key"); } } diff --git a/vcloud/core/src/main/java/org/jclouds/vcloud/xml/OrgHandler.java b/vcloud/core/src/main/java/org/jclouds/vcloud/xml/OrgHandler.java index 0feccf5be2..e81916f2f1 100644 --- a/vcloud/core/src/main/java/org/jclouds/vcloud/xml/OrgHandler.java +++ b/vcloud/core/src/main/java/org/jclouds/vcloud/xml/OrgHandler.java @@ -19,8 +19,9 @@ package org.jclouds.vcloud.xml; -import static org.jclouds.vcloud.util.Utils.newNamedResource; -import static org.jclouds.vcloud.util.Utils.putNamedResource; +import static org.jclouds.vcloud.util.Utils.cleanseAttributes; +import static org.jclouds.vcloud.util.Utils.newReferenceType; +import static org.jclouds.vcloud.util.Utils.putReferenceType; import java.util.List; import java.util.Map; @@ -28,8 +29,8 @@ import java.util.Map; import javax.inject.Inject; import org.jclouds.http.functions.ParseSax; -import org.jclouds.vcloud.domain.ReferenceType; import org.jclouds.vcloud.domain.Org; +import org.jclouds.vcloud.domain.ReferenceType; import org.jclouds.vcloud.domain.Task; import org.jclouds.vcloud.domain.internal.OrgImpl; import org.xml.sax.Attributes; @@ -68,24 +69,25 @@ public class OrgHandler extends ParseSax.HandlerWithResult { } @Override - public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { + public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException { + Map attributes = cleanseAttributes(attrs); if (qName.equals("Org")) { - org = newNamedResource(attributes); + org = newReferenceType(attributes); } else if (qName.equals("Link")) { - int typeIndex = attributes.getIndex("type"); - if (typeIndex != -1) { - if (attributes.getValue(typeIndex).indexOf("vdc+xml") != -1) { - putNamedResource(vdcs, attributes); - } else if (attributes.getValue(typeIndex).indexOf("catalog+xml") != -1) { - putNamedResource(catalogs, attributes); - } else if (attributes.getValue(typeIndex).indexOf("tasksList+xml") != -1) { - tasksList = newNamedResource(attributes); - } else if (attributes.getValue(typeIndex).indexOf("network+xml") != -1) { - putNamedResource(networks, attributes); + String type = attributes.get("type"); + if (type != null) { + if (type.indexOf("vdc+xml") != -1) { + putReferenceType(vdcs, attributes); + } else if (type.indexOf("catalog+xml") != -1) { + putReferenceType(catalogs, attributes); + } else if (type.indexOf("tasksList+xml") != -1) { + tasksList = newReferenceType(attributes); + } else if (type.indexOf("network+xml") != -1) { + putReferenceType(networks, attributes); } } } else { - taskHandler.startElement(uri, localName, qName, attributes); + taskHandler.startElement(uri, localName, qName, attrs); } } diff --git a/vcloud/core/src/main/java/org/jclouds/vcloud/xml/OrgListHandler.java b/vcloud/core/src/main/java/org/jclouds/vcloud/xml/OrgListHandler.java index 5505663033..47b28735f6 100755 --- a/vcloud/core/src/main/java/org/jclouds/vcloud/xml/OrgListHandler.java +++ b/vcloud/core/src/main/java/org/jclouds/vcloud/xml/OrgListHandler.java @@ -19,8 +19,8 @@ package org.jclouds.vcloud.xml; -import static org.jclouds.vcloud.VCloudExpressMediaType.ORG_XML; -import static org.jclouds.vcloud.util.Utils.putNamedResource; +import static org.jclouds.vcloud.util.Utils.cleanseAttributes; +import static org.jclouds.vcloud.util.Utils.putReferenceType; import java.util.Map; @@ -34,25 +34,24 @@ import com.google.common.collect.Maps; /** * @author Adrian Cole */ -public class OrgListHandler extends - ParseSax.HandlerWithResult> { +public class OrgListHandler extends ParseSax.HandlerWithResult> { - private Map org = Maps.newHashMap(); + private Map org = Maps.newHashMap(); - public Map getResult() { - return org; - } + public Map getResult() { + return org; + } - @Override - public void startElement(String uri, String localName, String qName, - Attributes attributes) throws SAXException { - if (qName.equals("Org")) { - int typeIndex = attributes.getIndex("type"); - if (typeIndex != -1) { - if (attributes.getValue(typeIndex).equals(ORG_XML)) { - putNamedResource(org, attributes); - } - } - } - } + @Override + public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException { + Map attributes = cleanseAttributes(attrs); + if (qName.equals("Org")) { + String type = attributes.get("type"); + if (type != null) { + if (type.indexOf("org+xml") != -1) { + putReferenceType(org, attributes); + } + } + } + } } diff --git a/vcloud/core/src/main/java/org/jclouds/vcloud/xml/OrgNetworkHandler.java b/vcloud/core/src/main/java/org/jclouds/vcloud/xml/OrgNetworkHandler.java index cbe0c743b3..ffbbb68145 100644 --- a/vcloud/core/src/main/java/org/jclouds/vcloud/xml/OrgNetworkHandler.java +++ b/vcloud/core/src/main/java/org/jclouds/vcloud/xml/OrgNetworkHandler.java @@ -19,9 +19,11 @@ package org.jclouds.vcloud.xml; -import static org.jclouds.vcloud.util.Utils.newNamedResource; +import static org.jclouds.vcloud.util.Utils.cleanseAttributes; +import static org.jclouds.vcloud.util.Utils.newReferenceType; import java.util.List; +import java.util.Map; import java.util.Set; import javax.inject.Inject; @@ -49,7 +51,6 @@ import org.jclouds.vcloud.domain.network.nat.NatType; import org.jclouds.vcloud.domain.network.nat.rules.OneToOneVmRule; import org.jclouds.vcloud.domain.network.nat.rules.PortForwardingRule; import org.jclouds.vcloud.domain.network.nat.rules.VmRule; -import org.jclouds.vcloud.util.Utils; import org.xml.sax.Attributes; import org.xml.sax.SAXException; @@ -142,22 +143,23 @@ public class OrgNetworkHandler extends ParseSax.HandlerWithResult { } @Override - public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { + public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException { + Map attributes = cleanseAttributes(attrs); if (qName.equals("OrgNetwork")) { - network = newNamedResource(attributes); + network = newReferenceType(attributes); } else if (qName.equals("FirewallRule")) { this.inFirewallRule = true; } else if (qName.equals("ParentNetwork")) { - parentNetwork = newNamedResource(attributes); - } else if (qName.equals("Link") && "up".equals(Utils.attrOrNull(attributes, "rel"))) { - org = newNamedResource(attributes); + parentNetwork = newReferenceType(attributes); + } else if (qName.equals("Link") && "up".equals(attributes.get("rel"))) { + org = newReferenceType(attributes); } else { - taskHandler.startElement(uri, localName, qName, attributes); + taskHandler.startElement(uri, localName, qName, attrs); } - int typeIndex = attributes.getIndex("type"); - if (typeIndex != -1) { - if (attributes.getValue(typeIndex).indexOf("networkPool+xml") != -1) { - networkPool = newNamedResource(attributes); + String type = attributes.get("type"); + if (type != null) { + if (type.indexOf("networkPool+xml") != -1) { + networkPool = newReferenceType(attributes); } } } diff --git a/vcloud/core/src/main/java/org/jclouds/vcloud/xml/TaskHandler.java b/vcloud/core/src/main/java/org/jclouds/vcloud/xml/TaskHandler.java index 078c76c95a..3182ffa1a9 100644 --- a/vcloud/core/src/main/java/org/jclouds/vcloud/xml/TaskHandler.java +++ b/vcloud/core/src/main/java/org/jclouds/vcloud/xml/TaskHandler.java @@ -19,8 +19,11 @@ package org.jclouds.vcloud.xml; +import static org.jclouds.vcloud.util.Utils.cleanseAttributes; + import java.text.ParseException; import java.util.Date; +import java.util.Map; import javax.inject.Inject; @@ -63,31 +66,30 @@ public class TaskHandler extends ParseSax.HandlerWithResult { } @Override - public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { + public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException { + Map attributes = cleanseAttributes(attrs); if (qName.equalsIgnoreCase("Task")) { - if (attributes.getIndex("href") != -1)// queued tasks may not have an + if (attributes.get("href") != null)// queued tasks may not have an // href yet - taskLink = Utils.newNamedResource(attributes); - status = TaskStatus.fromValue(attributes.getValue(attributes.getIndex("status"))); - if (attributes.getIndex("startTime") != -1) - startTime = parseDate(attributes, "startTime"); - if (attributes.getIndex("endTime") != -1) - endTime = parseDate(attributes, "endTime"); - if (attributes.getIndex("expiryTime") != -1) - expiryTime = parseDate(attributes, "expiryTime"); + taskLink = Utils.newReferenceType(attributes); + status = TaskStatus.fromValue(attributes.get("status")); + if (attributes.containsKey("startTime")) + startTime = parseDate(attributes.get("startTime")); + if (attributes.containsKey("endTime")) + endTime = parseDate(attributes.get("endTime")); + if (attributes.containsKey("expiryTime")) + expiryTime = parseDate(attributes.get("expiryTime")); // TODO technically the old Result object should only be owner for copy and delete tasks } else if (qName.equals("Owner") || qName.equals("Result")) { - owner = Utils.newNamedResource(attributes); - } else if (qName.equals("Link") && attributes.getIndex("rel") != -1 - && attributes.getValue(attributes.getIndex("rel")).equals("self")) { - taskLink = Utils.newNamedResource(attributes); + owner = Utils.newReferenceType(attributes); + } else if (qName.equals("Link") && "self".equals(attributes.get("rel"))) { + taskLink = Utils.newReferenceType(attributes); } else if (qName.equals("Error")) { error = Utils.newError(attributes); } } - private Date parseDate(Attributes attributes, String attribute) { - String toParse = attributes.getValue(attributes.getIndex(attribute)); + private Date parseDate(String toParse) { try { return dateService.iso8601DateParse(toParse); } catch (RuntimeException e) { diff --git a/vcloud/core/src/main/java/org/jclouds/vcloud/xml/TasksListHandler.java b/vcloud/core/src/main/java/org/jclouds/vcloud/xml/TasksListHandler.java index 5d1714bba0..3868fce28e 100644 --- a/vcloud/core/src/main/java/org/jclouds/vcloud/xml/TasksListHandler.java +++ b/vcloud/core/src/main/java/org/jclouds/vcloud/xml/TasksListHandler.java @@ -19,6 +19,9 @@ package org.jclouds.vcloud.xml; +import static org.jclouds.vcloud.util.Utils.cleanseAttributes; + +import java.util.Map; import java.util.SortedSet; import javax.inject.Inject; @@ -53,14 +56,14 @@ public class TasksListHandler extends ParseSax.HandlerWithResult { } @Override - public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { + public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException { + Map attributes = cleanseAttributes(attrs); if (qName.equals("TasksList")) { - resource = Utils.newNamedResource(attributes); - } else if (qName.equals("Link") && attributes.getIndex("rel") != -1 - && attributes.getValue(attributes.getIndex("rel")).equals("self")) { - resource = Utils.newNamedResource(attributes); + resource = Utils.newReferenceType(attributes); + } else if (qName.equals("Link") && "self".equals(attributes.get("rel"))) { + resource = Utils.newReferenceType(attributes); } else { - taskHandler.startElement(uri, localName, qName, attributes); + taskHandler.startElement(uri, localName, qName, attrs); } } diff --git a/vcloud/core/src/main/java/org/jclouds/vcloud/xml/VAppHandler.java b/vcloud/core/src/main/java/org/jclouds/vcloud/xml/VAppHandler.java index 5509c60985..a0ca0ac7a1 100644 --- a/vcloud/core/src/main/java/org/jclouds/vcloud/xml/VAppHandler.java +++ b/vcloud/core/src/main/java/org/jclouds/vcloud/xml/VAppHandler.java @@ -19,9 +19,11 @@ package org.jclouds.vcloud.xml; -import static org.jclouds.vcloud.util.Utils.newNamedResource; +import static org.jclouds.vcloud.util.Utils.cleanseAttributes; +import static org.jclouds.vcloud.util.Utils.newReferenceType; import java.util.List; +import java.util.Map; import java.util.Set; import javax.inject.Inject; @@ -33,7 +35,6 @@ import org.jclouds.vcloud.domain.Task; import org.jclouds.vcloud.domain.VApp; import org.jclouds.vcloud.domain.Vm; import org.jclouds.vcloud.domain.internal.VAppImpl; -import org.jclouds.vcloud.util.Utils; import org.xml.sax.Attributes; import org.xml.sax.SAXException; @@ -73,23 +74,23 @@ public class VAppHandler extends ParseSax.HandlerWithResult { } @Override - public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { + public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException { + Map attributes = cleanseAttributes(attrs); if (qName.equals("Children")) { inChildren = true; } else if (!inChildren && qName.equals("Tasks")) { inTasks = true; } if (inChildren) { - vmHandler.startElement(uri, localName, qName, attributes); + vmHandler.startElement(uri, localName, qName, attrs); } else if (inTasks) { - taskHandler.startElement(uri, localName, qName, attributes); + taskHandler.startElement(uri, localName, qName, attrs); } else if (qName.equals("VApp")) { - template = newNamedResource(attributes); - String status = Utils.attrOrNull(attributes, "status"); - if (status != null) - this.status = Status.fromValue(Integer.parseInt(status)); - } else if (qName.equals("Link") && "up".equals(Utils.attrOrNull(attributes, "rel"))) { - vdc = newNamedResource(attributes); + template = newReferenceType(attributes); + if (attributes.containsKey("status")) + this.status = Status.fromValue(Integer.parseInt(attributes.get("status"))); + } else if (qName.equals("Link") && "up".equals(attributes.get("rel"))) { + vdc = newReferenceType(attributes); } } diff --git a/vcloud/core/src/main/java/org/jclouds/vcloud/xml/VAppTemplateHandler.java b/vcloud/core/src/main/java/org/jclouds/vcloud/xml/VAppTemplateHandler.java index 9d6b5ad61e..acb84e1285 100644 --- a/vcloud/core/src/main/java/org/jclouds/vcloud/xml/VAppTemplateHandler.java +++ b/vcloud/core/src/main/java/org/jclouds/vcloud/xml/VAppTemplateHandler.java @@ -19,9 +19,11 @@ package org.jclouds.vcloud.xml; -import static org.jclouds.vcloud.util.Utils.newNamedResource; +import static org.jclouds.vcloud.util.Utils.cleanseAttributes; +import static org.jclouds.vcloud.util.Utils.newReferenceType; import java.util.List; +import java.util.Map; import java.util.Set; import javax.inject.Inject; @@ -33,7 +35,6 @@ import org.jclouds.vcloud.domain.Task; import org.jclouds.vcloud.domain.VAppTemplate; import org.jclouds.vcloud.domain.Vm; import org.jclouds.vcloud.domain.internal.VAppTemplateImpl; -import org.jclouds.vcloud.util.Utils; import org.xml.sax.Attributes; import org.xml.sax.SAXException; @@ -74,23 +75,23 @@ public class VAppTemplateHandler extends ParseSax.HandlerWithResult attributes = cleanseAttributes(attrs); if (qName.equals("Children")) { inChildren = true; } else if (!inChildren && qName.equals("Tasks")) { inTasks = true; } if (inChildren) { - vmHandler.startElement(uri, localName, qName, attributes); + vmHandler.startElement(uri, localName, qName, attrs); } else if (inTasks) { - taskHandler.startElement(uri, localName, qName, attributes); + taskHandler.startElement(uri, localName, qName, attrs); } else if (qName.equals("VAppTemplate")) { - template = newNamedResource(attributes); - String status = Utils.attrOrNull(attributes, "status"); - if (status != null) - this.status = Status.fromValue(Integer.parseInt(status)); - } else if (qName.equals("Link") && "up".equals(Utils.attrOrNull(attributes, "rel"))) { - vdc = newNamedResource(attributes); + template = newReferenceType(attributes); + if (attributes.containsKey("status")) + this.status = Status.fromValue(Integer.parseInt(attributes.get("status"))); + } else if (qName.equals("Link") && "up".equals(attributes.get("rel"))) { + vdc = newReferenceType(attributes); } } diff --git a/vcloud/core/src/main/java/org/jclouds/vcloud/xml/VCloudExpressNetworkHandler.java b/vcloud/core/src/main/java/org/jclouds/vcloud/xml/VCloudExpressNetworkHandler.java index db1f4ef11d..45d58c39ca 100644 --- a/vcloud/core/src/main/java/org/jclouds/vcloud/xml/VCloudExpressNetworkHandler.java +++ b/vcloud/core/src/main/java/org/jclouds/vcloud/xml/VCloudExpressNetworkHandler.java @@ -19,6 +19,10 @@ package org.jclouds.vcloud.xml; +import static org.jclouds.vcloud.util.Utils.cleanseAttributes; +import static org.jclouds.vcloud.util.Utils.newReferenceType; + +import java.util.Map; import java.util.Set; import javax.annotation.Resource; @@ -33,7 +37,6 @@ import org.jclouds.vcloud.domain.network.firewall.FirewallRule; import org.jclouds.vcloud.domain.network.internal.VCloudExpressNetworkImpl; import org.jclouds.vcloud.domain.network.nat.NatProtocol; import org.jclouds.vcloud.domain.network.nat.rules.PortForwardingRule; -import org.jclouds.vcloud.util.Utils; import org.xml.sax.Attributes; import org.xml.sax.SAXException; @@ -76,9 +79,10 @@ public class VCloudExpressNetworkHandler extends ParseSax.HandlerWithResult attributes = cleanseAttributes(attrs); if (qName.equals("Network")) { - network = Utils.newNamedResource(attributes); + network = newReferenceType(attributes); } } diff --git a/vcloud/core/src/main/java/org/jclouds/vcloud/xml/VCloudExpressVAppHandler.java b/vcloud/core/src/main/java/org/jclouds/vcloud/xml/VCloudExpressVAppHandler.java index b2ccfa831c..d30aca60bb 100644 --- a/vcloud/core/src/main/java/org/jclouds/vcloud/xml/VCloudExpressVAppHandler.java +++ b/vcloud/core/src/main/java/org/jclouds/vcloud/xml/VCloudExpressVAppHandler.java @@ -20,8 +20,11 @@ package org.jclouds.vcloud.xml; import static org.jclouds.Constants.PROPERTY_API_VERSION; +import static org.jclouds.vcloud.util.Utils.cleanseAttributes; +import static org.jclouds.vcloud.util.Utils.newReferenceType; import java.net.URI; +import java.util.Map; import java.util.Set; import javax.annotation.Resource; @@ -33,11 +36,10 @@ import org.jclouds.logging.Logger; import org.jclouds.vcloud.VCloudExpressMediaType; import org.jclouds.vcloud.domain.ReferenceType; import org.jclouds.vcloud.domain.ResourceAllocation; -import org.jclouds.vcloud.domain.VCloudExpressVApp; import org.jclouds.vcloud.domain.Status; +import org.jclouds.vcloud.domain.VCloudExpressVApp; import org.jclouds.vcloud.domain.VirtualSystem; import org.jclouds.vcloud.domain.internal.VCloudExpressVAppImpl; -import org.jclouds.vcloud.util.Utils; import org.xml.sax.Attributes; import org.xml.sax.SAXException; @@ -78,37 +80,36 @@ public class VCloudExpressVAppHandler extends ParseSax.HandlerWithResult attributes = cleanseAttributes(attrs); if (qName.equals("VApp")) { - ReferenceType resource = Utils.newNamedResource(attributes); + ReferenceType resource = newReferenceType(attributes); name = resource.getName(); location = resource.getHref(); - String statusString = attributes.getValue(attributes.getIndex("status")); + String statusString = attributes.get("status"); if (apiVersion.indexOf("0.8") != -1 && "2".equals(statusString)) status = Status.OFF; else status = Status.fromValue(statusString); - if (attributes.getIndex("size") != -1) - size = new Long(attributes.getValue(attributes.getIndex("size"))); + if (attributes.containsKey("size")) + size = new Long(attributes.get("size")); } else if (qName.equals("Link")) { // type should never be missing - if (attributes.getIndex("type") != -1 - && attributes.getValue(attributes.getIndex("type")).equals(VCloudExpressMediaType.VDC_XML)) { - vDC = Utils.newNamedResource(attributes); + if (attributes.containsKey("type") && attributes.get("type").equals(VCloudExpressMediaType.VDC_XML)) { + vDC = newReferenceType(attributes); } } else if (qName.equals("OperatingSystemSection")) { inOs = true; - for (int i = 0; i < attributes.getLength(); i++) - if (attributes.getQName(i).indexOf("id") != -1) - osType = Integer.parseInt(attributes.getValue(i)); + if (attributes.containsKey("id")) + osType = Integer.parseInt(attributes.get("id")); } else if (qName.endsWith("NetworkConnection")) { - networkName = attributes.getValue(attributes.getIndex("Network")); + networkName = attributes.get("Network"); } else { - systemHandler.startElement(uri, localName, qName, attributes); - allocationHandler.startElement(uri, localName, qName, attributes); + systemHandler.startElement(uri, localName, qName, attrs); + allocationHandler.startElement(uri, localName, qName, attrs); } } diff --git a/vcloud/core/src/main/java/org/jclouds/vcloud/xml/VCloudExpressVAppTemplateHandler.java b/vcloud/core/src/main/java/org/jclouds/vcloud/xml/VCloudExpressVAppTemplateHandler.java index aa425d7fe5..ab5b30614f 100644 --- a/vcloud/core/src/main/java/org/jclouds/vcloud/xml/VCloudExpressVAppTemplateHandler.java +++ b/vcloud/core/src/main/java/org/jclouds/vcloud/xml/VCloudExpressVAppTemplateHandler.java @@ -19,12 +19,16 @@ package org.jclouds.vcloud.xml; +import static org.jclouds.vcloud.util.Utils.cleanseAttributes; +import static org.jclouds.vcloud.util.Utils.newReferenceType; + +import java.util.Map; + import org.jclouds.http.functions.ParseSax; import org.jclouds.vcloud.domain.ReferenceType; import org.jclouds.vcloud.domain.Status; import org.jclouds.vcloud.domain.VCloudExpressVAppTemplate; import org.jclouds.vcloud.domain.internal.VCloudExpressVAppTemplateImpl; -import org.jclouds.vcloud.util.Utils; import org.xml.sax.Attributes; import org.xml.sax.SAXException; @@ -43,11 +47,12 @@ public class VCloudExpressVAppTemplateHandler extends ParseSax.HandlerWithResult } @Override - public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { + public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException { + Map attributes = cleanseAttributes(attrs); if (qName.equals("VAppTemplate")) { - catalog = Utils.newNamedResource(attributes); - if (attributes.getIndex("status") != -1) - status = Status.fromValue(attributes.getValue(attributes.getIndex("status"))); + catalog = newReferenceType(attributes); + if (attributes.containsKey("status")) + status = Status.fromValue(attributes.get("status")); } } diff --git a/vcloud/core/src/main/java/org/jclouds/vcloud/xml/VDCHandler.java b/vcloud/core/src/main/java/org/jclouds/vcloud/xml/VDCHandler.java index cf18473389..dfb5c83a39 100644 --- a/vcloud/core/src/main/java/org/jclouds/vcloud/xml/VDCHandler.java +++ b/vcloud/core/src/main/java/org/jclouds/vcloud/xml/VDCHandler.java @@ -19,8 +19,9 @@ package org.jclouds.vcloud.xml; -import static org.jclouds.vcloud.util.Utils.newNamedResource; -import static org.jclouds.vcloud.util.Utils.putNamedResource; +import static org.jclouds.vcloud.util.Utils.cleanseAttributes; +import static org.jclouds.vcloud.util.Utils.newReferenceType; +import static org.jclouds.vcloud.util.Utils.putReferenceType; import java.util.List; import java.util.Map; @@ -35,7 +36,6 @@ import org.jclouds.vcloud.domain.Task; import org.jclouds.vcloud.domain.VDC; import org.jclouds.vcloud.domain.VDCStatus; import org.jclouds.vcloud.domain.internal.VDCImpl; -import org.jclouds.vcloud.util.Utils; import org.xml.sax.Attributes; import org.xml.sax.SAXException; @@ -96,20 +96,21 @@ public class VDCHandler extends ParseSax.HandlerWithResult { } @Override - public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { + public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException { + Map attributes = cleanseAttributes(attrs); if (qName.equals("Vdc")) { - vDC = newNamedResource(attributes); - String status = Utils.attrOrNull(attributes, "status"); + vDC = newReferenceType(attributes); + String status = attributes.get("status"); if (status != null) this.status = VDCStatus.fromValue(Integer.parseInt(status)); } else if (qName.equals("Network")) { - putNamedResource(availableNetworks, attributes); + putReferenceType(availableNetworks, attributes); } else if (qName.equals("ResourceEntity")) { - putNamedResource(resourceEntities, attributes); - } else if (qName.equals("Link") && "up".equals(Utils.attrOrNull(attributes, "rel"))) { - org = newNamedResource(attributes); + putReferenceType(resourceEntities, attributes); + } else if (qName.equals("Link") && "up".equals(attributes.get("rel"))) { + org = newReferenceType(attributes); } else { - taskHandler.startElement(uri, localName, qName, attributes); + taskHandler.startElement(uri, localName, qName, attrs); } } diff --git a/vcloud/core/src/main/java/org/jclouds/vcloud/xml/VmHandler.java b/vcloud/core/src/main/java/org/jclouds/vcloud/xml/VmHandler.java index 4f67ee886e..08acfca0ef 100644 --- a/vcloud/core/src/main/java/org/jclouds/vcloud/xml/VmHandler.java +++ b/vcloud/core/src/main/java/org/jclouds/vcloud/xml/VmHandler.java @@ -19,9 +19,11 @@ package org.jclouds.vcloud.xml; -import static org.jclouds.vcloud.util.Utils.newNamedResource; +import static org.jclouds.vcloud.util.Utils.cleanseAttributes; +import static org.jclouds.vcloud.util.Utils.newReferenceType; import java.util.List; +import java.util.Map; import javax.inject.Inject; @@ -31,7 +33,6 @@ import org.jclouds.vcloud.domain.Status; import org.jclouds.vcloud.domain.Task; import org.jclouds.vcloud.domain.Vm; import org.jclouds.vcloud.domain.internal.VmImpl; -import org.jclouds.vcloud.util.Utils; import org.xml.sax.Attributes; import org.xml.sax.SAXException; @@ -65,19 +66,20 @@ public class VmHandler extends ParseSax.HandlerWithResult { } @Override - public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { + public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException { + Map attributes = cleanseAttributes(attrs); if (qName.equals("Tasks")) { inTasks = true; } if (inTasks) { - taskHandler.startElement(uri, localName, qName, attributes); + taskHandler.startElement(uri, localName, qName, attrs); } else if (qName.equals("Vm")) { - vm = newNamedResource(attributes); - String status = Utils.attrOrNull(attributes, "status"); + vm = newReferenceType(attributes); + String status = attributes.get("status"); if (status != null) this.status = Status.fromValue(Integer.parseInt(status)); - } else if (qName.equals("Link") && "up".equals(Utils.attrOrNull(attributes, "rel"))) { - vdc = newNamedResource(attributes); + } else if (qName.equals("Link") && "up".equals(attributes.get("rel"))) { + vdc = newReferenceType(attributes); } } diff --git a/vcloud/terremark/src/main/java/org/jclouds/vcloud/terremark/xml/TerremarkCatalogItemHandler.java b/vcloud/terremark/src/main/java/org/jclouds/vcloud/terremark/xml/TerremarkCatalogItemHandler.java index b32ddf6722..48c278828b 100644 --- a/vcloud/terremark/src/main/java/org/jclouds/vcloud/terremark/xml/TerremarkCatalogItemHandler.java +++ b/vcloud/terremark/src/main/java/org/jclouds/vcloud/terremark/xml/TerremarkCatalogItemHandler.java @@ -19,7 +19,10 @@ package org.jclouds.vcloud.terremark.xml; -import static org.jclouds.vcloud.util.Utils.newNamedResource; +import static org.jclouds.vcloud.util.Utils.cleanseAttributes; +import static org.jclouds.vcloud.util.Utils.newReferenceType; + +import java.util.Map; import org.jclouds.vcloud.domain.ReferenceType; import org.jclouds.vcloud.terremark.domain.TerremarkCatalogItem; @@ -37,20 +40,20 @@ public class TerremarkCatalogItemHandler extends CatalogItemHandler { private ReferenceType computeOptions; public TerremarkCatalogItem getResult() { - return new TerremarkCatalogItemImpl(catalogItem.getName(), catalogItem.getHref(), description, - computeOptions, customizationOptions, entity, properties); + return new TerremarkCatalogItemImpl(catalogItem.getName(), catalogItem.getHref(), description, computeOptions, + customizationOptions, entity, properties); } @Override - public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { - super.startElement(uri, localName, qName, attributes); + public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException { + Map attributes = cleanseAttributes(attrs); + super.startElement(uri, localName, qName, attrs); if (qName.equals("Link")) { - int nameIndex = attributes.getIndex("name"); - if (nameIndex != -1) { - if (attributes.getValue(nameIndex).equals("Customization Options")) { - customizationOptions = newNamedResource(attributes); - } else if (attributes.getValue(nameIndex).equals("Compute Options")) { - computeOptions = newNamedResource(attributes); + if (attributes.containsKey("name")) { + if (attributes.get("name").equals("Customization Options")) { + customizationOptions = newReferenceType(attributes); + } else if (attributes.get("name").equals("Compute Options")) { + computeOptions = newReferenceType(attributes); } } diff --git a/vcloud/terremark/src/main/java/org/jclouds/vcloud/terremark/xml/TerremarkOrgHandler.java b/vcloud/terremark/src/main/java/org/jclouds/vcloud/terremark/xml/TerremarkOrgHandler.java index 392c0438d1..4c95f23c93 100644 --- a/vcloud/terremark/src/main/java/org/jclouds/vcloud/terremark/xml/TerremarkOrgHandler.java +++ b/vcloud/terremark/src/main/java/org/jclouds/vcloud/terremark/xml/TerremarkOrgHandler.java @@ -20,7 +20,10 @@ package org.jclouds.vcloud.terremark.xml; import static org.jclouds.vcloud.terremark.TerremarkVCloudExpressMediaType.KEYSLIST_XML; -import static org.jclouds.vcloud.util.Utils.newNamedResource; +import static org.jclouds.vcloud.util.Utils.cleanseAttributes; +import static org.jclouds.vcloud.util.Utils.newReferenceType; + +import java.util.Map; import javax.inject.Inject; @@ -49,13 +52,13 @@ public class TerremarkOrgHandler extends OrgHandler { } @Override - public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { - super.startElement(uri, localName, qName, attributes); + public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException { + Map attributes = cleanseAttributes(attrs); + super.startElement(uri, localName, qName, attrs); if (qName.equals("Link")) { - int typeIndex = attributes.getIndex("type"); - if (typeIndex != -1) { - if (attributes.getValue(typeIndex).equals(KEYSLIST_XML)) { - keysList = newNamedResource(attributes); + if (attributes.containsKey("type")) { + if (attributes.get("type").equals(KEYSLIST_XML)) { + keysList = newReferenceType(attributes); } } } diff --git a/vcloud/terremark/src/main/java/org/jclouds/vcloud/terremark/xml/TerremarkVDCHandler.java b/vcloud/terremark/src/main/java/org/jclouds/vcloud/terremark/xml/TerremarkVDCHandler.java index 06b7626605..85f05ce5a4 100644 --- a/vcloud/terremark/src/main/java/org/jclouds/vcloud/terremark/xml/TerremarkVDCHandler.java +++ b/vcloud/terremark/src/main/java/org/jclouds/vcloud/terremark/xml/TerremarkVDCHandler.java @@ -19,6 +19,10 @@ package org.jclouds.vcloud.terremark.xml; +import static org.jclouds.vcloud.util.Utils.cleanseAttributes; + +import java.util.Map; + import javax.inject.Inject; import org.jclouds.vcloud.VCloudExpressMediaType; @@ -54,18 +58,19 @@ public class TerremarkVDCHandler extends VDCHandler { } @Override - public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { - super.startElement(uri, localName, qName, attributes); + public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException { + Map attributes = cleanseAttributes(attrs); + super.startElement(uri, localName, qName, attrs); if (qName.equals("Link")) { - String name = attributes.getValue(attributes.getIndex("name")); + String name = attributes.get("name"); if (name.equals("Internet Services")) { - internetServices = Utils.newNamedResource(attributes); + internetServices = Utils.newReferenceType(attributes); } else if (name.equals("Public IPs")) { - publicIps = Utils.newNamedResource(attributes); + publicIps = Utils.newReferenceType(attributes); } else { - String type = attributes.getValue(attributes.getIndex("type")); + String type = attributes.get("type"); if (type.equals(VCloudExpressMediaType.CATALOG_XML)) { - catalog = Utils.newNamedResource(attributes); + catalog = Utils.newReferenceType(attributes); } } }