View Javadoc

1   /*
2    * $Id: GenericAttribute.java,v 1.11 2005/07/12 17:07:00 jlerner Exp $
3    *
4    * Copyright (c) 1999-2004, BBN Technologies, LLC.
5    * All rights reserved.
6    * http://www.daml.org/legal/opensource/bbn_license.html
7    */
8   package com.bbn.swede.core.dom;
9   
10  import java.util.Map;
11  
12  import com.hp.hpl.jena.rdf.model.Model;
13  import com.hp.hpl.jena.rdf.model.Property;
14  import com.hp.hpl.jena.rdf.model.Resource;
15  import com.hp.hpl.jena.rdf.model.Statement;
16  
17  /***
18   * <p>Attribute node implementation for general non-language attributes.  Generic
19   * attributes are essentially a shorthand for literal properties.  For example,
20   * this:</p>
21   *
22   * <pre>
23   * &lt;owl:Thing foo="bar"/&gt;
24   * </pre>
25   *
26   * <p>Is equivalent to this:</p>
27   *
28   * <pre>
29   * &lt;owl:Thing&gt;
30   *   &lt;foo&gt;bar&lt;/foo&gt;
31   * &lt;/owl:Thing&gt;
32   * </pre>
33   * @author jlerner
34   */
35  public class GenericAttribute extends AttributeNode
36  {
37     private String _sQName;
38     /***
39      * Creates a new generic attribute.
40      * @param sQName The qualified name of the attribute.
41      * @param sValue The value of the attribute.
42      */
43     public GenericAttribute(String sQName, String sValue)
44     {
45        super(sValue);
46        _sQName = sQName;
47  
48        //reinitialize the regions since the qname wasn't set yet when
49        //AttributeNode's constructor tried to do it
50        initRegions();
51     }
52  
53     /*
54      *  (non-Javadoc)
55      * @see com.bbn.swede.core.dom.OASTNode#getNodeType()
56      */
57     public int getNodeType()
58     {
59        return GENERICATTRIBUTE;
60     }
61  
62     /*
63      *  (non-Javadoc)
64      * @see com.bbn.swede.core.dom.OASTNode#getUri()
65      */
66     public String getUri()
67     {
68        String sNS = getNamespace();
69        if (sNS.length() == 0)
70        {
71           String sDefault = getDefaultNamespace();
72           return (sDefault == null ? getName() : sDefault + getName());
73        }
74        Map map = getNamespaces();
75        String sUri = (String) map.get(sNS);
76        if (sUri == null)
77        {
78           return null;
79        }
80        return sUri + getName();
81     }
82  
83     /*
84      *  (non-Javadoc)
85      * @see com.bbn.swede.core.dom.OASTNode#getNamespace()
86      */
87     public String getNamespace()
88     {
89        int iPos = _sQName.indexOf(':');
90        if (iPos < 0)
91        {
92           return "";
93        }
94        return _sQName.substring(0, iPos);
95     }
96  
97     /***
98      * Sets the qualified name of the attribute.
99      * @param sQName The new QName.
100     */
101    public void setQName(String sQName)
102    {
103       _sQName = sQName;
104    }
105 
106    /***
107     * Retrieves the qualified name of the attribute.
108     * @return The attribute's QName.
109     */
110    public String getQName()
111    {
112       return _sQName;
113    }
114 
115    /*
116     *  (non-Javadoc)
117     * @see com.bbn.swede.core.dom.OASTNode#getName()
118     */
119    public String getName()
120    {
121       int iPos = _sQName.indexOf(':');
122       if (iPos < 0)
123       {
124          return _sQName;
125       }
126       return _sQName.substring(iPos + 1);
127    }
128 
129    private Statement _stmtAssoc;
130    /***
131     * Associates a Jena statement with the attribute.
132     * @param stmt The statement.
133     */
134    public void setAssociatedStatement(Statement stmt)
135    {
136       _stmtAssoc = stmt;
137    }
138 
139    /***
140     * Retrieves the Jena statement associated with the attribute.
141     * @return The associated statement, or <code>null</code> if no statement
142     *         is associated.
143     */
144    public Statement getAssociatedStatement()
145    {
146       return _stmtAssoc;
147    }
148 
149    /***
150     * Creates a Jena statement to represent the generic attribute.  The
151     * resulting statement will be of the form [parentResource,getQName(),
152     * getValue()].
153     * @param model The Jena model in which to create the statement.
154     * @return The created statement, or <code>null</code> if no statement could
155     *         be created.
156     */
157    public Statement createJenaStatement(Model model)
158    {
159       if (!(_nodeParent instanceof ClassNode))
160       {
161          return null;
162       }
163       Resource resSubject = ((ClassNode) _nodeParent).getAssociatedResource();
164       if (resSubject == null)
165       {
166          return null;
167       }
168 
169       String s = getUri();
170       if (s == null)
171       {
172          return null;
173       }
174       Property p = model.getProperty(s);
175 
176       com.hp.hpl.jena.rdf.model.Literal l = model.createLiteral(_sValue);
177 
178       Statement stmt = model.createStatement(resSubject, p, l);
179       setAssociatedStatement(stmt);
180       model.add(stmt);
181       return stmt;
182    }
183 
184    /***
185     * Removes this attribute's associated statement from the Jena model.  If
186     * the same statement is specified by another node in the document, it will
187     * not be removed.  If  there is no statement associated with this node, 
188     * nothing happens.
189     * @param model The model to remove the statement from.
190     */
191    public void removeJenaStatement(Model model)
192    {
193       if (getAssociatedStatement() == null)
194       {
195          return;
196       }
197       Statement stmt = getAssociatedStatement();
198       setAssociatedStatement(null);
199       if (stmt != null && getOWLAbstractSyntaxTree().getNode(stmt) == null)
200       {
201          //Nothing else is using this statement, it's safe to remove it.
202          model.remove(stmt);
203       }
204       //Otherwise, leave it there, it'll get removed when the last copy of it
205       //is taken out of the document.
206    }
207 }