View Javadoc
1   /*
2    * Copyright (C) 2013 4th Line GmbH, Switzerland
3    *
4    * The contents of this file are subject to the terms of either the GNU
5    * Lesser General Public License Version 2 or later ("LGPL") or the
6    * Common Development and Distribution License Version 1 or later
7    * ("CDDL") (collectively, the "License"). You may not use this file
8    * except in compliance with the License. See LICENSE.txt for more
9    * information.
10   *
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14   */
15  package org.fourthline.cling.support.model.dlna.message.header;
16  
17  import org.fourthline.cling.model.message.header.InvalidHeaderException;
18  import org.fourthline.cling.model.types.BytesRange;
19  import org.fourthline.cling.model.types.InvalidValueException;
20  import org.fourthline.cling.support.model.dlna.types.AvailableSeekRangeType;
21  import org.fourthline.cling.support.model.dlna.types.NormalPlayTimeRange;
22  
23  /**
24   * @author Mario Franco
25   */
26  public class AvailableSeekRangeHeader extends DLNAHeader<AvailableSeekRangeType> {
27  
28      public AvailableSeekRangeHeader() {
29      }
30  
31      public AvailableSeekRangeHeader(AvailableSeekRangeType timeSeekRange) {
32          setValue(timeSeekRange);
33      }
34  
35      @Override
36      public void setString(String s) throws InvalidHeaderException {
37          if (s.length() != 0) {
38              String[] params = s.split(" ");
39              if (params.length > 1) {
40                  try {
41                      AvailableSeekRangeType.Mode mode = null;
42                      NormalPlayTimeRange timeRange = null;
43                      BytesRange byteRange = null;
44  
45                      //Parse Mode
46                      try {
47                          mode = AvailableSeekRangeType.Mode.valueOf("MODE_" + params[0]);
48                      } catch (IllegalArgumentException e) {
49                          throw new InvalidValueException("Invalid AvailableSeekRange Mode");
50                      }
51  
52                      boolean useTime = true;
53                      //Parse Second Token
54                      try {
55                          timeRange = NormalPlayTimeRange.valueOf(params[1],true);
56                      } catch (InvalidValueException timeInvalidValueException) {
57                          try {
58                              byteRange = BytesRange.valueOf(params[1]);
59                              useTime = false;
60                          } catch (InvalidValueException bytesInvalidValueException) {
61                              throw new InvalidValueException("Invalid AvailableSeekRange Range");
62                          }
63                      }
64                      if (useTime) {
65                          if (params.length > 2) {
66                              //Parse Third Token
67                              byteRange = BytesRange.valueOf(params[2]);
68                              setValue(new AvailableSeekRangeType(mode, timeRange, byteRange));
69                          }
70                          else {
71                              setValue(new AvailableSeekRangeType(mode, timeRange));
72                          }
73                      } else {
74                          setValue(new AvailableSeekRangeType(mode, byteRange));
75                      }
76                      return;
77                  } catch (InvalidValueException invalidValueException) {
78                      throw new InvalidHeaderException("Invalid AvailableSeekRange header value: " + s + "; " + invalidValueException.getMessage());
79                  }
80              }
81          }
82          throw new InvalidHeaderException("Invalid AvailableSeekRange header value: " + s);
83      }
84  
85      @Override
86      public String getString() {
87          AvailableSeekRangeType t = getValue();
88          String s = Integer.toString(t.getModeFlag().ordinal());
89          if (t.getNormalPlayTimeRange() != null) {
90              s += " " + t.getNormalPlayTimeRange().getString(false);
91          }
92          if (t.getBytesRange() != null) {
93              s += " " + t.getBytesRange().getString(false);
94          }
95          return s;
96      }
97  }