1 22 23 28 29 package org.xquark.mapper.storage; 30 31 import java.sql.Connection ; 32 import java.sql.SQLException ; 33 34 import org.xml.sax.Locator ; 35 import org.xquark.mapper.RepositoryException; 36 import org.xquark.mapper.dbms.AbstractConnection; 37 import org.xquark.mapper.dbms.JDBCBatcher; 38 import org.xquark.mapper.metadata.Node; 39 import org.xquark.mapper.metadata.RepositoryConstants; 40 import org.xquark.mapper.metadata.StoragePathMetadata; 41 import org.xquark.mapper.util.RecyclingStack; 42 import org.xquark.schema.SchemaConstants; 43 import org.xquark.schema.validation.ElementPSVInfoset; 44 import org.xquark.schema.validation.PSVInfoset; 45 import org.xquark.schema.validation.SchemaValidationContext; 46 import org.xquark.xml.xdbc.XMLDBCException; 47 import org.xquark.xml.xdbc.XMLErrorHandler; 48 import org.xquark.xpath.NodeKind; 49 50 53 public abstract class AbstractModelBuilder 54 implements RepositoryConstants, _StorageContext, RecyclingStack.StackObjectFactory 55 { 56 private static final String RCSRevision = "$Revision: 1.2 $"; 57 private static final String RCSName = "$Name: $"; 58 59 61 protected RecyclingStack stack = null; 62 63 protected StorageBuffer storageBuffer; protected AbstractConnection connection; 65 protected JDBCBatcher batcher; 66 protected SchemaValidationContext validationContext; 67 68 private boolean autoFlush = true; 69 70 public AbstractModelBuilder(AbstractConnection connection, SchemaValidationContext schemaContext) 71 throws XMLDBCException 72 { 73 stack = new RecyclingStack(this); 74 this.connection = connection; 75 validationContext = schemaContext; 76 batcher = new JDBCBatcher(connection, autoFlush); 77 } 78 79 protected abstract AbstractModelNode getLogicalStructureNode(byte type, StoragePathMetadata path, String data, Locator locator) 80 throws SQLException , XMLDBCException; 81 82 protected abstract AbstractModelNode finalizeNode(Locator locator) 83 throws XMLDBCException, SQLException ; 84 85 86 public void endModel() throws XMLDBCException, SQLException 87 { 88 storageBuffer.flush(); 90 } 91 92 public void reset() 93 throws RepositoryException, SQLException 94 { 95 stack.clear(); 96 storageBuffer.reset(); 97 } 98 99 public void close() throws RepositoryException, SQLException 100 { 101 if (batcher != null) 102 { 103 batcher.close(); 104 batcher = null; 105 storageBuffer.close(); 106 storageBuffer = null; 107 stack = null; 108 } 109 } 110 111 public void setErrorHandler(XMLErrorHandler handler) 112 { 113 storageBuffer.setErrorHandler(handler); 114 } 115 116 public XMLErrorHandler getXMLErrorHandler() 117 { 118 return storageBuffer.getXMLErrorHandler(); 119 } 120 121 public AbstractModelNode getCurrentNode() 122 { 123 return (AbstractModelNode)stack.top(); 124 } 125 126 132 public AbstractModelNode newNode(StoragePathMetadata path, Locator locator) 133 throws SQLException , XMLDBCException 134 { 135 if (path == null) 136 return null; 137 return getLogicalStructureNode(path.getType(), path, "", locator); } 139 140 143 public AbstractModelNode leafNode(byte type, StoragePathMetadata path, String data, Locator locator) 144 throws XMLDBCException, SQLException 145 { 146 getLogicalStructureNode(type, path, data, locator); 147 return finalizeNode(locator); 148 } 149 150 public void setData(String data) 151 { 152 getCurrentNode().setData(data); 153 } 154 155 public void setXSIinfo(String attName, String value) 156 throws RepositoryException 157 { 158 getCurrentNode().setXSIinfo(attName, value); 159 } 160 161 public void incCharOffset(int length) 162 { 163 getCurrentNode().incCharOffset(length); 164 } 165 166 public boolean empty() 167 { 168 return stack.isEmpty(); 169 } 170 171 public void flushBuffer() throws XMLDBCException 172 { 173 batcher.flush(); 174 } 175 176 public void clearBuffer() throws XMLDBCException 177 { 178 try { 179 batcher.reset(); 180 } 181 catch (SQLException e) { 182 throw new RepositoryException(RepositoryException.DB_ERROR, 183 "Could not reset the JDBC Batcher"); 184 } 185 } 186 187 public void setAutoFlush(boolean mode) throws XMLDBCException 188 { 189 autoFlush = mode; 190 batcher.setAutoFlush(mode); 191 } 192 193 public boolean getAutoFlush() 194 { 195 return autoFlush; 196 } 197 public long getBucketOID() { return -1;} 201 public short getPathOID() { return -1;} 202 public String getDocumentID() { return null;} 203 public long getDocumentOID() { return -1;} 204 public long getOID() { return -1;} 205 public long getUOID() { return -1;} 206 207 public String getQName() 208 { 209 AbstractModelNode node = getCurrentNode(); 210 return (node.getPath().getNamespace() == null ? node.getPath().getLocalName() : node.getPath().getNamespace() + ":" + node.getPath().getLocalName()); 211 } 212 public int getNodeRank() 213 { 214 return getCurrentNode().rank; 215 } 216 public String getNodeData() { return getCurrentNode().data;} 217 public String getNormalizedNodeData() 218 { 219 String ret = null; 220 PSVInfoset nodeInfoSet = getNodeInfoSet(); 221 if (nodeInfoSet == null) 222 return null; 223 224 if ((nodeInfoSet.getSpecified() == null) || ( 227 (nodeInfoSet.getSpecified() == PSVInfoset.SCHEMA) && (getCurrentNode().getPath().getType() == NodeKind.ATTRIBUTE) 229 )) 230 ret = getNodeData(); 231 else 232 ret = nodeInfoSet.getNormalizedValue(); 233 234 return ret; 235 } 236 237 public Object getActualNodeData() 238 { 239 Object ret = null; 240 PSVInfoset nodeInfoSet = getNodeInfoSet(); 241 if (nodeInfoSet == null) 242 return null; 243 244 if ((nodeInfoSet.getSpecified() == null) || ( 247 (nodeInfoSet.getSpecified() == PSVInfoset.SCHEMA) && (getCurrentNode().getPath().getType() == NodeKind.ATTRIBUTE) 249 )) 250 ret = getNodeData(); 251 else 252 ret = nodeInfoSet.getActualValue(); 254 return ret; 255 } 256 public String getCanonicalString() 257 { 258 String ret = null; 259 PSVInfoset nodeInfoSet = getNodeInfoSet(); 260 if (nodeInfoSet == null) 261 return null; 262 263 if ((nodeInfoSet.getSpecified() == null) || ( 266 (nodeInfoSet.getSpecified() == PSVInfoset.SCHEMA) && (getCurrentNode().getPath().getType() == NodeKind.ATTRIBUTE) 268 )) 269 ret = getNodeData(); 270 else 271 ret = nodeInfoSet.getDeclaration().getType().getValueType().toXMLString( 272 nodeInfoSet.getActualValue(), null); 273 274 return ret; 275 } 276 public String getLocalName() { return getCurrentNode().getPath().getLocalName();} 277 public String getNamespaceURI() { return getCurrentNode().getPath().getNamespace();} 278 public Object getRefValue(int tableIndex, int columnIndex) 279 { 280 return storageBuffer.getTupleFactory(tableIndex).getParameter(columnIndex); 281 } 282 public Connection getConnection() 283 { 284 return storageBuffer.getConnection().getConnection(); 285 } 286 287 public RecyclingStack.StackObject newStackObject() 288 { 289 return new AbstractModelNode(); 290 } 291 292 296 private PSVInfoset getNodeInfoSet() 297 { 298 AbstractModelBuilder.AbstractModelNode wNode = getCurrentNode(); 299 if (wNode.data == null) 300 return null; 301 302 StoragePathMetadata node = wNode.getPath(); 303 ElementPSVInfoset infoSet = validationContext.getCurrentInfoset(); 304 305 PSVInfoset nodeInfoSet = null; 306 if (node.getType() == NodeKind.ELEMENT) 307 nodeInfoSet = infoSet; 308 else nodeInfoSet = infoSet.getAttributePSVInfoset(node.getNamespace(), node.getLocalName()); 310 311 return nodeInfoSet; 312 } 313 314 public class AbstractModelNode extends Node 318 implements RecyclingStack.StackObject 319 { 320 protected StoragePathMetadata pathMetadata; 321 protected short childCount = 0; private short rank = 0; private String data = null; 324 protected StorageBuffer.BufferNode tupleNode = null; 325 326 AbstractModelNode() 327 {} 328 void set(StoragePathMetadata path, byte type, String data, short rank) 329 { 330 pathMetadata = path; 331 this.type = type; 332 this.rank = rank; 333 this.path = path.getPathID(); 334 setData(data); 335 } 336 public void clear() 337 { 338 super.clear(); 339 pathMetadata = null; 340 childCount = 0; 341 rank = 0; 342 data = null; 343 tupleNode = null; 344 } 345 346 void setTupleNode(StorageBuffer.BufferNode node) 347 { 348 tupleNode = node; 349 } 350 351 StorageBuffer.BufferNode getTupleNode() 352 { 353 return tupleNode; 354 } 355 356 String getData() 357 { 358 return data; 359 } 360 361 short getRank() 362 { 363 return rank; 364 } 365 366 short incChildCount() 367 { 368 return ++childCount; 369 } 370 371 373 boolean isLeaf() 374 { 375 return (childCount == 0); 376 } 377 378 public void setData(String data) 379 { 380 this.data = data; 381 } 382 383 StoragePathMetadata getPath() 384 { 385 return pathMetadata; 386 } 387 public void incCharOffset(int length) 388 { 389 } 391 392 395 public boolean setXSIinfo(String attName, String value) 396 throws RepositoryException 397 { 398 if (attName.equals(SchemaConstants.XSI_NIL_ATTR) 399 && (value.equals(SchemaConstants.TRUE_VALUE) || value.equals("1"))) 400 { 401 setData(null); return true; 403 } 404 return false; 405 } 406 } 407 } 408 | Popular Tags |