Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (C) 2014 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License
     15  */
     16 
     17 package com.android.compatibility.common.util;
     18 
     19 import org.xmlpull.v1.XmlSerializer;
     20 
     21 import java.io.IOException;
     22 
     23 //TODO(stuartscott): Delete file for v2, ReportLog can serialize itself.
     24 /**
     25  * Serialize Metric data from {@link ReportLog} into compatibility report friendly XML
     26  */
     27 public final class MetricsXmlSerializer {
     28 
     29     private final XmlSerializer mXmlSerializer;
     30 
     31     public MetricsXmlSerializer(XmlSerializer xmlSerializer) {
     32         this.mXmlSerializer = xmlSerializer;
     33     }
     34 
     35     public void serialize(ReportLog reportLog) throws IOException {
     36         if (reportLog == null) {
     37             return;
     38         }
     39         ReportLog.Metric summary = reportLog.getSummary();
     40         // <Summary message="Average" scoreType="lower_better" unit="ms">195.2</Summary>
     41         if (summary != null) {
     42             mXmlSerializer.startTag(null, "Summary");
     43             mXmlSerializer.attribute(null, "message", summary.getMessage());
     44             mXmlSerializer.attribute(null, "scoreType", summary.getType().toReportString());
     45             mXmlSerializer.attribute(null, "unit", summary.getUnit().toReportString());
     46             mXmlSerializer.text(Double.toString(summary.getValues()[0]));
     47             mXmlSerializer.endTag(null, "Summary");
     48         }
     49     }
     50 }