KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > xml > querying > impl > xtas > resource > plugins > XMLDBResource


1 package org.exoplatform.services.xml.querying.impl.xtas.resource.plugins;
2
3 import java.net.MalformedURLException JavaDoc;
4
5 import java.io.IOException JavaDoc;
6 import java.io.ByteArrayInputStream JavaDoc;
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 JavaDoc;
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 /**
22  * Represents XML resource mapped to XML:DB compatible database (see www.xmldb.org)
23  * Such as Apache's Xindice or eXist(exist-db.org)
24  * NOTE: We use XMLDB collection or resource to execute XTAS query on it,
25  * we does not use query related (XUpdate or XMLDB-XPath) services...
26  * URL have to look like: xmldb:database-id://host-address-part/db/collection#resourceId
27  * @version $Id: XMLDBResource.java 566 2005-01-25 12:50:49Z kravchuk $
28  */

29 public class XMLDBResource extends Resource {
30
31
32     // XMLDB Database defined as Resource's context
33
private Database context = null;
34
35     private String JavaDoc xmldbCollectionId = null;
36     private String JavaDoc xmldbResourceId = null;
37
38     /**
39      * Creates resource named <code> resourceId </code>
40      */

41     public XMLDBResource(String JavaDoc resourceId) throws MalformedURLException JavaDoc
42     {
43        this();
44        this.resourceId = resourceId;
45     }
46
47     public XMLDBResource() throws MalformedURLException JavaDoc
48     {
49        int resIndex = resourceId.indexOf("#")+1;
50        if (resIndex > 0)
51           xmldbResourceId = resourceId.substring(resIndex);
52        else
53           throw new MalformedURLException JavaDoc("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 JavaDoc context)
60     {
61
62        if( context instanceof Database )
63           this.context = (Database) context;
64 /*
65        else if( context instanceof String ) {
66
67           Class cl = Class.forName((String) context);
68           this.context = (Database)cl.newInstance();
69           DatabaseManager.registerDatabase(this.context);
70 }*/

71
72         else
73           throw new ClassCastException JavaDoc("XmlDbResource.setContext():"+context.getClass().getName()+" not subclass of org.xmldb.api.base.Database!");
74
75     }
76
77     /**
78      * Loads XML from the Servlet Resource
79      */

80     public XMLWellFormedData load() throws UniFormTransformationException, IOException JavaDoc
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 JavaDoc("XMLDB:XMLResource <"+xmldbResourceId+"> not found");
91
92             String JavaDoc content = (String JavaDoc) res.getContent();
93
94             WellFormedUniFormTree tree = new WellFormedUniFormTree ();
95             tree.init ( new InputSource JavaDoc(new ByteArrayInputStream JavaDoc(content.getBytes()) ) );
96
97             return tree;
98
99         } catch (Exception JavaDoc 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 JavaDoc
106     {
107         try {
108
109            Collection col = DatabaseManager.getCollection(xmldbCollectionId);
110            String JavaDoc content = tree.getAsString();
111
112            XMLResource res=(XMLResource)col.getResource(xmldbResourceId);
113
114            if (res == null)
115                throw new Exception JavaDoc("XMLDB:XMLResource <"+xmldbResourceId+"> not found!");
116
117            res.setContent(content);
118            col.storeResource( res );
119
120         } catch (Exception JavaDoc e) {
121
122             throw new IOException JavaDoc("XmldbResource: Can not save WellFormedUniFormTree (XML) Reason: " + e);
123         }
124
125     }
126
127     /**
128      * Creates resource by Id
129      */

130     public void create(XMLWellFormedData initTree) throws IOException JavaDoc
131     {
132
133         try {
134
135            Collection col = DatabaseManager.getCollection(xmldbCollectionId);
136
137            // For XML Resource only!
138
if ((XMLResource)col.getResource(xmldbResourceId) != null)
139                throw new Exception JavaDoc("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 JavaDoc e) {
148
149             throw new IOException JavaDoc("XmldbResource: Can not create Resource:"+resourceId+ " Reason: " + e);
150         }
151
152     }
153
154     /**
155      * drops resource
156      */

157     public void drop() throws IOException JavaDoc
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 JavaDoc("XMLDB:XMLResource <"+xmldbResourceId+"> not found!");
166
167            col.removeResource(col.getResource(xmldbResourceId));
168
169         } catch (Exception JavaDoc e) {
170
171             throw new IOException JavaDoc("XmldbResource: Can not drop Resource:"+resourceId+ " Reason: " + e);
172         }
173     }
174
175 }
176
Popular Tags