KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snmp4j > agent > mo > snmp > DisplayString


1 /*_############################################################################
2   _##
3   _## SNMP4J-Agent - DisplayString.java
4   _##
5   _## Copyright (C) 2005-2007 Frank Fock (SNMP4J.org)
6   _##
7   _## Licensed under the Apache License, Version 2.0 (the "License");
8   _## you may not use this file except in compliance with the License.
9   _## You may obtain a copy of the License at
10   _##
11   _## http://www.apache.org/licenses/LICENSE-2.0
12   _##
13   _## Unless required by applicable law or agreed to in writing, software
14   _## distributed under the License is distributed on an "AS IS" BASIS,
15   _## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   _## See the License for the specific language governing permissions and
17   _## limitations under the License.
18   _##
19   _##########################################################################*/

20
21
22 package org.snmp4j.agent.mo.snmp;
23
24 import org.snmp4j.agent.mo.MOMutableColumn;
25 import org.snmp4j.agent.MOAccess;
26 import org.snmp4j.smi.Variable;
27 import org.snmp4j.smi.SMIConstants;
28 import org.snmp4j.smi.OctetString;
29 import org.snmp4j.agent.mo.MOValueValidationEvent;
30 import org.snmp4j.mp.SnmpConstants;
31 import org.snmp4j.agent.mo.snmp.smi.Constraints;
32 import org.snmp4j.agent.mo.snmp.smi.ConstraintsImpl;
33 import org.snmp4j.agent.mo.snmp.smi.Constraint;
34 import org.snmp4j.agent.mo.snmp.smi.ValueConstraintValidator;
35 import org.snmp4j.PDU;
36 import org.snmp4j.agent.mo.snmp.smi.ValueConstraint;
37
38 /**
39  * The <code>DisplayString</code> class implements the DisplayString textual
40  * convention as defined by the SNMPv2-TC MIB specification for columnar
41  * objects.
42  *
43  * @author Frank Fock
44  * @version 1.0
45  */

46 public class DisplayString extends MOMutableColumn {
47
48   public static final int MIB_SIZE = 0;
49   public static final int MAX_SIZE = 255;
50
51   private Constraints sizeConstraints = new ConstraintsImpl();
52
53   public DisplayString(int columnID,
54                        MOAccess access,
55                        Variable defaultValue,
56                        boolean mutableInService) {
57     super(columnID, SMIConstants.SYNTAX_OCTET_STRING,
58           access, defaultValue, mutableInService);
59   }
60
61   public DisplayString(int columnID,
62                        MOAccess access,
63                        Variable defaultValue) {
64     super(columnID, SMIConstants.SYNTAX_OCTET_STRING,
65           access, defaultValue);
66   }
67
68   public DisplayString(int columnID,
69                        MOAccess access,
70                        Variable defaultValue,
71                        boolean mutableInService, int minSize, int maxSize) {
72     super(columnID, SMIConstants.SYNTAX_OCTET_STRING,
73           access, defaultValue, mutableInService);
74     sizeConstraints.add(new Constraint(minSize, maxSize));
75   }
76
77   public synchronized int validate(Variable newValue, Variable oldValue) {
78     int status = super.validate(newValue, oldValue);
79     if (status == SnmpConstants.SNMP_ERROR_SUCCESS) {
80       status = validateDisplayString(newValue, sizeConstraints);
81     }
82     return status;
83   }
84
85   /**
86    * Validates a variable as a DisplayString OCTET STRING. If the variable
87    * is not an OctetString instance, wrongType is returned as error status.
88    * Otherwise wrongValue is returned if the string contains non-printable
89    * characters other than 'return' and 'new-line'.
90    *
91    * @param displayString
92    * a variable to validate.
93    * @param sizeContraints
94    * a constraint for the size (length) of the string.
95    * @return
96    * a SNMP error status if the variable is not a valid DisplayString or zero
97    * if it is.
98    */

99   public static int validateDisplayString(Variable displayString,
100                                           ValueConstraint sizeContraints) {
101     if (displayString instanceof OctetString) {
102       OctetString os = (OctetString)displayString;
103       int status = sizeContraints.validate(displayString);
104       if (status != PDU.noError) {
105         return status;
106       }
107       for (int i=0; i<os.length(); i++) {
108         if (os.get(i) < 0) {
109           return SnmpConstants.SNMP_ERROR_WRONG_VALUE;
110         }
111         if (os.get(i) == '\r') {
112           if (i+1 == os.length()) {
113             return SnmpConstants.SNMP_ERROR_WRONG_VALUE;
114           }
115           else if ((os.get(i+1) != 0) && (os.get(i+1) != '\n')) {
116             return SnmpConstants.SNMP_ERROR_WRONG_VALUE;
117           }
118         }
119       }
120       return SnmpConstants.SNMP_ERROR_SUCCESS;
121     }
122     else {
123       return SnmpConstants.SNMP_ERROR_WRONG_TYPE;
124     }
125   }
126
127   /**
128    * The <code>DisplayStringValidation</code> can be used to validate the
129    * contents of <code>OctetString</code> variables that follow the
130    * DisplayString TC rules.
131    *
132    * @author Frank Fock
133    * @version 1.0
134    */

135   public static class DisplayStringValidation extends ValueConstraintValidator
136   {
137     public DisplayStringValidation(Constraints valueConstraint) {
138       super(valueConstraint);
139     }
140
141     public DisplayStringValidation(int minSize, int maxSize) {
142       super(new ConstraintsImpl());
143       ((ConstraintsImpl)
144        getValueConstraint()).add(new Constraint(minSize, maxSize));
145     }
146
147     public void validate(MOValueValidationEvent validationEvent) {
148       Variable newValue = validationEvent.getNewValue();
149       int status =
150           DisplayString.validateDisplayString(newValue, getValueConstraint());
151       validationEvent.setValidationStatus(status);
152     }
153   }
154
155 }
156
Popular Tags