2 * Copyright (C) 2007 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
.reference
.endnote
.in
;
12 import static eu
.etaxonomy
.cdm
.io
.common
.ImportHelper
.OBLIGATORY
;
13 import static eu
.etaxonomy
.cdm
.io
.common
.ImportHelper
.OVERWRITE
;
15 import java
.util
.ArrayList
;
16 import java
.util
.Arrays
;
17 import java
.util
.HashSet
;
18 import java
.util
.List
;
22 import org
.apache
.log4j
.Logger
;
23 import org
.jdom
.Content
;
24 import org
.jdom
.Element
;
25 import org
.jdom
.Namespace
;
28 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
29 import eu
.etaxonomy
.cdm
.common
.ResultWrapper
;
30 import eu
.etaxonomy
.cdm
.common
.XmlHelp
;
31 import eu
.etaxonomy
.cdm
.io
.common
.CdmImportBase
;
32 import eu
.etaxonomy
.cdm
.io
.common
.CdmIoBase
;
33 import eu
.etaxonomy
.cdm
.io
.common
.IImportConfigurator
;
34 import eu
.etaxonomy
.cdm
.io
.common
.ImportHelper
;
35 import eu
.etaxonomy
.cdm
.io
.common
.MapWrapper
;
36 import eu
.etaxonomy
.cdm
.io
.tcsrdf
.CdmSingleAttributeXmlMapperBase
;
37 import eu
.etaxonomy
.cdm
.model
.agent
.Team
;
38 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
39 import eu
.etaxonomy
.cdm
.model
.common
.IdentifiableEntity
;
40 import eu
.etaxonomy
.cdm
.model
.reference
.Generic
;
41 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceBase
;
48 public abstract class EndNoteImportBase
extends CdmImportBase
<EndnoteImportConfigurator
, EndnoteImportState
> {
49 private static final Logger logger
= Logger
.getLogger(EndNoteImportBase
.class);
51 protected static Namespace nsTcom
= Namespace
.getNamespace("http://rs.tdwg.org/ontology/voc/Common#");
52 protected static Namespace nsTn
= Namespace
.getNamespace("http://rs.tdwg.org/ontology/voc/TaxonName#");
53 protected static Namespace nsTgeo
= Namespace
.getNamespace("http://rs.tdwg.org/ontology/voc/GeographicRegion#");
54 protected static Namespace nsTc
= Namespace
.getNamespace("http://rs.tdwg.org/ontology/voc/TaxonConcept#");
55 protected static Namespace nsTpub
= Namespace
.getNamespace("http://rs.tdwg.org/ontology/voc/PublicationCitation#");
56 protected static Namespace nsTpalm
= Namespace
.getNamespace("http://wp5.e-taxonomy.eu/import/palmae/common");
59 protected abstract boolean doInvoke(EndnoteImportState state
);
63 // * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doInvoke(eu.etaxonomy.cdm.io.common.IImportConfigurator, eu.etaxonomy.cdm.api.application.CdmApplicationController, java.util.Map)
66 // protected boolean doInvoke(IImportConfigurator config,
67 // Map<String, MapWrapper<? extends CdmBase>> stores){
68 // TcsXmlImportState state = ((TcsXmlImportConfigurator)config).getState();
69 // state.setConfig((TcsXmlImportConfigurator)config);
70 // return doInvoke(state);
74 protected boolean makeStandardMapper(Element parentElement
, CdmBase ref
, Set
<String
> omitAttributes
, CdmSingleAttributeXmlMapperBase
[] classMappers
){
75 if (omitAttributes
== null){
76 omitAttributes
= new HashSet
<String
>();
78 boolean result
= true;
79 for (CdmSingleAttributeXmlMapperBase mapper
: classMappers
){
80 Object value
= getValue(mapper
, parentElement
);
81 //write to destination
83 String destinationAttribute
= mapper
.getDestinationAttribute();
84 if (! omitAttributes
.contains(destinationAttribute
)){
85 result
&= ImportHelper
.addValue(value
, ref
, destinationAttribute
, mapper
.getTypeClass(), OVERWRITE
, OBLIGATORY
);
92 private Object
getValue(CdmSingleAttributeXmlMapperBase mapper
, Element parentElement
){
93 String sourceAttribute
= mapper
.getSourceAttribute();
94 Namespace sourceNamespace
= mapper
.getSourceNamespace(parentElement
);
95 Element child
= parentElement
.getChild(sourceAttribute
, sourceNamespace
);
99 if (child
.getContentSize() > 1){
100 logger
.warn("Element is not String");
102 Object value
= child
.getTextTrim();
106 protected boolean checkAdditionalContents(Element parentElement
, CdmSingleAttributeXmlMapperBase
[] classMappers
, CdmSingleAttributeXmlMapperBase
[] operationalMappers
, CdmSingleAttributeXmlMapperBase
[] unclearMappers
){
107 List
<Content
> additionalContentList
= new ArrayList
<Content
>();
108 List
<Content
> contentList
= parentElement
.getContent();
109 List
<CdmSingleAttributeXmlMapperBase
> mapperList
= new ArrayList
<CdmSingleAttributeXmlMapperBase
>();
111 mapperList
.addAll(Arrays
.asList(classMappers
));
112 mapperList
.addAll(Arrays
.asList(operationalMappers
));
113 mapperList
.addAll(Arrays
.asList(unclearMappers
));
115 for(Content content
: contentList
){
116 boolean contentExists
= false;
117 if (content
instanceof Element
){
118 for (CdmSingleAttributeXmlMapperBase mapper
: mapperList
){
119 if (mapper
.mapsSource(content
, parentElement
)){
120 contentExists
= true;
125 }else if (content
instanceof Text
){
127 if (((Text
)content
).getTextNormalize().equals("")){
128 contentExists
= true;
134 if (contentExists
== false){
135 additionalContentList
.add(content
);
138 for (Content additionalContent
: additionalContentList
){
139 logger
.warn("Additional content: " + additionalContent
);
141 return (additionalContentList
.size() == 0);
144 protected Element
getXmlElement(EndnoteImportConfigurator tcsConfig
){
145 Element root
= tcsConfig
.getSourceRoot();
147 if (! "xml".equals(root
.getName())){
148 logger
.error("Root element is not 'xml'");
151 if (tcsConfig
.getEndnoteNamespace() == null){
152 logger
.error("No namespace defined for tcs");
155 if (! tcsConfig
.getEndnoteNamespace().equals(root
.getNamespace())){
156 logger
.error("Wrong namespace for element 'xml'");
159 //TODO prevent multiple elements
164 // static public boolean checkFirstTwoFunctionElements(List<Object> objList){
165 // if (! (objList.get(0) instanceof TcsXmlImportConfigurator)){
166 // logger.error("first method object has wrong type. Must be " + TcsXmlImportConfigurator.class.getSimpleName() + " but is " + (objList.get(0) == null ? "null": objList.get(0).getClass().getSimpleName()));
169 // if (! (objList.get(1) == null) && ! (objList.get(1) instanceof Element)){
170 // logger.error("first method object has wrong type. Must be " + Element.class.getSimpleName() + " but is " + (objList.get(1) == null ? "null": objList.get(1).getClass().getSimpleName()));
177 protected boolean testAdditionalElements(Element parentElement
, List
<String
> excludeList
){
178 boolean result
= true;
179 List
<Element
> list
= parentElement
.getChildren();
180 for (Element element
: list
){
181 if (! excludeList
.contains(element
.getName())){
182 logger
.warn("Unknown element (" + element
.getName() + ") in parent element (" + parentElement
.getName() + ")");
190 protected <T
extends IdentifiableEntity
> T
makeReferenceType(Element element
, Class
<?
extends T
> clazz
, MapWrapper
<?
extends T
> objectMap
, ResultWrapper
<Boolean
> success
){
192 String linkType
= element
.getAttributeValue("linkType");
193 String ref
= element
.getAttributeValue("ref");
194 if(ref
== null && linkType
== null){
195 result
= getInstance(clazz
);
197 String title
= element
.getTextNormalize();
198 result
.setTitleCache(title
);
200 }else if (linkType
== null || linkType
.equals("local")){
202 result
= objectMap
.get(ref
);
204 logger
.warn("Object (ref = " + ref
+ ")could not be found in WrapperMap");
206 }else if(linkType
.equals("external")){
207 logger
.warn("External link types not yet implemented");
208 }else if(linkType
.equals("other")){
209 logger
.warn("Other link types not yet implemented");
211 logger
.warn("Unknown link type or missing ref");
214 success
.setValue(false);
220 protected ReferenceBase
makeAccordingTo(Element elAccordingTo
, MapWrapper
<ReferenceBase
> referenceMap
, ResultWrapper
<Boolean
> success
){
221 ReferenceBase result
= null;
222 if (elAccordingTo
!= null){
223 String childName
= "AccordingToDetailed";
224 boolean obligatory
= false;
225 Element elAccordingToDetailed
= XmlHelp
.getSingleChildElement(success
, elAccordingTo
, childName
, elAccordingTo
.getNamespace(), obligatory
);
227 childName
= "Simple";
229 Element elSimple
= XmlHelp
.getSingleChildElement(success
, elAccordingTo
, childName
, elAccordingTo
.getNamespace(), obligatory
);
231 if (elAccordingToDetailed
!= null){
232 result
= makeAccordingToDetailed(elAccordingToDetailed
, referenceMap
, success
);
234 result
= Generic
.NewInstance();
235 String title
= elSimple
.getTextNormalize();
236 result
.setTitleCache(title
);
243 private ReferenceBase
makeAccordingToDetailed(Element elAccordingToDetailed
, MapWrapper
<ReferenceBase
> referenceMap
, ResultWrapper
<Boolean
> success
){
244 ReferenceBase result
= null;
245 Namespace tcsNamespace
= elAccordingToDetailed
.getNamespace();
246 if (elAccordingToDetailed
!= null){
248 String childName
= "AuthorTeam";
249 boolean obligatory
= false;
250 Element elAuthorTeam
= XmlHelp
.getSingleChildElement(success
, elAccordingToDetailed
, childName
, tcsNamespace
, obligatory
);
251 makeAccordingToAuthorTeam(elAuthorTeam
, success
);
254 childName
= "PublishedIn";
256 Element elPublishedIn
= XmlHelp
.getSingleChildElement(success
, elAccordingToDetailed
, childName
, tcsNamespace
, obligatory
);
257 result
= makeReferenceType(elPublishedIn
, Generic
.class, referenceMap
, success
);
260 childName
= "MicroReference";
262 Element elMicroReference
= XmlHelp
.getSingleChildElement(success
, elAccordingToDetailed
, childName
, tcsNamespace
, obligatory
);
263 String microReference
= elMicroReference
.getTextNormalize();
264 if (CdmUtils
.Nz(microReference
).equals("")){
266 logger
.warn("MicroReference not yet implemented for AccordingToDetailed");
272 private Team
makeAccordingToAuthorTeam(Element elAuthorTeam
, ResultWrapper
<Boolean
> succes
){
274 if (elAuthorTeam
!= null){
276 logger
.warn("AuthorTeam not yet implemented for AccordingToDetailed");