001    /*
002     * Copyright (C) 2006-2010 Enrique Lara (k957@68k.org)
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006      * You may obtain a copy of the License at
007     *
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    package net.sf.jmorse;
018    
019    import net.sf.jmorse.util.logging.Logger;
020    
021    import java.util.Locale;
022    import java.util.MissingResourceException;
023    import java.util.ResourceBundle;
024    
025    /**
026     * @author <a href="mailto:k957@68k.org">Enrique Lara</a>
027     */
028    public abstract class AbstractMorseWriter implements MorseWriter {
029        private static final Logger s_log = Logger.getLogger(AbstractMorseWriter.class);
030        private static final boolean DEBUG_ENABLED = s_log.isDebugEnabled();
031        ResourceBundle morseResBundle;
032    
033        // --------------------------------------------------- Instance Variables
034        protected int wpm = MorseUtil.DEFAULT_WPM;
035    
036        public int getWpm() {
037            return this.wpm;
038        }
039    
040        public void setWpm(int wpm) {
041            this.wpm = wpm;
042        }
043    
044        public synchronized void setLocale(Locale locale) {
045            try {
046                morseResBundle = ResourceBundle.getBundle("net.sf.jmorse.morse", locale);
047            } catch (Exception e) {
048                s_log.fatal("Problems Loading Morse Resource Bundle.", e);
049            }
050        }
051    
052        public AbstractMorseWriter() {
053            try {
054                morseResBundle = ResourceBundle.getBundle("net.sf.jmorse.morse");
055            } catch (Exception e) {
056                s_log.fatal("Problems Loading Morse Resource Bundle.", e);
057            }
058        }
059    
060        public AbstractMorseWriter(Locale locale) {
061            setLocale(locale);
062        }
063    
064        protected String toMorse(char c) throws MissingResourceException {
065            String rval = "= ";
066    
067            if (Character.isWhitespace(c)) {
068                rval = "R ";
069            } else {
070                rval = morseResBundle.getString(Character.toString(c).toUpperCase()) + " ";
071            }
072    
073            return rval;
074        }
075    
076        protected String toMorse(char[] msg, int len) throws MissingResourceException {
077            StringBuffer morseTxt = new StringBuffer();
078    
079            for (int i = 0; i < len; i++) {
080                char c = msg[i];
081                morseTxt.append(toMorse(c));
082            }
083    
084            String rval = morseTxt.toString();
085    
086            if (DEBUG_ENABLED) {
087                s_log.debug("morseTxt=" + rval);
088            }
089    
090            return rval;
091        }
092    
093        public void write(String msg) {
094            if (msg == null) {
095                throw new IllegalArgumentException("Message may not be null.");
096            }
097    
098            write(msg.toCharArray(), msg.length());
099        }
100    
101        public void write(char[] msg, int len) {
102            if (DEBUG_ENABLED) {
103                s_log.debug("msg=" + new String(msg, 0, len));
104                s_log.debug("wpm=" + wpm);
105            }
106    
107            String morseTxt = toMorse(msg, len);
108            writeMorse(morseTxt.toCharArray(), morseTxt.length());
109        }
110    
111        protected abstract void writeMorse(char[] msg, int len);
112    }