View Javadoc
1   /*
2    * Copyright (C) 2003-2014 eXo Platform SAS.
3    *
4    * This is free software; you can redistribute it and/or modify it
5    * under the terms of the GNU Lesser General Public License as
6    * published by the Free Software Foundation; either version 3 of
7    * the License, or (at your option) any later version.
8    *
9    * This software 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 GNU
12   * Lesser General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this software; if not, write to the Free
16   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
17   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
18   */
19  package org.exoplatform.model;
20  
21  import java.util.List;
22  import java.util.Locale;
23  
24  import android.os.Parcel;
25  import android.os.Parcelable;
26  
27  public class ExoFile implements Parcelable {
28  
29    /**
30     * File's JCR URL
31     */
32    public String        path;
33  
34    /**
35     * Whether this file is a folder
36     */
37    public boolean       isFolder;
38  
39    /**
40     * Whether this file can be deleted, renamed
41     */
42    public boolean       canRemove;
43  
44    /**
45     * Name of the file/folder
46     */
47    public String        name;
48  
49    /**
50     * The path of file
51     */
52    public String        currentFolder;
53  
54    /**
55     * The drive name in which this file is
56     */
57    public String        driveName;
58  
59    /**
60     * The workspace in which this file is
61     */
62    public String        workspaceName;
63  
64    /**
65     * File content type
66     */
67    public String        nodeType;
68  
69    /**
70     * Natural name used instead of the technical name when the file is a group
71     * folder<br/>
72     * e.g. .space.mobile => Mobile
73     */
74    private String       naturalName;
75  
76    /**
77     * List of sub-files and sub-folders
78     */
79    public List<ExoFile> children;
80  
81    // Default constructors
82    public ExoFile() {
83      path = "";
84      isFolder = true;
85      canRemove = false;
86      name = "";
87      currentFolder = "";
88      driveName = "";
89      workspaceName = "";
90      nodeType = "";
91      naturalName = "";
92      children = null;
93    }
94  
95    public ExoFile(String driverName) {
96      path = "";
97      isFolder = true;
98      canRemove = false;
99      name = "";
100     currentFolder = "";
101     this.driveName = driverName;
102     workspaceName = "";
103     nodeType = "";
104     naturalName = "";
105     children = null;
106   }
107 
108   private ExoFile(Parcel in) {
109     readFromParcel(in);
110   }
111 
112   /**
113    * The <i>name</i> of the file / folder.<br/>
114    * If the <i>naturalName</i> exists, <i>naturalName</i> is returned instead.
115    * 
116    * @return a name for the file / folder
117    */
118   public String getName() {
119     if (naturalName == null || "".equals(naturalName))
120       return name;
121     else
122       return naturalName;
123   }
124 
125   /**
126    * Create the natural name of the folder, based on the <i>name</i> property,
127    * as follows
128    * <ul>
129    * <li>1st dot is removed</li>
130    * <li>other '.' and '_' are replaced by spaces</li>
131    * <li>all remaining words must start by a capital letter</li>
132    * <li>exo is replaced by eXo</li>
133    * <li>ending 's' in 'spaces' is removed</li>
134    * </ul>
135    * The <i>naturalName</i> property is created.
136    */
137   public void createNaturalName() {
138     StringBuilder sb = new StringBuilder();
139     // replace all _ and - by . to be treated by the split
140     String tmpName = name.replace('_', '.');
141     tmpName = tmpName.replace('-', '.');
142     // initial 'spaces' is removed
143     if (tmpName.startsWith(".spaces."))
144       tmpName = tmpName.substring(".spaces.".length());
145     // split by . will remove them from the resulting string
146     String[] words = tmpName.split("\\.");
147     for (String word : words) {
148       if ("exo".equals(word)) { // exo is replaced by eXo
149         sb.append("eXo ");
150         continue;
151       }
152       // all remaining words must start by a capital letter
153       sb.append(toCapitalCase(word)).append(" ");
154     }
155     naturalName = sb.toString().trim();
156   }
157 
158   /**
159    * Returns a string with the initial letter in upper case and the rest in
160    * lower case
161    * 
162    * @param word the word to capitalize
163    * @return the capitalized word
164    */
165   private String toCapitalCase(String word) {
166     if (word == null || word.length() == 0)
167       return "";
168     String initial = String.valueOf(word.charAt(0)).toUpperCase(Locale.US);
169     String remainder = word.substring(1).toLowerCase(Locale.US);
170     return initial + remainder;
171   }
172 
173   private void readFromParcel(Parcel in) {
174     path = in.readString();
175     isFolder = (Boolean) in.readValue(null);
176     canRemove = (Boolean) in.readValue(null);
177     name = in.readString();
178     currentFolder = in.readString();
179     driveName = in.readString();
180     workspaceName = in.readString();
181     nodeType = in.readString();
182     naturalName = in.readString();
183     in.readList(children, this.getClass().getClassLoader());
184   }
185 
186   public static final Parcelable.Creator<ExoFile> CREATOR = new Parcelable.Creator<ExoFile>() {
187     public ExoFile createFromParcel(Parcel in) {
188       return new ExoFile(in);
189     }
190 
191     public ExoFile[] newArray(int size) {
192       return new ExoFile[size];
193     }
194   };
195 
196   /*
197    * (non-Javadoc)
198    * @see android.os.Parcelable#describeContents()
199    */
200   @Override
201   public int describeContents() {
202     return 0;
203   }
204 
205   /*
206    * (non-Javadoc)
207    * @see android.os.Parcelable#writeToParcel(android.os.Parcel, int)
208    */
209   @Override
210   public void writeToParcel(Parcel par, int flags) {
211     par.writeString(path);
212     par.writeValue(isFolder);
213     par.writeValue(canRemove);
214     par.writeString(name);
215     par.writeString(currentFolder);
216     par.writeString(driveName);
217     par.writeString(workspaceName);
218     par.writeString(nodeType);
219     par.writeString(naturalName);
220     par.writeList(children);
221   }
222 }