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.oclc.oai.server.crosswalk.Crosswalk;
|
25
|
import ORG.oclc.oai.server.verb.CannotDisseminateFormatException;
|
26
|
import ORG.oclc.oai.server.verb.OAIInternalServerError;
|
27
|
|
28
|
|
29
|
/**
|
30
|
* Convert native "item" to oai_dc. In this case, the native "item" is assumed
|
31
|
* to already be formatted as an OAI <record> element, with the possible
|
32
|
* exception that multiple metadataFormats may be present in the <metadata>
|
33
|
* element. The "crosswalk", merely involves pulling out the one that is
|
34
|
* requested.
|
35
|
*/
|
36
|
public class Eml2oai_dc extends Crosswalk {
|
37
|
|
38
|
/* Class fields */
|
39
|
|
40
|
|
41
|
private static String dirPath = null;
|
42
|
private static final String XSLT_NAME_200 = "eml200toDublinCore.xsl";
|
43
|
private static final String XSLT_NAME_201 = "eml201toDublinCore.xsl";
|
44
|
private static final String XSLT_NAME_210 = "eml210toDublinCore.xsl";
|
45
|
private static final String SCHEMA_LOCATION =
|
46
|
"http://www.openarchives.org/OAI/2.0/oai_dc/ " +
|
47
|
"http://www.openarchives.org/OAI/2.0/oai_dc.xsd";
|
48
|
|
49
|
|
50
|
/* Instance fields */
|
51
|
|
52
|
private Transformer transformer = null;
|
53
|
private Transformer transformer200 = null;
|
54
|
private Transformer transformer201 = null;
|
55
|
private Transformer transformer210 = null;
|
56
|
|
57
|
|
58
|
/* Constructors */
|
59
|
|
60
|
/**
|
61
|
* The constructor assigns the schemaLocation associated with this crosswalk.
|
62
|
* Since the crosswalk is trivial in this case, no properties are utilized.
|
63
|
*
|
64
|
* @param properties
|
65
|
* properties that are needed to configure the crosswalk.
|
66
|
*/
|
67
|
public Eml2oai_dc(Properties properties) throws OAIInternalServerError {
|
68
|
super(SCHEMA_LOCATION);
|
69
|
String xsltPath200 = dirPath + "/" + XSLT_NAME_200;
|
70
|
String xsltPath201 = dirPath + "/" + XSLT_NAME_201;
|
71
|
String xsltPath210 = dirPath + "/" + XSLT_NAME_210;
|
72
|
|
73
|
try {
|
74
|
TransformerFactory tFactory200 = TransformerFactory.newInstance();
|
75
|
FileInputStream fileInputStream200 = new FileInputStream(xsltPath200);
|
76
|
StreamSource xslSource200 = new StreamSource(fileInputStream200);
|
77
|
this.transformer200 = tFactory200.newTransformer(xslSource200);
|
78
|
|
79
|
TransformerFactory tFactory201 = TransformerFactory.newInstance();
|
80
|
FileInputStream fileInputStream201 = new FileInputStream(xsltPath201);
|
81
|
StreamSource xslSource201 = new StreamSource(fileInputStream201);
|
82
|
this.transformer201 = tFactory201.newTransformer(xslSource201);
|
83
|
|
84
|
TransformerFactory tFactory210 = TransformerFactory.newInstance();
|
85
|
FileInputStream fileInputStream210 = new FileInputStream(xsltPath210);
|
86
|
StreamSource xslSource210 = new StreamSource(fileInputStream210);
|
87
|
this.transformer210 = tFactory210.newTransformer(xslSource210);
|
88
|
|
89
|
}
|
90
|
catch (Exception e) {
|
91
|
e.printStackTrace();
|
92
|
throw new OAIInternalServerError(e.getMessage());
|
93
|
}
|
94
|
}
|
95
|
|
96
|
|
97
|
/* Class methods */
|
98
|
|
99
|
public static void setDirPath(String configDir) {
|
100
|
Eml2oai_dc.dirPath = configDir;
|
101
|
}
|
102
|
|
103
|
|
104
|
|
105
|
/* Instance methods */
|
106
|
|
107
|
|
108
|
/**
|
109
|
* Perform the actual crosswalk.
|
110
|
*
|
111
|
* @param nativeItem
|
112
|
* the native "item". In this case, it is already formatted as an OAI
|
113
|
* <record> element, with the possible exception that multiple
|
114
|
* metadataFormats are present in the <metadata> element.
|
115
|
* @return a String containing the FileMap to be stored within the <metadata>
|
116
|
* element.
|
117
|
* @exception CannotDisseminateFormatException
|
118
|
* nativeItem doesn't support this format.
|
119
|
*/
|
120
|
public String createMetadata(Object nativeItem)
|
121
|
throws CannotDisseminateFormatException {
|
122
|
HashMap recordMap = (HashMap) nativeItem;
|
123
|
try {
|
124
|
//String xmlRec = (new String((byte[]) recordMap.get("recordBytes"),
|
125
|
// "UTF-8")).trim();
|
126
|
String xmlRec = (String) recordMap.get("recordBytes");
|
127
|
xmlRec = xmlRec.trim();
|
128
|
|
129
|
if (xmlRec.startsWith("<?")) {
|
130
|
int offset = xmlRec.indexOf("?>");
|
131
|
xmlRec = xmlRec.substring(offset + 2);
|
132
|
}
|
133
|
|
134
|
if (xmlRec.contains("eml://ecoinformatics.org/eml-2.0.0")) {
|
135
|
transformer = transformer200;
|
136
|
}
|
137
|
else if (xmlRec.contains("eml://ecoinformatics.org/eml-2.0.1")) {
|
138
|
transformer = transformer201;
|
139
|
}
|
140
|
else if (xmlRec.contains("eml://ecoinformatics.org/eml-2.1.0")) {
|
141
|
transformer = transformer210;
|
142
|
}
|
143
|
|
144
|
StringReader stringReader = new StringReader(xmlRec);
|
145
|
StreamSource streamSource = new StreamSource(stringReader);
|
146
|
StringWriter stringWriter = new StringWriter();
|
147
|
synchronized (transformer) {
|
148
|
transformer.transform(streamSource, new StreamResult(stringWriter));
|
149
|
}
|
150
|
return stringWriter.toString();
|
151
|
}
|
152
|
catch (Exception e) {
|
153
|
throw new CannotDisseminateFormatException(e.getMessage());
|
154
|
}
|
155
|
}
|
156
|
|
157
|
|
158
|
/**
|
159
|
* Can this nativeItem be represented in DC format?
|
160
|
*
|
161
|
* @param nativeItem
|
162
|
* a record in native format
|
163
|
* @return true if DC format is possible, false otherwise.
|
164
|
*/
|
165
|
public boolean isAvailableFor(Object nativeItem) {
|
166
|
return true;
|
167
|
}
|
168
|
|
169
|
}
|