KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dspace > administer > RegistryLoader


1 /*
2  * RegistryLoader.java
3  *
4  * Version: $Revision: 1.9 $
5  *
6  * Date: $Date: 2005/11/16 21:40:50 $
7  *
8  * Copyright (c) 2002-2005, Hewlett-Packard Company and Massachusetts
9  * Institute of Technology. All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions are
13  * met:
14  *
15  * - Redistributions of source code must retain the above copyright
16  * notice, this list of conditions and the following disclaimer.
17  *
18  * - Redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in the
20  * documentation and/or other materials provided with the distribution.
21  *
22  * - Neither the name of the Hewlett-Packard Company nor the name of the
23  * Massachusetts Institute of Technology nor the names of their
24  * contributors may be used to endorse or promote products derived from
25  * this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
38  * DAMAGE.
39  */

40 package org.dspace.administer;
41
42 import java.io.File JavaDoc;
43 import java.io.IOException JavaDoc;
44 import java.sql.SQLException JavaDoc;
45
46 import javax.xml.parsers.DocumentBuilder JavaDoc;
47 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
48 import javax.xml.parsers.ParserConfigurationException JavaDoc;
49 import javax.xml.transform.TransformerException JavaDoc;
50
51 import org.apache.log4j.Logger;
52 import org.apache.xpath.XPathAPI;
53 import org.dspace.authorize.AuthorizeException;
54 import org.dspace.content.BitstreamFormat;
55 import org.dspace.content.MetadataField;
56 import org.dspace.content.MetadataSchema;
57 import org.dspace.content.NonUniqueMetadataException;
58 import org.dspace.core.Context;
59 import org.dspace.core.LogManager;
60 import org.w3c.dom.Document JavaDoc;
61 import org.w3c.dom.Node JavaDoc;
62 import org.w3c.dom.NodeList JavaDoc;
63 import org.xml.sax.SAXException JavaDoc;
64
65 /**
66  * Loads the bitstream format and Dublin Core type registries into the database.
67  * Intended for use as a command-line tool.
68  * <P>
69  * Example usage:
70  * <P>
71  * <code>RegistryLoader -bitstream bitstream-formats.xml</code>
72  * <P>
73  * <code>RegistryLoader -dc dc-types.xml</code>
74  *
75  * @author Robert Tansley
76  * @version $Revision: 1.9 $
77  */

78 public class RegistryLoader
79 {
80     /** log4j category */
81     private static Logger log = Logger.getLogger(RegistryLoader.class);
82
83     /**
84      * For invoking via the command line
85      *
86      * @param argv
87      * command-line arguments
88      */

89     public static void main(String JavaDoc[] argv) throws Exception JavaDoc
90     {
91         String JavaDoc usage = "Usage: " + RegistryLoader.class.getName()
92                 + " (-bitstream | -dc) registry-file.xml";
93
94         Context context = null;
95
96         try
97         {
98             context = new Context();
99
100             // Can't update registries anonymously, so we need to turn off
101
// authorisation
102
context.setIgnoreAuthorization(true);
103
104             // Work out what we're loading
105
if (argv[0].equalsIgnoreCase("-bitstream"))
106             {
107                 RegistryLoader.loadBitstreamFormats(context, argv[1]);
108             }
109             else if (argv[0].equalsIgnoreCase("-dc"))
110             {
111                 loadDublinCoreTypes(context, argv[1]);
112             }
113             else
114             {
115                 System.err.println(usage);
116             }
117
118             context.complete();
119
120             System.exit(0);
121         }
122         catch (ArrayIndexOutOfBoundsException JavaDoc ae)
123         {
124             System.err.println(usage);
125
126             if (context != null)
127             {
128                 context.abort();
129             }
130
131             System.exit(1);
132         }
133         catch (Exception JavaDoc e)
134         {
135             log.fatal(LogManager.getHeader(context, "error_loading_registries",
136                     ""), e);
137
138             if (context != null)
139             {
140                 context.abort();
141             }
142
143             System.exit(1);
144         }
145     }
146
147     /**
148      * Load Bitstream Format metadata
149      *
150      * @param context
151      * DSpace context object
152      * @param filename
153      * the filename of the XML file to load
154      */

155     public static void loadBitstreamFormats(Context context, String JavaDoc filename)
156             throws SQLException JavaDoc, IOException JavaDoc, ParserConfigurationException JavaDoc,
157             SAXException JavaDoc, TransformerException JavaDoc, AuthorizeException
158     {
159         Document JavaDoc document = loadXML(filename);
160
161         // Get the nodes corresponding to formats
162
NodeList JavaDoc typeNodes = XPathAPI.selectNodeList(document,
163                 "dspace-bitstream-types/bitstream-type");
164
165         // Add each one as a new format to the registry
166
for (int i = 0; i < typeNodes.getLength(); i++)
167         {
168             Node JavaDoc n = typeNodes.item(i);
169             loadFormat(context, n);
170         }
171
172         log.info(LogManager.getHeader(context, "load_bitstream_formats",
173                 "number_loaded=" + typeNodes.getLength()));
174     }
175
176     /**
177      * Process a node in the bitstream format registry XML file. The node must
178      * be a "bitstream-type" node
179      *
180      * @param context
181      * DSpace context object
182      * @param node
183      * the node in the DOM tree
184      */

185     private static void loadFormat(Context context, Node JavaDoc node)
186             throws SQLException JavaDoc, IOException JavaDoc, TransformerException JavaDoc,
187             AuthorizeException
188     {
189         // Get the values
190
String JavaDoc mimeType = getElementData(node, "mimetype");
191         String JavaDoc shortDesc = getElementData(node, "short_description");
192         String JavaDoc desc = getElementData(node, "description");
193
194         String JavaDoc supportLevelString = getElementData(node, "support_level");
195         int supportLevel = Integer.parseInt(supportLevelString);
196
197         String JavaDoc internalString = getElementData(node, "internal");
198         boolean internal = new Boolean JavaDoc(internalString).booleanValue();
199
200         String JavaDoc[] extensions = getRepeatedElementData(node, "extension");
201
202         // Create the format object
203
BitstreamFormat format = BitstreamFormat.create(context);
204
205         // Fill it out with the values
206
format.setMIMEType(mimeType);
207         format.setShortDescription(shortDesc);
208         format.setDescription(desc);
209         format.setSupportLevel(supportLevel);
210         format.setInternal(internal);
211         format.setExtensions(extensions);
212
213         // Write to database
214
format.update();
215     }
216
217     /**
218      * Load Dublin Core types
219      *
220      * @param context
221      * DSpace context object
222      * @param filename
223      * the filename of the XML file to load
224      * @throws NonUniqueMetadataException
225      */

226     public static void loadDublinCoreTypes(Context context, String JavaDoc filename)
227             throws SQLException JavaDoc, IOException JavaDoc, ParserConfigurationException JavaDoc,
228             SAXException JavaDoc, TransformerException JavaDoc, AuthorizeException,
229             NonUniqueMetadataException
230     {
231         Document JavaDoc document = loadXML(filename);
232
233         // Get the nodes corresponding to formats
234
NodeList JavaDoc typeNodes = XPathAPI.selectNodeList(document,
235                 "/dspace-dc-types/dc-type");
236
237         // Add each one as a new format to the registry
238
for (int i = 0; i < typeNodes.getLength(); i++)
239         {
240             Node JavaDoc n = typeNodes.item(i);
241             loadDCType(context, n);
242         }
243
244         log.info(LogManager.getHeader(context, "load_dublin_core_types",
245                 "number_loaded=" + typeNodes.getLength()));
246     }
247
248     /**
249      * Process a node in the bitstream format registry XML file. The node must
250      * be a "bitstream-type" node
251      *
252      * @param context
253      * DSpace context object
254      * @param node
255      * the node in the DOM tree
256      * @throws NonUniqueMetadataException
257      */

258     private static void loadDCType(Context context, Node JavaDoc node)
259             throws SQLException JavaDoc, IOException JavaDoc, TransformerException JavaDoc,
260             AuthorizeException, NonUniqueMetadataException
261     {
262         // Get the values
263
String JavaDoc schema = getElementData(node, "schema");
264         String JavaDoc element = getElementData(node, "element");
265         String JavaDoc qualifier = getElementData(node, "qualifier");
266         String JavaDoc scopeNote = getElementData(node, "scope_note");
267
268         // If the schema is not provided default to DC
269
if (schema == null)
270         {
271             schema = MetadataSchema.DC_SCHEMA;
272         }
273
274         // Find the matching schema object
275
MetadataSchema schemaObj = MetadataSchema.find(context, schema);
276         
277         MetadataField field = new MetadataField();
278         field.setSchemaID(schemaObj.getSchemaID());
279         field.setElement(element);
280         field.setQualifier(qualifier);
281         field.setScopeNote(scopeNote);
282         field.create(context);
283     }
284
285     // ===================== XML Utility Methods =========================
286

287     /**
288      * Load in the XML from file.
289      *
290      * @param filename
291      * the filename to load from
292      *
293      * @return the DOM representation of the XML file
294      */

295     private static Document JavaDoc loadXML(String JavaDoc filename) throws IOException JavaDoc,
296             ParserConfigurationException JavaDoc, SAXException JavaDoc
297     {
298         DocumentBuilder JavaDoc builder = DocumentBuilderFactory.newInstance()
299                 .newDocumentBuilder();
300
301         return builder.parse(new File JavaDoc(filename));
302     }
303
304     /**
305      * Get the CDATA of a particular element. For example, if the XML document
306      * contains:
307      * <P>
308      * <code>
309      * &lt;foo&gt;&lt;mimetype&gt;application/pdf&lt;/mimetype&gt;&lt;/foo&gt;
310      * </code>
311      * passing this the <code>foo</code> node and <code>mimetype</code> will
312      * return <code>application/pdf</code>.
313      * </P>
314      * Why this isn't a core part of the XML API I do not know...
315      *
316      * @param parentElement
317      * the element, whose child element you want the CDATA from
318      * @param childName
319      * the name of the element you want the CDATA from
320      *
321      * @return the CDATA as a <code>String</code>
322      */

323     private static String JavaDoc getElementData(Node JavaDoc parentElement, String JavaDoc childName)
324             throws TransformerException JavaDoc
325     {
326         // Grab the child node
327
Node JavaDoc childNode = XPathAPI.selectSingleNode(parentElement, childName);
328
329         if (childNode == null)
330         {
331             // No child node, so no values
332
return null;
333         }
334
335         // Get the #text
336
Node JavaDoc dataNode = childNode.getFirstChild();
337
338         if (dataNode == null)
339         {
340             return null;
341         }
342
343         // Get the data
344
String JavaDoc value = dataNode.getNodeValue().trim();
345
346         return value;
347     }
348
349     /**
350      * Get repeated CDATA for a particular element. For example, if the XML
351      * document contains:
352      * <P>
353      * <code>
354      * &lt;foo&gt;
355      * &lt;bar&gt;val1&lt;/bar&gt;
356      * &lt;bar&gt;val2&lt;/bar&gt;
357      * &lt;/foo&gt;
358      * </code>
359      * passing this the <code>foo</code> node and <code>bar</code> will
360      * return <code>val1</code> and <code>val2</code>.
361      * </P>
362      * Why this also isn't a core part of the XML API I do not know...
363      *
364      * @param parentElement
365      * the element, whose child element you want the CDATA from
366      * @param childName
367      * the name of the element you want the CDATA from
368      *
369      * @return the CDATA as a <code>String</code>
370      */

371     private static String JavaDoc[] getRepeatedElementData(Node JavaDoc parentElement,
372             String JavaDoc childName) throws TransformerException JavaDoc
373     {
374         // Grab the child node
375
NodeList JavaDoc childNodes = XPathAPI.selectNodeList(parentElement, childName);
376
377         String JavaDoc[] data = new String JavaDoc[childNodes.getLength()];
378
379         for (int i = 0; i < childNodes.getLength(); i++)
380         {
381             // Get the #text node
382
Node JavaDoc dataNode = childNodes.item(i).getFirstChild();
383
384             // Get the data
385
data[i] = dataNode.getNodeValue().trim();
386         }
387
388         return data;
389     }
390 }
391
Popular Tags