View Javadoc
1 /*
2 * Created on Feb 21, 2003
3 *
4 * To change this generated comment go to
5 * Window>Preferences>Java>Code Generation>Code Template
6 */
7 package net.sf.fastxmldb.example;
8
9 import net.sf.fastxmldb.utils.FastXMLDBConstants;
10
11 import org.xmldb.api.DatabaseManager;
12 import org.xmldb.api.base.Collection;
13 import org.xmldb.api.base.Database;
14 import org.xmldb.api.base.Resource;
15 import org.xmldb.api.base.XMLDBException;
16 import org.xmldb.api.modules.CollectionManagementService;
17
18 /***
19 * @author jbirchfield
20 */
21 public class XMLDBClient {
22
23 static {
24 try {
25 Database database =
26 (Database) Class
27 .forName(FastXMLDBConstants.FAST_XMLDB_DATABASE_CLASSNAME)
28 .newInstance();
29 DatabaseManager.registerDatabase(database);
30 } catch (InstantiationException e) {
31 e.printStackTrace();
32 } catch (IllegalAccessException e) {
33 e.printStackTrace();
34 } catch (ClassNotFoundException e) {
35 e.printStackTrace();
36 } catch (XMLDBException e) {
37 e.printStackTrace();
38 }
39 }
40
41 /***
42 * Default constructor
43 * This class finds or creates a collection, creates a resource and
44 * sets the contents. Once done, it locates the collection again,
45 * looks up the resource and prints out the contents.
46 * @see java.lang.Object#Object()
47 */
48 public XMLDBClient() {
49 Collection collection = null;
50 try {
51 collection = findOrCreateCollection("Addresses");
52 Resource resource =
53 collection.createResource("jbirchfield", "XMLResource");
54
55 String content =
56 "<address>"
57 + "<person name=\"James Birchfield\"/>"
58 + "<street>123 Easy Street</street>"
59 + "<city>Easyville</city>"
60 + "<state>KY</state>"
61 + "<zip>12345</zip>"
62 + "</address>";
63
64 resource.setContent(content);
65 } catch (XMLDBException e) {
66 e.printStackTrace();
67 } finally {
68 try {
69 collection.close();
70 } catch (XMLDBException e) {
71 e.printStackTrace();
72 }
73
74 }
75
76 //now that we have created a resource, let's look it up and print it out
77
78 try {
79 collection = findOrCreateCollection("Addresses");
80 Resource resource = collection.getResource("jbirchfield");
81 System.out.println(resource.getContent());
82
83 } catch (XMLDBException e) {
84 e.printStackTrace();
85 } finally {
86 try {
87 collection.close();
88 } catch (XMLDBException e) {
89 e.printStackTrace();
90 }
91
92 }
93 }
94
95 /***
96 * Finds or creates a collection
97 * @param name the name of the collection
98 * @return Collection the Collection found or created
99 * @throws XMLDBException if anything goes wrong
100 */
101 public Collection findOrCreateCollection(String name)
102 throws XMLDBException {
103
104 Collection collection =
105 DatabaseManager.getCollection(
106 FastXMLDBConstants.DEFAULT_COLLECTION_URI + name);
107 if (collection != null) {
108 return collection;
109 }
110 Collection col =
111 DatabaseManager.getCollection(
112 FastXMLDBConstants.DEFAULT_COLLECTION_URI);
113
114 CollectionManagementService service =
115 (CollectionManagementService) col.getService(
116 "CollectionManagementService",
117 "1.0");
118 return service.createCollection("Addresses");
119 }
120
121 /***
122 * Main entry poitn
123 * @param args no arguments needed
124 */
125 public static void main(String[] args) {
126 new XMLDBClient();
127 }
128 }
This page was automatically generated by Maven