1
|
/**
|
2
|
* Copyright 2006 OCLC Online Computer Library Center Licensed under the Apache
|
3
|
* License, Version 2.0 (the "License"); you may not use this file except in
|
4
|
* compliance with the License. You may obtain a copy of the License at
|
5
|
* http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or
|
6
|
* agreed to in writing, software distributed under the License is distributed on
|
7
|
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
|
8
|
* or implied. See the License for the specific language governing permissions and
|
9
|
* limitations under the License.
|
10
|
*/
|
11
|
package edu.ucsb.nceas.metacat.oaipmh.provider.server.crosswalk;
|
12
|
|
13
|
import java.io.FileInputStream;
|
14
|
import java.io.StringReader;
|
15
|
import java.io.StringWriter;
|
16
|
import java.util.HashMap;
|
17
|
import java.util.Properties;
|
18
|
|
19
|
import javax.xml.transform.Transformer;
|
20
|
import javax.xml.transform.TransformerFactory;
|
21
|
import javax.xml.transform.stream.StreamResult;
|
22
|
import javax.xml.transform.stream.StreamSource;
|
23
|
|
24
|
import org.apache.commons.io.IOUtils;
|
25
|
|
26
|
import ORG.oclc.oai.server.crosswalk.Crosswalk;
|
27
|
import ORG.oclc.oai.server.verb.CannotDisseminateFormatException;
|
28
|
import ORG.oclc.oai.server.verb.OAIInternalServerError;
|
29
|
|
30
|
|
31
|
/**
|
32
|
* Convert native "item" to oai_dc. In this case, the native "item" is assumed
|
33
|
* to already be formatted as an OAI <record> element, with the possible
|
34
|
* exception that multiple metadataFormats may be present in the <metadata>
|
35
|
* element. The "crosswalk", merely involves pulling out the one that is
|
36
|
* requested.
|
37
|
*/
|
38
|
public class Eml2oai_dc extends Crosswalk {
|
39
|
|
40
|
/* Class fields */
|
41
|
|
42
|
|
43
|
private static String dirPath = null;
|
44
|
private static final String XSLT_NAME_200 = "eml200toDublinCore.xsl";
|
45
|
private static final String XSLT_NAME_201 = "eml201toDublinCore.xsl";
|
46
|
private static final String XSLT_NAME_210 = "eml210toDublinCore.xsl";
|
47
|
private static final String XSLT_NAME_211 = "eml211toDublinCore.xsl";
|
48
|
|
49
|
private static final String SCHEMA_LOCATION =
|
50
|
"http://www.openarchives.org/OAI/2.0/oai_dc/ " +
|
51
|
"http://www.openarchives.org/OAI/2.0/oai_dc.xsd";
|
52
|
|
53
|
|
54
|
/* Instance fields */
|
55
|
|
56
|
private Transformer transformer = null;
|
57
|
private Transformer transformer200 = null;
|
58
|
private Transformer transformer201 = null;
|
59
|
private Transformer transformer210 = null;
|
60
|
private Transformer transformer211 = null;
|
61
|
|
62
|
|
63
|
/* Constructors */
|
64
|
|
65
|
/**
|
66
|
* The constructor assigns the schemaLocation associated with this crosswalk.
|
67
|
* Since the crosswalk is trivial in this case, no properties are utilized.
|
68
|
*
|
69
|
* @param properties
|
70
|
* properties that are needed to configure the crosswalk.
|
71
|
*/
|
72
|
public Eml2oai_dc(Properties properties) throws OAIInternalServerError {
|
73
|
super(SCHEMA_LOCATION);
|
74
|
String xsltPath200 = dirPath + "/" + XSLT_NAME_200;
|
75
|
String xsltPath201 = dirPath + "/" + XSLT_NAME_201;
|
76
|
String xsltPath210 = dirPath + "/" + XSLT_NAME_210;
|
77
|
String xsltPath211 = dirPath + "/" + XSLT_NAME_211;
|
78
|
|
79
|
FileInputStream fileInputStream200 = null;
|
80
|
FileInputStream fileInputStream201 = null;
|
81
|
FileInputStream fileInputStream210 = null;
|
82
|
FileInputStream fileInputStream211 = null;
|
83
|
|
84
|
|
85
|
try {
|
86
|
TransformerFactory tFactory200 = TransformerFactory.newInstance();
|
87
|
fileInputStream200 = new FileInputStream(xsltPath200);
|
88
|
StreamSource xslSource200 = new StreamSource(fileInputStream200);
|
89
|
this.transformer200 = tFactory200.newTransformer(xslSource200);
|
90
|
fileInputStream200.close();
|
91
|
|
92
|
TransformerFactory tFactory201 = TransformerFactory.newInstance();
|
93
|
fileInputStream201 = new FileInputStream(xsltPath201);
|
94
|
StreamSource xslSource201 = new StreamSource(fileInputStream201);
|
95
|
this.transformer201 = tFactory201.newTransformer(xslSource201);
|
96
|
fileInputStream201.close();
|
97
|
|
98
|
TransformerFactory tFactory210 = TransformerFactory.newInstance();
|
99
|
fileInputStream210 = new FileInputStream(xsltPath210);
|
100
|
StreamSource xslSource210 = new StreamSource(fileInputStream210);
|
101
|
this.transformer210 = tFactory210.newTransformer(xslSource210);
|
102
|
fileInputStream210.close();
|
103
|
|
104
|
TransformerFactory tFactory211 = TransformerFactory.newInstance();
|
105
|
fileInputStream211 = new FileInputStream(xsltPath211);
|
106
|
StreamSource xslSource211 = new StreamSource(fileInputStream211);
|
107
|
this.transformer211 = tFactory210.newTransformer(xslSource211);
|
108
|
fileInputStream211.close();
|
109
|
|
110
|
}
|
111
|
catch (Exception e) {
|
112
|
e.printStackTrace();
|
113
|
throw new OAIInternalServerError(e.getMessage());
|
114
|
} finally {
|
115
|
IOUtils.closeQuietly(fileInputStream200);
|
116
|
IOUtils.closeQuietly(fileInputStream201);
|
117
|
IOUtils.closeQuietly(fileInputStream210);
|
118
|
IOUtils.closeQuietly(fileInputStream211);
|
119
|
|
120
|
}
|
121
|
}
|
122
|
|
123
|
|
124
|
/* Class methods */
|
125
|
|
126
|
public static void setDirPath(String configDir) {
|
127
|
Eml2oai_dc.dirPath = configDir;
|
128
|
}
|
129
|
|
130
|
|
131
|
|
132
|
/* Instance methods */
|
133
|
|
134
|
|
135
|
/**
|
136
|
* Perform the actual crosswalk.
|
137
|
*
|
138
|
* @param nativeItem
|
139
|
* the native "item". In this case, it is already formatted as an OAI
|
140
|
* <record> element, with the possible exception that multiple
|
141
|
* metadataFormats are present in the <metadata> element.
|
142
|
* @return a String containing the FileMap to be stored within the <metadata>
|
143
|
* element.
|
144
|
* @exception CannotDisseminateFormatException
|
145
|
* nativeItem doesn't support this format.
|
146
|
*/
|
147
|
public String createMetadata(Object nativeItem)
|
148
|
throws CannotDisseminateFormatException {
|
149
|
HashMap recordMap = (HashMap) nativeItem;
|
150
|
try {
|
151
|
//String xmlRec = (new String((byte[]) recordMap.get("recordBytes"),
|
152
|
// "UTF-8")).trim();
|
153
|
String xmlRec = (String) recordMap.get("recordBytes");
|
154
|
xmlRec = xmlRec.trim();
|
155
|
|
156
|
if (xmlRec.startsWith("<?")) {
|
157
|
int offset = xmlRec.indexOf("?>");
|
158
|
xmlRec = xmlRec.substring(offset + 2);
|
159
|
}
|
160
|
|
161
|
if (xmlRec.contains("eml://ecoinformatics.org/eml-2.0.0")) {
|
162
|
transformer = transformer200;
|
163
|
}
|
164
|
else if (xmlRec.contains("eml://ecoinformatics.org/eml-2.0.1")) {
|
165
|
transformer = transformer201;
|
166
|
}
|
167
|
else if (xmlRec.contains("eml://ecoinformatics.org/eml-2.1.0")) {
|
168
|
transformer = transformer210;
|
169
|
}
|
170
|
else if (xmlRec.contains("eml://ecoinformatics.org/eml-2.1.1")) {
|
171
|
transformer = transformer211;
|
172
|
}
|
173
|
|
174
|
StringReader stringReader = new StringReader(xmlRec);
|
175
|
StreamSource streamSource = new StreamSource(stringReader);
|
176
|
StringWriter stringWriter = new StringWriter();
|
177
|
synchronized (transformer) {
|
178
|
transformer.transform(streamSource, new StreamResult(stringWriter));
|
179
|
}
|
180
|
return stringWriter.toString();
|
181
|
}
|
182
|
catch (Exception e) {
|
183
|
e.printStackTrace();
|
184
|
throw new CannotDisseminateFormatException(e.getMessage());
|
185
|
}
|
186
|
}
|
187
|
|
188
|
|
189
|
/**
|
190
|
* Can this nativeItem be represented in DC format?
|
191
|
*
|
192
|
* @param nativeItem
|
193
|
* a record in native format
|
194
|
* @return true if DC format is possible, false otherwise.
|
195
|
*/
|
196
|
public boolean isAvailableFor(Object nativeItem) {
|
197
|
return true;
|
198
|
}
|
199
|
|
200
|
}
|