1 /*
2 * Copyright 2004 The Apache Software Foundation
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 */
17 package org.apache.asn1.ber.digester.rules ;
18
19
20 import org.apache.asn1.ber.TypeClass;
21 import org.apache.asn1.ber.digester.AbstractRule;
22 import org.apache.asn1.ber.digester.BERDigester;
23 import org.apache.commons.lang.exception.NestableRuntimeException;
24
25
26 /**
27 * Rule implementation that creates a new object and pushes it onto the
28 * object stack when a TLV is encountered. When the TLV is complete,
29 * the object will be popped off of the stack.
30 *
31 * @author <a href="mailto:dev@directory.apache.org">Apache Direclectory Project</a>
32 * @version $Rev: 157644 $
33 */
34 public class ObjectCreateRule extends AbstractRule
35 {
36 /** the class of object to instantiate and push */
37 private final Class clazz ;
38
39
40 /**
41 * Creates a rule that creates an instance of an object when the tag
42 *
43 * @param clazz the class to create an instance of.
44 */
45 public ObjectCreateRule( BERDigester digester, Class clazz )
46 {
47 this.clazz = clazz ;
48 setDigester( digester ) ;
49 }
50
51
52 /* (non-Javadoc)
53 * @see org.apache.snickers.ber.Rule#tag(int, boolean,
54 * org.apache.asn1.ber.TypeClass)
55 */
56 public void tag( int id, boolean isPrimitive, TypeClass typeClass )
57 {
58 try
59 {
60 Object obj = clazz.newInstance() ;
61 getDigester().push( obj ) ;
62 }
63 catch ( InstantiationException e )
64 {
65 throw new NestableRuntimeException( e ) ;
66 }
67 catch ( IllegalAccessException e )
68 {
69 throw new NestableRuntimeException( e ) ;
70 }
71 }
72
73
74 /* (non-Javadoc)
75 * @see org.apache.snickers.ber.Rule#finish()
76 */
77 public void finish()
78 {
79 getDigester().pop() ;
80 }
81 }
82