2 * Copyright (C) 2009 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.cdm
.io
.markup
;
12 import javax
.xml
.stream
.XMLEventReader
;
13 import javax
.xml
.stream
.XMLStreamException
;
14 import javax
.xml
.stream
.events
.XMLEvent
;
16 import org
.apache
.log4j
.Logger
;
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
;
35 public class MarkupModsImport
extends MarkupImportBase
{
36 @SuppressWarnings("unused")
37 private static final Logger logger
= Logger
.getLogger(MarkupModsImport
.class);
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";
57 public MarkupModsImport(MarkupDocumentImport docImport
) {
61 public void handleMods(MarkupImportState state
, XMLEventReader reader
, XMLEvent parentEvent
)
62 throws XMLStreamException
{
63 checkNoAttributes(parentEvent
);
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
);
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();
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
);
87 handleUnexpectedElement(next
);
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
);
98 if (isMyEndingElement(next
, parentEvent
)) {
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);
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);
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);
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);
124 modsRef
.setEdition(CdmUtils
.concat(";", modsRef
.getEdition(), edition
));
126 handleUnexpectedElement(next
);
132 private void handleIdentifier(MarkupImportState state
, XMLEventReader reader
, XMLEvent parentEvent
, Reference
<?
> modsRef
) throws XMLStreamException
{
133 checkNoAttributes(parentEvent
);
136 String identifier
= getCData(state
, reader
, parentEvent
, true).trim();
138 if (GeneralParser
.isIsbn(identifier
)){
139 modsRef
.setIsbn(identifier
);
141 String message
= "Identifier pattern not recognized: %s";
142 fireWarningEvent(String
.format(message
, identifier
), parentEvent
, 4);
149 * Reads all titleInfo information.
150 * ! Preliminary implementation !
152 private void handleTitleInfo(MarkupImportState state
, XMLEventReader reader
, XMLEvent parentEvent
, Reference
<?
> modsRef
)
153 throws XMLStreamException
{
154 checkNoAttributes(parentEvent
);
157 String subTitle
= null;
158 String partNumber
= null;
159 String partName
= null;
161 while (reader
.hasNext()) {
162 XMLEvent next
= readNoWhitespace(reader
);
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
);
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
);
181 handleUnexpectedElement(next
);
189 * Reads all titleInfo information.
190 * ! Preliminary implementation !
192 private void handleName(MarkupImportState state
, XMLEventReader reader
, XMLEvent parent
, Reference
<?
> modsRef
)
193 throws XMLStreamException
{
194 String type
= getOnlyAttribute(parent
, "type", true);
196 String description
= null;
197 String namePart
= null;
198 String partNumber
= null;
199 String affiliation
= null;
201 while (reader
.hasNext()) {
202 XMLEvent next
= readNoWhitespace(reader
);
204 if (isMyEndingElement(next
, parent
)) {
205 if (! type
.equals("personal")){
206 fireUnexpectedAttributeValue(parent
, "type", type
); //currently we handle only "personal"
208 Person person
= Person
.NewInstance();
209 TeamOrPersonBase
<?
> author
= modsRef
.getAuthorship();
211 modsRef
.setAuthorship(person
);
212 }else if (author
.isInstanceOf(Person
.class)){
213 Team team
= Team
.NewInstance();
214 team
.addTeamMember(person
);
215 modsRef
.setAuthorship(team
);
217 CdmBase
.deproxy(author
, Team
.class).addTeamMember(person
);
219 if (isNotBlank(namePart
)){
220 person
.setTitleCache(namePart
, true);
222 if (isNotBlank(description
)){
223 fireWarningEvent("Mods:description needs to be handled manually",this.makeLocationStr(parent
.getLocation()), 1);
225 if (isNotBlank(affiliation
)){
226 Institution institution
= Institution
.NewInstance();
227 institution
.setTitleCache(affiliation
, true);
228 person
.addInstitutionalMembership(institution
, null, null, null);
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
);
241 handleUnexpectedElement(next
);