1 22 23 package org.xquark.mapper.storage; 24 25 import java.sql.SQLException ; 26 import java.util.ArrayList ; 27 import java.util.List ; 28 29 import org.xml.sax.SAXException ; 30 import org.xquark.mapper.RepositoryException; 31 import org.xquark.mapper.metadata.RepositoryConstants; 32 import org.xquark.mapper.metadata.StoragePathMetadata; 33 import org.xquark.mapper.util.DestructionToken; 34 import org.xquark.mapper.util.Functions; 35 import org.xquark.schema.validation.SchemaValidationContext; 36 import org.xquark.xml.xdbc.*; 37 import org.xquark.xpath.NodeKind; 38 39 47 public class CollectionFiler extends SAXHandler 48 { 49 private static final String RCSRevision = "$Revision: 1.3 $"; 50 private static final String RCSName = "$Name: $"; 51 52 protected _RepositoryCollection collection; 56 protected String docID; 57 private boolean IDSetByUser = false; 58 private DestructionToken destructor; 59 private boolean storePrefixes = true; private List declaredPrefixes; 61 62 63 65 public CollectionFiler(_RepositoryCollection collection, SchemaValidationContext schemaContext, boolean storePrefixes, DestructionToken destructor) throws XMLDBCException 66 { 67 super( 68 collection.getMetadata().getPathSet().createIterator(), 69 collection.getRepositoryConnection().getConnection(), 70 new ModelBuilder(collection, schemaContext) 71 ); 72 this.storePrefixes = storePrefixes; 73 if (storePrefixes) 74 declaredPrefixes = new ArrayList (3); 75 this.collection = collection; 76 this.destructor = destructor; 77 textData.ensureCapacity(collection.getInfo().getMaxTextLength()); 78 } 79 80 public void reset() throws RepositoryException 81 { 82 super.reset(); 83 IDSetByUser = false; 84 declaredPrefixes.clear(); 85 } 86 87 public void close() throws XMLDBCException 88 { 89 super.close(); 90 if (destructor != null) 91 { 92 declaredPrefixes = null; 93 destructor.destruct(this); 94 destructor = null; 95 } 96 } 97 98 protected void processFatalError(Exception e) throws SAXException 99 { 100 super.processFatalError(e); 101 collection.restoreDID(builder.getDocumentOID()); 102 } 103 104 public void comment(char ch[], int start, int length) throws SAXException 108 { 109 111 try 113 { 114 ((ModelBuilder)builder).extraNode(NodeKind.COMMENT, context.getStoragePathMetadata(), new String (ch, start, length), locator); 115 } 116 catch(XMLDBCException e) 117 { 118 processFatalError(e); 119 } 120 catch(SQLException e) 121 { 122 processFatalError(e); 123 } 124 } 125 126 public void startCDATA() throws SAXException 127 { 128 130 try 132 { 133 ((ModelBuilder)builder).extraNode(RepositoryConstants.START_CDATA_SECTION,context.getStoragePathMetadata(), null, locator); 134 } 135 catch(XMLDBCException e) 136 { 137 processFatalError(e); 138 } 139 catch(SQLException e) 140 { 141 processFatalError(e); 142 } 143 } 144 145 public void endCDATA() throws SAXException 146 { 147 149 try 151 { 152 ((ModelBuilder)builder).extraNode(RepositoryConstants.END_CDATA_SECTION,context.getStoragePathMetadata(), null, locator); 153 } 154 catch(XMLDBCException e) 155 { 156 processFatalError(e); 157 } 158 catch(SQLException e) 159 { 160 processFatalError(e); 161 } 162 } 163 public void startPrefixMapping(String prefix, String uri) 167 throws SAXException 168 { 169 declaredPrefixes.add("{" + uri + "}" + prefix); 172 } 173 174 protected void processPrefixes() 175 throws XMLDBCException, SQLException 176 { 177 if (storePrefixes) 178 { 179 int prefixCount = declaredPrefixes.size(); 180 String prefix; 181 for (int i = 0; i < prefixCount; i++) 182 { 183 ((ModelBuilder)builder).extraNode( 185 NodeKind.NAMESPACE, 186 context.getStoragePathMetadata(), 187 (String )declaredPrefixes.get(i), 188 locator 189 ); 190 } 191 declaredPrefixes.clear(); 192 } 193 } 194 195 public void processingInstruction(String target, String data) throws SAXException 196 { 197 199 try 201 { 202 ((ModelBuilder)builder).extraNode(NodeKind.PI, context.getStoragePathMetadata(), target + " " + data, locator); 203 } 204 catch(XMLDBCException e) 205 { 206 processFatalError(e); 207 } 208 catch(SQLException e) 209 { 210 processFatalError(e); 211 } 212 } 213 214 protected boolean textBetweenTransitions() 215 { 216 boolean ret = textBetweenTransitions; 217 218 if (removeWhitespace) 220 { 221 int i, max = textData.length(); 222 for (i = 0; i < max; i++) 223 if (!Functions.isWhitespace(textData.charAt(i))) 224 break; 225 if (i == max) { 227 textData.setLength(0); 228 ret = false; 229 } 230 } 231 return ret; 232 } 233 234 protected StoragePathMetadata push(String namespaceURI, String localName) 238 throws SAXException , RepositoryException 239 { 240 StoragePathMetadata pathNode = super.push(namespaceURI, localName); 241 if (pathNode == null) 243 throw new SAXException ("Path {" + namespaceURI + "}" + localName 244 + " could not be written in the underlying database. Storage is cancelled to avoid metadata corruption."); 245 return pathNode; 246 } 247 248 258 protected void startParsing() throws RepositoryException, SQLException 259 { 260 super.startParsing(); 261 262 if (!IDSetByUser) 264 docID = null; 265 266 docID = ((ModelBuilder)builder).allocateDocOID(docID); 268 } 269 270 public void setDocumentId(String ID) throws XMLDBCException 274 { 275 if ((ID != null) && (ID.length() > ID_LENGTH)) 276 throw new RepositoryException(RepositoryException.ILLEGAL_EXPRESSION, "The maximum size authorized for document ID (" 277 + ID_LENGTH + ") is exceeded."); 278 279 if (ID != null) 280 { 281 docID = ID.trim(); 282 IDSetByUser = true; 283 } 284 } 285 286 public XMLCollection getCollection() 287 { 288 return collection; 289 } 290 291 public String getDocumentId() 292 { 293 return docID; } 295 296 public void setXMLProperty(String propertyId, Object value) throws XMLDBCNotRecognizedException, XMLDBCNotSupportedException 297 { 298 throw new XMLDBCNotSupportedException("Not supported yet"); 299 } 301 302 public Object getXMLProperty(String propertyId) throws XMLDBCNotRecognizedException 303 { 304 throw new UnsupportedOperationException ("Not supported yet"); 305 } 307 308 public String [] getXMLFeatureList() 309 { 310 throw new UnsupportedOperationException ("Not supported yet"); 311 } 313 314 public void setXMLFeature(String featureId, boolean state) throws XMLDBCNotRecognizedException, XMLDBCNotSupportedException 315 { 316 throw new XMLDBCNotSupportedException("Not supported yet"); 317 } 319 320 public boolean getXMLFeature(String featureId) throws XMLDBCNotRecognizedException 321 { 322 throw new UnsupportedOperationException ("Not supported yet"); 323 } 325 326 public String [] getXMLPropertyList() 327 { 328 throw new UnsupportedOperationException ("Not supported yet"); 329 } 331 332 } 333 334 335 | Popular Tags |