KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * METSDisseminationCrosswalk.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.ByteArrayOutputStream JavaDoc;
53 import java.io.ByteArrayInputStream JavaDoc;
54 import java.io.OutputStream JavaDoc;
55
56 import java.sql.SQLException JavaDoc;
57 import org.apache.log4j.Logger;
58
59 import org.dspace.core.Context;
60 import org.dspace.core.Constants;
61 import org.dspace.content.Item;
62 import org.dspace.content.DCDate;
63 import org.dspace.content.DCValue;
64 import org.dspace.content.DSpaceObject;
65 import org.dspace.content.packager.PackageParameters;
66 import org.dspace.content.packager.PackageDisseminator;
67 import org.dspace.content.packager.PackageException;
68 import org.dspace.authorize.AuthorizeException;
69 import org.dspace.core.ConfigurationManager;
70 import org.dspace.core.PluginManager;
71
72 import org.jdom.*;
73 import org.jdom.output.XMLOutputter;
74 import org.jdom.output.Format;
75 import org.jdom.input.SAXBuilder;
76 import org.jdom.input.JDOMParseException;
77
78 /**
79  * METS dissemination crosswalk
80  * <p>
81  * Produces a METS manifest for the DSpace item as a metadata
82  * description -- intended to work within an application like the
83  * OAI server.
84  *
85  * @author Larry Stone
86  * @version $Revision: 1.1 $
87  */

88 public class METSDisseminationCrosswalk
89     implements DisseminationCrosswalk
90 {
91     /** log4j category */
92     private static Logger log = Logger.getLogger(METSDisseminationCrosswalk.class);
93
94     // Plugin Name of METS packager to use for manifest;
95
// maybe make this configurable.
96
private static final String JavaDoc METS_PACKAGER_PLUGIN = "METS";
97
98     /**
99      * MODS namespace.
100      */

101     public static final Namespace MODS_NS =
102         Namespace.getNamespace("mods", "http://www.loc.gov/mods/v3");
103
104     private static final Namespace XLINK_NS =
105         Namespace.getNamespace("xlink", "http://www.w3.org/TR/xlink");
106
107
108     /** METS namespace -- includes "mets" prefix for use in XPaths */
109     private static Namespace METS_NS = Namespace
110             .getNamespace("mets", "http://www.loc.gov/METS/");
111
112     private static final Namespace namespaces[] = { METS_NS, MODS_NS, XLINK_NS };
113
114     /** URL of METS XML Schema */
115     private static final String JavaDoc METS_XSD = "http://www.loc.gov/standards/mets/mets.xsd";
116
117     private static final String JavaDoc schemaLocation =
118         METS_NS.getURI()+" "+METS_XSD;
119
120     private static XMLOutputter outputUgly = new XMLOutputter();
121     private static XMLOutputter outputPretty = new XMLOutputter(Format.getPrettyFormat());
122     private static SAXBuilder builder = new SAXBuilder();
123
124     public Namespace[] getNamespaces()
125     {
126         return namespaces;
127     }
128
129     public String JavaDoc getSchemaLocation()
130     {
131         return schemaLocation;
132     }
133
134     public List JavaDoc disseminateList(DSpaceObject dso)
135         throws CrosswalkException,
136                IOException JavaDoc, SQLException JavaDoc, AuthorizeException
137     {
138         List JavaDoc result = new ArrayList JavaDoc(1);
139         result.add(disseminateElement(dso));
140         return result;
141     }
142
143     public Element disseminateElement(DSpaceObject dso)
144         throws CrosswalkException,
145                IOException JavaDoc, SQLException JavaDoc, AuthorizeException
146     {
147         if (dso.getType() != Constants.ITEM)
148             throw new CrosswalkObjectNotSupported("METSDisseminationCrosswalk can only crosswalk an Item.");
149         Item item = (Item)dso;
150
151         PackageDisseminator dip = (PackageDisseminator)
152           PluginManager.getNamedPlugin(PackageDisseminator.class, METS_PACKAGER_PLUGIN);
153         if (dip == null)
154             throw new CrosswalkInternalException("Cannot find a disseminate plugin for package="+METS_PACKAGER_PLUGIN);
155
156         try
157         {
158             // Set the manifestOnly=true param so we just get METS document
159
PackageParameters pparams = new PackageParameters();
160             pparams.put("manifestOnly", "true");
161
162             // "pipe" the output into a parser to create JDOM document.
163
ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
164             Context context = new Context();
165             dip.disseminate(context, item, pparams, baos);
166             ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(baos.toByteArray());
167
168             try
169             {
170                 SAXBuilder builder = new SAXBuilder();
171                 Document metsDocument = builder.build(bais);
172                 return metsDocument.getRootElement();
173             }
174             catch (JDOMException je)
175             {
176                 throw new MetadataValidationException("Error parsing METS: ",je);
177             }
178         }
179         catch (PackageException pe)
180         {
181             throw new CrosswalkInternalException("Failed making METS manifest in packager: ",pe);
182         }
183     }
184
185     public boolean canDisseminate(DSpaceObject dso)
186     {
187         return true;
188     }
189
190     public boolean preferList()
191     {
192         return false;
193     }
194 }
195
Popular Tags