Annotation of java/classes/org/w3c/rdf/Element.java, revision 1.6

1.1       jsaarela    1: /**
                      2:  * A simple Element class for storing the element name, attributes
                      3:  * and children.
                      4:  *
1.4       jsaarela    5:  * $Log: Element.java,v $
1.6     ! jsaarela    6:  * Revision 1.5  1998/08/12 07:54:55  jsaarela
        !             7:  * Namespace management now corresponds with the W3C Working
        !             8:  * Draft dated 2-Aug-98.
        !             9:  *
1.5       jsaarela   10:  * Revision 1.4  1998/07/30 13:39:32  jsaarela
                     11:  * multiple internal references fixed,
                     12:  * properties without children fixed.
                     13:  *
1.4       jsaarela   14:  * Revision 1.3  1998/07/27 12:20:54  jsaarela
                     15:  * 1st distribution version logged in.
                     16:  *
1.3       jsaarela   17:  *
1.1       jsaarela   18:  * @author Janne Saarela
                     19:  */
                     20: package org.w3c.rdf;
                     21: 
                     22: import java.util.*;
                     23: import java.io.*;
                     24: import org.xml.sax.AttributeList;
1.4       jsaarela   25: import org.xml.sax.SAXException;
1.1       jsaarela   26: 
                     27: public class Element
                     28: {
1.5       jsaarela   29:   private String       m_sNamespace = null;
1.1       jsaarela   30:   private String       m_sName = null;
1.6     ! jsaarela   31:   private Hashtable    m_attributes = new Hashtable();
        !            32:   private Hashtable    m_attributeNamespaces = new Hashtable();
1.1       jsaarela   33:   private Vector       m_children = new Vector();
1.2       jsaarela   34:   private String       m_sID = null;
1.4       jsaarela   35:   private String       m_sBagID = null;
1.1       jsaarela   36:   private Hashtable    m_nodes = new Hashtable ();
                     37:   private Element      m_target = null;
1.2       jsaarela   38:   private boolean      m_bDone = false;
1.1       jsaarela   39: 
                     40:   private static Vector        s_resolveQueue = new Vector ();
                     41:   private static Hashtable s_hIDtable = new Hashtable ();
                     42: 
1.2       jsaarela   43:   private static int   s_iReificationCounter = 0;
                     44: 
                     45:   public static void init () {
                     46:        s_iReificationCounter = 0;
1.3       jsaarela   47:        s_resolveQueue.removeAllElements ();
                     48:        s_hIDtable.clear ();
1.2       jsaarela   49:   }
                     50: 
1.5       jsaarela   51:   public Element (String sNamespace,
                     52:                  String sName,
                     53:                  AttributeList al) throws SAXException {
                     54:     m_sNamespace = sNamespace;
1.1       jsaarela   55:     m_sName = sName;
                     56: 
                     57:     if (al != null) {
                     58:        int iLength = al.getLength ();
                     59:       if (al == null) {
1.3       jsaarela   60:          // System.out.println("[Attributes not available]");
1.1       jsaarela   61:       } else {
                     62:        for (int x = 0; x < iLength; x++) {
1.6     ! jsaarela   63:          String aName = al.getName (x);
        !            64:          String aValue = al.getValue (x);
        !            65:          String aNamespace = al.getType (x);
1.1       jsaarela   66: 
1.6     ! jsaarela   67:          m_attributes.put (aName, aValue);
        !            68:          m_attributeNamespaces.put (aName, aNamespace);
1.1       jsaarela   69:        }
                     70:       }
                     71:     }
                     72: 
                     73:     if (m_attributes != null) {
1.3       jsaarela   74:       String sResource = (String)m_attributes.get ("resource");
1.4       jsaarela   75:       if (sResource != null && sResource.startsWith("#")) {
                     76:          Element.resolveLater (this);
1.2       jsaarela   77:       }
                     78:       String sAboutEach = (String)m_attributes.get ("aboutEach");
                     79:       if (sAboutEach != null && sAboutEach.startsWith("#")) {
1.4       jsaarela   80:          Element.resolveLater (this);
1.1       jsaarela   81:       }
1.3       jsaarela   82:       String sAbout = (String)m_attributes.get ("about");
                     83:       if (sAbout != null) {
                     84:          if (sAbout.startsWith("#")) {
                     85:              Element.resolveLater (this);
                     86:          } else {
                     87:              Element.registerResource (this);
                     88:          }
                     89:       }
1.4       jsaarela   90:       String sBagID = (String)m_attributes.get ("bagID");
                     91:       if (sBagID != null) {
1.1       jsaarela   92:        Element.registerBag (this);
1.4       jsaarela   93:        bagID (sBagID);
1.1       jsaarela   94:       }
1.2       jsaarela   95:       String sId = (String)m_attributes.get ("ID");
                     96:       if (sId != null) {
                     97:        Element.registerID (this);
1.3       jsaarela   98:        ID (sId); // default value
                     99:       }
                    100: 
                    101:       if (sId != null && sAbout != null) {
1.4       jsaarela  102:          throw new SAXException ("'ID' and 'about' attribute may not appear within the same Description block");
1.2       jsaarela  103:       }
1.1       jsaarela  104:     }
                    105:   }
                    106: 
                    107:   public String name() {
                    108:     return m_sName;
                    109:   }
                    110: 
1.5       jsaarela  111:   public String namespace () {
                    112:     return m_sNamespace;
                    113:   }
                    114: 
1.1       jsaarela  115:   public void addChild (Element e) {
                    116:     m_children.addElement (e);
                    117:   }
                    118: 
                    119:   public Enumeration children () {
                    120:     return m_children.elements();
                    121:   }
                    122: 
1.2       jsaarela  123:   public Enumeration attributes () {
                    124:       return m_attributes.keys();
                    125:   }
                    126: 
                    127:   public void addAttribute (String sName, String sValue) {
1.3       jsaarela  128:       if (sName == null ||
                    129:          sValue == null)
                    130:          return;
1.2       jsaarela  131:       if (m_attributes == null) {
                    132:          m_attributes = new Hashtable ();
                    133:       }
                    134:       m_attributes.put (sName, sValue);
                    135:   }
                    136: 
1.3       jsaarela  137:   public void removeAttribute (String sName) {
                    138:       m_attributes.remove (sName);
                    139:   }
                    140: 
1.1       jsaarela  141:   public String getAttribute (String sName) {
                    142:     return (String)m_attributes.get (sName);
                    143:   }
                    144: 
1.6     ! jsaarela  145:   public String getAttributeNamespace (String sName) {
        !           146:     return (String)m_attributeNamespaces.get (sName);
        !           147:   }
        !           148: 
1.1       jsaarela  149:   public String expandName () {
1.5       jsaarela  150:     if (m_sNamespace == null ||
                    151:        m_sNamespace.trim().length() == 0) {
                    152:        return m_sName;
1.3       jsaarela  153:     } else {
1.6     ! jsaarela  154:        return m_sNamespace + m_sName;
1.3       jsaarela  155:     }
1.1       jsaarela  156:   }
                    157: 
                    158:   public static void resolveLater (Element e) {
                    159:     s_resolveQueue.addElement (e);
                    160:   }
                    161: 
                    162:   public static void resolve () {
                    163:     for (int x = 0; x < s_resolveQueue.size(); x++) {
                    164:       Element e = (Element)s_resolveQueue.elementAt(x);
1.3       jsaarela  165: 
1.2       jsaarela  166:       String sAbout = e.getAttribute ("about");
                    167:       if (sAbout != null) {
1.3       jsaarela  168:          if (sAbout.startsWith ("#"))
                    169:              sAbout = sAbout.substring (1);
1.2       jsaarela  170:          Element e2 = (Element)lookforNode(sAbout);
                    171:          if (e2 != null) {
                    172:              e.setTarget (e2);
1.3       jsaarela  173:          }     
                    174:       }
                    175: 
                    176:       String sResource = e.getAttribute ("resource");
                    177:       if (sResource != null) {
1.4       jsaarela  178:          if (sResource.startsWith ("#"))
                    179:              sResource = sResource.substring (1);
1.3       jsaarela  180:          Element e2 = (Element)lookforNode(sResource);
                    181:          if (e2 != null) {
                    182:              e.setTarget (e2);
1.2       jsaarela  183:          }     
                    184:       }
1.3       jsaarela  185: 
1.2       jsaarela  186:       String sAboutEach = e.getAttribute ("aboutEach");
                    187:       if (sAboutEach != null) {
                    188:          sAboutEach = sAboutEach.substring (1);
                    189:          Element e2 = (Element)lookforNode(sAboutEach);
1.1       jsaarela  190:          if (e2 != null) {
                    191:              e.setTarget (e2);
                    192:          }     
                    193:       }
                    194:     }
                    195:     s_resolveQueue.removeAllElements();
                    196:   }
                    197: 
1.4       jsaarela  198:   public static Element lookforNode (String sID) {
                    199:       if (sID == null)
                    200:          return null;
                    201:       else
                    202:          return (Element)s_hIDtable.get (sID);
1.1       jsaarela  203:   }
                    204: 
1.4       jsaarela  205:   public static void registerID (Element e) throws SAXException {
1.2       jsaarela  206:     if (e.getAttribute ("ID") != null) {
1.4       jsaarela  207:        if (s_hIDtable.get (e.getAttribute ("ID")) != null)
                    208:            throw new SAXException ("ID attribute value '"+e.getAttribute("ID")+"' not unique");
                    209:        else
                    210:            s_hIDtable.put (e.getAttribute("ID"), e);
1.1       jsaarela  211:     }
                    212:   }
                    213: 
                    214:   public static void registerBag (Element e) {
1.2       jsaarela  215:     if (e.getAttribute ("bagID") != null) {
                    216:       s_hIDtable.put (e.getAttribute ("bagID"), e);
1.1       jsaarela  217:     }
                    218:   }
1.3       jsaarela  219: 
                    220:   public static void registerResource (Element e) {
                    221:     if (e.getAttribute ("about") != null) {
                    222:       s_hIDtable.put (e.getAttribute ("about"), e);
                    223:     }
                    224:   }
                    225: 
1.1       jsaarela  226: 
                    227:   public void setTarget (Element e) {
                    228:     m_target = e;
                    229:   }
                    230: 
                    231:   public Element target () {
                    232:     return m_target;
                    233:   }
                    234: 
                    235:   static public String newReificationID () {
                    236:     s_iReificationCounter++;
                    237:     return  new String ("genid" + s_iReificationCounter);
                    238:   }
                    239: 
1.2       jsaarela  240:   public void ID (String sID) {
                    241:     m_sID = sID;
1.1       jsaarela  242:   }
                    243: 
1.2       jsaarela  244:   public String ID () {
                    245:     return m_sID;
1.4       jsaarela  246:   }
                    247: 
                    248:   public void bagID (String sBagID) {
                    249:     m_sBagID = sBagID;
                    250:   }
                    251: 
                    252:   public String bagID () {
                    253:     return m_sBagID;
1.1       jsaarela  254:   }
                    255: 
                    256:   public void linearize (int indent, PrintStream ps) {
                    257:     for (int x = 0; x < indent; x++) {
                    258:       ps.print (" ");
                    259:     }
                    260:     System.out.println ("Element "+name());
                    261:     Enumeration e = children();
                    262:     while (e.hasMoreElements()) {
                    263:       Element ele = (Element)e.nextElement();
                    264:       ele.linearize (indent + 2, ps);
                    265:     }
1.2       jsaarela  266:   }
                    267: 
                    268:   public boolean done () {
                    269:       return m_bDone;
                    270:   }
                    271: 
                    272:   public void done (boolean b) {
                    273:       m_bDone = b;
1.1       jsaarela  274:   }
                    275: }

Webmaster