Project

General

Profile

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.OutputKeys;
20
import javax.xml.transform.Transformer;
21
import javax.xml.transform.TransformerFactory;
22
import javax.xml.transform.stream.StreamResult;
23
import javax.xml.transform.stream.StreamSource;
24

    
25
import ORG.oclc.oai.server.crosswalk.Crosswalk;
26
import ORG.oclc.oai.server.verb.CannotDisseminateFormatException;
27
import ORG.oclc.oai.server.verb.OAIInternalServerError;
28

    
29

    
30
/**
31
 * Convert native "item" to oai_dc. In this case, the native "item" is assumed
32
 * to already be formatted as an OAI <record> element, with the possible
33
 * exception that multiple metadataFormats may be present in the <metadata>
34
 * element. The "crosswalk", merely involves pulling out the one that is
35
 * requested.
36
 */
37
public class Eml2oai_dc extends Crosswalk {
38
  
39
  /* Class fields */
40

    
41
  
42
  private static String dirPath = null;
43
  private static final String XSLT_NAME_200 = "eml200toDublinCore.xsl";
44
  private static final String XSLT_NAME_201 = "eml201toDublinCore.xsl";
45
  private static final String XSLT_NAME_210 = "eml210toDublinCore.xsl";
46
  private static final String SCHEMA_LOCATION =
47
    "http://www.openarchives.org/OAI/2.0/oai_dc/ " +
48
    "http://www.openarchives.org/OAI/2.0/oai_dc.xsd";
49

    
50

    
51
  /* Instance fields */
52
  
53
  private Transformer transformer = null;
54
  private Transformer transformer200 = null;
55
  private Transformer transformer201 = null;
56
  private Transformer transformer210 = null;
57
  
58
  
59
  /* Constructors */
60

    
61
  /**
62
   * The constructor assigns the schemaLocation associated with this crosswalk.
63
   * Since the crosswalk is trivial in this case, no properties are utilized.
64
   * 
65
   * @param properties
66
   *          properties that are needed to configure the crosswalk.
67
   */
68
  public Eml2oai_dc(Properties properties) throws OAIInternalServerError {
69
    super(SCHEMA_LOCATION);
70
    String xsltPath200 = dirPath + "/" + XSLT_NAME_200;
71
    String xsltPath201 = dirPath + "/" + XSLT_NAME_201;
72
    String xsltPath210 = dirPath + "/" + XSLT_NAME_210;
73
    
74
    try {
75
      TransformerFactory tFactory200 = TransformerFactory.newInstance();
76
      FileInputStream fileInputStream200 = new FileInputStream(xsltPath200);
77
      StreamSource xslSource200 = new StreamSource(fileInputStream200);
78
      this.transformer200 = tFactory200.newTransformer(xslSource200);
79

    
80
      TransformerFactory tFactory201 = TransformerFactory.newInstance();
81
      FileInputStream fileInputStream201 = new FileInputStream(xsltPath201);
82
      StreamSource xslSource201 = new StreamSource(fileInputStream201);
83
      this.transformer201 = tFactory201.newTransformer(xslSource201);
84

    
85
      TransformerFactory tFactory210 = TransformerFactory.newInstance();
86
      FileInputStream fileInputStream210 = new FileInputStream(xsltPath210);
87
      StreamSource xslSource210 = new StreamSource(fileInputStream210);
88
      this.transformer210 = tFactory210.newTransformer(xslSource210);
89

    
90
    } 
91
    catch (Exception e) {
92
      e.printStackTrace();
93
      throw new OAIInternalServerError(e.getMessage());
94
    }
95
  }
96
  
97
  
98
  /* Class methods */
99
  
100
  public static void setDirPath(String configDir) {
101
    Eml2oai_dc.dirPath = configDir;
102
  }
103

    
104

    
105
  
106
  /* Instance methods */
107

    
108

    
109
  /**
110
   * Perform the actual crosswalk.
111
   * 
112
   * @param nativeItem
113
   *          the native "item". In this case, it is already formatted as an OAI
114
   *          <record> element, with the possible exception that multiple
115
   *          metadataFormats are present in the <metadata> element.
116
   * @return a String containing the FileMap to be stored within the <metadata>
117
   *         element.
118
   * @exception CannotDisseminateFormatException
119
   *              nativeItem doesn't support this format.
120
   */
121
  public String createMetadata(Object nativeItem)
122
      throws CannotDisseminateFormatException {
123
    HashMap recordMap = (HashMap) nativeItem;
124
    try {
125
      //String xmlRec = (new String((byte[]) recordMap.get("recordBytes"),
126
      //    "UTF-8")).trim();
127
      String xmlRec = (String) recordMap.get("recordBytes");
128
      xmlRec = xmlRec.trim();
129
      
130
      if (xmlRec.startsWith("<?")) {
131
        int offset = xmlRec.indexOf("?>");
132
        xmlRec = xmlRec.substring(offset + 2);
133
      }
134
      
135
      if (xmlRec.contains("eml://ecoinformatics.org/eml-2.0.0")) {
136
        transformer = transformer200;
137
      }
138
      else if (xmlRec.contains("eml://ecoinformatics.org/eml-2.0.1")) {
139
        transformer = transformer201;
140
      }
141
      else if (xmlRec.contains("eml://ecoinformatics.org/eml-2.1.0")) {
142
        transformer = transformer210;
143
      }
144
      
145
      StringReader stringReader = new StringReader(xmlRec);
146
      StreamSource streamSource = new StreamSource(stringReader);
147
      StringWriter stringWriter = new StringWriter();
148
      synchronized (transformer) {
149
        transformer.transform(streamSource, new StreamResult(stringWriter));
150
      }
151
      return stringWriter.toString();
152
    } 
153
    catch (Exception e) {
154
      throw new CannotDisseminateFormatException(e.getMessage());
155
    }
156
  }
157
  
158
  
159
  /**
160
   * Can this nativeItem be represented in DC format?
161
   * 
162
   * @param nativeItem
163
   *          a record in native format
164
   * @return true if DC format is possible, false otherwise.
165
   */
166
  public boolean isAvailableFor(Object nativeItem) {
167
    return true;
168
  }
169
  
170
}
    (1-1/1)