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

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.10    ! jsaarela    6:  * Revision 1.9  1998/12/15 17:00:44  jsaarela
        !             7:  * New distribution release V1.7 on 15-Dec-98.
        !             8:  *
1.9       jsaarela    9:  * Revision 1.8  1998/10/09 17:26:50  jsaarela
                     10:  * Parser conformance to RDF Model&Syntax dated 19981008
                     11:  *
1.8       jsaarela   12:  * Revision 1.7  1998/09/08 15:54:01  jsaarela
                     13:  * Distribution release V1.4 - aboutEachPrefix added, namespace management
                     14:  * improved.
                     15:  *
1.7       jsaarela   16:  * Revision 1.6  1998/08/28 10:03:15  jsaarela
                     17:  * Distribution release 1.3 on 28-Aug-98.
                     18:  *
1.6       jsaarela   19:  * Revision 1.5  1998/08/12 07:54:55  jsaarela
                     20:  * Namespace management now corresponds with the W3C Working
                     21:  * Draft dated 2-Aug-98.
                     22:  *
1.5       jsaarela   23:  * Revision 1.4  1998/07/30 13:39:32  jsaarela
                     24:  * multiple internal references fixed,
                     25:  * properties without children fixed.
                     26:  *
1.4       jsaarela   27:  * Revision 1.3  1998/07/27 12:20:54  jsaarela
                     28:  * 1st distribution version logged in.
                     29:  *
1.3       jsaarela   30:  *
1.1       jsaarela   31:  * @author Janne Saarela
                     32:  */
                     33: package org.w3c.rdf;
                     34: 
1.10    ! jsaarela   35: import java.net.URL;
1.1       jsaarela   36: import java.util.*;
                     37: import java.io.*;
                     38: import org.xml.sax.AttributeList;
1.4       jsaarela   39: import org.xml.sax.SAXException;
1.1       jsaarela   40: 
                     41: public class Element
                     42: {
1.7       jsaarela   43:     private String     m_sName = null;
                     44:     private Hashtable  m_attributes = new Hashtable();
                     45:     private Vector     m_children = new Vector();
1.10    ! jsaarela   46:     private String     m_sResource = null;
1.7       jsaarela   47:     private String     m_sID = null;
                     48:     private String     m_sBagID = null;
1.10    ! jsaarela   49:     private String     m_sAbout = null;
        !            50:     private String     m_sAboutEach = null;
        !            51:     private String     m_sAboutEachPrefix = null;
1.7       jsaarela   52:     private Vector     m_vTargets = new Vector ();
                     53:     private boolean    m_bDone = false;
1.9       jsaarela   54:     private String     m_sPrefix = null;
1.7       jsaarela   55: 
                     56:     public Element (String sName, AttributeList al) throws SAXException {
                     57:        m_sName = sName;
                     58: 
                     59:        if (al != null) {
                     60:            int iLength = al.getLength ();
                     61:            if (al == null) {
                     62:                // System.out.println("[Attributes not available]");
                     63:            } else {
                     64:                for (int x = 0; x < iLength; x++) {
                     65:                    String aName = al.getName (x);
                     66:                    String aValue = al.getValue (x);
                     67: 
                     68:                    m_attributes.put (aName, aValue);
                     69:                }
                     70:            }
1.1       jsaarela   71:        }
                     72:     }
                     73: 
1.7       jsaarela   74:     public String name() {
1.5       jsaarela   75:        return m_sName;
1.9       jsaarela   76:     }
                     77: 
                     78:     public void prefix (String sPrefix) {
                     79:        m_sPrefix = sPrefix;
                     80:     }
                     81: 
                     82:     public String prefix () {
                     83:        if (m_sPrefix != null)
                     84:            return m_sPrefix + ":";
                     85:        else
                     86:            return "";
1.3       jsaarela   87:     }
1.1       jsaarela   88: 
1.7       jsaarela   89:     public void addChild (Element e) {
                     90:        m_children.addElement (e);
1.1       jsaarela   91:     }
                     92: 
1.7       jsaarela   93:     public Enumeration children () {
                     94:        return m_children.elements();
1.1       jsaarela   95:     }
1.3       jsaarela   96: 
1.7       jsaarela   97:     public Enumeration attributes () {
                     98:        return m_attributes.keys();
1.3       jsaarela   99:     }
                    100: 
1.7       jsaarela  101:     public void addAttribute (String sName, String sValue) {
                    102:        if (sName == null ||
                    103:            sValue == null)
                    104:            return;
                    105:        m_attributes.put (sName, sValue);
                    106:     }
1.1       jsaarela  107: 
1.7       jsaarela  108:     public void removeAttribute (String sName) {
                    109:        m_attributes.remove (sName);
                    110:     }
1.1       jsaarela  111: 
1.7       jsaarela  112:     public String getAttribute (String sName) {
                    113:        return (String)m_attributes.get (sName);
                    114:     }
1.1       jsaarela  115: 
1.7       jsaarela  116:     public String getAttribute (String sNamespace, String sName) {
                    117:        return (String)m_attributes.get (sNamespace+sName);
                    118:     }
1.1       jsaarela  119: 
1.7       jsaarela  120:     public void addTarget (Element e) {
                    121:        m_vTargets.addElement (e);
                    122:     }
1.1       jsaarela  123: 
1.7       jsaarela  124:     public Enumeration targets () {
                    125:        return m_vTargets.elements();
                    126:     }
1.4       jsaarela  127: 
1.7       jsaarela  128:     public Element target () {
                    129:        if (m_vTargets.size() == 0)
                    130:            return null;
                    131:        else
                    132:            return (Element)m_vTargets.elementAt(0);
                    133:     }
1.4       jsaarela  134: 
1.10    ! jsaarela  135:     public void resource (String sResource) {
        !           136:        m_sResource = sResource;
        !           137:     }
        !           138: 
        !           139:     public void resource (String sResource, String sContext) {
        !           140:        m_sResource = makeAbsolute (sResource, sContext);
        !           141:     }
        !           142: 
        !           143:     public String resource () {
        !           144:        return m_sResource;
        !           145:     }
        !           146: 
1.7       jsaarela  147:     public void ID (String sID) {
                    148:        m_sID = sID;
                    149:     }
                    150: 
1.10    ! jsaarela  151:     public void ID (String sID, String sContext) {
        !           152:        m_sID = makeAbsolute (sID, sContext);
        !           153:     }
        !           154: 
1.7       jsaarela  155:     public String ID () {
                    156:        return m_sID;
                    157:     }
                    158: 
                    159:     public void bagID (String sBagID) {
                    160:        m_sBagID = sBagID;
                    161:     }
1.1       jsaarela  162: 
1.10    ! jsaarela  163: 
        !           164:     public void bagID (String sBagID, String sContext) {
        !           165:        m_sBagID = makeAbsolute (sBagID, sContext);
        !           166:     }
        !           167: 
1.7       jsaarela  168:     public String bagID () {
                    169:        return m_sBagID;
1.1       jsaarela  170:     }
1.7       jsaarela  171: 
1.10    ! jsaarela  172:     public void about (String sAbout) {
        !           173:        m_sAbout = sAbout;
        !           174:     }
        !           175: 
        !           176:     public void about (String sAbout, String sContext) {
        !           177:        m_sAbout = makeAbsolute (sAbout, sContext);
        !           178:     }
        !           179: 
        !           180:     public String about () {
        !           181:        return m_sAbout;
        !           182:     }
        !           183: 
        !           184:     public void aboutEach (String sAboutEach) {
        !           185:        m_sAboutEach = sAboutEach;
        !           186:     }
        !           187: 
        !           188:     public void aboutEach (String sAboutEach, String sContext) {
        !           189:        m_sAboutEach = makeAbsolute (sAboutEach, sContext);
        !           190:     }
        !           191: 
        !           192:     public String aboutEach () {
        !           193:        return m_sAboutEach;
        !           194:     }
        !           195: 
        !           196:     public void aboutEachPrefix (String sAboutEachPrefix) {
        !           197:        m_sAboutEachPrefix = sAboutEachPrefix;
        !           198:     }
        !           199: 
        !           200:     public void aboutEachPrefix (String sAboutEachPrefix, String sContext) {
        !           201:        m_sAboutEachPrefix = makeAbsolute (sAboutEachPrefix, sContext);
        !           202:     }
        !           203: 
        !           204:     public String aboutEachPrefix () {
        !           205:        return m_sAboutEachPrefix;
        !           206:     }
        !           207: 
1.7       jsaarela  208:     public void linearize (int indent, PrintStream ps) {
                    209:        for (int x = 0; x < indent; x++) {
                    210:            ps.print (" ");
                    211:        }
1.8       jsaarela  212:        System.out.print ("Element "+name()+" (");
                    213: 
                    214:        Enumeration eKeys = m_attributes.keys ();
                    215:        while (eKeys.hasMoreElements()) {
                    216:            String sName = (String)eKeys.nextElement ();
                    217:            String sValue = (String)m_attributes.get (sName);
                    218:            System.out.print (" "+sName+"="+sValue);
                    219:        }
                    220:        System.out.print (")\n");
                    221: 
1.7       jsaarela  222:        Enumeration e = children();
                    223:        while (e.hasMoreElements()) {
                    224:            Element ele = (Element)e.nextElement();
                    225:            ele.linearize (indent + 2, ps);
                    226:        }
1.1       jsaarela  227:     }
1.2       jsaarela  228: 
1.7       jsaarela  229:     public boolean done () {
                    230:        return m_bDone;
                    231:     }
1.2       jsaarela  232: 
1.7       jsaarela  233:     public void done (boolean b) {
                    234:        m_bDone = b;
1.10    ! jsaarela  235:     }
        !           236: 
        !           237:     /**
        !           238:      * Private methods for this class only
        !           239:      */
        !           240: 
        !           241:     private String     makeAbsolute (String sURI, String context) {
        !           242:        String sResult = new String ();
        !           243:        if (sURI != null &&
        !           244:            context != null) {
        !           245: 
        !           246:            /**
        !           247:             * If sURI has .. then it indicates relative URI which
        !           248:             * we must make absolute
        !           249:             */
        !           250:            if (sURI.startsWith ("..")) {
        !           251:                try {
        !           252:                    URL absoluteURL = new URL (new URL(context), sURI);
        !           253:                    sResult = absoluteURL.toString();
        !           254:                } catch(Exception e) {
        !           255:                    System.err.println("RDF Resource - cannot combine " + 
        !           256:                                       context + " with " +sURI);
        !           257:                }
        !           258:            } else {
        !           259:                /**
        !           260:                 * If sURI is an absolute URI, don't bother
        !           261:                 * with it
        !           262:                 */
        !           263:                try {
        !           264:                    URL absoluteURL = new URL (sURI);
        !           265:                    sResult = absoluteURL.toString();
        !           266: 
        !           267:                } catch(Exception e) {
        !           268:                    /**
        !           269:                     * Well, the sURI wasn't absolute either,
        !           270:                     * try making absolute with the context
        !           271:                     */
        !           272:                    if (sURI.indexOf ('/') > -1) {
        !           273:                        try {
        !           274:                            URL absoluteURL = new URL (new URL(context), sURI);
        !           275:                            sResult = absoluteURL.toString();
        !           276:                        } catch (Exception e2) {
        !           277:                            sResult = context + "#" + sURI;
        !           278:                        }
        !           279:                    } else {
        !           280:                        sResult = context + "#" + sURI;
        !           281:                    }
        !           282:                }
        !           283:            }
        !           284:            return sResult;
        !           285:        } else {
        !           286:            return sURI;
        !           287:        }
1.7       jsaarela  288:     }
1.1       jsaarela  289: }

Webmaster