Project

General

Profile

Download (4.56 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

    
42

    
43
	static URI identifier = null;
44

    
45
	static String nameSpace = "http://purl.org/dc/elements/1.1/";
46

    
47
	static {
48
		try {
49
			identifier = new URI("info:srw/schema/1/dc-v1.1");
50
		} catch (URISyntaxException e) {
51
			// should never happen
52
		}
53
	}
54

    
55
	/* (non-Javadoc)
56
	 * @see eu.etaxonomy.cdm.ext.schema.SchemaAdapter#getIdentifier()
57
	 */
58
	@Override
59
	public URI getIdentifier() {
60
		return identifier;
61
	}
62

    
63
	/* (non-Javadoc)
64
	 * @see eu.etaxonomy.cdm.ext.schema.SchemaAdapter#getShortName()
65
	 */
66
	@Override
67
	public String getShortName() {
68
		return "dc";
69
	}
70

    
71
	/* (non-Javadoc)
72
	 * @see eu.etaxonomy.cdm.ext.schema.SchemaAdapter#getCmdEntities(java.io.Reader)
73
	 */
74
	@Override
75
	public List<Reference> getCmdEntities(InputStream inputStream) {
76

    
77
		SAXParserFactory factory = SAXParserFactory.newInstance();
78
	    factory.setNamespaceAware(true);
79
	    SAXParser parser = null;
80
		try {
81
			parser = factory.newSAXParser();
82
		} catch (ParserConfigurationException e) {
83
			logger.error(e);
84
		} catch (SAXException e) {
85
			logger.error(e);
86
		}
87

    
88

    
89
		DcSaxHandler handler = new DcSaxHandler();
90

    
91
	    try {
92
	    	if(parser != null){
93
	    		parser.parse(inputStream, handler);
94
	    	} else {
95
	    		logger.error("parser is null");
96
	    	}
97
		} catch (SAXException e) {
98
			logger.error(e);
99
		} catch (IOException e) {
100
			logger.error(e);
101
		}
102

    
103

    
104
		return handler.referenceList;
105
	}
106

    
107
	class DcSaxHandler extends DefaultHandler {
108

    
109
		private static final String DC_DC = "dc:dc";
110

    
111
		private static final String DC_TITLE = "dc:title";
112
		private static final String DC_CREATOR = "dc:creator";
113
		private static final String DC_PUBLISHER = "dc:publisher";
114
		private static final String DC_DATE = "dc:date";
115
		private static final String DC_IDENTIFIER = "dc:identifier"; //TODO map this
116

    
117
		List<Reference> referenceList = new ArrayList<Reference>();
118

    
119
		Reference reference = null;
120

    
121
		String dcFieldName = null;
122

    
123

    
124

    
125
		@Override
126
		public void startElement(String uri, String localName,
127
				String qName, Attributes attributes) throws SAXException {
128

    
129
			if (uri.equals(nameSpace)) {
130
				logger.debug("Start Element :" + qName + "; " + uri);
131

    
132
				if (qName.equals(DC_DC)) {
133
					reference = ReferenceFactory.newGeneric();
134
				} else {
135
					dcFieldName = qName;
136
				}
137
			}
138
		}
139

    
140
		@Override
141
		public void endElement(String uri, String localName, String qName)
142
				throws SAXException {
143

    
144
			if (uri.equals(nameSpace)) {
145
				if(reference != null) {
146
					logger.debug("End Element :" + qName + "; " + uri);
147

    
148
					if (qName.equals(DC_DC)) {
149
						referenceList.add(reference);
150
						reference = null;
151
					} else {
152
						dcFieldName = null;
153
					}
154
				}
155
			}
156

    
157
		}
158

    
159
		@Override
160
		public void characters(char ch[], int start, int length)
161
				throws SAXException {
162

    
163
			if(reference != null && dcFieldName != null){
164
				String text = new String(ch, start, length);
165
				logger.debug("Characters : " + text);
166
				if(dcFieldName.equals(DC_TITLE)){
167
					reference.setTitleCache(text, true);
168
				}
169
				if(dcFieldName.equals(DC_DATE)){
170
					reference.setDatePublished(TimePeriodParser.parseStringVerbatim(text));
171
				}
172
				if(dcFieldName.equals(DC_PUBLISHER)){
173
					reference.setPublisher(text);
174
				}
175
				if(dcFieldName.equals(DC_CREATOR)){
176
					TeamOrPersonBase authorship = new Team();
177
					authorship.setTitleCache(text, true);
178
					reference.setAuthorship(authorship);
179
				}
180

    
181
			}
182
		}
183

    
184
	}
185

    
186
}
    (1-1/1)