KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dspace > app > oai > PluginCrosswalk


1 /*
2  * PluginCrosswalk.java
3  *
4  * Version: $Revision: 1.2 $
5  *
6  * Date: $Date: 2006/03/17 00:04:39 $
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.app.oai;
41
42 import java.util.Properties JavaDoc;
43 import java.io.StringWriter JavaDoc;
44 import java.io.PrintWriter JavaDoc;
45
46 import org.dspace.content.Item;
47 import org.dspace.core.PluginManager;
48 import org.dspace.search.HarvestedItemInfo;
49 import org.dspace.content.crosswalk.DisseminationCrosswalk;
50
51 import org.apache.log4j.Logger;
52
53 import ORG.oclc.oai.server.crosswalk.Crosswalk;
54 import ORG.oclc.oai.server.verb.CannotDisseminateFormatException;
55
56 import org.jdom.*;
57 import org.jdom.output.XMLOutputter;
58 import org.jdom.output.Format;
59
60 /**
61  * An OAICat Crosswalk implementation that calls, in turn, on
62  * <code>DisseminationCrosswalk</code> plugins.
63  * It is configured so its "OAI schema label" matches the name of a
64  * <code>DisseminationCrosswalk</code> plugin. This class
65  * will then recognize its name and invoke the correct crosswalk
66  * to produce the results it sends out.
67  * <p>
68  * <b>Configuration:</b>
69  * In the OAICat configuration file (e.g. <code>oaicat.properties</code>
70  * add a line like this for each plugin crosswalk you wish to provide:
71  * <pre>
72  * Crosswalks.<cite>Plugin-Name</cite>=org.dspace.app.oai.PluginCrosswalk
73  *
74  * e.g.
75  *
76  * Crosswalks.DC=org.dspace.app.oai.PluginCrosswalk
77  * </pre>
78  * This creates an OAI metadata prefix "DC" which is implemented
79  * by the dissemination crosswalk plugin that answers to the name "DC".
80  * It, in turn, could be found in the DSpace configuration in a line like:
81  * <pre>
82  * plugin.named.org.dspace.content.crosswalk.DisseminationCrosswalk = \
83  * org.dspace.content.crosswalk.SimpleDCDisseminationCrosswalk = DC
84  * </pre>
85  *
86  * <p>
87  * Note that all OAI crosswalks are instances of this same class, since
88  * the instance gets bound to a specific <code>DisseminationCrosswalk</code>
89  * when it is created.
90  * <p>
91  * WARNING: This requires at the OAICAT java library version 1.5.38.
92  * It does NOT work with some older versions.
93  *
94  * @author Larry Stone
95  * @version $Revision: 1.2 $
96  */

97 public class PluginCrosswalk extends Crosswalk
98 {
99     /** log4j category */
100     private static Logger log = Logger.getLogger(PluginCrosswalk.class);
101
102     private DisseminationCrosswalk xwalk = null;
103
104     // preserve the label from config property for diagnostics.
105
private String JavaDoc schemaLabel = null;
106
107     private static XMLOutputter outputUgly = new XMLOutputter();
108
109     /**
110      * Prepare a "schema location" string for the oaicat Crosswalk
111      * class's initialization. This is a string consisting of the
112      * namespace URI, a space, and the schema URL, for the XML
113      * element to be included in the OAI report. This is not documented
114      * in oaicat's manuals so we mention it here.
115      *
116      * Since this gets called by the constructor, we can't initialize the
117      * xwalk field so the plugin gets thrown away.
118      */

119     private static String JavaDoc makeSchemaLocation(String JavaDoc schemaLabel)
120     {
121         DisseminationCrosswalk xwalk = (DisseminationCrosswalk)
122                     PluginManager.getNamedPlugin(DisseminationCrosswalk.class,
123                                                  schemaLabel);
124         if (xwalk != null)
125         {
126             String JavaDoc schemaLoc = xwalk.getSchemaLocation();
127
128             // initialize the oaicat Crosswalk with a "schemalocation" string,
129
// which is "{namespace-URI} {schema-URL}" (space separated)
130
if (schemaLoc != null)
131             {
132                 log.debug("Initialized schemaLabel="+schemaLabel+" with schemaLocation = \""+schemaLoc+"\"");
133                 return schemaLoc;
134             }
135             log.error("makeSchemaLocation: crosswalk cannot provide schemaLocation, label="+schemaLabel);
136             return "Error No-schemaLocation-for-"+schemaLabel;
137         }
138         log.error("No crosswalk found, makeSchemaLocation giving up, label="+schemaLabel);
139         return "Error No-crosswalk-for-"+schemaLabel;
140     }
141
142     /**
143      * Constructor; called by
144      * ORG.oclc.oai.server.crosswalk.Crosswalks, which tries first with
145      * args (String schemaLabel, Properties properties). This is
146      * convenient since it lets us use that label to initialize this
147      * instance of the plugin with the DisseminationCrosswalk crosswalk
148      * corresponding to that schemaLabel, instead of creating a subclass
149      * for each one.
150      * <p>
151      * WARNING: This requires at the OAICAT java library version 1.5.37.
152      * It does NOT work with some older versions.
153      */

154     public PluginCrosswalk(String JavaDoc schemaLabel, Properties JavaDoc properties)
155     {
156         super(makeSchemaLocation(schemaLabel));
157         xwalk = (DisseminationCrosswalk)PluginManager.getNamedPlugin(
158                     DisseminationCrosswalk.class, schemaLabel);
159         this.schemaLabel = schemaLabel;
160     }
161
162     /**
163      * @return true if this dissemination is available for the item.
164      */

165     public boolean isAvailableFor(Object JavaDoc nativeItem)
166     {
167         Item item = ((HarvestedItemInfo) nativeItem).item;
168         return xwalk.canDisseminate(item);
169     }
170
171     /**
172      * Do the crosswalk. Returns serialized XML in a string.
173      */

174     public String JavaDoc createMetadata(Object JavaDoc nativeItem)
175             throws CannotDisseminateFormatException
176     {
177         Item item = ((HarvestedItemInfo) nativeItem).item;
178         try
179         {
180             log.debug("OAI plugin, schema="+schemaLabel+", preferList="+String.valueOf(xwalk.preferList()));
181             if (xwalk.preferList())
182                 return outputUgly.outputString(xwalk.disseminateList(item));
183             else
184                 return outputUgly.outputString(xwalk.disseminateElement(item));
185         }
186         catch (Exception JavaDoc e)
187         {
188             log.error(this.getClass().getName()+
189                     ": hiding exception in CannotDisseminateFormatException:"+
190                     e.toString());
191
192                 StringWriter JavaDoc sw = new StringWriter JavaDoc();
193                 e.printStackTrace(new PrintWriter JavaDoc(sw));
194                 log.error("*** Stack trace follows:");
195                 log.error(sw.toString());
196
197             throw new CannotDisseminateFormatException(schemaLabel);
198         }
199     }
200 }
201
Popular Tags