KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * OAIDCCrosswalk.java
3  *
4  * Version: $Revision: 1.7 $
5  *
6  * Date: $Date: 2005/04/20 14:22:34 $
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
44 import org.dspace.content.DCValue;
45 import org.dspace.content.Item;
46 import org.dspace.search.HarvestedItemInfo;
47
48 import ORG.oclc.oai.server.crosswalk.Crosswalk;
49 import ORG.oclc.oai.server.verb.CannotDisseminateFormatException;
50
51 /**
52  * An OAICat Crosswalk implementation that extracts unqualified Dublin Core from
53  * DSpace items into the oai_dc format.
54  *
55  * @author Robert Tansley
56  * @version $Revision: 1.7 $
57  */

58 public class OAIDCCrosswalk extends Crosswalk
59 {
60     public OAIDCCrosswalk(Properties JavaDoc properties)
61     {
62         super(
63                 "http://www.openarchives.org/OAI/2.0/oai_dc/ http://www.openarchives.org/OAI/2.0/oai_dc.xsd");
64     }
65
66     public boolean isAvailableFor(Object JavaDoc nativeItem)
67     {
68         // We have DC for everything
69
return true;
70     }
71
72     public String JavaDoc createMetadata(Object JavaDoc nativeItem)
73             throws CannotDisseminateFormatException
74     {
75         Item item = ((HarvestedItemInfo) nativeItem).item;
76
77         // Get all the DC
78
DCValue[] allDC = item.getDC(Item.ANY, Item.ANY, Item.ANY);
79
80         StringBuffer JavaDoc metadata = new StringBuffer JavaDoc();
81
82         metadata
83                 .append(
84                         "<oai_dc:dc xmlns:oai_dc=\"http://www.openarchives.org/OAI/2.0/oai_dc/\" ")
85                 .append("xmlns:dc=\"http://purl.org/dc/elements/1.1/\" ")
86                 .append(
87                         "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" ")
88                 .append(
89                         "xsi:schemaLocation=\"http://www.openarchives.org/OAI/2.0/oai_dc/ http://www.openarchives.org/OAI/2.0/oai_dc.xsd\">");
90
91         for (int i = 0; i < allDC.length; i++)
92         {
93             // Do not include description.provenance
94
boolean description = allDC[i].element.equals("description");
95             boolean provenance = (allDC[i].qualifier != null)
96                     && allDC[i].qualifier.equals("provenance");
97
98             if (!(description && provenance))
99             {
100                 String JavaDoc element = allDC[i].element;
101
102                 // contributor.author exposed as 'creator'
103
if (allDC[i].element.equals("contributor")
104                         && (allDC[i].qualifier != null)
105                         && allDC[i].qualifier.equals("author"))
106                 {
107                     element = "creator";
108                 }
109
110                 // Escape XML chars <, > and &
111
String JavaDoc value = allDC[i].value;
112
113                 // First do &'s - need to be careful not to replace the
114
// & in "&amp;" again!
115
int c = -1;
116
117                 while ((c = value.indexOf("&", c + 1)) > -1)
118                 {
119                     value = value.substring(0, c) + "&amp;"
120                             + value.substring(c + 1);
121                 }
122
123                 while ((c = value.indexOf("<")) > -1)
124                 {
125                     value = value.substring(0, c) + "&lt;"
126                             + value.substring(c + 1);
127                 }
128
129                 while ((c = value.indexOf(">")) > -1)
130                 {
131                     value = value.substring(0, c) + "&gt;"
132                             + value.substring(c + 1);
133                 }
134
135                 metadata.append("<dc:").append(element).append(">").append(
136                         value).append("</dc:").append(element).append(">");
137             }
138         }
139
140         metadata.append("</oai_dc:dc>");
141
142         return metadata.toString();
143     }
144 }
145
Popular Tags