EMMA Coverage Report (generated Mon Oct 17 05:41:20 EDT 2011)
[all classes][org.jclouds.vcloud.xml]

COVERAGE SUMMARY FOR SOURCE FILE [TaskHandler.java]

nameclass, %method, %block, %line, %
TaskHandler.java100% (1/1)100% (6/6)96%  (202/211)96%  (43/45)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class TaskHandler100% (1/1)100% (6/6)96%  (202/211)96%  (43/45)
parseDate (String): Date 100% (1/1)76%  (28/37)75%  (6/8)
TaskHandler (DateService): void 100% (1/1)100% (9/9)100% (4/4)
characters (char [], int, int): void 100% (1/1)100% (1/1)100% (1/1)
endElement (String, String, String): void 100% (1/1)100% (56/56)100% (12/12)
getResult (): Task 100% (1/1)100% (3/3)100% (1/1)
startElement (String, String, String, Attributes): void 100% (1/1)100% (105/105)100% (19/19)

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 */
19package org.jclouds.vcloud.xml;
20 
21import java.text.ParseException;
22import java.util.Date;
23import java.util.Map;
24 
25import javax.annotation.Resource;
26import javax.inject.Inject;
27 
28import org.jclouds.date.DateService;
29import org.jclouds.http.functions.ParseSax;
30import org.jclouds.logging.Logger;
31import org.jclouds.util.SaxUtils;
32import org.jclouds.vcloud.domain.ReferenceType;
33import org.jclouds.vcloud.domain.Task;
34import org.jclouds.vcloud.domain.TaskStatus;
35import org.jclouds.vcloud.domain.VCloudError;
36import org.jclouds.vcloud.domain.internal.TaskImpl;
37import org.jclouds.vcloud.util.Utils;
38import org.xml.sax.Attributes;
39import org.xml.sax.SAXException;
40 
41/**
42 * @author Adrian Cole
43 */
44public class TaskHandler extends ParseSax.HandlerWithResult<Task> {
45   protected final DateService dateService;
46   private String operation;
47   private ReferenceType taskLink;
48   private ReferenceType owner;
49   private TaskStatus status;
50   private Date startTime;
51   private Date endTime;
52   private Date expiryTime;
53   private Task task;
54   private VCloudError error;
55   private boolean inOwner;
56 
57   @Resource
58   protected Logger logger = Logger.NULL;
59 
60   @Inject
61   public TaskHandler(DateService dateService) {
62      this.dateService = dateService;
63   }
64 
65   public Task getResult() {
66      return task;
67   }
68 
69   @Override
70   public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException {
71      Map<String, String> attributes = SaxUtils.cleanseAttributes(attrs);
72      if (qName.equalsIgnoreCase("Task")) {
73         if (attributes.get("href") != null && !inOwner)// queued tasks may not have an
74            // href yet
75            taskLink = Utils.newReferenceType(attributes);
76         status = TaskStatus.fromValue(attributes.get("status"));
77         operation = attributes.get("operation");
78         if (attributes.containsKey("startTime"))
79            startTime = parseDate(attributes.get("startTime"));
80         if (attributes.containsKey("endTime"))
81            endTime = parseDate(attributes.get("endTime"));
82         if (attributes.containsKey("expiryTime"))
83            expiryTime = parseDate(attributes.get("expiryTime"));
84         // TODO technically the old Result object should only be owner for copy and delete tasks
85      } else if (qName.equals("Owner") || qName.equals("Result")) {
86         owner = Utils.newReferenceType(attributes);
87      } else if (qName.equals("Link") && "self".equals(attributes.get("rel"))) {
88         taskLink = Utils.newReferenceType(attributes);
89      } else if (qName.equals("Error")) {
90         error = Utils.newError(attributes);
91      }
92   }
93 
94   private Date parseDate(String toParse) {
95      try {
96         return dateService.iso8601DateParse(toParse);
97      } catch (RuntimeException e) {
98         if (e.getCause() instanceof ParseException) {
99            if (!toParse.endsWith("Z"))
100               toParse += "Z";
101            return dateService.iso8601SecondsDateParse(toParse);
102         } else {
103            logger.error(e, "error parsing date");
104         }
105      }
106      return null;
107   }
108 
109   @Override
110   public void endElement(String uri, String localName, String qName) {
111      if (qName.equalsIgnoreCase("Task")) {
112         this.task = new TaskImpl(taskLink.getHref(), operation, status, startTime, endTime, expiryTime, owner, error);
113         operation = null;
114         taskLink = null;
115         status = null;
116         startTime = null;
117         endTime = null;
118         owner = null;
119         error = null;
120      } else if (qName.equalsIgnoreCase("Owner")) {
121         inOwner = false;
122      }
123   }
124 
125   public void characters(char ch[], int start, int length) {
126   }
127 
128}

[all classes][org.jclouds.vcloud.xml]
EMMA 2.0.5312 (C) Vladimir Roubtsov