001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package javax.swing.text.html.parser;
019:
020: import java.io.IOException;
021: import java.math.BigInteger;
022:
023: import org.apache.harmony.security.asn1.ASN1Boolean;
024: import org.apache.harmony.security.asn1.ASN1Implicit;
025: import org.apache.harmony.security.asn1.ASN1Integer;
026: import org.apache.harmony.security.asn1.ASN1Sequence;
027: import org.apache.harmony.security.asn1.ASN1StringType;
028: import org.apache.harmony.security.asn1.ASN1Type;
029: import org.apache.harmony.security.asn1.BerInputStream;
030:
031: /**
032: * It implements ASN.1 codification tools for
033: * <code>javax.swing.text.html.parser.Entity</code>. Given an
034: * <code>Entity</code>, its values are codified in ASN.1 according to the
035: * following rule:
036: *
037: * <pre>
038: * HTMLEntity ::= SEQUENCE {
039: * name UTF8String,
040: * value INTEGER,
041: * general [0] IMPLICIT BOOLEAN DEFAULT FALSE,
042: * parameter [1] IMPLICIT BOOLEAN DEFAULT FALSE,
043: * data UTF8String
044: * }
045: * </pre>
046: *
047: * The class can be used to obtain a byte array representing the codification of
048: * an <code>Entity</code>, as well as an <code>Entity</code> from a byte
049: * array (previously obtained codifying an <code>Entity</code>). In fact, it
050: * serves as a wrapper for the codification and the <code>Entity</code>
051: * itself.
052: *
053: */
054: class Asn1Entity {
055:
056: /**
057: * It stores the definition of an <code>Entity</code> as an ASN.1 valid
058: * type according to the ASN.1 framework.
059: */
060: private static ASN1Type ASN1_ENTITY;
061:
062: /**
063: * The definition of the ASN1_ENTITY type according to the rule defined for
064: * an <code>Entity</code>. It also defines a custom encoder/decoder for
065: * this type.
066: */
067: static {
068: ASN1_ENTITY = new ASN1Sequence(new ASN1Type[] {
069: ASN1StringType.UTF8STRING, // Name
070: ASN1Integer.getInstance(), // Value
071: new ASN1Implicit(0, ASN1Boolean.getInstance()), // General
072: new ASN1Implicit(1, ASN1Boolean.getInstance()), // Parameter
073: ASN1StringType.UTF8STRING // UTF8String
074: }) {
075:
076: {
077: setDefault(Boolean.FALSE, 2); // GENERAL default value
078: setDefault(Boolean.FALSE, 3); // PARAMETER default value
079: }
080:
081: /**
082: * Overrided method used to decodified the information that
083: * represents an <code>Entity</code>. It makes a completely new
084: * <code>Entity</code> with the information interpreted from the
085: * stream.
086: *
087: * @param in
088: * The <code>BerInputStream</code> where the
089: * codificated information will be read from.
090: * @return An <code>Entity</code> filled with the information read
091: * from the stream.
092: */
093: public Object getDecodedObject(BerInputStream in) {
094: Object values[] = (Object[]) in.content;
095:
096: String name = (String) values[0];
097: int type = new BigInteger((byte[]) values[1])
098: .intValue();
099: boolean general = ((Boolean) values[2]).booleanValue();
100: boolean parameter = ((Boolean) values[3])
101: .booleanValue();
102: String data = (String) values[4];
103:
104: return new Entity(name, type, data, general, parameter);
105: }
106:
107: /**
108: * Overrided method used to codify the information stored in an
109: * <code>Entity</code> into an array of bytes, according to its
110: * ASN.1 specification.
111: *
112: * @param object
113: * The object where the information to be codified is
114: * stored. It actually consists of an
115: * <code>Asn1Entity</code> object which contains the
116: * <code>Entity</code> to be codified.
117: *
118: * @param values
119: * An array of objects where the entity's name, type,
120: * data, and isGeneral and isParameter information will
121: * be stored, ready for codification.
122: */
123: public void getValues(Object object, Object values[]) {
124: Asn1Entity asn1 = (Asn1Entity) object;
125:
126: try {
127: values[0] = asn1.getEntity().getName();
128: values[1] = BigInteger.valueOf(
129: asn1.getEntity().getType()).toByteArray();
130: values[2] = Boolean.valueOf(asn1.getEntity()
131: .isGeneral());
132: values[3] = Boolean.valueOf(asn1.getEntity()
133: .isParameter());
134: values[4] = String.valueOf(asn1.getEntity()
135: .getData());
136: } catch (IOException e) {
137: throw new AssertionError(e); // this should not happen
138: }
139: }
140: };
141: }
142:
143: /**
144: * It returns an <code>ASN1Type</code> value that contains the ASN.1
145: * codification rules for an <code>Entity</code> with its encoder and
146: * decoder.
147: * <p>
148: * Among other things, this method can be used to declare the types of new
149: * fields in other structures, as for example, in an <code>Asn1DTD</code>.
150: *
151: * @return The value that defines an ASN.1 <code>Entity</code>
152: * representation with its encoder/decoder.
153: */
154: static ASN1Type getInstance() {
155: return ASN1_ENTITY;
156: }
157:
158: /**
159: * An internal copy of the <code>Entity</code> to be codified.
160: */
161: private Entity entity;
162:
163: /**
164: * An internal copy of the byte array which contains the codification of an
165: * <code>Entity</code>. From this variable, the information used to
166: * decodify an <code>Entity</code> is read from.
167: */
168: private byte[] encoded;
169:
170: /**
171: * Constructs a new instance of an <code>Asn1Entity</code> class from a
172: * byte array. The byte array received as argument can be later decodified
173: * into an <code>Entity</code>.
174: *
175: * @param encoded
176: * A byte array containing the codified information of an
177: * <code>Entity</code>.
178: */
179: public Asn1Entity(byte[] encoded) {
180: byte[] copy = new byte[encoded.length];
181: System.arraycopy(encoded, 0, copy, 0, copy.length);
182: this .encoded = copy;
183: }
184:
185: /**
186: * Constructs a new instance of an <code>Asn1Entity</code> class from an
187: * <code>Entity</code>. The <code>Entity</code> received as argument
188: * can be then codified into a byte array.
189: * <p>
190: * The value received as argument should not be null. If so, a
191: * <code>NullPointerException</code> is thrown.
192: *
193: * @param entity
194: * The <code>Entity</code> to be codified.
195: */
196: public Asn1Entity(Entity entity) {
197: if (entity == null) {
198: throw new NullPointerException();
199: }
200: this .entity = entity;
201: }
202:
203: /**
204: * Returns the representation of an <code>Entity</code> in ASN.1
205: * codification.
206: * <p>
207: * If the <code >Asn1Entity</code> object was created with an
208: * <code>Entity</code>, then the <code>Entity</code> is codified and
209: * returned as a byte array. On the other hand, if the instance was created
210: * using a byte array, then no codification process is made.
211: *
212: * @return If at construction time an <code>Entity</code> was given, its
213: * representation in ASN.1 codification. If at construction time a
214: * byte array was given, a copy of the same array is returned.
215: */
216: public byte[] getEncoded() {
217: if (encoded == null) {
218: return ASN1_ENTITY.encode(entity);
219: } else {
220: byte[] copy = new byte[encoded.length];
221: System.arraycopy(encoded, 0, copy, 0, copy.length);
222: return copy;
223: }
224: }
225:
226: /**
227: * Returns the <code>Entity</code> obtained from the decodification of an
228: * ASN.1 codified byte array.
229: * <p>
230: * If the <code>Asn1Entity</code> was created giving an
231: * <code>Entity</code>, a reference to the same <code>Entity</code> is
232: * obtained. Otherwise, the byte array given at construction time is
233: * decodificated into a new <code>Entity</code> object.
234: *
235: * @return If at construction time an <code>Entity</code> was given, the
236: * same <code>Entity</code> is returned. If at construction time a
237: * byte array was given, an <code>Entity</code> constructed with
238: * the information stored in that byte array is returned.
239: * @throws IOException
240: * If the decodification process could not be carried out
241: * properly.
242: */
243: public Entity getEntity() throws IOException {
244: if (entity == null) {
245: return (Entity) ASN1_ENTITY.decode(encoded);
246: } else {
247: return entity;
248: }
249: }
250: }
|