001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 *
019 */
020package org.apache.mina.transport.serial;
021
022import java.beans.PropertyEditor;
023
024import org.apache.mina.integration.beans.AbstractPropertyEditor;
025import org.apache.mina.transport.serial.SerialAddress.DataBits;
026import org.apache.mina.transport.serial.SerialAddress.FlowControl;
027import org.apache.mina.transport.serial.SerialAddress.Parity;
028import org.apache.mina.transport.serial.SerialAddress.StopBits;
029
030/**
031 * A {@link PropertyEditor} which converts a {@link String} into a
032 * {@link SerialAddress} and vice versa.  Valid values specify 6 address
033 * components separated by colon (e.g. <tt>COM1:9600:7:1:even:rtscts-in</tt>);
034 * port name, bauds, data bits, stop bits, parity and flow control respectively.
035 *
036 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
037 */
038public class SerialAddressEditor extends AbstractPropertyEditor {
039    @Override
040    protected String toText(Object value) {
041        SerialAddress addr = (SerialAddress) value;
042        return addr.getName() + ':' + addr.getBauds() + ':' + toText(addr.getDataBits()) + ':'
043                + toText(addr.getStopBits()) + ':' + toText(addr.getParity()) + ':' + toText(addr.getFlowControl());
044    }
045
046    private String toText(DataBits bits) {
047        switch (bits) {
048        case DATABITS_5:
049            return "5";
050        case DATABITS_6:
051            return "6";
052        case DATABITS_7:
053            return "7";
054        case DATABITS_8:
055            return "8";
056        default:
057            throw new IllegalArgumentException("Unknown dataBits: " + bits);
058        }
059    }
060
061    private String toText(StopBits bits) {
062        switch (bits) {
063        case BITS_1:
064            return "1";
065        case BITS_1_5:
066            return "1.5";
067        case BITS_2:
068            return "2";
069        default:
070            throw new IllegalArgumentException("Unknown stopBits: " + bits);
071        }
072    }
073
074    private String toText(Parity parity) {
075        switch (parity) {
076        case EVEN:
077            return "even";
078        case ODD:
079            return "odd";
080        case MARK:
081            return "mark";
082        case NONE:
083            return "none";
084        case SPACE:
085            return "space";
086        default:
087            throw new IllegalArgumentException("Unknown parity: " + parity);
088        }
089    }
090
091    private String toText(FlowControl flowControl) {
092        switch (flowControl) {
093        case NONE:
094            return "none";
095        case RTSCTS_IN:
096            return "rtscts-in";
097        case RTSCTS_OUT:
098            return "rtscts-out";
099        case XONXOFF_IN:
100            return "xonxoff-in";
101        case XONXOFF_OUT:
102            return "xonxoff-out";
103        default:
104            throw new IllegalArgumentException("Unknown flowControl: " + flowControl);
105        }
106    }
107
108    @Override
109    protected Object toValue(String text) throws IllegalArgumentException {
110        String[] components = text.split(":");
111        if (components.length != 6) {
112            throw new IllegalArgumentException("SerialAddress must have 6 components separated " + "by colon: " + text);
113        }
114
115        return new SerialAddress(components[0].trim(), toBauds(components[1].trim()), toDataBits(components[2].trim()),
116                toStopBits(components[3].trim()), toParity(components[4].trim()), toFlowControl(components[5].trim()));
117    }
118
119    private int toBauds(String text) {
120        try {
121            return Integer.parseInt(text);
122        } catch (NumberFormatException e) {
123            throw new IllegalArgumentException("bauds: " + text);
124        }
125    }
126
127    private DataBits toDataBits(String text) {
128        try {
129            return DataBits.valueOf("DATABITS_" + Integer.parseInt(text));
130        } catch (IllegalArgumentException e) {
131            throw new IllegalArgumentException("dataBits: " + text);
132        }
133    }
134
135    private StopBits toStopBits(String text) {
136        try {
137            return StopBits.valueOf("BITS_" + text.replace('.', '_'));
138        } catch (IllegalArgumentException e) {
139            throw new IllegalArgumentException("stopBits: " + text);
140        }
141    }
142
143    private Parity toParity(String text) {
144        try {
145            return Parity.valueOf(text.toUpperCase());
146        } catch (IllegalArgumentException e) {
147            throw new IllegalArgumentException("parity: " + text);
148        }
149    }
150
151    private FlowControl toFlowControl(String text) {
152        String normalizedText = text.toUpperCase().replaceAll("(-|_)", "");
153        if (normalizedText.endsWith("IN")) {
154            normalizedText = normalizedText.substring(0, normalizedText.length() - 2) + "_IN";
155        }
156        if (normalizedText.endsWith("OUT")) {
157            normalizedText = normalizedText.substring(0, normalizedText.length() - 3) + "_OUT";
158        }
159
160        try {
161            return FlowControl.valueOf(normalizedText);
162        } catch (IllegalArgumentException e) {
163            throw new IllegalArgumentException("flowControl: " + text);
164        }
165    }
166}