View Javadoc

1   /*
2    * Copyright (C) 2006  Tom Gibara
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2.1 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12   * Lesser General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this library; if not, write to the Free Software
16   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17   */
18  package com.tomgibara.pronto.control.impl;
19  
20  import java.text.ParseException;
21  import java.util.logging.Level;
22  import java.util.regex.Pattern;
23  
24  import com.tomgibara.pronto.control.EngineControlAdapter;
25  import com.tomgibara.pronto.core.impl.StaticProperties;
26  import com.tomgibara.pronto.util.Arguments;
27  
28  /**
29   * An EngineControlAdapter that wraps another to ensure that the names
30   * supplied-to/received-from it are valid.
31   * 
32   * @param <S>
33   *            the type of state
34   * @param <L>
35   *            the type of label
36   * @param <P>
37   *            the type of parameter
38   * 
39   * @author Tom Gibara
40   * 
41   */
42  
43  class CheckedAdapter<S, L, P> implements EngineControlAdapter<S, L, P> {
44  
45      // statics
46  
47      private static final Pattern LABEL_NAME = StaticProperties.getPattern("pronto.control.label-name-pattern");
48  
49      // fields
50  
51      private final EngineControlAdapter<S, L, P> adapter;
52  
53      // constructors
54  
55      CheckedAdapter(final EngineControlAdapter<S, L, P> adapter) {
56          this.adapter = adapter;
57      }
58  
59      // accessors
60  
61      EngineControlAdapter<S, L, P> getAdapter() {
62          return adapter;
63      }
64  
65      // adapter methods
66  
67      public L labelFromName(final String name) {
68          Arguments.notNull(name, "name");
69          // double check the label-name so that the underlying adapter always
70          // gets a valid argument
71          if (!LABEL_NAME.matcher(name).matches()) return null;
72          return adapter.labelFromName(name);
73      }
74  
75      public String nameFromLabel(final L label) {
76          Arguments.notNull(label, "label");
77          String name = adapter.nameFromLabel(label);
78          if (name != null && !LABEL_NAME.matcher(name).matches()) {
79              ControlFactoryImpl.LOGGER.log(Level.WARNING, "Adapter returned invalid name: {0}", name);
80              return null;
81          } else {
82              return name;
83          }
84      }
85  
86      public P parseParameter(final String string) throws ParseException {
87          return adapter.parseParameter(string);
88      }
89  
90  }