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  
16  package org.fourthline.cling.support.messagebox.model;
17  
18  import org.fourthline.cling.support.messagebox.parser.MessageDOM;
19  import org.fourthline.cling.support.messagebox.parser.MessageDOMParser;
20  import org.fourthline.cling.support.messagebox.parser.MessageElement;
21  import org.seamless.xml.ParserException;
22  
23  import java.util.Random;
24  
25  /**
26   * https://sourceforge.net/apps/mediawiki/samygo/index.php?title=MessageBoxService_request_format
27   * 
28   * @author Christian Bauer
29   */
30  public abstract class Message implements ElementAppender {
31  
32      final protected Random randomGenerator = new Random();
33  
34      public enum Category {
35          SMS("SMS"),
36          INCOMING_CALL("Incoming Call"),
37          SCHEDULE_REMINDER("Schedule Reminder");
38  
39          public String text;
40  
41          Category(String text) {
42              this.text = text;
43          }
44      }
45  
46      public enum DisplayType {
47  
48          MINIMUM("Minimum"),
49          MAXIMUM("Maximum");
50  
51          public String text;
52  
53          DisplayType(String text) {
54              this.text = text;
55          }
56      }
57  
58      private final int id;
59      private final Category category;
60      private DisplayType displayType;
61  
62      public Message(Category category, DisplayType displayType) {
63          this(0, category, displayType);
64      }
65  
66      public Message(int id, Category category, DisplayType displayType) {
67          if (id == 0) id = randomGenerator.nextInt(Integer.MAX_VALUE);
68          this.id = id;
69          this.category = category;
70          this.displayType = displayType;
71      }
72  
73      public int getId() {
74          return id;
75      }
76  
77      public Category getCategory() {
78          return category;
79      }
80  
81      public DisplayType getDisplayType() {
82          return displayType;
83      }
84  
85      @Override
86      public boolean equals(Object o) {
87          if (this == o) return true;
88          if (o == null || getClass() != o.getClass()) return false;
89  
90          Message message = (Message) o;
91  
92          if (id != message.id) return false;
93  
94          return true;
95      }
96  
97      @Override
98      public int hashCode() {
99          return id;
100     }
101 
102     @Override
103     public String toString() {
104         try {
105             MessageDOMParser mp = new MessageDOMParser();
106             MessageDOM dom = mp.createDocument();
107 
108             MessageElement root = dom.createRoot(mp.createXPath(), "Message");
109             root.createChild("Category").setContent(getCategory().text);
110             root.createChild("DisplayType").setContent(getDisplayType().text);
111             appendMessageElements(root);
112 
113             String s = mp.print(dom, 0, false);
114 
115             // Cut the root element, what we send to the TV is not really XML, just
116             // random element soup which I'm sure the Samsung guys think is XML...
117             return s.replaceAll("<Message xmlns=\"urn:samsung-com:messagebox-1-0\">", "")
118                     .replaceAll("</Message>", "");
119 
120             
121         } catch (ParserException ex) {
122             throw new RuntimeException(ex);
123         }
124     }
125 }