Project

General

Profile

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

    
10
package eu.etaxonomy.cdm.io.markup;
11

    
12
import javax.xml.stream.XMLEventReader;
13
import javax.xml.stream.XMLStreamException;
14
import javax.xml.stream.events.XMLEvent;
15

    
16
import org.apache.log4j.Logger;
17

    
18
import eu.etaxonomy.cdm.common.CdmUtils;
19
import eu.etaxonomy.cdm.common.GeneralParser;
20
import eu.etaxonomy.cdm.model.agent.Institution;
21
import eu.etaxonomy.cdm.model.agent.Person;
22
import eu.etaxonomy.cdm.model.agent.Team;
23
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
24
import eu.etaxonomy.cdm.model.common.CdmBase;
25
import eu.etaxonomy.cdm.model.common.TimePeriod;
26
import eu.etaxonomy.cdm.model.reference.Reference;
27
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
28
import eu.etaxonomy.cdm.strategy.parser.TimePeriodParser;
29

    
30
/**
31
 * @author a.mueller
32
 * @created 30.05.2012
33
 *
34
 */
35
public class MarkupModsImport extends MarkupImportBase {
36
	@SuppressWarnings("unused")
37
	private static final Logger logger = Logger.getLogger(MarkupModsImport.class);
38

    
39
	protected static final String MODS_TITLEINFO = "titleInfo";
40
	protected static final String MODS_ABSTRACT = "abstract";
41
	protected static final String MODS_TITLE = "title";
42
	protected static final String MODS_SUBTITLE = "subTitle";
43
	protected static final String MODS_PARTNUMBER = "partNumber";
44
	protected static final String MODS_PARTNAME = "partName";
45
	protected static final String MODS_NAME = "name";
46
	protected static final String MODS_ORIGININFO = "originInfo";
47
	protected static final String MODS_IDENTIFIER = "identifier";
48
	protected static final String MODS_DESCRIPTION = "description";
49
	protected static final String MODS_NAME_PART = "namePart";
50
	protected static final String MODS_AFFILIATION = "affiliation";
51
	protected static final String MODS_PUBLISHER ="publisher";
52
	protected static final String MODS_DATE_ISSUED ="dateIssued";
53
	protected static final String MODS_PLACE ="place";
54
	protected static final String MODS_EDITION ="edition";
55

    
56

    
57
	public MarkupModsImport(MarkupDocumentImport docImport) {
58
		super(docImport);
59
	}
60

    
61
	public void handleMods(MarkupImportState state, XMLEventReader reader, XMLEvent parentEvent)
62
			throws XMLStreamException {
63
		checkNoAttributes(parentEvent);
64

    
65
		Reference modsRef = ReferenceFactory.newGeneric();
66
		while (reader.hasNext()) {
67
			XMLEvent next = readNoWhitespace(reader);
68
			if (isMyEndingElement(next, parentEvent)) {
69
				//set the source reference
70
				state.getConfig().setSourceReference(modsRef);
71
				return;
72
			}else if (isStartingElement(next, MODS_TITLEINFO)) {
73
				handleTitleInfo(state, reader, next, modsRef);
74
			}else if (isStartingElement(next, MODS_ABSTRACT)) {
75
				String abstractStr = getCData(state, reader, next, true).trim();
76
				if (abstractStr.startsWith("ABSTRACT")){
77
					abstractStr = abstractStr.replaceFirst("ABSTRACT", "").trim();
78
				}
79
				modsRef.setReferenceAbstract(abstractStr);
80
			} else if (isStartingElement(next, MODS_IDENTIFIER)) {
81
				handleIdentifier(state, reader, next, modsRef);
82
			} else if (isStartingElement(next, MODS_NAME)) {
83
				handleName(state, reader, next, modsRef);
84
			} else if (isStartingElement(next, MODS_ORIGININFO)) {
85
				handleOriginInfo(state, reader, next, modsRef);
86
			} else {
87
				handleUnexpectedElement(next);
88
			}
89
		}
90
		return;
91
	}
92

    
93
	private void handleOriginInfo(MarkupImportState state, XMLEventReader reader, XMLEvent parentEvent, Reference modsRef) throws XMLStreamException {
94
		checkNoAttributes(parentEvent);
95
		while (reader.hasNext()) {
96
			XMLEvent next = readNoWhitespace(reader);
97

    
98
			if (isMyEndingElement(next, parentEvent)) {
99
				return;
100
			}else if (isStartingElement(next, MODS_PUBLISHER)) {
101
				String publisher = this.getCData(state, reader, next);
102
				if (modsRef.getPublisher() != null){
103
					fireWarningEvent("Multiple publisher infos given. Concat by ;", next, 2);
104
				}
105
				modsRef.setPublisher(CdmUtils.concat(";", modsRef.getPublisher(), publisher));
106
			}else if (isStartingElement(next, MODS_DATE_ISSUED)) {
107
				String dateIssued = this.getCData(state, reader, next);
108
				if (modsRef.getDatePublished() != null && ! modsRef.getDatePublished().isEmpty()){
109
					fireWarningEvent("Multiple publish date infos given. I overwrite older information. Please check manually ;", next, 4);
110
				}
111
				TimePeriod timePeriod = TimePeriodParser.parseString(dateIssued);
112
				modsRef.setDatePublished(timePeriod);
113
			}else if (isStartingElement(next, MODS_PLACE)) {
114
				String place = this.getCData(state, reader, next);
115
				if (modsRef.getPlacePublished() != null){
116
					fireWarningEvent("Multiple place published infos given. Concat by ;", next, 2);
117
				}
118
				modsRef.setPlacePublished(CdmUtils.concat(";", modsRef.getPlacePublished(), place));
119
			}else if (isStartingElement(next, MODS_EDITION)) {
120
				String edition = this.getCData(state, reader, next);
121
				if (modsRef.getEdition() != null){
122
					fireWarningEvent("Multiple edition infos given. Concat by ;", next, 2);
123
				}
124
				modsRef.setEdition(CdmUtils.concat(";", modsRef.getEdition(), edition));
125
			} else {
126
				handleUnexpectedElement(next);
127
			}
128
		}
129
		return;
130
	}
131

    
132
	private void handleIdentifier(MarkupImportState state, XMLEventReader reader, XMLEvent parentEvent, Reference modsRef) throws XMLStreamException {
133
		checkNoAttributes(parentEvent);
134

    
135

    
136
		String identifier = getCData(state, reader, parentEvent, true).trim();
137

    
138
		if (GeneralParser.isIsbn(identifier)){
139
			modsRef.setIsbn(identifier);
140
		}else{
141
			String message = "Identifier pattern not recognized: %s";
142
			fireWarningEvent(String.format(message, identifier), parentEvent, 4);
143
		}
144

    
145
		return;
146
	}
147

    
148
	/**
149
	 * Reads all titleInfo information.
150
	 * ! Preliminary implementation !
151
	 */
152
	private void handleTitleInfo(MarkupImportState state, XMLEventReader reader, XMLEvent parentEvent, Reference modsRef)
153
			throws XMLStreamException {
154
		checkNoAttributes(parentEvent);
155

    
156
		String title = null;
157
		String subTitle = null;
158
		String partNumber = null;
159
		String partName = null;
160

    
161
		while (reader.hasNext()) {
162
			XMLEvent next = readNoWhitespace(reader);
163

    
164
			if (isMyEndingElement(next, parentEvent)) {
165
				String all = CdmUtils.concat(" - ", title, subTitle);
166
				//TODO according to http://library.princeton.edu/departments/tsd/metadoc/mods/titleinfo.html
167
				//partNumber and partName can be repeated and the order should be kept
168
				String part = CdmUtils.concat(" ", partNumber, partName);
169
				all = CdmUtils.concat(", ", all, part);
170
				modsRef.setTitle(all);
171
				return;
172
			}else if (isStartingElement(next, MODS_TITLE)) {
173
				title = this.getCData(state, reader, next);
174
			}else if (isStartingElement(next, MODS_SUBTITLE)) {
175
				subTitle = this.getCData(state, reader, next);
176
			}else if (isStartingElement(next, MODS_PARTNAME)) {
177
				partName = this.getCData(state, reader, next);
178
			}else if (isStartingElement(next, MODS_PARTNUMBER)) {
179
				partNumber = this.getCData(state, reader, next);
180
			} else {
181
				handleUnexpectedElement(next);
182
			}
183
		}
184
		return;
185

    
186
	}
187

    
188
	/**
189
	 * Reads all titleInfo information.
190
	 * ! Preliminary implementation !
191
	 */
192
	private void handleName(MarkupImportState state, XMLEventReader reader, XMLEvent parent, Reference modsRef)
193
			throws XMLStreamException {
194
		String type = getOnlyAttribute(parent, "type", true);
195

    
196
		String description = null;
197
		String namePart = null;
198
		String partNumber = null;
199
		String affiliation = null;
200

    
201
		while (reader.hasNext()) {
202
			XMLEvent next = readNoWhitespace(reader);
203

    
204
			if (isMyEndingElement(next, parent)) {
205
				if (! type.equals("personal")){
206
					fireUnexpectedAttributeValue(parent, "type", type);  //currently we handle only "personal"
207
				}else{
208
					Person person = Person.NewInstance();
209
					TeamOrPersonBase<?> author = modsRef.getAuthorship();
210
					if (author == null){
211
						modsRef.setAuthorship(person);
212
					}else if (author.isInstanceOf(Person.class)){
213
						Team team = Team.NewInstance();
214
						team.addTeamMember(person);
215
						modsRef.setAuthorship(team);
216
					}else {
217
						CdmBase.deproxy(author, Team.class).addTeamMember(person);
218
					}
219
					if (isNotBlank(namePart)){
220
						person.setTitleCache(namePart, true);
221
					}
222
					if (isNotBlank(description)){
223
						fireWarningEvent("Mods:description needs to be handled manually",this.makeLocationStr(parent.getLocation()), 1);
224
					}
225
					if (isNotBlank(affiliation)){
226
						Institution institution = Institution.NewInstance();
227
						institution.setTitleCache(affiliation, true);
228
						person.addInstitutionalMembership(institution, null, null, null);
229
					}
230

    
231
				}
232

    
233
				return;
234
			}else if (isStartingElement(next, MODS_DESCRIPTION)) {
235
				description = this.getCData(state, reader, next);
236
			}else if (isStartingElement(next, MODS_NAME_PART)) {
237
				namePart = this.getCData(state, reader, next);
238
			}else if (isStartingElement(next, MODS_AFFILIATION)) {
239
				affiliation = this.getCData(state, reader, next);
240
			} else {
241
				handleUnexpectedElement(next);
242
			}
243
		}
244
		return;
245

    
246
	}
247
}
(14-14/19)