Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/voytechs/jnetstream/codec/FieldImpl.java


1   /*
2    * File: FieldImpl.java
3    * Auth: Mark Bednarczyk
4    * Date: DATE
5    *   Id: $Id: FieldImpl.java,v 1.1.1.1 2003/09/22 16:32:13 voytechs Exp $
6    ********************************************
7    Copyright (C) 2003  Mark Bednarczyk
8   
9    This program is free software; you can redistribute it and/or
10   modify it under the terms of the GNU General Public License
11   as published by the Free Software Foundation; either version 2
12   of the License, or (at your option) any later version.
13  
14   This program is distributed in the hope that it will be useful,
15   but WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   GNU General Public License for more details.
18  
19   You should have received a copy of the GNU General Public License
20   along with this program; if not, write to the Free Software
21   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22   ********************************************
23   * $Log: FieldImpl.java,v $
24   * Revision 1.1.1.1  2003/09/22 16:32:13  voytechs
25   * Initial import.
26   *
27   */
28  package com.voytechs.jnetstream.codec;
29  
30  import com.voytechs.jnetstream.primitive.*;
31  import com.voytechs.jnetstream.io.OutputBuffer;
32  
33  import java.lang.*;
34  import java.util.*;
35  
36  /**
37   * 
38   */
39  public class FieldImpl 
40    implements Field, MutableField {
41  
42    /* Internal attributes */
43    private static final boolean debug = false;
44  
45    public static final String VALUE_NAME = "valueName";
46  
47    /* Store the fields in 2 structures. 1=array based and second hashtable based
48     * for easy lookup by name.
49     */
50    private ArrayList fieldList = new ArrayList();
51    private Hashtable fieldHash = new Hashtable();
52    private ArrayList notes = new ArrayList();
53  
54    private String name;
55  
56    private Hashtable perm;
57    private Hashtable temp = new Hashtable();
58  
59    private Primitive primitive = null;
60  
61    private String valueName = null;
62  
63    /**
64     *
65     * @param
66     * @exception
67     */
68    public FieldImpl(String name, Hashtable permProperties, Primitive primitive) {
69      this.name = name;
70      this.perm = permProperties;
71      this.primitive = primitive;
72    }
73  
74    /**
75     * Returns the value Primitive of this field.
76     */
77    public Primitive getPrimitive() {
78      return(primitive);
79    }
80  
81    public int getSize() {
82      if(primitive == null)
83        return(0);
84      return(primitive.getSize());
85    }
86  
87    /**
88     * Returns true if this field has sub fields of its own.
89     */
90    public boolean hasFields() {
91      return(fieldList.size() != 0);
92    }
93  
94  
95    /**
96     */
97    public void setProperty(String name, Primitive property) {
98      temp.put(name, property);
99    }
100 
101   public void addNote(Note note) {
102     notes.add(note);
103   }
104   
105 
106   /**
107    */
108   public Primitive getProperty(String name) {
109 
110     Primitive prim = (Primitive)temp.get(name);
111 
112     if(prim != null)
113       return(prim);
114 
115     prim = (Primitive)perm.get(name);
116     if(prim != null)
117       return(prim);
118 
119     return(null);
120   }
121 
122   
123   /**
124    * Adds a new field to the packet.
125    */
126   public void addField(Field field) {
127     fieldList.add(field);
128     fieldHash.put(field.getName(), field);
129   }
130 
131 
132   /**
133    * Returns the named field from the packet.
134    */
135   public Field getField(String name) {
136     return((Field)fieldHash.get(name));
137   }
138 
139   /**
140    * Returns the indexed field from the packet.
141    */
142   public Field getField(int index) {
143     return((Field)fieldList.get(index));
144   }
145 
146   /**
147    *  Returns the size of this packet. Size refers to number of fields
148    * decoded. Only fields that this codec understands are decoded. There may
149    * be additional undecoded fields of other protocoal still in the stack.
150    * @return Number of fields in the packet.
151    */
152   public int getFieldCount() {
153     return(fieldList.size());
154   }
155 
156 
157 
158   /**
159    *
160    */
161   public String getName() { 
162     if(getProperty(Field.COMMON) != null)
163       return((String)getProperty(Field.COMMON).getValue());
164     else
165       return(name); 
166   }
167 
168   public void setValueName(String name) {
169     this.valueName = name;
170 
171     StringPrimitive p = new StringPrimitive();
172     p.setValue(name);
173 
174     setProperty(VALUE_NAME, p);
175   }
176 
177   public String getValueName() {
178     return(valueName);
179   }
180 
181 
182 
183 
184   static String bits = "...........................................................................................................";
185   static String space = "                                                                                                           ";
186 
187   public String toString() {
188     return(toStringPre(new OutputBuffer(), "").toString());
189   }
190 
191   public OutputBuffer print(OutputBuffer s, String pre) {
192     return(toStringPre(new OutputBuffer(), pre));
193   }
194 
195   public OutputBuffer toStringPre(OutputBuffer s, String pre) {
196 
197 //    OutputBuffer s = new OutputBuffer();
198     String ap = "";
199 
200     boolean showname = (getProperty(Field.HIDENAME) == null);
201 
202 
203     PrimitiveDisplay display = (PrimitiveDisplay)primitive;
204 
205     s.append(pre);
206 
207     String n = "";
208     if(showname) {
209       n = getName() + " = ";
210     }
211 
212     pre += space.substring(0, n.length());
213     s.append(n);
214 
215 
216     if(getProperty(Field.HIDEVALUE) == null) {
217       if(fieldList.size() != 0)
218         s.append(display.toBinString());
219 
220       if(display.isMultiLine()) {
221         s.append(display.toString(pre, false)); // Prefix, Indent first line
222       }
223       else {
224         s.append(" ").append(display.toString());
225 
226         if(getProperty(Field.UNITS) != null) { // Check if we can display units with the value.
227           s.append(" (");
228 
229           if(getProperty(Field.UNITVALUE) != null) // Now see if we have a converted value using units above.
230             s.append(getProperty(Field.UNITVALUE)).append(" ");
231 
232 
233           s.append(getProperty(Field.UNITS)).append(")");
234         }
235       }
236 
237       if(valueName != null)
238          s.append(" \"").append(valueName).append("\"");
239 
240     }
241 
242     if(notes.size() > 0) {
243         s.append(" Note: ").append(notes);
244     }
245 
246       s.append("\n");
247 
248     int size = 0;
249     int max = getSize();
250 
251     if(display.isMultiLine())
252     for(int i = 0; i < fieldList.size(); i ++) {
253       FieldImpl field = (FieldImpl)getField(i);
254       field.toStringPre(s, pre);
255     }
256 
257     if(display.isMultiLine() == false)
258     for(int i = 0; i < fieldList.size(); i ++) {
259       FieldImpl field = (FieldImpl)getField(i);
260       size = field.getSize();
261 
262       field.toStringPost(s, pre + ap, (max  - ap.length() >= 0)?max - ap.length():0);
263 //      s.append(field.toStringPost(pre + ap, (max  - ap.length() >= 0)?max - ap.length():0));
264 
265       ap += bits.substring(0, size);
266     }
267 
268     return(s);
269   }
270 
271 
272   public boolean isMultiLine() {
273     if(primitive == null)
274       return false;
275 
276     return(((PrimitiveDisplay)primitive).isMultiLine());
277   }
278  
279   public OutputBuffer toStringPost(OutputBuffer s, String pre, int max) {
280 
281 //    OutputBuffer s = new OutputBuffer(pre);
282     s.append(pre);
283     String ap = "";
284 
285     PrimitiveDisplay display = (PrimitiveDisplay)primitive;
286 
287     s.append(display.toBinString());
288     s.append(bits.substring(0, (max - getSize() >= 0)?max - getSize():0));
289 
290     s.append(" ").append(display.toString());
291     s.append(" = [").append(getName()).append("]") ;
292 
293     if(valueName != null) 
294       s.append(" \"").append(valueName).append("\"");
295     s.append("\n");
296 
297     int size = 0;
298     int m = getSize();
299 
300     for(int i = 0; i < fieldList.size(); i ++) {
301       FieldImpl field = (FieldImpl)getField(i);
302       size = field.getSize();
303 
304       field.toStringPost(s, pre + ap, (m  - ap.length() >= 0)?(m - ap.length()):0);
305 //      s.append(field.toStringPost(pre + ap, (m  - ap.length() >= 0)?(m - ap.length()):0));
306 
307       ap += bits.substring(0, size);
308     }
309 
310     return(s);
311   }
312 
313   /**
314    * Test function for FieldImpl
315    * @param args command line arguments
316    */
317   public static void main(String [] args) {
318   }
319 
320 } /* END OF: FieldImpl */