/** * A simple Element class for storing the element name, attributes * and children. * * $Log: Element.java,v $ * Revision 1.4 1998/07/30 13:39:32 jsaarela * multiple internal references fixed, * properties without children fixed. * * Revision 1.3 1998/07/27 12:20:54 jsaarela * 1st distribution version logged in. * * * @author Janne Saarela */ package org.w3c.rdf; import java.util.*; import java.io.*; import org.xml.sax.AttributeList; import org.xml.sax.SAXException; public class Element { private String m_sName = null; private Hashtable m_attributes = null; private Vector m_children = new Vector(); private String m_sID = null; private String m_sBagID = null; private Hashtable m_nodes = new Hashtable (); private Element m_target = null; private boolean m_bDone = false; private static Vector s_resolveQueue = new Vector (); private static Hashtable s_hIDtable = new Hashtable (); public final static String RDFSCHEMA = new String ("http://www.w3.org/TR/WD-RDF-syntax"); private static int s_iReificationCounter = 0; public static void init () { s_iReificationCounter = 0; s_resolveQueue.removeAllElements (); s_hIDtable.clear (); } public Element (String sName, AttributeList al) throws SAXException { m_sName = sName; if (al != null) { int iLength = al.getLength (); if (al == null) { // System.out.println("[Attributes not available]"); } else { m_attributes = new Hashtable(); for (int x = 0; x < iLength; x++) { String aname = al.getName (x); String avalue = al.getValue (x); m_attributes.put (aname, avalue); } } } if (m_attributes != null) { String sResource = (String)m_attributes.get ("resource"); if (sResource != null && sResource.startsWith("#")) { Element.resolveLater (this); } String sAboutEach = (String)m_attributes.get ("aboutEach"); if (sAboutEach != null && sAboutEach.startsWith("#")) { Element.resolveLater (this); } String sAbout = (String)m_attributes.get ("about"); if (sAbout != null) { if (sAbout.startsWith("#")) { Element.resolveLater (this); } else { Element.registerResource (this); } } String sBagID = (String)m_attributes.get ("bagID"); if (sBagID != null) { Element.registerBag (this); bagID (sBagID); } String sId = (String)m_attributes.get ("ID"); if (sId != null) { Element.registerID (this); ID (sId); // default value } if (sId != null && sAbout != null) { throw new SAXException ("'ID' and 'about' attribute may not appear within the same Description block"); } } } public String name() { return m_sName; } public void addChild (Element e) { m_children.addElement (e); } public Enumeration children () { return m_children.elements(); } public Enumeration attributes () { return m_attributes.keys(); } public void addAttribute (String sName, String sValue) { if (sName == null || sValue == null) return; if (m_attributes == null) { m_attributes = new Hashtable (); } m_attributes.put (sName, sValue); } public void removeAttribute (String sName) { m_attributes.remove (sName); } public String getAttribute (String sName) { return (String)m_attributes.get (sName); } public String prefix () { int i = name().indexOf (':'); if (i > 0) { return name().substring (0, i); } else { return null; } } public String postfix () { int i = name().indexOf (':'); if (i > 0) { return name().substring (i + 1); } else { return null; } } public String expandName () { String sPrefix = prefix (); String sPostfix = postfix (); if (sPrefix != null) { String sNamespace = SiRPAC.namespace (sPrefix); return sNamespace + "#" + sPostfix; } else { return name(); } } public static void resolveLater (Element e) { s_resolveQueue.addElement (e); } public static void resolve () { for (int x = 0; x < s_resolveQueue.size(); x++) { Element e = (Element)s_resolveQueue.elementAt(x); String sAbout = e.getAttribute ("about"); if (sAbout != null) { if (sAbout.startsWith ("#")) sAbout = sAbout.substring (1); Element e2 = (Element)lookforNode(sAbout); if (e2 != null) { e.setTarget (e2); } } String sResource = e.getAttribute ("resource"); if (sResource != null) { if (sResource.startsWith ("#")) sResource = sResource.substring (1); Element e2 = (Element)lookforNode(sResource); if (e2 != null) { e.setTarget (e2); } } String sAboutEach = e.getAttribute ("aboutEach"); if (sAboutEach != null) { sAboutEach = sAboutEach.substring (1); Element e2 = (Element)lookforNode(sAboutEach); if (e2 != null) { e.setTarget (e2); } } } s_resolveQueue.removeAllElements(); } public static Element lookforNode (String sID) { if (sID == null) return null; else return (Element)s_hIDtable.get (sID); } public static void registerID (Element e) throws SAXException { if (e.getAttribute ("ID") != null) { if (s_hIDtable.get (e.getAttribute ("ID")) != null) throw new SAXException ("ID attribute value '"+e.getAttribute("ID")+"' not unique"); else s_hIDtable.put (e.getAttribute("ID"), e); } } public static void registerBag (Element e) { if (e.getAttribute ("bagID") != null) { s_hIDtable.put (e.getAttribute ("bagID"), e); } } public static void registerResource (Element e) { if (e.getAttribute ("about") != null) { s_hIDtable.put (e.getAttribute ("about"), e); } } public void setTarget (Element e) { m_target = e; } public Element target () { return m_target; } static public String newReificationID () { s_iReificationCounter++; return new String ("genid" + s_iReificationCounter); } public void ID (String sID) { m_sID = sID; } public String ID () { return m_sID; } public void bagID (String sBagID) { m_sBagID = sBagID; } public String bagID () { return m_sBagID; } public void linearize (int indent, PrintStream ps) { for (int x = 0; x < indent; x++) { ps.print (" "); } System.out.println ("Element "+name()); Enumeration e = children(); while (e.hasMoreElements()) { Element ele = (Element)e.nextElement(); ele.linearize (indent + 2, ps); } } public boolean done () { return m_bDone; } public void done (boolean b) { m_bDone = b; } }