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
.tcsrdf
;
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
.io
.common
.CdmImportBase
;
29 import eu
.etaxonomy
.cdm
.io
.common
.CdmIoBase
;
30 import eu
.etaxonomy
.cdm
.io
.common
.IImportConfigurator
;
31 import eu
.etaxonomy
.cdm
.io
.common
.IXmlMapper
;
32 import eu
.etaxonomy
.cdm
.io
.common
.ImportHelper
;
33 import eu
.etaxonomy
.cdm
.io
.common
.ImportStateBase
;
34 import eu
.etaxonomy
.cdm
.io
.common
.MapWrapper
;
35 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
42 public abstract class TcsRdfImportBase
extends CdmImportBase
<TcsRdfImportConfigurator
, TcsRdfImportState
> {
43 private static final Logger logger
= Logger
.getLogger(TcsRdfImportBase
.class);
45 protected static Namespace nsTcom
= Namespace
.getNamespace("http://rs.tdwg.org/ontology/voc/Common#");
46 protected static Namespace nsTn
= Namespace
.getNamespace("http://rs.tdwg.org/ontology/voc/TaxonName#");
47 protected static Namespace nsTgeo
= Namespace
.getNamespace("http://rs.tdwg.org/ontology/voc/GeographicRegion#");
48 protected static Namespace nsTc
= Namespace
.getNamespace("http://rs.tdwg.org/ontology/voc/TaxonConcept#");
49 protected static Namespace nsTpub
= Namespace
.getNamespace("http://rs.tdwg.org/ontology/voc/PublicationCitation#");
50 protected static Namespace nsTpalm
= Namespace
.getNamespace("http://wp5.e-taxonomy.eu/import/palmae/common");
53 protected abstract boolean doInvoke(TcsRdfImportState state
);
56 // * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doInvoke(eu.etaxonomy.cdm.io.common.IImportConfigurator, eu.etaxonomy.cdm.api.application.CdmApplicationController, java.util.Map)
59 // protected boolean doInvoke(IImportConfigurator config,
60 // Map<String, MapWrapper<? extends CdmBase>> stores){
61 // TcsRdfImportState state = ((TcsRdfImportConfigurator)config).getState();
62 // state.setConfig((TcsRdfImportConfigurator)config);
63 // return doInvoke(state);
66 protected boolean makeStandardMapper(Element parentElement
, CdmBase ref
, Set
<String
> omitAttributes
, CdmSingleAttributeXmlMapperBase
[] classMappers
){
67 if (omitAttributes
== null){
68 omitAttributes
= new HashSet
<String
>();
70 boolean result
= true;
71 for (CdmSingleAttributeXmlMapperBase mapper
: classMappers
){
72 Object value
= getValue(mapper
, parentElement
);
73 //write to destination
75 String destinationAttribute
= mapper
.getDestinationAttribute();
76 if (! omitAttributes
.contains(destinationAttribute
)){
77 result
&= ImportHelper
.addValue(value
, ref
, destinationAttribute
, mapper
.getTypeClass(), OVERWRITE
, OBLIGATORY
);
84 private Object
getValue(CdmSingleAttributeXmlMapperBase mapper
, Element parentElement
){
85 String sourceAttribute
= mapper
.getSourceAttribute();
86 Namespace sourceNamespace
= mapper
.getSourceNamespace(parentElement
);
87 Element child
= parentElement
.getChild(sourceAttribute
, sourceNamespace
);
91 if (child
.getContentSize() > 1){
92 logger
.warn("Element is not String");
94 Object value
= child
.getTextTrim();
98 protected boolean checkAdditionalContents(Element parentElement
, IXmlMapper
[] classMappers
, CdmSingleAttributeXmlMapperBase
[] operationalMappers
, CdmSingleAttributeXmlMapperBase
[] unclearMappers
){
99 List
<Content
> additionalContentList
= new ArrayList
<Content
>();
100 List
<Content
> contentList
= parentElement
.getContent();
101 List
<IXmlMapper
> mapperList
= new ArrayList
<IXmlMapper
>();
103 mapperList
.addAll(Arrays
.asList(classMappers
));
104 mapperList
.addAll(Arrays
.asList(operationalMappers
));
105 mapperList
.addAll(Arrays
.asList(unclearMappers
));
107 for(Content content
: contentList
){
108 boolean contentExists
= false;
109 if (content
instanceof Element
){
110 Element elementContent
= (Element
)content
;
111 for (IXmlMapper mapper
: mapperList
){
112 if (mapper
.mapsSource(content
, parentElement
)){
113 contentExists
= true;
118 }else if (content
instanceof Text
){
120 if (((Text
)content
).getTextNormalize().equals("")){
121 contentExists
= true;
127 if (contentExists
== false){
128 additionalContentList
.add(content
);
131 for (Content additionalContent
: additionalContentList
){
132 logger
.warn("Additional content: " + additionalContent
);
134 return (additionalContentList
.size() == 0);