Annotation of edoc-applet/com/exploringxml/xml/JSArray.java, revision 1.1.1.1

1.1       rogo        1: /* Copyright (c) 2000 Michael Claßen <mclassen@internet.com>
                      2:  *
                      3:  * This program is free software; you can redistribute it and/or
                      4:  * modify it under the terms of the GNU General Public License
                      5:  * as published by the Free Software Foundation; either version 2
                      6:  * of the License, or (at your option) any later version.
                      7:  *
                      8:  * This program is distributed in the hope that it will be useful,
                      9:  * but WITHOUT ANY WARRANTY; without even the implied warranty of
                     10:  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     11:  * GNU General Public License for more details.
                     12:  *
                     13:  * You should have received a copy of the GNU General Public License
                     14:  * along with this program; if not, write to the Free Software
                     15:  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
                     16:  *
                     17:  * $Id: JSArray.java,v 1.1.1.1 2003/06/13 14:59:38 rogo Exp $
                     18:  */
                     19: 
                     20: package com.exploringxml.xml;
                     21: 
                     22: import java.util.Hashtable;
                     23: import java.util.Vector;
                     24: 
                     25: /**
                     26:  * Mimics a Javascript array;
                     27:  * not completely generic because only specific properties are allowed
                     28:  * <p>would need Reflection to make it completely generic
                     29:  *
                     30:  * @author    Michael Claßen
                     31:  * @version   $Revision: 1.1.1.1 $
                     32:  */
                     33: public class JSArray {
                     34: 
                     35:   // underlying element holder
                     36:   public Vector v = new Vector();
                     37: 
                     38:   // constants for the currently supported object properties
                     39:   static final String Name = "name";
                     40:   static final String Attributes = "attributes";
                     41:   static final String Contents = "contents";
                     42:   static final String Value = "value";
                     43: 
                     44:   /**
                     45:    * gets the object with a certain index
                     46:    *
                     47:    * @param     idx the object's index in the array
                     48:    * @return    Object at the respective index
                     49:    */
                     50:   Object elementAt(int idx) {
                     51:     return v.elementAt(idx);
                     52:   }
                     53: 
                     54:   /**
                     55:    * sets the object with a certain index
                     56:    *
                     57:    * @param     val the object to be set in the array
                     58:    * @param     idx the object's index in the array
                     59:    */
                     60:   void setElementAt(Object val, int idx) {
                     61:     v.insertElementAt(val, idx);
                     62:   }
                     63: 
                     64:   /**
                     65:    * sets the object with a certain index
                     66:    *
                     67:    * @param     val the object' property to be set
                     68:    * @param     idx the object's index in the array
                     69:    * @param     prop the name of the object's property to be set;
                     70:    *            currently can only be: name, attributes, contents, value
                     71:    */
                     72:   void setElementAt(Object val, int idx, String prop) {
                     73:     Node n = (Node) v.elementAt(idx);
                     74:     if (prop == Name) {
                     75:       n.name = (String) val;
                     76:     }
                     77:     else if (prop == Attributes) {
                     78:       n.attributes = (Hashtable) val;
                     79:     }
                     80:     else if (prop == Contents) {
                     81:       n.contents = (JSArray) val;
                     82:     }
                     83:     else if (prop == Value) {
                     84:       n.value = (String) val;
                     85:     }
                     86:   }
                     87: 
                     88:   /**
                     89:    * returns the length / size of the array
                     90:    *
                     91:    * @return    the array length
                     92:    */
                     93: public  int length() {
                     94:     return v.size();
                     95:   }
                     96: 
                     97:   /**
                     98:    * splits a string into an array of strings, broken at a distinct separator
                     99:    *
                    100:    * @param     str the String to be split
                    101:    * @param     sep the seperator at which to split
                    102:    */
                    103:   void split(String str, String sep) {
                    104:     v.removeAllElements();
                    105:     int oldidx = 0, newidx, skip = sep.length();
                    106:     while((newidx = str.indexOf(sep, oldidx)) != -1) {
                    107:       v.addElement(str.substring(oldidx, newidx));
                    108:       oldidx = newidx + skip;
                    109:     }
                    110:     v.addElement(str.substring(oldidx));
                    111:   }
                    112: 
                    113:   /**
                    114:    * join this array into one string delimited by a separator
                    115:    *
                    116:    * @param     sep the seperator to put in between the array elements
                    117:    * @return    the joined String
                    118:    */
                    119:   String join(String sep) {
                    120:     int no = 0;
                    121:     StringBuffer sb = new StringBuffer();
                    122:     while (no < v.size()) {
                    123:       sb.append(v.elementAt(no));
                    124:       if (++no < v.size()) sb.append(sep);
                    125:     }
                    126:     return sb.toString();
                    127:   }
                    128: 
                    129: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>