1 package org.exoplatform.services.xml.querying.impl.xtas.resource.plugins; 2 3 import java.net.MalformedURLException ; 4 5 import java.io.IOException ; 6 import java.io.ByteArrayInputStream ; 7 8 9 import org.exoplatform.services.xml.querying.UniFormTransformationException; 10 import org.exoplatform.services.xml.querying.XMLWellFormedData; 11 import org.exoplatform.services.xml.querying.impl.xtas.WellFormedUniFormTree; 12 import org.exoplatform.services.xml.querying.impl.xtas.resource.Resource; 13 import org.xml.sax.InputSource ; 14 15 import org.xmldb.api.DatabaseManager; 16 import org.xmldb.api.modules.XMLResource; 17 import org.xmldb.api.base.Database; 18 import org.xmldb.api.base.Collection; 19 20 21 29 public class XMLDBResource extends Resource { 30 31 32 private Database context = null; 34 35 private String xmldbCollectionId = null; 36 private String xmldbResourceId = null; 37 38 41 public XMLDBResource(String resourceId) throws MalformedURLException 42 { 43 this(); 44 this.resourceId = resourceId; 45 } 46 47 public XMLDBResource() throws MalformedURLException 48 { 49 int resIndex = resourceId.indexOf("#")+1; 50 if (resIndex > 0) 51 xmldbResourceId = resourceId.substring(resIndex); 52 else 53 throw new MalformedURLException ("Could not create XMLDBResource. Indefinite XML:DB resource-id (#resourceId) in'"+resourceId+"'"); 54 55 xmldbCollectionId = resourceId.substring(0,resIndex-1); 56 57 } 58 59 public void setContext(Object context) 60 { 61 62 if( context instanceof Database ) 63 this.context = (Database) context; 64 71 72 else 73 throw new ClassCastException ("XmlDbResource.setContext():"+context.getClass().getName()+" not subclass of org.xmldb.api.base.Database!"); 74 75 } 76 77 80 public XMLWellFormedData load() throws UniFormTransformationException, IOException 81 { 82 83 try { 84 85 Collection col = DatabaseManager.getCollection(xmldbCollectionId); 86 87 XMLResource res = (XMLResource)col.getResource(xmldbResourceId); 88 89 if (res == null) 90 throw new Exception ("XMLDB:XMLResource <"+xmldbResourceId+"> not found"); 91 92 String content = (String ) res.getContent(); 93 94 WellFormedUniFormTree tree = new WellFormedUniFormTree (); 95 tree.init ( new InputSource (new ByteArrayInputStream (content.getBytes()) ) ); 96 97 return tree; 98 99 } catch (Exception e) { 100 101 throw new UniFormTransformationException("XmldbResource: Can not create WellFormedUniFormTree (XML) Reason: " + e); 102 } 103 } 104 105 public void save(XMLWellFormedData tree) throws IOException 106 { 107 try { 108 109 Collection col = DatabaseManager.getCollection(xmldbCollectionId); 110 String content = tree.getAsString(); 111 112 XMLResource res=(XMLResource)col.getResource(xmldbResourceId); 113 114 if (res == null) 115 throw new Exception ("XMLDB:XMLResource <"+xmldbResourceId+"> not found!"); 116 117 res.setContent(content); 118 col.storeResource( res ); 119 120 } catch (Exception e) { 121 122 throw new IOException ("XmldbResource: Can not save WellFormedUniFormTree (XML) Reason: " + e); 123 } 124 125 } 126 127 130 public void create(XMLWellFormedData initTree) throws IOException 131 { 132 133 try { 134 135 Collection col = DatabaseManager.getCollection(xmldbCollectionId); 136 137 if ((XMLResource)col.getResource(xmldbResourceId) != null) 139 throw new Exception ("XMLDB:XMLResource <"+xmldbResourceId+"> already exist!"); 140 141 XMLResource res = (XMLResource)col.createResource(xmldbResourceId,"XMLResource"); 142 res.setContent("<null/>"); 143 144 col.storeResource( res ); 145 146 147 } catch (Exception e) { 148 149 throw new IOException ("XmldbResource: Can not create Resource:"+resourceId+ " Reason: " + e); 150 } 151 152 } 153 154 157 public void drop() throws IOException 158 { 159 try { 160 161 Collection col = DatabaseManager.getCollection(xmldbCollectionId); 162 Resource res=(Resource)col.getResource(xmldbResourceId); 163 164 if (res == null) 165 throw new Exception ("XMLDB:XMLResource <"+xmldbResourceId+"> not found!"); 166 167 col.removeResource(col.getResource(xmldbResourceId)); 168 169 } catch (Exception e) { 170 171 throw new IOException ("XmldbResource: Can not drop Resource:"+resourceId+ " Reason: " + e); 172 } 173 } 174 175 } 176 | Popular Tags |