2 * Copyright 2011 BitMeister Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package jp.bitmeister.asn1.type;
18 import java.nio.charset.Charset;
19 import java.util.regex.Pattern;
21 import jp.bitmeister.asn1.exception.ASN1IllegalArgument;
22 import jp.bitmeister.asn1.processor.ASN1Visitor;
23 import jp.bitmeister.asn1.type.builtin.OCTET_STRING;
26 * The base class for character string types.
29 * This class provides generic interfaces and common methods for classes that
30 * represents character string types.
33 * @author WATANABE, Jun. <jwat at bitmeister.jp>
35 public abstract class StringType extends OCTET_STRING {
38 * Set the {@code String} to this instance. If the type has character
39 * limitation, characters contained in the string are checked before
43 * The string value to be assigned.
45 public void set(String value) {
46 checkCharacters(value);
47 set(value.getBytes(charset()));
51 * Converts the array of {@code byte} value of this instance to a
52 * {@code String} and returns it.
54 * @return The string value assigned to this instance.
56 public String stringValue() {
57 return new String(value(), charset());
61 * Returns the {@code Charset} used for encoding and decodiong the value of
64 * @return The {@code Charset}.
66 protected abstract Charset charset();
69 * Returns the {@code Pattern} used for checking characters that to be set
72 * @return The {@code Pattern}.
74 protected abstract Pattern pattern();
77 * Tests if the characters are valid for this type.
80 * {@code true} when the characters is valid.
82 protected void checkCharacters(String value) {
83 if (pattern() != null && !pattern().matcher(value).matches()) {
84 ASN1IllegalArgument ex = new ASN1IllegalArgument();
88 + "' contains characters which are not allowed to be contained in this type.",
89 null, getClass(), null, null);
98 * jp.bitmeister.asn1.type.builtin.OCTET_STRING#accept(jp.bitmeister.asn1
99 * .processor.ASN1Visitor)
102 public <E extends Throwable> void accept(ASN1Visitor<E> visitor) throws E {