1 9 package org.ozoneDB.xml.cli.resources; 10 11 12 import java.io.StringReader ; 13 import java.io.StringWriter ; 14 15 import org.ozoneDB.ExternalDatabase; 16 import org.ozoneDB.ExternalTransaction; 17 import org.ozoneDB.xml.util.XMLContainer; 18 import org.ozoneDB.xml.cli.CollectionImpl; 19 20 import org.w3c.dom.Node ; 21 import org.w3c.dom.Document ; 22 import javax.xml.parsers.SAXParser ; 24 import org.xml.sax.helpers.ParserAdapter ; 25 import org.xml.sax.ContentHandler ; 26 import org.xml.sax.InputSource ; 27 import org.xml.sax.SAXException ; 28 import javax.xml.parsers.SAXParserFactory ; 30 import javax.xml.parsers.DocumentBuilderFactory ; 31 import javax.xml.parsers.DocumentBuilder ; 32 33 import org.xmldb.api.base.Collection; 34 import org.xmldb.api.base.ErrorCodes; 35 import org.xmldb.api.base.XMLDBException; 36 import org.xmldb.api.modules.XMLResource; 37 38 import org.apache.xml.serialize.XMLSerializer; 39 import org.apache.xml.serialize.OutputFormat; 40 import org.apache.log4j.Logger; 41 42 51 public class XMLResourceImpl implements XMLResource { 52 53 private static final Logger logger = Logger.getLogger(XMLResourceImpl.class); 54 private final SAXParserFactory parserFactory = new org.apache.xerces.jaxp.SAXParserFactoryImpl(); 56 private ExternalDatabase database; 57 private XMLContainer container; 59 private Collection collection; 61 private String id; 63 64 65 public XMLResourceImpl( String id, ExternalDatabase database, Collection collection, 66 XMLContainer container ) { 67 logger.debug("XMLResourceImpl CREATED ***********"); 68 this.database = database; 69 this.container = container; 70 this.collection = collection; 71 this.id = id; 72 73 } 74 75 81 public Collection getParentCollection() throws XMLDBException { 82 return collection; 83 } 84 85 91 public String getId() throws XMLDBException { 92 return id; 93 } 94 95 101 public void setContent( Object value ) throws XMLDBException { 102 if (value instanceof String ) { 103 ExternalTransaction tx = database.newTransaction(); 104 try { 106 StringReader in = new StringReader ((String )value); 107 InputSource source = new InputSource (in); 108 SAXParser parser = parserFactory.newSAXParser(); 109 ParserAdapter adapter = new ParserAdapter ( parser.getParser() ); 110 tx.begin(); 111 adapter.setContentHandler( container.storeSAX() ); 112 adapter.parse( source ); 113 tx.commit(); 114 } 115 catch (SAXException e) { 116 try { 117 if (tx.getStatus() == tx.STATUS_ACTIVE) 118 tx.rollback(); 119 throw new XMLDBException(ErrorCodes.INVALID_RESOURCE, e.getMessage()); 120 } 121 catch (Exception rollbackException) { 122 throw new XMLDBException(ErrorCodes.VENDOR_ERROR, rollbackException.toString()); 123 } 124 } 125 catch (Exception e) { 126 try { 127 if (tx.getStatus() == tx.STATUS_ACTIVE) 128 tx.rollback(); 129 throw new XMLDBException(ErrorCodes.VENDOR_ERROR, e.getMessage()); 130 } 131 catch (Exception rollbackException) { 132 throw new XMLDBException(ErrorCodes.VENDOR_ERROR, rollbackException.toString()); 133 } 134 } 135 } 136 else if (value instanceof Node ) { 137 setContentAsDOM((Node )value); 138 } 139 else 140 throw new XMLDBException( ErrorCodes.VENDOR_ERROR, ErrorCodes.UNKNOWN_RESOURCE_TYPE ); 141 } 142 143 149 public Object getContent() throws XMLDBException { 150 try { 151 return toString( "xml", "UTF-8", true ); 152 } 153 catch (Exception e) { 154 throw new XMLDBException( ErrorCodes.VENDOR_ERROR, e.getMessage()); 155 } 156 } 157 158 163 public String getResourceType() throws XMLDBException { 164 return XMLResource.RESOURCE_TYPE; 165 } 166 167 172 public Node getContentAsDOM() throws XMLDBException { 173 ExternalTransaction tx = database.newTransaction(); 174 try { 175 tx.begin(); 176 DocumentBuilderFactory builderFactory = new org.apache.xerces.jaxp.DocumentBuilderFactoryImpl(); 177 DocumentBuilder documentBuilder = builderFactory.newDocumentBuilder(); 178 Document doc = container.extractDOM(documentBuilder.newDocument()); 179 tx.commit(); 180 return doc; 181 } 182 catch (Exception e) { 183 try { 184 if (tx.getStatus() == tx.STATUS_ACTIVE) 185 tx.rollback(); 186 throw new XMLDBException( ErrorCodes.VENDOR_ERROR, e.getMessage()); 187 } 188 catch (Exception rollbackException) { 189 throw new XMLDBException(ErrorCodes.VENDOR_ERROR, rollbackException.toString()); 190 } 191 } 192 } 193 194 200 public void setContentAsDOM( Node content ) throws XMLDBException { 201 ExternalTransaction tx = database.newTransaction(); 202 try { 203 if (content == null) { 204 logger.debug("XMLResourceImpl.setContentAsDOM() - Content is null"); 205 throw new XMLDBException(ErrorCodes.INVALID_RESOURCE); 206 } 207 if (content instanceof Document ) { 208 Document doc = (Document )content; 209 tx.begin(); 210 container.storeDOM(doc); 211 tx.commit(); 212 } 213 else { 214 logger.info("Cannot store Nodes right now, must be a Document"); 215 } 216 } 217 catch (Exception e) { 218 e.printStackTrace(); 219 try { 220 logger.debug("XMLResourceImpl.setContentAsDOM() - Transaction status is " + tx.getStatus()); 221 if (tx.getStatus() == tx.STATUS_ACTIVE) 222 tx.rollback(); 223 throw new XMLDBException( ErrorCodes.VENDOR_ERROR, e.getMessage()); 224 } 225 catch (Exception rollbackException) { 226 throw new XMLDBException(ErrorCodes.VENDOR_ERROR, rollbackException.getMessage()); 227 } 228 } 229 } 230 231 238 public void getContentAsSAX( ContentHandler handler ) throws XMLDBException { 239 ExternalTransaction tx = database.newTransaction(); 240 try { 241 tx.begin(); 242 container.extractSAX(handler); 243 tx.commit(); 244 } 245 catch (Exception e) { 246 try { 247 if (tx.getStatus() == tx.STATUS_ACTIVE) 248 tx.rollback(); 249 throw new XMLDBException( ErrorCodes.VENDOR_ERROR, e.getMessage()); 250 } 251 catch (Exception rollbackException) { 252 throw new XMLDBException(ErrorCodes.VENDOR_ERROR, rollbackException.toString()); 253 } 254 } 255 } 256 257 264 public ContentHandler setContentAsSAX() throws XMLDBException { 265 ExternalTransaction tx = database.newTransaction(); 266 try { 267 tx.begin(); 268 ContentHandler handler = container.storeSAX(); 269 tx.commit(); 270 return handler; 271 } 272 catch (Exception e) { 273 try { 274 if (tx.getStatus() == tx.STATUS_ACTIVE) 275 tx.rollback(); 276 throw new XMLDBException( ErrorCodes.VENDOR_ERROR, e.getMessage()); 277 } 278 catch (Exception rollbackException) { 279 throw new XMLDBException(ErrorCodes.VENDOR_ERROR, rollbackException.toString()); 280 } 281 } 282 } 283 284 289 private String toString(String type, String encoding, boolean indenting) throws Exception { 290 int depth = -1; 291 ExternalTransaction tx = database.newTransaction(); 292 tx.begin(); 293 try { 294 StringWriter writer = new StringWriter (); 295 XMLSerializer serializer = new XMLSerializer( writer, new OutputFormat(type, encoding, indenting) ); 296 ContentHandler handler = serializer.asContentHandler(); 297 container.extractSAX( handler, null, depth ); 298 writer.flush(); 299 tx.commit(); 300 return writer.toString(); 301 } 302 catch (Exception e) { 303 if (tx.getStatus() == tx.STATUS_ACTIVE) 304 tx.rollback(); 305 throw e; 306 } 307 } 308 } 309 | Popular Tags |