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>{
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 unaccentued equivalent
207            subtitle = subtitle.replaceAll("[èéêë]","e");
208            subtitle = subtitle.replaceAll("[ûùü]","u");
209            subtitle = subtitle.replaceAll("[ïî]","i");
210            subtitle = subtitle.replaceAll("[àâä]","a");
211            subtitle = subtitle.replaceAll("[öô]","o");
212            subtitle = subtitle.replaceAll("ç","c");
213            subtitle = subtitle.replaceAll("[ÈÉÊË]","E");
214            subtitle = subtitle.replaceAll("[ÛÙ]","U");
215            subtitle = subtitle.replaceAll("[ÏÎ]","I");
216            subtitle = subtitle.replaceAll("[ÀÂ]","A");
217            subtitle = subtitle.replaceAll("Ô","O");
218            subtitle = subtitle.replaceAll("Ç","C");
219            
220        }
221    
222        /**
223         * Method htmlRemove.
224         * <br><b>Summary:</b><br>
225         * This method remove the Html tags from subtitle.
226         */
227        public void htmlRemove() {
228            //remove the html tags.
229            int first = subtitle.indexOf("<");
230            int last = subtitle.indexOf(">", first);
231            //while there is a html tag to remove, remove.
232            while (first != -1 && last != -1){
233                //get subtitle before tag.
234                String temp =subtitle.substring(0,first);
235                //If tag is not at the end of subtitle.
236                if(last < subtitle.length()-1){
237                    //add subtitle part that is after the html tag.
238                    temp += subtitle.substring(last+1, subtitle.length());
239                }
240                //save subtitle.
241                subtitle = temp;
242                //get next tag index.
243                first = subtitle.indexOf("<");
244                last = subtitle.indexOf(">", first);
245            }
246        }
247    
248            /* (non-Javadoc)
249             * @see java.lang.Comparable#compareTo(java.lang.Object)
250             */
251            public int compareTo(Subtitle subtitle) {
252                    //              The result of the method.
253                    int result = 0;
254    
255                    //Compare two subtitle, means compare the start dates.
256                    int start = subtitle.getStartDate();
257                    if (start > startDate) {
258                            result = -1;
259                    } else if (start < startDate) {
260                            result = 1;
261                    }
262    
263                    //return the result.
264                    return result;
265            }
266    
267            /**
268             * Method isAnchored.
269             * <br><b>Summary:</b><br>
270             * Return true, if the subtitle is anchored to a destination time.
271             * @return  (<b>boolean</b>)   true, if the subtitle is anchored to a destination time.
272             */
273            public boolean isAnchored() {
274                    return anchor != -1;
275            }
276            
277            /**
278             * Method anchor.
279             * <br><b>Summary:</b><br>
280             * Permits to set the anchor of the subtitle.
281             * @param anchor                The anchor to set (in seconds).
282             */
283            public void anchor(int anchor){
284                    this.anchor = anchor;
285            }
286    
287            /**
288             * Method unanchor.
289             * <br><b>Summary:</b><br>
290             * remove the anchor from a subtitle.
291             */
292            public void unanchor() {
293                    anchor = -1;
294            }
295    
296            /**
297             * Method getAnchor.
298             * <br><b>Summary:</b><br>
299             * Return the anchor in milliseconds.
300             * @return the anchor
301             */
302            public int getAnchor() {
303                    return anchor*1000;
304            }
305            
306    }