View Javadoc

1   /*
2    * $Id: ModelCleanupVisitor.java,v 1.11 2005/07/12 19:57:59 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.List;
11  import java.util.Map;
12  
13  import com.hp.hpl.jena.rdf.model.Model;
14  import com.hp.hpl.jena.rdf.model.Resource;
15  import com.hp.hpl.jena.rdf.model.Statement;
16  
17  /***
18   * A visitor to tidy the Jena model prior to node removal.
19   * Resource and Statement instances associated with any node encountered by
20   * visit() are removed from the model.
21   * @author jlerner
22   */
23  public class ModelCleanupVisitor implements IOASTNodeVisitor
24  {
25     /***
26      * The model to clean.
27      */
28     protected Model _model;
29     /***
30      * A map containing lists of nodes associated with Jena resources.
31      */
32     protected Map _resources;
33     /***
34      * A map containing lists of nodes associated with Jena statements.
35      */
36     protected Map _statements;
37     
38     /***
39      * Creates a new ModelCleanupVisitor to run against a specific model.
40      * @param model The model to remove Resources and Statements from
41      * @param statementToNode Maps resources to lists of referencing nodes
42      * @param resourceToNode Maps statements to lists of referencing nodes
43      */
44     public ModelCleanupVisitor(Model model, Map resourceToNode, Map statementToNode)
45     {
46        _model = model;
47        _resources = resourceToNode;
48        _statements = statementToNode;
49     }
50  
51     /* (non-Javadoc)
52      * @see com.bbn.swede.core.dom.IOASTNodeVisitor#visit(com.bbn.swede.core.dom.OASTNode)
53      */
54     public boolean visit(OASTNode node)
55     {
56        if (node instanceof ClassNode)
57        {
58           ClassNode cn = (ClassNode) node;
59           
60           Resource res = cn.getAssociatedResource();
61           removeResource(res, node);
62           Statement stmt = cn.getImplicitTypeStatement();
63           removeStatement(stmt, node);
64           
65           cn.removeJenaResource(_model);
66        }
67        else if (node instanceof PropertyNode)
68        {
69           PropertyNode pn = (PropertyNode) node;
70           
71           Resource res = pn.getReification();
72           removeResource(res, node);
73           Statement stmt = pn.getAssociatedStatement();
74           removeStatement(stmt, node);
75           
76           pn.removeJenaStatement(_model);
77        }
78        else if (node instanceof GenericAttribute)
79        {
80           GenericAttribute ga = (GenericAttribute) node;
81           
82           Statement stmt = ga.getAssociatedStatement();
83           removeStatement(stmt, node);
84           
85           ga.removeJenaStatement(_model);
86        }
87        return true;
88     }
89  
90     /***
91      * Removes a resource/node pair from the resource map.
92      * @param res The resource
93      * @param node The node.  Must not be null.
94      */
95     private void removeResource(Resource res, OASTNode node)
96     {
97        if (res == null)
98        {
99           return;
100       }
101       
102       List l = (List)_resources.get(res);
103       if (l != null)
104       {
105          l.remove(node);
106       }
107    }
108 
109    /***
110     * Removes a statement/node pair from the resource map.
111     * @param stmt The statement
112     * @param node The node.  Must not be null.
113     */
114    private void removeStatement(Statement stmt, OASTNode node)
115    {
116       if (stmt == null)
117       {
118          return;
119       }
120       
121       List l = (List)_statements.get(stmt);
122       if (l != null)
123       {
124          l.remove(node);
125       }
126    }
127 }