View Javadoc
1   /*
2    * Copyright (C) 2003-2008 eXo Platform SAS.
3    *
4    * This program is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Affero General Public License
6    * as published by the Free Software Foundation; either version 3
7    * of the License, or (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program; if not, see<http://www.gnu.org/licenses/>.
16   */
17  package org.exoplatform.ecm.connector.fckeditor;
18  
19  import java.text.MessageFormat;
20  import java.util.Locale;
21  import java.util.ResourceBundle;
22  
23  import javax.xml.parsers.DocumentBuilder;
24  import javax.xml.parsers.DocumentBuilderFactory;
25  
26  import org.w3c.dom.Document;
27  import org.w3c.dom.Element;
28  
29  /*
30   * Created by The eXo Platform SAS @author : Hoa.Pham hoa.pham@exoplatform.com
31   * Jun 23, 2008
32   */
33  /**
34   * The Class FCKMessage.
35   */
36  public class FCKMessage {
37  
38    /** The Constant ERROR. */
39    public static final String ERROR                      = "Error";
40  
41    /** The Constant INFO. */
42    public static final String INFO                       = "Info";
43  
44    /** The Constant FOLDER_CREATED. */
45    public static final int    FOLDER_CREATED             = 100;
46  
47    /** The Constant FOLDER_EXISTED. */
48    public static final int    FOLDER_EXISTED             = 101;
49  
50    /** The Constant FOLDER_INVALID_NAME. */
51    public static final int    FOLDER_INVALID_NAME        = 102;
52  
53    /** The Constant FOLDER_PERMISSION_CREATING. */
54    public static final int    FOLDER_PERMISSION_CREATING = 103;
55  
56    /** The Constant FOLDER_NOT_CREATED. */
57    public static final int    FOLDER_NOT_CREATED         = 104;
58  
59    /** The Constant UNKNOWN_ERROR. */
60    public static final int    UNKNOWN_ERROR              = 110;
61  
62    /** The Constant FILE_EXISTED. */
63    public static final int    FILE_EXISTED               = 201;
64  
65    /** The Constant FILE_NOT_FOUND. */
66    public static final int    FILE_NOT_FOUND             = 202;
67  
68    /** The Constant FILE_UPLOAD_RESTRICTION. */
69    public static final int    FILE_UPLOAD_RESTRICTION    = 203;
70  
71    /** The Constant FILE_NOT_UPLOADED. */
72    public static final int FILE_NOT_UPLOADED = 204;
73  
74    /** The Constant FILE_EXCEED_LIMIT. */
75    public static final int FILE_EXCEED_LIMIT = 205;
76  
77    /** The Constant FCK_RESOURCE_BUNDLE. */
78    public static final String FCK_RESOURCE_BUNDLE_FILE   = "locale.services.fckeditor.FCKConnector"
79                                                              ;
80  
81    /**
82     * Instantiates a new fCK message.
83     */
84    public FCKMessage() {
85    }
86  
87    public Document createMessage(int messageCode, String messageType, String language, Object[] args)
88        throws Exception {
89      String message = getMessage(messageCode, args, language);
90      return createMessage(messageCode, message, messageType);
91    }
92  
93    public Document createMessage(int messageCode, String message, String messageType)
94        throws Exception {
95      DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
96      Document document = documentBuilder.newDocument();
97      Element element = document.createElement("Message");
98      element.setAttribute("number", Integer.toString(messageCode));
99      element.setAttribute("text", message);
100     element.setAttribute("type", messageType);
101     document.appendChild(element);
102     return document;
103   }
104 
105   /**
106    * Gets the message.
107    *
108    * @param messageNum the message num
109    * @param args the args
110    * @param language the language
111    * @return the message
112    * @throws Exception the exception
113    */
114   public String getMessage(int messageNum, Object[] args, String language) throws Exception {
115     String messageKey = getMessageKey(messageNum);
116     return getMessage(messageKey, args, language);
117   }
118 
119   /**
120    * Gets the message.
121    *
122    * @param messageKey the message key
123    * @param args the args
124    * @param language the language
125    * @return the message
126    * @throws Exception the exception
127    */
128   public String getMessage(String messageKey, Object[] args, String language) throws Exception {
129     Locale locale = null;
130     if (language == null) {
131       locale = Locale.ENGLISH;
132     } else {
133       locale = new Locale(language);
134     }
135     ResourceBundle resourceBundle = ResourceBundle.getBundle(FCK_RESOURCE_BUNDLE_FILE, locale);
136     String message = resourceBundle.getString(messageKey);
137     if (args == null) {
138       return message;
139     }
140     return MessageFormat.format(message, args);
141   }
142 
143   protected String getMessageKey(int number) {
144     String messageKey = null;
145     switch (number) {
146     case FOLDER_CREATED:
147       messageKey = "fckeditor.folder-created";
148       break;
149     case FOLDER_NOT_CREATED:
150       messageKey = "fckeditor.folder-not-created";
151       break;
152     case FOLDER_INVALID_NAME:
153       messageKey = "fckeditor.folder-invalid-name";
154       break;
155     case FOLDER_EXISTED:
156       messageKey = "fckeditor.folder-existed";
157       break;
158     case FOLDER_PERMISSION_CREATING:
159       messageKey = "fckeditor.folder-permission-denied";
160       break;
161     case FILE_EXISTED:
162       messageKey = "fckeditor.file-existed";
163       break;
164     case FILE_NOT_FOUND:
165       messageKey = "fckeditor.file-not-found";
166       break;
167     case FILE_NOT_UPLOADED:
168       messageKey = "fckeditor.file-not-uploaded";
169       break;
170     case FILE_UPLOAD_RESTRICTION:
171       messageKey = "fckeditor.file-uploaded-restriction";
172       break;
173     case FILE_EXCEED_LIMIT:
174       messageKey = "fckeditor.file-exceed-limit";
175       break;
176     default:
177       messageKey = "connector.fckeditor.unknowm-message";
178       break;
179     }
180     return messageKey;
181   }
182 }