OSDN Git Service

The first commit.
[bm-asn1/bm-asn1.git] / jp / bitmeister / asn1 / type / StringType.java
1 /*
2  * Copyright 2011 BitMeister Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package jp.bitmeister.asn1.type;
17
18 import java.nio.charset.Charset;
19 import java.util.regex.Pattern;
20
21 import jp.bitmeister.asn1.exception.ASN1IllegalArgument;
22 import jp.bitmeister.asn1.processor.ASN1Visitor;
23 import jp.bitmeister.asn1.type.builtin.OCTET_STRING;
24
25 /**
26  * The base class for character string types.
27  * 
28  * <p>
29  * This class provides generic interfaces and common methods for classes that
30  * represents character string types.
31  * </p>
32  * 
33  * @author WATANABE, Jun. <jwat at bitmeister.jp>
34  */
35 public abstract class StringType extends OCTET_STRING {
36         
37         /**
38          * Set the {@code String} to this instance. If the type has character
39          * limitation, characters contained in the string are checked before
40          * assignment.
41          * 
42          * @param value
43          *            The string value to be assigned.
44          */
45         public void set(String value) {
46                 checkCharacters(value);
47                 set(value.getBytes(charset()));
48         }
49
50         /**
51          * Converts the array of {@code byte} value of this instance to a
52          * {@code String} and returns it.
53          * 
54          * @return The string value assigned to this instance.
55          */
56         public String stringValue() {
57                 return new String(value(), charset());
58         }
59
60         /**
61          * Returns the {@code Charset} used for encoding and decodiong the value of
62          * this data.
63          * 
64          * @return The {@code Charset}.
65          */
66         protected abstract Charset charset();
67
68         /**
69          * Returns the {@code Pattern} used for checking characters that to be set
70          * to this data.
71          * 
72          * @return The {@code Pattern}.
73          */
74         protected abstract Pattern pattern();
75
76         /**
77          * Tests if the characters are valid for this type.
78          * 
79          * @param value
80          *            {@code true} when the characters is valid.
81          */
82         protected void checkCharacters(String value) {
83                 if (pattern() != null && !pattern().matcher(value).matches()) {
84                         ASN1IllegalArgument ex = new ASN1IllegalArgument();
85                         ex.setMessage(
86                                         "The value '"
87                                                         + value
88                                                         + "' contains characters which are not allowed to be contained in this type.",
89                                         null, getClass(), null, null);
90                         throw ex;
91                 }
92         }
93
94         /*
95          * (non-Javadoc)
96          * 
97          * @see
98          * jp.bitmeister.asn1.type.builtin.OCTET_STRING#accept(jp.bitmeister.asn1
99          * .processor.ASN1Visitor)
100          */
101         @Override
102         public <E extends Throwable> void accept(ASN1Visitor<E> visitor) throws E {
103                 visitor.visit(this);
104         }
105
106 }