Home | History | Annotate | Download | only in bugreport
      1 /*
      2  * Copyright (C) 2016 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.bugreport.bugreport;
     18 
     19 import com.android.bugreport.util.Line;
     20 import com.android.bugreport.util.Lines;
     21 import com.android.bugreport.util.Utils;
     22 
     23 import java.util.Calendar;
     24 import java.util.GregorianCalendar;
     25 import java.util.regex.Pattern;
     26 import java.util.regex.Matcher;
     27 
     28 /**
     29  * Parse the header and footer of the bugreport.
     30  */
     31 public class MetadataParser {
     32 
     33     private static final Pattern DUMPSTATE_LINE_RE = Pattern.compile(
     34             "== dumpstate: " + Utils.DATE_TIME_PATTERN);
     35     private static final Pattern HEADER_LINE_RE = Pattern.compile(
     36             "([^:]+): (.*)");
     37 
     38     private final Matcher mDumpstateLineRe = DUMPSTATE_LINE_RE.matcher("");
     39     private final Matcher mHeaderLineRe = HEADER_LINE_RE.matcher("");
     40 
     41     private Bugreport mBugreport;
     42 
     43     /**
     44      * Constructor
     45      */
     46     public MetadataParser() {
     47     }
     48 
     49     /**
     50      * Set the Bugreport that we're working on.
     51      */
     52     public void setBugreport(Bugreport bugreport) {
     53         mBugreport = bugreport;
     54     }
     55 
     56     /**
     57      * Parse the preamble.
     58      */
     59     public void parseHeader(Lines<? extends Line> lines) {
     60         Matcher m;
     61         int lineno = 0;
     62 
     63         while (lines.hasNext()) {
     64             final Line line = lines.next();
     65             final String text = line.text;
     66 
     67             if ((m = Utils.match(mDumpstateLineRe, text)) != null) {
     68                 mBugreport.startTime = Utils.parseCalendar(m, 1, false);
     69             } else if ((m = Utils.match(mHeaderLineRe, text)) != null) {
     70                 final String key = m.group(1);
     71                 final String value = m.group(2);
     72                 if ("Build".equals(key)) {
     73                     mBugreport.buildId = value;
     74                 }
     75             }
     76         }
     77 
     78     }
     79 
     80     /**
     81      * Parse the footer.
     82      */
     83     public void parseFooter(Lines<? extends Line> lines, int durationMs) {
     84         if (mBugreport.startTime != null) {
     85             mBugreport.endTime = (GregorianCalendar)mBugreport.startTime.clone();
     86             mBugreport.endTime.add(Calendar.MILLISECOND, durationMs);
     87         }
     88     }
     89 }
     90 
     91