001    /////////////////////////////////////////////////
002    // This file is part of Sears project.
003    // Subtitle Editor And Re-Synch
004    // A tool to easily modify and resynch movies subtitles.
005    /////////////////////////////////////////////////
006    //This program is free software; 
007    //you can redistribute it and/or modify it under the terms 
008    //of the GNU General Public License 
009    //as published by the Free Software Foundation; 
010    //either version 2 of the License, or (at your option) any later version.
011    /////////////////////////////////////////////////
012    //Sears project is available under sourceforge
013    // at adress: http://sourceforge.net/projects/sears/
014    //Copyright (C) 2005 Booba Skaya
015    //Mail: booba.skaya@gmail.com
016    ////////////////////////////////////////////////
017    package sears.file;
018    
019    /**
020     * Class .
021     * <br><b>Summary:</b><br>
022     * Represent a Subtitle.
023     */
024    public class Subtitle implements Comparable<Subtitle>, Cloneable{
025        /**The index of the subtitle*/
026        private int number;
027    
028        /**Its start date (in milliseconds)*/
029        private int startDate;
030    
031        /**Its end date (in milliseconds)*/
032        private int endDate;
033    
034        /**The subtitle to display*/
035        private String subtitle;
036    
037        /** (<b>int</b>) anchor: The anchor defined for this subtitle, -1 means non anchored.*/
038        private int anchor;
039        
040        /**
041         * Constructor Subtitle.
042         * <br><b>Summary:</b><br>
043         * Constructor of the class.
044         * @param _number           The index of the subtitle.  
045         * @param _startDate        Its start date (in milliseconds)
046         * @param _endDate          Its end date (in milliseconds)
047         * @param _subtitle         The subtitle to display
048         */
049        public Subtitle(int _number, int _startDate, int _endDate, String _subtitle) {
050            number = _number;
051            startDate = _startDate;
052            endDate = _endDate;
053            subtitle = _subtitle;
054            anchor = -1;
055        }
056    
057        /**
058         * Constructor Subtitle.
059         * <br><b>Summary:</b><br>
060         * Constructor of the class.
061         * Copy another subtitle.
062         * @param _subtitle  The <b>Subtitle</b> to copy.
063         */
064        public Subtitle(Subtitle _subtitle) {
065            this( _subtitle.getNumber(), _subtitle.getStartDate(), _subtitle.getEndDate(), _subtitle.getSubtitle());
066        }
067    
068        /**
069         * Constructor Subtitle.
070         * <br><b>Summary:</b><br>
071         * Constructor of the class.
072         */
073        public Subtitle() {
074            anchor = -1;
075        }
076    
077        /**
078         * Method delay.
079         * <br><b>Summary:</b><br>
080         * Apply a delay to the subtitle.
081         * @param delay The delay to apply.
082         */
083        public void delay(int delay) {
084            startDate += delay;
085            endDate += delay;
086        }
087    
088    
089        
090        /**
091             * Method getEndDate.
092             * <br><b>Summary:</b><br>
093             * Return the endDate.
094             * @return the endDate
095             */
096            public int getEndDate() {
097                    return endDate;
098            }
099    
100            /**
101             * Method setEndDate.
102             * <br><b>Summary:</b><br>
103             * Set the endDate.
104             * @param endDate the endDate to set
105             */
106            public void setEndDate(int endDate) {
107                    this.endDate = endDate;
108            }
109    
110            /**
111             * Method getNumber.
112             * <br><b>Summary:</b><br>
113             * Return the number.
114             * @return the number
115             */
116            public int getNumber() {
117                    return number;
118            }
119    
120            /**
121             * Method setNumber.
122             * <br><b>Summary:</b><br>
123             * Set the number.
124             * @param number the number to set
125             */
126            public void setNumber(int number) {
127                    this.number = number;
128            }
129    
130            /**
131             * Method getStartDate.
132             * <br><b>Summary:</b><br>
133             * Return the startDate.
134             * @return the startDate
135             */
136            public int getStartDate() {
137                    return startDate;
138            }
139    
140            /**
141             * Method setStartDate.
142             * <br><b>Summary:</b><br>
143             * Set the startDate.
144             * @param startDate the startDate to set
145             */
146            public void setStartDate(int startDate) {
147                    this.startDate = startDate;
148            }
149    
150            /**
151             * Method getSubtitle.
152             * <br><b>Summary:</b><br>
153             * Return the subtitle.
154             * @return the subtitle
155             */
156            public String getSubtitle() {
157                    return subtitle;
158            }
159    
160            /**
161             * Method setSubtitle.
162             * <br><b>Summary:</b><br>
163             * Set the subtitle.
164             * @param subtitle the subtitle to set
165             */
166            public void setSubtitle(String subtitle) {
167                    this.subtitle = subtitle;
168            }
169    
170            /* (non-Javadoc)
171         * @see java.lang.Object#equals(java.lang.Object)
172         */
173        public boolean equals(Object object){
174            //the result of the method
175            boolean result = false;
176            //We can only compare Subtitle Objects.
177            if(object instanceof Subtitle){
178                Subtitle subtitle = (Subtitle) object;
179                result = true;
180                //check all parameters.
181                //If one comparison fails, global result fails.
182                //compare number.
183                result = result && (getNumber() == subtitle.getNumber());
184                //start date
185                result = result && (getStartDate() == subtitle.getStartDate());
186                //end date
187                result = result && (getEndDate() == subtitle.getEndDate());
188                //Subtitle
189                result = result && 
190                            (
191                                    (getSubtitle() == null && subtitle.getSubtitle() == null)
192                                    ||
193                                    (getSubtitle().equals(subtitle.getSubtitle()))
194                            );
195            }
196            //return the result
197            return result;
198        }
199    
200        /**
201         * Method accentRemove.
202         * <br><b>Summary:</b><br>
203         * This method remove the accents and other bad characters from subtitle.
204         */
205        public void accentRemove() {
206            //Just have to remove accent chars, and replace them by their non accent equivalent
207            subtitle = subtitle.replaceAll("[ÀÁÂÃÄÅÆĀĂĄ]","A");
208            subtitle = subtitle.replaceAll("[àáâãäåæāăą]","a");   
209            
210            subtitle = subtitle.replaceAll("[èéêëēĕėęě]","e");
211            subtitle = subtitle.replaceAll("[ÈÉÊËĒĔĖĘĚ]","E");
212            
213            subtitle = subtitle.replaceAll("[ìíîï]","i");
214            subtitle = subtitle.replaceAll("[ÌÍÎÏ]","I");
215            
216            subtitle = subtitle.replaceAll("[ñ]","n");
217            subtitle = subtitle.replaceAll("[Ñ]","N");
218            
219            subtitle = subtitle.replaceAll("[çćĉċč]","c");
220            subtitle = subtitle.replaceAll("[ÇĆĈĊČ]","C");
221            
222            subtitle = subtitle.replaceAll("[ðòóôõöø]","o");
223            subtitle = subtitle.replaceAll("[ÐÒÓÔÕÖ]","O");
224    
225            subtitle = subtitle.replaceAll("[ùúûü]","u");
226            subtitle = subtitle.replaceAll("[ÙÚÛÜ]","U");
227            
228            subtitle = subtitle.replaceAll("[ýÿ]","y");
229            subtitle = subtitle.replaceAll("[ÝŸ]","Y");
230            
231            subtitle = subtitle.replaceAll("[ďđ]","d");
232            subtitle = subtitle.replaceAll("[ĎĐ]","D");
233        }
234    
235        /**
236         * Method htmlRemove.
237         * <br><b>Summary:</b><br>
238         * This method remove the Html tags from subtitle.
239         */
240        public void htmlRemove() {
241            //remove the html tags.
242            int first = subtitle.indexOf("<");
243            int last = subtitle.indexOf(">", first);
244            //while there is a html tag to remove, remove.
245            while (first != -1 && last != -1){
246                //get subtitle before tag.
247                String temp =subtitle.substring(0,first);
248                //If tag is not at the end of subtitle.
249                if(last < subtitle.length()-1){
250                    //add subtitle part that is after the html tag.
251                    temp += subtitle.substring(last+1, subtitle.length());
252                }
253                //save subtitle.
254                subtitle = temp;
255                //get next tag index.
256                first = subtitle.indexOf("<");
257                last = subtitle.indexOf(">", first);
258            }
259        }
260    
261            /* (non-Javadoc)
262             * @see java.lang.Comparable#compareTo(java.lang.Object)
263             */
264            public int compareTo(Subtitle subtitle) {
265                    //              The result of the method.
266                    int result = 0;
267    
268                    //Compare two subtitle, means compare the start dates.
269                    int start = subtitle.getStartDate();
270                    if (start > startDate) {
271                            result = -1;
272                    } else if (start < startDate) {
273                            result = 1;
274                    }
275    
276                    //return the result.
277                    return result;
278            }
279    
280            /**
281             * Method isAnchored.
282             * <br><b>Summary:</b><br>
283             * Return true, if the subtitle is anchored to a destination time.
284             * @return  (<b>boolean</b>)   true, if the subtitle is anchored to a destination time.
285             */
286            public boolean isAnchored() {
287                    return anchor != -1;
288            }
289            
290            /**
291             * Method anchor.
292             * <br><b>Summary:</b><br>
293             * Permits to set the anchor of the subtitle.
294             * @param anchor                The anchor to set (in seconds).
295             */
296            public void anchor(int anchor){
297                    this.anchor = anchor;
298            }
299    
300            /**
301             * Method unanchor.
302             * <br><b>Summary:</b><br>
303             * remove the anchor from a subtitle.
304             */
305            public void unanchor() {
306                    anchor = -1;
307            }
308    
309            /**
310             * Method getAnchor.
311             * <br><b>Summary:</b><br>
312             * Return the anchor in milliseconds.
313             * @return the anchor
314             */
315            public int getAnchor() {
316                    return anchor*1000;
317            }
318            
319            /* (non-Javadoc)
320             * @see java.lang.Object#clone()
321             */
322            public Subtitle cloneSubtitle(){
323                    return new Subtitle(number, startDate, endDate, new String(subtitle));
324            }
325            
326    }