Project

General

Profile

Download (4.52 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.bci;
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
 * @date 25.08.2010
38
 */
39
public class BciSchemaAdapter 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
		
116
		List<Reference> referenceList = new ArrayList<Reference>();
117

    
118
		Reference reference = null;
119
		
120
		String dcFieldName = null;
121
		
122
		
123

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

    
128
			if (uri.equals(nameSpace)) {
129
				logger.debug("Start Element :" + qName + "; " + uri);
130
				
131
				if (qName.equals(DC_DC)) {
132
					reference = ReferenceFactory.newGeneric();
133
				} else {
134
					dcFieldName = qName;
135
				}
136
			}
137
		}
138

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

    
143
			if (uri.equals(nameSpace)) {
144
				if(reference != null) {
145
					logger.debug("End Element :" + qName + "; " + uri);
146
					
147
					if (qName.equals(DC_DC)) {
148
						referenceList.add(reference);
149
						reference = null;
150
					} else {
151
						dcFieldName = null;
152
					}
153
				}	
154
			}
155

    
156
		}
157

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

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