File:  [Public] / java / classes / org / w3c / rdf / Element.java
Revision 1.2: download - view: text, annotated - select for diffs
Wed Jun 17 09:17:04 1998 UTC (26 years ago) by jsaarela
Branches: MAIN
CVS tags: R_2_0_beta_2_1, HEAD
Updated the translator to manage new RDF constructs such as
distributive referents and changed attribute names.

/**
 * A simple Element class for storing the element name, attributes
 * and children.
 *
 * @author Janne Saarela
 */
package org.w3c.rdf;

import java.util.*;
import java.io.*;
import org.xml.sax.AttributeList;

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 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/schemas/RDFschema");

  private static int	s_iReificationCounter = 0;

  public static void init () {
	s_iReificationCounter = 0;
  }

  public Element (String sName, AttributeList al) {
    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 sAbout = (String)m_attributes.get ("about");
      if (sAbout != null && sAbout.startsWith("#")) {
	Element.resolveLater (this);
      }
      String sAboutEach = (String)m_attributes.get ("aboutEach");
      if (sAboutEach != null && sAboutEach.startsWith("#")) {
	Element.resolveLater (this);
      }
      String sBagId = (String)m_attributes.get ("bagID");
      if (sBagId != null) {
	Element.registerBag (this);
      }
      String sId = (String)m_attributes.get ("ID");
      if (sId != null) {
	Element.registerID (this);
      }
    }
  }

  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 (m_attributes == null) {
	  m_attributes = new Hashtable ();
      }
      m_attributes.put (sName, sValue);
  }

  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 ();

    String sNamespace = RDF2LP2.namespace (sPrefix);

    return sNamespace + "#" + sPostfix;
  }

  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) {
	  sAbout = sAbout.substring (1);
	  Element e2 = (Element)lookforNode(sAbout);
	  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;
    return (Element)s_hIDtable.get (sId);
  }

  public static void registerID (Element e) {
    if (e.getAttribute ("ID") != null) {
      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 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 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;
  }
}

Webmaster