Project

General

Profile

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