1 6 7 package com.hp.hpl.jena.rdf.model.impl; 8 9 import com.hp.hpl.jena.rdf.model.*; 10 import com.hp.hpl.jena.vocabulary.RDF; 11 12 import com.hp.hpl.jena.graph.*; 13 import com.hp.hpl.jena.enhanced.*; 14 import com.hp.hpl.jena.shared.*; 15 16 import java.util.*; 17 18 24 25 public class ContainerImpl extends ResourceImpl 26 implements Container, ContainerI { 27 28 static NodeIteratorFactory iteratorFactory; 29 30 static { 31 iteratorFactory = new ContNodeIteratorFactoryImpl(); 32 } 33 34 35 public ContainerImpl( ModelCom model ) { 36 super(model); 37 } 38 39 public ContainerImpl( String uri, ModelCom model ){ 40 super(uri, model); 41 } 42 43 public ContainerImpl(Resource r, ModelCom model) { 44 super(r.asNode(), model); 45 } 46 47 public ContainerImpl(Node n, EnhGraph g) { 48 super(n,g); 49 } 50 51 protected ContainerImpl( Resource r ) 52 { this( r, (ModelCom) r.getModel() ); } 53 54 private boolean is( Resource r ) { 55 return hasProperty(RDF.type, r); 56 } 57 58 public boolean isAlt() { 59 return is(RDF.Alt); 60 } 61 62 public boolean isBag() { 63 return is(RDF.Bag); 64 } 65 66 public boolean isSeq() { 67 return is(RDF.Seq); 68 } 69 70 public Container add(RDFNode n) { 71 int i = size(); 72 addProperty(RDF.li(i+1), n); 73 return this; 74 } 75 76 public Container add(boolean o) { 77 return add( String.valueOf( o ) ); 78 } 79 80 public Container add(long o) { 81 return add( String.valueOf( o ) ); 82 } 83 84 public Container add(char o) { 85 return add( String.valueOf( o ) ); 86 } 87 88 public Container add(float o) { 89 return add( String.valueOf( o ) ); 90 } 91 92 public Container add(double o) { 93 return add( String.valueOf( o ) ); 94 } 95 96 public Container add(Object o) { 97 return add( String.valueOf( o ) ); 98 } 99 100 public Container add(String o) { 101 return add( o, "" ); 102 } 103 104 public Container add(String o, String l) { 105 return add( literal( o, l ) ); 106 } 107 108 public boolean contains(RDFNode n) { 109 return containerContains( n ); 110 } 111 112 public boolean contains(boolean o) { 113 return contains( String.valueOf( o ) ); 114 } 115 116 public boolean contains(long o) { 117 return contains( String.valueOf( o ) ); 118 } 119 120 public boolean contains(char o) { 121 return contains( String.valueOf( o ) ); 122 } 123 124 public boolean contains(float o) { 125 return contains( String.valueOf( o ) ); 126 } 127 128 public boolean contains(double o) { 129 return contains( String.valueOf( o ) ); 130 } 131 132 public boolean contains(Object o) { 133 return contains( String.valueOf( o ) ); 134 } 135 136 public boolean contains(String o) { 137 return contains( o, "" ); 138 } 139 140 public boolean contains( String o, String l ) { 141 return contains( literal( o, l ) ); 142 } 143 144 private Literal literal( String s, String lang ) 145 { return new LiteralImpl( Node.createLiteral( s, lang, false ), getModelCom() ); } 146 147 public NodeIterator iterator() 148 { return listContainerMembers( iteratorFactory ); } 149 150 public int size() 151 { 152 int result = 0; 153 StmtIterator iter = listProperties(); 154 while (iter.hasNext()) 155 if (iter.nextStatement().getPredicate().getOrdinal() != 0) result += 1; 156 iter.close(); 157 return result; 158 } 159 160 public Container remove(Statement s) { 161 int size = size(); 162 Statement last = null; 163 if (s.getPredicate().getOrdinal() == size) { getModel().remove(s); 165 } else { 166 last = getModel().getRequiredProperty(this, RDF.li(size)); 167 s.changeObject(last.getObject()); 168 getModel().remove(last); 169 } 170 if (size() != (size -1)) 171 throw new AssertionFailureException( "container size" ); 172 return this; 173 } 174 175 public Container remove(int index, RDFNode object) { 176 remove(getModel().createStatement(this, RDF.li(index), object)); 177 return this; 178 } 179 180 185 public NodeIterator listContainerMembers( NodeIteratorFactory f ) 186 { 187 StmtIterator iter = listProperties(); 188 Vector result = new Vector(); 189 int maxOrdinal = 0; 190 while (iter.hasNext()) { 191 Statement stmt = iter.nextStatement(); 192 int ordinal = stmt.getPredicate().getOrdinal(); 193 if (ordinal != 0) { 194 if (ordinal > maxOrdinal) { 195 maxOrdinal = ordinal; 196 result.setSize(ordinal); 197 } 198 result.setElementAt(stmt, ordinal-1); 199 } 200 } 201 iter.close(); 202 return f.createIterator( result.iterator(), result, this ); 203 } 204 205 public int containerIndexOf( RDFNode n ) { 206 int result = 0; 207 StmtIterator iter = listProperties(); 208 while (iter.hasNext()) { 209 Statement stmt = iter.nextStatement(); 210 int ordinal = stmt.getPredicate().getOrdinal(); 211 if (ordinal != 0 && n.equals( stmt.getObject() )) { 212 result = ordinal; 213 break; 214 } 215 } 216 iter.close(); 217 return result; 218 } 219 220 public boolean containerContains( RDFNode n) 221 { return containerIndexOf( n ) != 0; } 222 223 } 224 225 255 | Popular Tags |