Project

General

Profile

Download (9.18 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.logging.log4j.LogManager;import org.apache.logging.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.VerbatimTimePeriod;
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
 * @since 30.05.2012
33
 *
34
 */
35
public class MarkupModsImport extends MarkupImportBase {
36
	@SuppressWarnings("unused")
37
	private static final Logger logger = LogManager.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
	protected static final String MODS_COPYRIGHT_DATE = "copyrightDate";
56

    
57

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

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

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

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

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

    
135
	private void handleIdentifier(MarkupImportState state, XMLEventReader reader, XMLEvent parentEvent, Reference modsRef) throws XMLStreamException {
136
		checkNoAttributes(parentEvent);
137

    
138

    
139
		String identifier = getCData(state, reader, parentEvent, true).trim();
140

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

    
148
		return;
149
	}
150

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

    
159
		String title = null;
160
		String subTitle = null;
161
		String partNumber = null;
162
		String partName = null;
163

    
164
		while (reader.hasNext()) {
165
			XMLEvent next = readNoWhitespace(reader);
166

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

    
189
	}
190

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

    
199
		String description = null;
200
		String namePart = null;
201

    
202
		String affiliation = null;
203

    
204
		while (reader.hasNext()) {
205
			XMLEvent next = readNoWhitespace(reader);
206

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

    
235
				}
236

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

    
250
	}
251
}
(14-14/19)