1 5 6 package com.hp.hpl.jena.db.impl; 7 8 import com.hp.hpl.jena.graph.*; 9 import com.hp.hpl.jena.util.iterator.*; 10 import com.hp.hpl.jena.vocabulary.DB; 11 12 import java.util.*; 13 14 32 public class DBPropGraph extends DBProp { 33 34 public static Node_URI graphName = (Node_URI)DB.graphName.getNode(); 35 public static Node_URI graphType = (Node_URI)DB.graphType.getNode(); 36 public static Node_URI graphLSet = (Node_URI)DB.graphLSet.getNode(); 37 public static Node_URI graphPrefix = (Node_URI)DB.graphPrefix.getNode(); 38 public static Node_URI graphId = (Node_URI)DB.graphId.getNode(); 39 public static Node_URI stmtTable = (Node_URI)DB.stmtTable.getNode(); 40 public static Node_URI reifTable = (Node_URI)DB.reifTable.getNode(); 41 42 43 44 public DBPropGraph( SpecializedGraph g, String symbolicName, String type) { 45 super(g); 46 47 putPropString(graphName, symbolicName); 48 putPropString(graphType, type); 49 } 50 51 public DBPropGraph( SpecializedGraph g, Node n) { 52 super(g,n); 53 } 54 55 public DBPropGraph( SpecializedGraph g, String newSymbolicName, Graph oldProperties) { 56 super(g); 57 58 putPropString(graphName, newSymbolicName); 59 Iterator it = oldProperties.find( Node.ANY, Node.ANY, Node.ANY); 61 while ( it.hasNext() ) { 62 Triple t = (Triple) it.next(); 63 if ( t.getPredicate().equals(graphName) || 64 t.getPredicate().equals(graphId) || 65 t.getPredicate().equals(stmtTable) || 66 t.getPredicate().equals(reifTable) ) 67 continue; 68 putPropNode((Node_URI)t.getPredicate(),t.getObject()); 69 } 70 } 71 72 73 public void addLSet( DBPropLSet lset ) { 74 putPropNode( graphLSet, lset.getNode() ); 75 } 76 77 public void addPrefix( DBPropPrefix prefix ) { 78 DBPropPrefix existing = getPrefix( prefix.getValue()); 80 if( existing != null) 81 removePrefix( existing); 82 existing = getURI( prefix.getURI()); 83 if( existing != null && !prefix.getValue().equals("")) 84 removePrefix( existing); 85 putPropNode( graphPrefix, prefix.getNode() ); 86 } 87 88 public void removePrefix( DBPropPrefix prefix ) { 89 SpecializedGraph.CompletionFlag complete = newComplete(); 90 Iterator matches = graph.find( self, graphPrefix, prefix.getNode(), complete); 91 if( matches.hasNext() ) { 92 graph.delete( (Triple)(matches.next()), complete ); 93 prefix.remove(); 94 } 95 } 96 97 public void addPrefix( String prefix, String uri ) { 98 addPrefix( new DBPropPrefix( graph, prefix, uri) ); 99 } 100 101 public void removePrefix( String prefix ) { 102 DBPropPrefix existing = getPrefix( prefix ); 103 if (existing != null) removePrefix( existing ); 104 } 105 106 public void addGraphId( int id ) { 107 putPropString(graphId, Integer.toString(id)); 108 } 109 110 public void addStmtTable( String table ) { 111 putPropString(stmtTable, table); 112 } 113 114 public void addReifTable( String table ) { 115 putPropString(reifTable, table); 116 } 117 118 119 120 public String getName() { return getPropString( graphName); } 121 public String getType() { return getPropString( graphType); } 122 public String getStmtTable() { return getPropString(stmtTable); } 123 public String getReifTable() { return getPropString(reifTable); } 124 public int getGraphId() { 125 String i = getPropString(graphId); 126 return i == null ? -1 : Integer.parseInt(i); 127 } 128 129 public ExtendedIterator getAllLSets() { 130 return 131 graph.find( self, graphLSet, null, newComplete() ) 132 .mapWith ( new MapToLSet() ); 133 } 134 135 public ExtendedIterator getAllPrefixes() { 136 return 137 graph.find( self, graphPrefix, null, newComplete() ) 138 .mapWith ( new MapToPrefix() ); 139 } 140 141 public DBPropPrefix getPrefix( String value ) { 142 ExtendedIterator prefixes = getAllPrefixes(); 143 while( prefixes.hasNext() ) { 144 DBPropPrefix prefix = (DBPropPrefix)prefixes.next(); 145 if( prefix.getValue().compareTo(value)==0) 146 return prefix; 147 } 148 return null; 149 } 150 151 public DBPropPrefix getURI( String uri ) { 152 ExtendedIterator prefixes = getAllPrefixes(); 153 while( prefixes.hasNext() ) { 154 DBPropPrefix prefix = (DBPropPrefix)prefixes.next(); 155 if( prefix.getURI().compareTo(uri)==0) 156 return prefix; 157 } 158 return null; 159 } 160 161 public ExtendedIterator listTriples() { 162 ExtendedIterator result = DBProp.listTriples( graph, self ); 164 165 ExtendedIterator lsets = getAllLSets(); 167 while( lsets.hasNext()) { 168 result = result.andThen( ((DBPropLSet)lsets.next()).listTriples() ); 169 } 170 171 ExtendedIterator prefixes = getAllPrefixes(); 173 while( prefixes.hasNext()) { 174 result = result.andThen( ((DBPropPrefix)prefixes.next()).listTriples() ); 175 } 176 return result; 177 } 178 179 180 private class MapToLSet implements Map1 { 181 public Object map1( Object o) { 182 Triple t = (Triple) o; 183 return new DBPropLSet( graph, t.getObject() ); 184 } 185 } 186 187 private class MapToPrefix implements Map1 { 188 public Object map1( Object o) { 189 Triple t = (Triple) o; 190 return new DBPropPrefix( graph, t.getObject() ); 191 } 192 } 193 194 public static DBPropGraph findPropGraphByName( SpecializedGraph graph, String name ) { 195 Node myNode = Node.createLiteral( name ); 196 ClosableIterator it = graph.find( null, graphName, myNode, newComplete() ); 197 if( it.hasNext() ) 198 try { return new DBPropGraph( graph, ((Triple)it.next()).getSubject()); } 199 finally { it.close(); } 200 else 201 return null; 202 } 203 204 208 209 public boolean isDBPropGraphOk ( String name ) { 210 String s = getName(); 211 boolean res = (s == null) ? false : s.equals(name); 212 res = res & (getGraphId() != -1); 213 res = res & (getType() != null); 214 res = res & (getStmtTable() != null); 215 res = res & (getReifTable() != null); 216 return res; 217 } 218 219 public void remove() { 220 Iterator it = getAllPrefixes(); 221 while( it.hasNext()) { 222 ((DBPropPrefix)it.next()).remove(); 223 } 224 it = getAllLSets(); 225 while( it.hasNext()) { 226 ((DBPropLSet)it.next()).remove(); 227 } 228 super.remove(); 229 } 230 231 } 232 233 | Popular Tags |