Project

General

Profile

Download (4.2 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2009 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.cdm.ext.dc;
10

    
11
import java.io.IOException;
12
import java.io.InputStream;
13
import java.net.URI;
14
import java.net.URISyntaxException;
15
import java.util.ArrayList;
16
import java.util.List;
17

    
18
import javax.xml.parsers.ParserConfigurationException;
19
import javax.xml.parsers.SAXParser;
20
import javax.xml.parsers.SAXParserFactory;
21

    
22
import org.xml.sax.Attributes;
23
import org.xml.sax.SAXException;
24
import org.xml.sax.helpers.DefaultHandler;
25

    
26
import eu.etaxonomy.cdm.ext.common.SchemaAdapterBase;
27
import eu.etaxonomy.cdm.model.agent.Team;
28
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
29
import eu.etaxonomy.cdm.model.reference.Reference;
30
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
31
import eu.etaxonomy.cdm.strategy.parser.TimePeriodParser;
32

    
33

    
34

    
35
/**
36
 * @author a.kohlbecker
37
 * @since 25.08.2010
38
 */
39
public class DublinCoreSchemaAdapter extends SchemaAdapterBase<Reference>{
40

    
41
	private static URI identifier = null;
42

    
43
	static String nameSpace = "http://purl.org/dc/elements/1.1/";
44

    
45
	static {
46
	    identifier = URI.create("info:srw/schema/1/dc-v1.1");
47
	}
48

    
49
	@Override
50
	public URI getIdentifier() {
51
		return identifier;
52
	}
53

    
54
	@Override
55
	public String getShortName() {
56
		return "dc";
57
	}
58

    
59
	@Override
60
	public List<Reference> getCmdEntities(InputStream inputStream) {
61

    
62
		SAXParserFactory factory = SAXParserFactory.newInstance();
63
	    factory.setNamespaceAware(true);
64
	    SAXParser parser = null;
65
		try {
66
			parser = factory.newSAXParser();
67
		} catch (ParserConfigurationException e) {
68
			logger.error(e);
69
		} catch (SAXException e) {
70
			logger.error(e);
71
		}
72

    
73
		DcSaxHandler handler = new DcSaxHandler();
74

    
75
	    try {
76
	    	if(parser != null){
77
	    		parser.parse(inputStream, handler);
78
	    	} else {
79
	    		logger.error("parser is null");
80
	    	}
81
		} catch (SAXException e) {
82
			logger.error(e);
83
		} catch (IOException e) {
84
			logger.error(e);
85
		}
86

    
87
		return handler.referenceList;
88
	}
89

    
90
	class DcSaxHandler extends DefaultHandler {
91

    
92
		private static final String DC_DC = "dc:dc";
93

    
94
		private static final String DC_TITLE = "dc:title";
95
		private static final String DC_CREATOR = "dc:creator";
96
		private static final String DC_PUBLISHER = "dc:publisher";
97
		private static final String DC_DATE = "dc:date";
98
		private static final String DC_IDENTIFIER = "dc:identifier"; //TODO map this
99

    
100
		List<Reference> referenceList = new ArrayList<Reference>();
101

    
102
		Reference reference = null;
103

    
104
		String dcFieldName = null;
105

    
106
		@Override
107
		public void startElement(String uri, String localName,
108
				String qName, Attributes attributes) throws SAXException {
109

    
110
			if (uri.equals(nameSpace)) {
111
				logger.debug("Start Element :" + qName + "; " + uri);
112

    
113
				if (qName.equals(DC_DC)) {
114
					reference = ReferenceFactory.newGeneric();
115
				} else {
116
					dcFieldName = qName;
117
				}
118
			}
119
		}
120

    
121
		@Override
122
		public void endElement(String uri, String localName, String qName)
123
				throws SAXException {
124

    
125
			if (uri.equals(nameSpace)) {
126
				if(reference != null) {
127
					logger.debug("End Element :" + qName + "; " + uri);
128

    
129
					if (qName.equals(DC_DC)) {
130
						referenceList.add(reference);
131
						reference = null;
132
					} else {
133
						dcFieldName = null;
134
					}
135
				}
136
			}
137
		}
138

    
139
		@Override
140
		public void characters(char ch[], int start, int length)
141
				throws SAXException {
142

    
143
			if(reference != null && dcFieldName != null){
144
				String text = new String(ch, start, length);
145
				logger.debug("Characters : " + text);
146
				if(dcFieldName.equals(DC_TITLE)){
147
					reference.setTitleCache(text, true);
148
				}
149
				if(dcFieldName.equals(DC_DATE)){
150
					reference.setDatePublished(TimePeriodParser.parseStringVerbatim(text));
151
				}
152
				if(dcFieldName.equals(DC_PUBLISHER)){
153
					reference.setPublisher(text);
154
				}
155
				if(dcFieldName.equals(DC_CREATOR)){
156
					TeamOrPersonBase<?> authorship = new Team();
157
					authorship.setTitleCache(text, true);
158
					reference.setAuthorship(authorship);
159
				}
160
			}
161
		}
162
	}
163
}
    (1-1/1)