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.model.types;
16  
17  /**
18   *
19   * @author Mario Franco
20   */
21  public class PragmaType {
22  
23      private String token;
24      private boolean quote;
25      private String value;
26  
27      public PragmaType(String token, String value, boolean quote) {
28          this.token = token;
29          this.value = value;
30          this.quote = quote;
31      }
32      
33      public PragmaType(String token, String value) {
34          this.token = token;
35          this.value = value;
36      }
37  
38      public PragmaType(String value) {
39          this.token = null;
40          this.value = value;
41      }
42  
43      
44      /**
45       * @return the token
46       */
47      public String getToken() {
48          return token;
49      }
50  
51      /**
52       * @return the value
53       */
54      public String getValue() {
55          return value;
56      }
57      
58      /**
59       * 
60       * @return String format of Bytes Range for response message header 
61       */
62      public String getString() {
63          String s ="";
64          if (token!=null)
65              s += token + "=";
66  
67          s += quote? "\""+value+"\"" : value;
68          return s;
69      }
70  
71      public static PragmaType valueOf(String s) throws InvalidValueException {
72          if (s.length() != 0) {
73              String token=null, value = null;
74              boolean quote = false;
75              String[] params = s.split("=");
76              if (params.length > 1) {
77                  token = params[0];
78                  value = params[1];
79                  if (value.startsWith("\"") && value.endsWith("\"")) {
80                      quote = true;
81                      value = value.substring(1, value.length()-1);
82                  }
83              }
84              else {
85                  value = s;
86              }
87              return new PragmaType(token, value, quote);
88          }
89          throw new InvalidValueException("Can't parse Bytes Range: " + s);
90      }
91  
92  }