View Javadoc

    1   /*
    2    *  Licensed to the Apache Software Foundation (ASF) under one
    3    *  or more contributor license agreements.  See the NOTICE file
    4    *  distributed with this work for additional information
    5    *  regarding copyright ownership.  The ASF licenses this file
    6    *  to you under the Apache License, Version 2.0 (the
    7    *  "License"); you may not use this file except in compliance
    8    *  with the License.  You may obtain a copy of the License at
    9    *
    10   *    http://www.apache.org/licenses/LICENSE-2.0
    11   *
    12   *  Unless required by applicable law or agreed to in writing,
    13   *  software distributed under the License is distributed on an
    14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    15   *  KIND, either express or implied.  See the License for the
    16   *  specific language governing permissions and limitations
    17   *  under the License.
    18   *
    19   */
    20  package org.apache.mina.transport.serial;
    21
    22  import java.beans.PropertyEditor;
    23
    24  import org.apache.mina.integration.beans.AbstractPropertyEditor;
    25  import org.apache.mina.transport.serial.SerialAddress.DataBits;
    26  import org.apache.mina.transport.serial.SerialAddress.FlowControl;
    27  import org.apache.mina.transport.serial.SerialAddress.Parity;
    28  import org.apache.mina.transport.serial.SerialAddress.StopBits;
    29
    30  /**
    31   * A {@link PropertyEditor} which converts a {@link String} into a
    32   * {@link SerialAddress} and vice versa.  Valid values specify 6 address
    33   * components separated by colon (e.g. <tt>COM1:9600:7:1:even:rtscts-in</tt>);
    34   * port name, bauds, data bits, stop bits, parity and flow control respectively.
    35   *
    36   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
    37   */
    38  public class SerialAddressEditor extends AbstractPropertyEditor {
    39      @Override
    40      protected String toText(Object value) {
    41          SerialAddress addr = (SerialAddress) value;
    42          return addr.getName() + ':' + addr.getBauds() + ':' + toText(addr.getDataBits()) + ':'
    43                  + toText(addr.getStopBits()) + ':' + toText(addr.getParity()) + ':' + toText(addr.getFlowControl());
    44      }
    45
    46      private String toText(DataBits bits) {
    47          switch (bits) {
    48          case DATABITS_5:
    49              return "5";
    50          case DATABITS_6:
    51              return "6";
    52          case DATABITS_7:
    53              return "7";
    54          case DATABITS_8:
    55              return "8";
    56          default:
    57              throw new IllegalArgumentException("Unknown dataBits: " + bits);
    58          }
    59      }
    60
    61      private String toText(StopBits bits) {
    62          switch (bits) {
    63          case BITS_1:
    64              return "1";
    65          case BITS_1_5:
    66              return "1.5";
    67          case BITS_2:
    68              return "2";
    69          default:
    70              throw new IllegalArgumentException("Unknown stopBits: " + bits);
    71          }
    72      }
    73
    74      private String toText(Parity parity) {
    75          switch (parity) {
    76          case EVEN:
    77              return "even";
    78          case ODD:
    79              return "odd";
    80          case MARK:
    81              return "mark";
    82          case NONE:
    83              return "none";
    84          case SPACE:
    85              return "space";
    86          default:
    87              throw new IllegalArgumentException("Unknown parity: " + parity);
    88          }
    89      }
    90
    91      private String toText(FlowControl flowControl) {
    92          switch (flowControl) {
    93          case NONE:
    94              return "none";
    95          case RTSCTS_IN:
    96              return "rtscts-in";
    97          case RTSCTS_OUT:
    98              return "rtscts-out";
    99          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 }