KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dspace > content > crosswalk > XSLTDisseminationCrosswalk


1 /*
2  * XSLTDisseminationCrosswalk.java
3  *
4  * Version: $Revision: 1.1 $
5  *
6  * Date: $Date: 2006/03/17 00:04:38 $
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
41 package org.dspace.content.crosswalk;
42
43 import java.io.InputStream JavaDoc;
44 import java.io.IOException JavaDoc;
45 import java.sql.SQLException JavaDoc;
46 import java.util.Iterator JavaDoc;
47 import java.util.List JavaDoc;
48 import java.util.ArrayList JavaDoc;
49 import java.util.HashMap JavaDoc;
50 import java.util.Properties JavaDoc;
51 import java.util.Enumeration JavaDoc;
52 import java.io.OutputStream JavaDoc;
53 import java.io.StringReader JavaDoc;
54 import java.io.File JavaDoc;
55 import java.io.FileInputStream JavaDoc;
56
57 import java.sql.SQLException JavaDoc;
58 import org.apache.log4j.Logger;
59
60 import org.dspace.core.Context;
61 import org.dspace.core.Constants;
62 import org.dspace.content.Item;
63 import org.dspace.content.DCDate;
64 import org.dspace.content.DCValue;
65 import org.dspace.content.DSpaceObject;
66 import org.dspace.authorize.AuthorizeException;
67 import org.dspace.core.ConfigurationManager;
68 import org.dspace.core.SelfNamedPlugin;
69
70 import org.jdom.*;
71 import org.jdom.output.XMLOutputter;
72 import org.jdom.output.Format;
73 import org.jdom.input.SAXBuilder;
74 import org.jdom.input.JDOMParseException;
75 import org.jdom.xpath.XPath;
76 import org.jdom.transform.XSLTransformer;
77 import org.jdom.transform.XSLTransformException;
78
79 /**
80  * Configurable XSLT-driven dissemination Crosswalk
81  * <p>
82  * See the XSLTCrosswalk superclass for details on configuration.
83  * <p>
84  * <h3>Additional Configuration of Dissemination crosswalk:</h3>
85  * The disseminator also needs to be configured with an XML Namespace
86  * (including prefix and URI) and an XML Schema for output format. This
87  * is configured on additional properties in the DSpace Configuration, i.e.:
88  * <pre>
89  * crosswalk.dissemination.<i>PluginName</i>.namespace.<i>Prefix</i> = <i>namespace-URI</i>
90  * crosswalk.dissemination.<i>PluginName</i>.schemaLocation = <i>schemaLocation value</i>
91  * crosswalk.dissemination.<i>PluginName</i>.preferList = <i>boolean</i> (default is false)
92  * </pre>
93  * For example:
94  * <pre>
95  * crosswalk.dissemination.qdc.namespace.dc = http://purl.org/dc/elements/1.1/
96  * crosswalk.dissemination.qdc.namespace.dcterms = http://purl.org/dc/terms/
97  * crosswalk.dissemination.qdc.schemaLocation = \
98  * http://purl.org/dc/elements/1.1/ http://dublincore.org/schemas/xmls/qdc/2003/04/02/qualifieddc.xsd
99  * crosswalk.dissemination.qdc.preferList = true
100  * </pre>
101  *
102  * @author Larry Stone
103  * @version $Revision: 1.1 $
104  * @see XSLTCrosswalk
105  */

106 public class XSLTDisseminationCrosswalk
107     extends XSLTCrosswalk
108     implements DisseminationCrosswalk
109 {
110     /** log4j category */
111     private static Logger log = Logger.getLogger(XSLTDisseminationCrosswalk.class);
112
113     private final static String JavaDoc DIRECTION = "dissemination";
114
115     private static XMLOutputter outputPretty = new XMLOutputter(Format.getPrettyFormat());
116
117     private static String JavaDoc aliases[] = makeAliases(DIRECTION);
118
119     public static String JavaDoc[] getPluginNames()
120     {
121         return aliases;
122     }
123
124     // namespace and schema; don't worry about initializing these
125
// until there's an instance, so do it in constructor.
126
private String JavaDoc schemaLocation = null;
127
128     private Namespace namespaces[] = null;
129
130     private String JavaDoc rootName = null;
131
132     private boolean preferList = false;
133
134     // load the namespace and schema from config
135
private void init()
136         throws CrosswalkInternalException
137     {
138         if (namespaces != null || schemaLocation != null)
139             return;
140         String JavaDoc myAlias = getPluginInstanceName();
141         if (myAlias == null)
142         {
143             log.error("Must use PluginManager to instantiate XSLTDisseminationCrosswalk so the class knows its name.");
144             throw new CrosswalkInternalException("Must use PluginManager to instantiate XSLTDisseminationCrosswalk so the class knows its name.");
145         }
146
147         // all configs for this plugin instance start with this:
148
String JavaDoc prefix = CONFIG_PREFIX+DIRECTION+"."+myAlias+".";
149
150         // get the schema location string, should already be in the
151
// right format for value of "schemaLocation" attribute.
152
schemaLocation = ConfigurationManager.getProperty(prefix+"schemaLocation");
153         if (schemaLocation == null)
154             log.warn("No schemaLocation for crosswalk="+myAlias+", key="+prefix+"schemaLocation");
155
156         // sanity check: schemaLocation should have space.
157
else if (schemaLocation.length() > 0 && schemaLocation.indexOf(" ") < 0)
158             log.warn("Possible INVALID schemaLocation (no space found) for crosswalk="+
159                       myAlias+", key="+prefix+"schemaLocation"+
160                       "\n\tCorrect format is \"{namespace} {schema-URL}\"");
161
162         // grovel for namespaces of the form:
163
// crosswalk.diss.{PLUGIN_NAME}.namespace.{PREFIX} = {URI}
164
String JavaDoc nsPrefix = prefix + "namespace.";
165         Enumeration JavaDoc pe = ConfigurationManager.propertyNames();
166         List JavaDoc nsList = new ArrayList JavaDoc();
167         while (pe.hasMoreElements())
168         {
169             String JavaDoc key = (String JavaDoc)pe.nextElement();
170             if (key.startsWith(nsPrefix))
171                 nsList.add(Namespace.getNamespace(key.substring(nsPrefix.length()),
172                              ConfigurationManager.getProperty(key)));
173         }
174         namespaces = (Namespace[])nsList.toArray(new Namespace[nsList.size()]);
175
176         preferList = ConfigurationManager.getBooleanProperty(prefix+"preferList", false);
177     }
178
179     public Namespace[] getNamespaces()
180     {
181         try
182         {
183             init();
184         }
185         catch (CrosswalkInternalException e)
186         {
187             log.error(e.toString());
188         }
189         return namespaces;
190     }
191
192     public String JavaDoc getSchemaLocation()
193     {
194         try
195         {
196             init();
197         }
198         catch (CrosswalkInternalException e)
199         {
200             log.error(e.toString());
201         }
202         return schemaLocation;
203     }
204
205     public Element disseminateElement(DSpaceObject dso)
206         throws CrosswalkException,
207                IOException JavaDoc, SQLException JavaDoc, AuthorizeException
208     {
209         init();
210
211         if (dso.getType() != Constants.ITEM)
212             throw new CrosswalkObjectNotSupported("XSLTDisseminationCrosswalk can only crosswalk an Item.");
213         Item item = (Item)dso;
214         XSLTransformer xform = getTransformer(DIRECTION);
215         if (xform == null)
216             throw new CrosswalkInternalException("Failed to initialize transformer, probably error loading stylesheet.");
217
218         try
219         {
220             Document ddim = new Document(getDim(item));
221             Document result = xform.transform(ddim);
222             return result.getRootElement();
223         }
224         catch (XSLTransformException e)
225         {
226             log.error("Got error: "+e.toString());
227             throw new CrosswalkInternalException("XSL translation failed: "+e.toString());
228         }
229     }
230
231     public List JavaDoc disseminateList(DSpaceObject dso)
232         throws CrosswalkException,
233                IOException JavaDoc, SQLException JavaDoc, AuthorizeException
234     {
235         init();
236
237         if (dso.getType() != Constants.ITEM)
238             throw new CrosswalkObjectNotSupported("XSLTDisseminationCrosswalk can only crosswalk an Item.");
239         Item item = (Item)dso;
240         XSLTransformer xform = getTransformer(DIRECTION);
241         if (xform == null)
242             throw new CrosswalkInternalException("Failed to initialize transformer, probably error loading stylesheet.");
243
244         try
245         {
246             return xform.transform(getDim(item).getChildren());
247         }
248         catch (XSLTransformException e)
249         {
250             log.error("Got error: "+e.toString());
251             throw new CrosswalkInternalException("XSL translation failed: "+e.toString());
252         }
253     }
254
255
256     // build DIM expression of Item's metadata.
257
private Element getDim(Item item)
258     {
259         DCValue[] dc = item.getMetadata(Item.ANY, Item.ANY, Item.ANY, Item.ANY);
260         Element dim = new Element("dim", DIM_NS);
261         for (int i = 0; i < dc.length; i++)
262         {
263             Element field = new Element("field", DIM_NS);
264             field.setAttribute("mdschema", dc[i].schema);
265             field.setAttribute("element", dc[i].element);
266             if (dc[i].qualifier != null)
267                 field.setAttribute("qualifier", dc[i].qualifier);
268             if (dc[i].language != null)
269                 field.setAttribute("lang", dc[i].language);
270             if (dc[i].value != null)
271                 field.setText(dc[i].value);
272             dim.addContent(field);
273         }
274         return dim;
275     }
276
277     public boolean canDisseminate(DSpaceObject dso)
278     {
279         return dso.getType() == Constants.ITEM;
280     }
281
282     public boolean preferList()
283     {
284         try
285         {
286             init();
287         }
288         catch (CrosswalkInternalException e)
289         {
290             log.error(e.toString());
291         }
292         return preferList;
293     }
294 }
295
Popular Tags