change parent pom version in 3.3
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / sdd / out / SDDDocumentBuilder.java
1 /**
2 * Copyright (C) 2007 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.sdd.out;
11
12 import java.io.File;
13 import java.io.FileOutputStream;
14 import java.io.IOException;
15 import java.io.OutputStreamWriter;
16 import java.io.Writer;
17 import java.util.HashMap;
18 import java.util.HashSet;
19 import java.util.Iterator;
20 import java.util.List;
21 import java.util.Map;
22 import java.util.Set;
23
24 import javax.xml.bind.Marshaller;
25
26 import org.apache.log4j.Logger;
27 import org.apache.xerces.dom.DocumentImpl;
28 import org.apache.xerces.dom.ElementImpl;
29 import org.apache.xerces.impl.xpath.regex.ParseException;
30 import org.apache.xml.serialize.DOMSerializer;
31 import org.apache.xml.serialize.OutputFormat;
32 import org.apache.xml.serialize.XMLSerializer;
33 import org.joda.time.DateTime;
34 import org.joda.time.format.DateTimeFormatter;
35 import org.joda.time.format.ISODateTimeFormat;
36 import org.springframework.util.ResourceUtils;
37 import org.xml.sax.SAXException;
38
39 import eu.etaxonomy.cdm.io.jaxb.CdmMarshallerListener;
40 import eu.etaxonomy.cdm.model.agent.AgentBase;
41 import eu.etaxonomy.cdm.model.agent.Person;
42 import eu.etaxonomy.cdm.model.agent.Team;
43 import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
44 import eu.etaxonomy.cdm.model.common.Annotation;
45 import eu.etaxonomy.cdm.model.common.AnnotationType;
46 import eu.etaxonomy.cdm.model.common.DefinedTerm;
47 import eu.etaxonomy.cdm.model.common.DefinedTermBase;
48 import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
49 import eu.etaxonomy.cdm.model.common.IdentifiableSource;
50 import eu.etaxonomy.cdm.model.common.Language;
51 import eu.etaxonomy.cdm.model.common.LanguageString;
52 import eu.etaxonomy.cdm.model.common.Marker;
53 import eu.etaxonomy.cdm.model.common.Representation;
54 import eu.etaxonomy.cdm.model.common.TermBase;
55 import eu.etaxonomy.cdm.model.common.TermVocabulary;
56 import eu.etaxonomy.cdm.model.common.VersionableEntity;
57 import eu.etaxonomy.cdm.model.description.CategoricalData;
58 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
59 import eu.etaxonomy.cdm.model.description.Feature;
60 import eu.etaxonomy.cdm.model.description.FeatureNode;
61 import eu.etaxonomy.cdm.model.description.FeatureTree;
62 import eu.etaxonomy.cdm.model.description.QuantitativeData;
63 import eu.etaxonomy.cdm.model.description.State;
64 import eu.etaxonomy.cdm.model.description.StateData;
65 import eu.etaxonomy.cdm.model.description.StatisticalMeasure;
66 import eu.etaxonomy.cdm.model.description.StatisticalMeasurementValue;
67 import eu.etaxonomy.cdm.model.description.TaxonDescription;
68 import eu.etaxonomy.cdm.model.description.TextData;
69 import eu.etaxonomy.cdm.model.location.NamedArea;
70 import eu.etaxonomy.cdm.model.media.IdentifiableMediaEntity;
71 import eu.etaxonomy.cdm.model.media.Media;
72 import eu.etaxonomy.cdm.model.media.MediaRepresentation;
73 import eu.etaxonomy.cdm.model.media.MediaRepresentationPart;
74 import eu.etaxonomy.cdm.model.media.Rights;
75 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
76 import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
77 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
78 import eu.etaxonomy.cdm.model.reference.IDatabase;
79 import eu.etaxonomy.cdm.model.reference.Reference;
80 import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
81 import eu.etaxonomy.cdm.model.reference.ReferenceType;
82 import eu.etaxonomy.cdm.model.taxon.Taxon;
83 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
84 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
85
86 /**
87 * Writes the SDD XML file.
88 *
89 * @author h.fradin
90 * @created 10.12.2008
91 * @version 1.0
92 */
93
94 public class SDDDocumentBuilder {
95
96 private final DocumentImpl document;
97 private XMLSerializer xmlserializer;
98 private Writer writer;
99 private DOMSerializer domi;
100 private SDDDataSet cdmSource;
101
102 private final Map<Person, String> agents = new HashMap<Person, String>();
103 private final Map<TaxonNameBase<?,?>, String> taxonNames = new HashMap<TaxonNameBase<?,?>, String>();
104 private final Map<Feature, String> characters = new HashMap<Feature, String>();
105 private final Map<FeatureNode, String> featureNodes = new HashMap<FeatureNode, String>();
106 private final Map<Feature, String> descriptiveConcepts = new HashMap<Feature, String>();
107 private final Map<TaxonDescription, String> codedDescriptions = new HashMap<TaxonDescription, String>();
108 private final Map<Media, String> medias = new HashMap<Media, String>();
109 private final Map<State, String> states = new HashMap<State, String>();
110 private final Map<Reference<?>, String> articles = new HashMap<Reference<?>, String>();
111 private final Map<VersionableEntity, String> featuretrees = new HashMap<VersionableEntity, String>();
112 private final Map<DefinedTerm, String> modifiers = new HashMap<DefinedTerm, String>();
113 private final Map<TaxonNode, String> taxonNodes = new HashMap<TaxonNode, String>();
114 private final Map<NamedArea, String> namedAreas = new HashMap<NamedArea, String>();
115 private final Map<DerivedUnit, String> specimens = new HashMap<DerivedUnit, String>();
116
117 private final Map<VersionableEntity, String> features = new HashMap<VersionableEntity, String>();
118 private int agentsCount = 0;
119 private int articlesCount = 0;
120 private int codedDescriptionsCount = 0;
121 private int taxonNamesCount = 0;
122 private int charactersCount = 0;
123 private int textcharactersCount = 0;
124 private int mediasCount = 0;
125 private int statesCount = 0;
126 private final int featureNodesCount = 0;
127 private int chartreeCount = 0;
128 private int charnodeCount = 0;
129 private int taxonNodesCount = 0;
130 private int namedAreasCount = 0;
131 private int specimenCount = 0;
132 private int descriptiveConceptCount = 0;
133 private int modifiersCount = 0;
134
135 private final String AGENT = "Agent";
136 private final String AGENTS = "Agents";
137 private final String CATEGORICAL = "Categorical";
138 private final String CATEGORICAL_CHARACTER = "CategoricalCharacter";
139 private final String CHARACTER = "Character";
140 private final String CHARACTERS = "Characters";
141 private final String CHARACTER_TREE = "CharacterTree";
142 private final String CHARACTER_TREES = "CharacterTrees";
143 private final String CHAR_NODE = "CharNode";
144 private final String CITATION = "Citation";
145 private final String CODED_DESCRIPTION = "CodedDescription";
146 private final String CODED_DESCRIPTIONS = "CodedDescriptions";
147 private final String CONTENT = "Content";
148 private final String CREATORS = "Creators";
149 private final String DATASET = "Dataset";
150 private final String DATASETS = "Datasets";
151 private final String DATE_CREATED = "DateCreated";
152 private final String DATE_MODIFIED = "DateModified";
153 private final String DEPENDENCY_RULES = "DependencyRules";
154 private final String DESCRIPTIVE_CONCEPT = "DescriptiveConcept";
155 private final String DESCRIPTIVE_CONCEPTS = "DescriptiveConcepts";
156 private final String DETAIL = "Detail";
157 private final String GENERATOR = "Generator";
158 private final String ID = "id";
159 private final String IMAGE = "Image";
160 private final String INAPPLICABLE_IF = "InapplicableIf";
161 private final String IPR_STATEMENT = "IPRStatement";
162 private final String IPR_STATEMENTS = "IPRStatements";
163 private final String LABEL = "Label";
164 private final String MEASURE = "Measure";
165 private final String MEDIA_OBJECT = "MediaObject";
166 private final String MEDIA_OBJECTS = "MediaObjects";
167 private final String NODE = "Node";
168 private final String NODES = "Nodes";
169 private final String NOTE = "Note";
170 private final String PARENT = "Parent";
171 private final String PUBLICATIONS = "Publications";
172 private final String QUANTITATIVE = "Quantitative";
173 private final String QUANTITATIVE_CHARACTER = "QuantitativeCharacter";
174 private final String REF = "ref";
175 private final String REPRESENTATION = "Representation";
176 private final String REVISION_DATA = "RevisionData";
177 private final String ROLE = "role";
178 private final String SCOPE = "Scope";
179 private final String SHOULD_CONTAIN_ALL_CHARACTERS = "ShouldContainAllCharacters";
180 private final String SOURCE = "Source";
181 private final String STATE = "State";
182 private final String STATE_DEFINITION = "StateDefinition";
183 private final String STATES = "States";
184 private final String STATUS = "Status";
185 private final String SUMMARY_DATA = "SummaryData";
186 private final String TAXON_NAME = "TaxonName";
187 private final String TAXON_NAMES = "TaxonNames";
188 private final String TECHNICAL_METADATA = "TechnicalMetadata";
189 private final String TEXT = "text";
190 private final String TEXT_CHAR = "TextChar";
191 private final String TEXT_CHARACTER = "TextCharacter";
192 private final String TYPE = "Type";
193 private final String URI = "uri";
194
195 private final Language defaultLanguage = Language.DEFAULT();
196
197 private static final Logger logger = Logger
198 .getLogger(SDDDocumentBuilder.class);
199
200 private final String NEWLINE = System.getProperty("line.separator");
201
202 public SDDDocumentBuilder() throws SAXException, IOException {
203
204 document = new DocumentImpl();
205
206 }
207
208 public void marshal(SDDDataSet cdmSource, File sddDestination)
209 throws IOException {
210
211 this.cdmSource = cdmSource;
212 Marshaller marshaller;
213 CdmMarshallerListener marshallerListener = new CdmMarshallerListener();
214 logger.info("Start marshalling");
215 writeCDMtoSDD(sddDestination);
216
217 }
218
219 public void marshal(SDDDataSet cdmSource, String sddDestinationFileName)
220 throws IOException {
221
222 this.cdmSource = cdmSource;
223 Marshaller marshaller;
224 CdmMarshallerListener marshallerListener = new CdmMarshallerListener();
225 logger.info("Start marshalling");
226 writeCDMtoSDD(ResourceUtils.getFile(sddDestinationFileName));
227
228 }
229
230 /**
231 * Write the DOM document.
232 *
233 * @param base
234 * @throws IOException
235 */
236 public void writeCDMtoSDD(File sddDestination) throws IOException {
237
238 try {
239 buildDocument();
240 } catch (ParseException e) {
241 e.printStackTrace();
242 }
243
244 OutputFormat format = new OutputFormat(document, "UTF-8", true);
245
246 FileOutputStream fos = new FileOutputStream(sddDestination);
247
248 writer = new OutputStreamWriter(fos, "UTF-8");
249
250 xmlserializer = new XMLSerializer(writer, format);
251 domi = xmlserializer.asDOMSerializer(); // As a DOM Serializer
252
253 domi.serialize(document.getDocumentElement());
254
255 writer.close();
256 }
257
258 // #############
259 // # BUILD DOM #
260 // #############
261
262 /**
263 * Builds the whole document.
264 *
265 * @param base
266 * the Base
267 * @throws ParseException
268 */
269 public void buildDocument() throws ParseException {
270
271 // create <Datasets> = root node
272 ElementImpl baselement = new ElementImpl(document, DATASETS);
273 baselement.setAttribute("xmlns:xsi",
274 "http://www.w3.org/2001/XMLSchema-instance");
275 baselement.setAttribute("xmlns", "http://rs.tdwg.org/UBIF/2006/");
276 baselement
277 .setAttribute("xsi:schemaLocation",
278 "http://rs.tdwg.org/UBIF/2006 http://rs.tdwg.org/UBIF/2006/Schema/1.1/SDD.xsd");
279
280 buildTechnicalMetadata(baselement);
281
282 List<Reference> references = cdmSource.getReferences();
283 Iterator<Reference> iterator = references.iterator();
284 IDatabase d = ReferenceFactory.newDatabase();
285 while (iterator.hasNext()) {
286 Reference<?> reference = iterator.next();
287 if (reference.getType().equals(ReferenceType.Database)) {
288 buildDataset(baselement, reference);
289 }
290 }
291 document.appendChild(baselement);
292 }
293
294 // #############
295 // # BUILD DOM #
296 // #############
297
298 /**
299 * Builds TechnicalMetadata associated with the SDD file
300 */
301 public void buildTechnicalMetadata(ElementImpl baselement)
302 throws ParseException {
303 // create TechnicalMetadata
304 ElementImpl technicalMetadata = new ElementImpl(document,
305 TECHNICAL_METADATA);
306 // select different databases associated to different descriptions TODO
307 List<Reference> references = cdmSource.getReferences();
308 Iterator<Reference> iterator = references.iterator();
309 boolean database = false;
310 IDatabase d = ReferenceFactory.newDatabase();
311 while ((iterator.hasNext()) && (!database)) {
312 Reference<?> reference = iterator.next();
313 if (reference.getType().equals(ReferenceType.Database)) {
314 d = reference;
315 }
316 }
317 DateTime dt = d.getCreated();
318 String date = dt.toString().substring(0, 19);
319 technicalMetadata.setAttribute("created", date);
320
321 ElementImpl generator = new ElementImpl(document, GENERATOR);
322 generator.setAttribute("name", "EDIT CDM");
323 generator.setAttribute("version", "v1");
324 generator
325 .setAttribute(
326 "notes",
327 "This SDD file has been generated by the SDD export functionality of the EDIT platform for Cybertaxonomy - Copyright (c) 2008");
328 technicalMetadata.appendChild(generator);
329
330 baselement.appendChild(technicalMetadata);
331 }
332
333 // Builds the information associated with a dataset
334 public void buildDataset(ElementImpl baselement, IDatabase reference)
335 throws ParseException {
336 // create Dataset and language
337 ElementImpl dataset = new ElementImpl(document, DATASET);
338 // no default language associated with a dataset in the CDM
339 dataset.setAttribute("xml:lang", Language.DEFAULT().getIso639_1());
340 baselement.appendChild(dataset);
341 buildRepresentation(dataset, reference);
342 buildRevisionData(dataset, reference);
343 buildIPRStatements(dataset, reference);
344 buildTaxonNames(dataset);
345 buildDescriptiveConcepts(dataset);
346 buildCharacters(dataset);
347 buildCodedDescriptions(dataset);
348 buildAgents(dataset);
349 buildPublications(dataset);
350 buildMediaObjects(dataset);
351 buildCharacterTrees(dataset);
352 buildClassifications(dataset);
353 buildGeographicAreas(dataset);
354 buildSpecimens(dataset);
355 }
356
357 /**
358 * Builds a Representation element using a Reference
359 */
360 public void buildRepresentation(ElementImpl element, IDatabase reference)
361 throws ParseException {
362
363 // create <Representation> element
364 ElementImpl representation = new ElementImpl(document, REPRESENTATION);
365 element.appendChild(representation);
366 buildLabel(representation, reference.getTitleCache());
367
368 Set<Annotation> annotations = ((Reference) reference).getAnnotations();
369 Iterator<Annotation> iterator = annotations.iterator();
370 String detailText = null;
371 if (iterator.hasNext()) {
372 Annotation annotation = (Annotation) iterator.next();
373 detailText = annotation.getText();
374 }
375
376 if (detailText != null && !detailText.equals("")) {
377 ElementImpl detail = new ElementImpl(document, DETAIL);
378 detail.appendChild(document.createTextNode(detailText));
379 representation.appendChild(detail);
380 }
381
382 Set<Media> rm = ((Reference) reference).getMedia();
383
384 if (rm != null && rm.size() > 0) {
385 ElementImpl mediaObject;
386
387 for (int i = 0; i < rm.size(); i++) {
388 mediaObject = new ElementImpl(document, MEDIA_OBJECT);
389 mediasCount = buildReference((Media) rm.toArray()[i], medias,
390 REF, mediaObject, "m", mediasCount);
391 representation.appendChild(mediaObject);
392 }
393 }
394
395 }
396
397 /**
398 * Builds a Representation element using a Feature
399 */
400 public void buildRepresentation(ElementImpl element, TermBase tb)
401 throws ParseException {
402
403 // create <Representation> element
404 ElementImpl representation = new ElementImpl(document, REPRESENTATION);
405 element.appendChild(representation);
406
407 Set<Representation> representations = tb.getRepresentations();
408 if (representations != null) {
409 if (!representations.isEmpty()) {
410 String label = ((Representation) representations.toArray()[0])
411 .getLabel();
412 buildLabel(representation, label);
413 String detailText = tb.getDescription();
414
415 if (detailText != null && !detailText.equals("")) {
416 if (!detailText.equals(label)) {
417 ElementImpl detail = new ElementImpl(document, DETAIL);
418 detail.appendChild(document.createTextNode(detailText));
419 representation.appendChild(detail);
420 }
421 }
422
423 }
424 }
425
426 if (tb instanceof DefinedTermBase) {
427 DefinedTermBase dtb = (DefinedTermBase) tb;
428 Set<Media> rm = dtb.getMedia();
429
430 if (rm != null && rm.size() > 0) {
431 ElementImpl mediaObject;
432
433 for (int i = 0; i < rm.size(); i++) {
434 mediaObject = new ElementImpl(document, MEDIA_OBJECT);
435 mediasCount = buildReference((Media) rm.toArray()[i],
436 medias, REF, mediaObject, "m", mediasCount);
437 representation.appendChild(mediaObject);
438 }
439 }
440 }
441 }
442
443 /**
444 * Builds a Representation element using an IdentifiableEntity
445 */
446 public void buildRepresentation(ElementImpl element, IdentifiableEntity ie)
447 throws ParseException {
448
449 // create <Representation> element
450 ElementImpl representation = new ElementImpl(document, REPRESENTATION);
451 element.appendChild(representation);
452 buildLabel(representation, ie.getTitleCache());
453
454 Set<Annotation> annotations = ie.getAnnotations();
455 Iterator iterator = annotations.iterator();
456 String detailText = null;
457 if (iterator.hasNext()) {
458 Annotation annotation = (Annotation) iterator.next();
459 detailText = annotation.getText();
460 }
461
462 if (detailText != null && !detailText.equals("")) {
463 ElementImpl detail = new ElementImpl(document, DETAIL);
464 detail.appendChild(document.createTextNode(detailText));
465 representation.appendChild(detail);
466 }
467
468 if (ie instanceof DefinedTermBase) {
469 DefinedTermBase dtb = (DefinedTermBase) ie;
470 Set<Media> rm = dtb.getMedia();
471
472 if (rm != null && rm.size() > 0) {
473 ElementImpl mediaObject;
474
475 for (int i = 0; i < rm.size(); i++) {
476 mediaObject = new ElementImpl(document, MEDIA_OBJECT);
477 mediasCount = buildReference((Media) rm.toArray()[i],
478 medias, REF, mediaObject, "m", mediasCount);
479 representation.appendChild(mediaObject);
480 }
481 }
482 }
483 if (ie instanceof IdentifiableMediaEntity) {
484 IdentifiableMediaEntity ime = (IdentifiableMediaEntity) ie;
485 Set<Media> medias = ime.getMedia();
486 if (medias != null) {
487 ElementImpl elLinks = new ElementImpl(document, "Links");
488 for (Iterator<Media> m = medias.iterator(); m.hasNext();) {
489 Media media = m.next();
490 Set<MediaRepresentation> smr = media.getRepresentations();
491 for (Iterator<MediaRepresentation> mr = smr.iterator(); mr
492 .hasNext();) {
493 MediaRepresentation mediarep = mr.next();
494 List<MediaRepresentationPart> lmrp = mediarep
495 .getParts();
496 for (Iterator<MediaRepresentationPart> mrp = lmrp
497 .iterator(); mrp.hasNext();) {
498 MediaRepresentationPart mediareppart = mrp.next();
499 ElementImpl elLink = new ElementImpl(document,
500 "Link");
501 elLink.setAttribute("href", mediareppart.getUri()
502 .toString());
503 elLinks.appendChild(elLink);
504 }
505 }
506 }
507 element.appendChild(elLinks);
508 }
509 }
510
511 }
512
513 /**
514 * Builds RevisionData associated with the Dataset
515 */
516 public void buildRevisionData(ElementImpl dataset, IDatabase database)
517 throws ParseException {
518
519 // <RevisionData>
520 // <Creators>
521 // <Agent role="aut" ref="a1"/>
522 // <Agent role="aut" ref="a2"/>
523 // <Agent role="edt" ref="a3"/>
524 // </Creators>
525 // <DateModified>2006-04-08T00:00:00</DateModified>
526 // </RevisionData>
527
528 ElementImpl revisionData = new ElementImpl(document, REVISION_DATA);
529
530 // authors
531 TeamOrPersonBase authors = database.getAuthorTeam();
532 // TeamOrPersonBase editors = database.getUpdatedBy();
533
534 if ((authors != null)) { // || (editors != null)) {
535 ElementImpl creators = new ElementImpl(document, CREATORS);
536 if (authors != null) {
537 buildRefAgent(creators, authors, "aut");
538 }
539 // if (editors != null) {
540 // buildRefAgent(creators, editors, "edt");
541 // }
542 revisionData.appendChild(creators);
543 }
544
545 buildDateModified(revisionData, database);
546
547 dataset.appendChild(revisionData);
548 }
549
550 /**
551 * Builds ModifiedDate associated with RevisionData
552 */
553 public void buildDateModified(ElementImpl revisionData, IDatabase database)
554 throws ParseException {
555
556 // <DateModified>2006-04-08T00:00:00</DateModified>
557
558 if (((Reference) database).getUpdated() != null) {
559 ElementImpl dateModified = new ElementImpl(document, DATE_MODIFIED);
560
561 DateTime c = ((Reference) database).getUpdated();
562 DateTimeFormatter fmt = ISODateTimeFormat.dateTime();
563
564 String date = fmt.print(c);
565 dateModified.appendChild(document.createTextNode(date));
566
567 revisionData.appendChild(dateModified);
568 }
569
570 }
571
572 /**
573 * Builds IPRStatements associated with the Dataset
574 */
575 public void buildIPRStatements(ElementImpl dataset, IDatabase database)
576 throws ParseException {
577
578 // <IPRStatements>
579 // <IPRStatement role="Copyright">
580 // <Label xml:lang="en-au">(c) 2003-2006 Centre for Occasional
581 // Botany.</Label>
582 // </IPRStatement>
583 // </IPRStatements>
584
585 if (database.getRights() != null) {
586 // create IPRStatements
587 ElementImpl iprStatements = new ElementImpl(document,
588 IPR_STATEMENTS);
589 dataset.appendChild(iprStatements);
590
591 // mapping between IPRStatement Copyright (SDD) and first Right in
592 // the list of Rights
593 ElementImpl iprStatement = new ElementImpl(document, IPR_STATEMENT);
594 iprStatement.setAttribute("role", "Copyright");
595 iprStatements.appendChild(iprStatement);
596 if (!database.getRights().isEmpty()) {
597 buildLabel(iprStatement, ((Rights) database.getRights()
598 .toArray()[0]).getText());
599 }
600 }
601
602 }
603
604 /**
605 * Creates a Label element
606 *
607 * @param base
608 * @param element
609 */
610 public void buildLabel(ElementImpl element, String text) {
611 ElementImpl label = new ElementImpl(document, LABEL);
612 label.appendChild(document.createTextNode(text));
613 element.appendChild(label);
614 }
615
616 // ################
617 // # GENERIC BRICKS #
618 // ################
619
620 /**
621 * Builds TaxonNames associated with the Dataset
622 */
623 public void buildTaxonNames(ElementImpl dataset) throws ParseException {
624
625 // <TaxonNames>
626 // <TaxonName id="t1" uri="urn:lsid:authority:namespace:my-own-id">
627 // <Representation>
628 // <Label xml:lang="la">Viola hederacea Labill.</Label>
629 // </Representation>
630 // </TaxonName>
631 // </TaxonNames>
632
633 if (cdmSource.getTaxonomicNames() != null) {
634 ElementImpl elTaxonNames = new ElementImpl(document, TAXON_NAMES);
635
636 for (int i = 0; i < cdmSource.getTaxonomicNames().size(); i++) {
637 ElementImpl elTaxonName = new ElementImpl(document, TAXON_NAME);
638 TaxonNameBase tnb = cdmSource.getTaxonomicNames().get(i);
639
640 taxonNamesCount = buildReference(tnb, taxonNames, ID,
641 elTaxonName, "t", taxonNamesCount);
642
643 buildRepresentation(elTaxonName, tnb);
644
645 elTaxonNames.appendChild(elTaxonName);
646 }
647
648 dataset.appendChild(elTaxonNames);
649 }
650
651 }
652
653 public void buildDescriptiveConcepts(ElementImpl dataset)
654 throws ParseException {
655
656 if (cdmSource.getFeatureData() != null) {
657 ElementImpl elFeatures = new ElementImpl(document,
658 DESCRIPTIVE_CONCEPTS);
659 int f = cdmSource.getTerms().size();
660 for (int i = 0; i < f; i++) {
661 DefinedTermBase dtb = cdmSource.getTerms().get(i);
662 if (dtb instanceof Feature) {
663 ElementImpl elFeat = new ElementImpl(document,
664 DESCRIPTIVE_CONCEPT);
665 Feature feature = (Feature) dtb;
666 if (feature.getMarkers() != null) {
667 Set<Marker> markers = feature.getMarkers();
668 for (Iterator<Marker> m = markers.iterator(); m
669 .hasNext();) {
670 Marker marker = m.next();
671 if (marker.getMarkerType().getLabel()
672 .equals("DescriptiveConcept")) {
673 descriptiveConceptCount = buildReference(dtb,
674 descriptiveConcepts, ID, elFeat, "dc",
675 descriptiveConceptCount);
676 buildRepresentation(elFeat, feature);
677 if (!feature
678 .getRecommendedModifierEnumeration()
679 .isEmpty()) {
680 ElementImpl elModifiers = new ElementImpl(
681 document, "Modifiers");
682 for (Iterator<TermVocabulary<DefinedTerm>> menum = feature
683 .getRecommendedModifierEnumeration()
684 .iterator(); menum.hasNext();) {
685 TermVocabulary<DefinedTerm> termVoc = menum.next();
686 Set<DefinedTerm> sm = termVoc.getTerms();
687 for (Iterator<DefinedTerm> modif = sm.iterator(); modif.hasNext();) {
688 DefinedTerm modifier = modif.next();
689 ElementImpl elModifier = new ElementImpl(
690 document, "Modifier");
691 modifiersCount = buildReference(
692 modifier, modifiers, ID,
693 elModifier, "mod",
694 modifiersCount);
695 buildRepresentation(elModifier,
696 modifier);
697 elModifiers.appendChild(elModifier);
698 }
699 }
700 elFeat.appendChild(elModifiers);
701 }
702 elFeatures.appendChild(elFeat);
703 }
704 }
705 }
706 }
707 }
708 dataset.appendChild(elFeatures);
709 }
710 }
711
712 /**
713 * Builds Characters associated with the Dataset
714 */
715 public void buildCharacters(ElementImpl dataset) throws ParseException {
716
717 if (cdmSource.getTerms() != null) {
718 ElementImpl elCharacters = new ElementImpl(document, CHARACTERS);
719
720 int f = cdmSource.getTerms().size();
721 for (int i = 0; i < f; i++) {
722 if (cdmSource.getTerms().get(i) instanceof Feature) {
723 Feature character = (Feature) cdmSource.getTerms().get(i);
724 if (character.isSupportsQuantitativeData()) {
725 ElementImpl elQuantitativeCharacter = new ElementImpl(
726 document, QUANTITATIVE_CHARACTER);
727 charactersCount = buildReference(character, characters,
728 ID, elQuantitativeCharacter, "c",
729 charactersCount);
730 buildRepresentation(elQuantitativeCharacter, character);
731 elCharacters.appendChild(elQuantitativeCharacter);
732 }
733
734 if (character.isSupportsCategoricalData()) {
735 ElementImpl elCategoricalCharacter = new ElementImpl(
736 document, CATEGORICAL_CHARACTER);
737 charactersCount = buildReference(character, characters,
738 ID, elCategoricalCharacter, "c",
739 charactersCount);
740 buildRepresentation(elCategoricalCharacter, character);
741
742 Set<TermVocabulary<State>> enumerations = character
743 .getSupportedCategoricalEnumerations();
744 if (enumerations != null) {
745 if (enumerations.size() > 0) {
746 ElementImpl elStates = new ElementImpl(
747 document, STATES);
748 TermVocabulary tv = (TermVocabulary) enumerations
749 .toArray()[0];
750 Set<State> stateList = tv.getTerms();
751 for (int j = 0; j < stateList.size(); j++) {
752 ElementImpl elStateDefinition = new ElementImpl(
753 document, STATE_DEFINITION);
754 State state = (State) stateList.toArray()[j];
755 statesCount = buildReference(state, states,
756 ID, elStateDefinition, "s",
757 statesCount);
758 buildRepresentation(elStateDefinition,
759 state);
760 elStates.appendChild(elStateDefinition);
761 }
762 elCategoricalCharacter.appendChild(elStates);
763 elCharacters
764 .appendChild(elCategoricalCharacter);
765 }
766 }
767 }
768 if (character.isSupportsTextData()) {
769 ElementImpl elTextCharacter = new ElementImpl(document,
770 TEXT_CHARACTER);
771 textcharactersCount = buildReference(character,
772 characters, ID, elTextCharacter, TEXT,
773 textcharactersCount);
774 buildRepresentation(elTextCharacter, character);
775 elCharacters.appendChild(elTextCharacter);
776 }
777 }
778 }
779
780 dataset.appendChild(elCharacters);
781 }
782
783 }
784
785 public void buildCodedDescriptions(ElementImpl dataset)
786 throws ParseException {
787
788 if (cdmSource.getTaxa() != null) {
789 ElementImpl elCodedDescriptions = new ElementImpl(document,
790 CODED_DESCRIPTIONS);
791
792 for (Iterator<? extends TaxonBase> tb = cdmSource.getTaxa()
793 .iterator(); tb.hasNext();) {
794 Taxon taxon = (Taxon) tb.next();
795 Set<TaxonDescription> descriptions = taxon.getDescriptions();
796 for (Iterator<TaxonDescription> td = descriptions.iterator(); td
797 .hasNext();) {
798 TaxonDescription taxonDescription = td.next();
799 ElementImpl elCodedDescription = new ElementImpl(document,
800 CODED_DESCRIPTION);
801 codedDescriptionsCount = buildReference(taxonDescription,
802 codedDescriptions, ID, elCodedDescription, "D",
803 codedDescriptionsCount);
804 buildRepresentation(elCodedDescription, taxonDescription);
805 buildScope(elCodedDescription, taxonDescription);
806 buildSummaryData(elCodedDescription, taxonDescription);
807 elCodedDescriptions.appendChild(elCodedDescription);
808 }
809 }
810
811 dataset.appendChild(elCodedDescriptions);
812 }
813
814 }
815
816 /**
817 * Builds Scope associated with a CodedDescription
818 */
819 public void buildScope(ElementImpl element,
820 TaxonDescription taxonDescription) throws ParseException {
821
822 // <Scope>
823 // <TaxonName ref="t1"/>
824 // <Citation ref="p1" location="p. 30"/>
825 // </Scope>
826
827 ElementImpl scope = new ElementImpl(document, SCOPE);
828
829 Taxon taxon = taxonDescription.getTaxon();
830 if (taxon != null) {
831 TaxonNameBase taxonNameBase = taxon.getName();
832 if (taxonNameBase != null) {
833 String ref = taxonNames.get(taxonNameBase);
834 if (!ref.equals("")) {
835 ElementImpl taxonName = new ElementImpl(document,
836 TAXON_NAME);
837 taxonName.setAttribute(REF, ref);
838 scope.appendChild(taxonName);
839 }
840 }
841 }
842
843 Set<Reference> descriptionSources = new HashSet<Reference>();
844 for (IdentifiableSource source : taxonDescription.getSources()){
845 descriptionSources.add(source.getCitation());
846 }
847 for (Iterator<Reference> rb = descriptionSources.iterator(); rb.hasNext();) {
848 Reference descriptionSource = rb.next();
849 if (descriptionSource.getType().equals(ReferenceType.Article)) {
850
851 ElementImpl citation = new ElementImpl(document, CITATION);
852 articlesCount = buildReference(descriptionSource, articles,
853 REF, citation, "p", articlesCount);
854
855 Set<Annotation> annotations = descriptionSource.getAnnotations();
856 for (Iterator<Annotation> a = annotations.iterator(); a.hasNext();) {
857 Annotation annotation = a.next();
858 AnnotationType annotationType = annotation.getAnnotationType();
859 if (annotationType != null) {
860 String type = annotationType.getLabel();
861 if (type.equals("location")) {
862 citation.setAttribute("location",annotation.getText());
863 }
864 }
865 }
866
867 scope.appendChild(citation);
868 }
869 }
870
871 element.appendChild(scope);
872 }
873
874 /**
875 * Builds SummaryData associated with a CodedDescription
876 */
877 public void buildSummaryData(ElementImpl element,
878 TaxonDescription taxonDescription) throws ParseException {
879
880 // <SummaryData>
881 // <Categorical ref="c4">
882 // <State ref="s3"/>
883 // <State ref="s4"/>
884 // </Categorical>
885
886 ElementImpl summaryData = new ElementImpl(document, SUMMARY_DATA);
887 Set<DescriptionElementBase> elements = taxonDescription.getElements();
888 for (Iterator<DescriptionElementBase> deb = elements.iterator(); deb
889 .hasNext();) {
890 DescriptionElementBase descriptionElement = deb.next();
891 if (descriptionElement instanceof CategoricalData) {
892 CategoricalData categoricalData = (CategoricalData) descriptionElement;
893 buildCategorical(summaryData, categoricalData);
894 }
895 if (descriptionElement instanceof QuantitativeData) {
896 QuantitativeData quantitativeData = (QuantitativeData) descriptionElement;
897 buildQuantitative(summaryData, quantitativeData);
898 }
899 if (descriptionElement instanceof TextData) {
900 TextData textData = (TextData) descriptionElement;
901 buildTextChar(summaryData, textData);
902 }
903 }
904 element.appendChild(summaryData);
905 }
906
907 /**
908 * Builds Categorical associated with a SummaryData
909 */
910 public void buildCategorical(ElementImpl element,
911 CategoricalData categoricalData) throws ParseException {
912
913 // <SummaryData>
914 // <Categorical ref="c4">
915 // <State ref="s3"/>
916 // <State ref="s4"/>
917 // </Categorical>
918
919 ElementImpl categorical = new ElementImpl(document, CATEGORICAL);
920 Feature feature = categoricalData.getFeature();
921 buildReference(feature, characters, REF, categorical, "c",
922 charactersCount);
923 List<StateData> states = categoricalData.getStateData();
924 for (Iterator<StateData> sd = states.iterator(); sd.hasNext();) {
925 StateData stateData = sd.next();
926 State s = stateData.getState();
927 buildState(categorical, s);
928 }
929 element.appendChild(categorical);
930 }
931
932 /**
933 * Builds State associated with a Categorical
934 */
935 public void buildState(ElementImpl element, State s) throws ParseException {
936
937 // <SummaryData>
938 // <Categorical ref="c4">
939 // <State ref="s3"/>
940 // <State ref="s4"/>
941 // </Categorical>
942
943 ElementImpl state = new ElementImpl(document, STATE);
944 buildReference(s, states, REF, state, "s", statesCount);
945 element.appendChild(state);
946 }
947
948 /**
949 * Builds Quantitative associated with a SummaryData
950 */
951 public void buildQuantitative(ElementImpl element,
952 QuantitativeData quantitativeData) throws ParseException {
953
954 // <Quantitative ref="c2">
955 // <Measure type="Min" value="2.3"></Measure>
956 // <Measure type="Mean" value="5.1"/>
957 // <Measure type="Max" value="7.9"/>
958 // <Measure type="SD" value="1.3"/>
959 // <Measure type="N" value="20"/>
960 // </Quantitative>
961
962 ElementImpl quantitative = new ElementImpl(document, QUANTITATIVE);
963 Feature feature = quantitativeData.getFeature();
964 buildReference(feature, characters, REF, quantitative, "c",
965 charactersCount);
966 Set<StatisticalMeasurementValue> statisticalValues = quantitativeData
967 .getStatisticalValues();
968 for (Iterator<StatisticalMeasurementValue> smv = statisticalValues
969 .iterator(); smv.hasNext();) {
970 StatisticalMeasurementValue statisticalValue = smv.next();
971 buildMeasure(quantitative, statisticalValue);
972 }
973 element.appendChild(quantitative);
974 }
975
976 /**
977 * Builds Measure associated with a Quantitative
978 */
979 public void buildMeasure(ElementImpl element,
980 StatisticalMeasurementValue statisticalValue) throws ParseException {
981
982 // <Quantitative ref="c2">
983 // <Measure type="Min" value="2.3"></Measure>
984 // <Measure type="Mean" value="5.1"/>
985 // <Measure type="Max" value="7.9"/>
986 // <Measure type="SD" value="1.3"/>
987 // <Measure type="N" value="20"/>
988 // </Quantitative>
989
990 ElementImpl measure = new ElementImpl(document, MEASURE);
991 StatisticalMeasure type = statisticalValue.getType();
992 String label = type.getLabel();
993 if (label.equals("Average")) {
994 measure.setAttribute("type", "Mean");
995 } else if (label.equals("StandardDeviation")) {
996 measure.setAttribute("type", "SD");
997 } else if (label.equals("SampleSize")) {
998 measure.setAttribute("type", "N");
999 } else {
1000 measure.setAttribute("type", label);
1001 }
1002 float value = statisticalValue.getValue();
1003 measure.setAttribute("value", String.valueOf(value));
1004 element.appendChild(measure);
1005 }
1006
1007 /**
1008 * Builds TextChar associated with a SummaryData
1009 */
1010 public void buildTextChar(ElementImpl element, TextData textData)
1011 throws ParseException {
1012
1013 // <TextChar ref="c3">
1014 // <Content>Free form text</Content>
1015 // </TextChar>
1016
1017 ElementImpl textChar = new ElementImpl(document, TEXT_CHAR);
1018 Feature feature = textData.getFeature();
1019 buildReference(feature, characters, REF, textChar, "c", charactersCount);
1020 Map<Language, LanguageString> multilanguageText = textData
1021 .getMultilanguageText();
1022 for (Language language : multilanguageText.keySet()) {
1023 LanguageString languageString = multilanguageText.get(language);
1024 buildContent(textChar, languageString);
1025 }
1026 element.appendChild(textChar);
1027 }
1028
1029 /**
1030 * Builds Content associated with a TextChar
1031 */
1032 public void buildContent(ElementImpl element, LanguageString languageString)
1033 throws ParseException {
1034
1035 // <TextChar ref="c3">
1036 // <Content>Free form text</Content>
1037 // </TextChar>
1038
1039 ElementImpl content = new ElementImpl(document, CONTENT);
1040 Language language = languageString.getLanguage();
1041 String text = languageString.getText();
1042 if (!language.getIso639_1().equals(defaultLanguage.getIso639_1())) {
1043 content.setAttribute("xml:lang", language.getIso639_1());
1044 }
1045 content.setTextContent(text);
1046 element.appendChild(content);
1047 }
1048
1049 /**
1050 * Builds an element Agent referring to Agent defined later in the SDD file
1051 */
1052 public void buildRefAgent(ElementImpl element, TeamOrPersonBase ag,
1053 String role) throws ParseException {
1054 if (ag instanceof Person) {
1055 Person p = (Person) ag;
1056 ElementImpl agent = new ElementImpl(document, AGENT);
1057 if (ag.getMarkers() != null) {
1058 Set<Marker> markers = ag.getMarkers();
1059 for (Iterator<Marker> m = markers.iterator(); m.hasNext();) {
1060 Marker marker = m.next();
1061 if (marker.getMarkerType().getLabel().equals("editor")) {
1062 agent.setAttribute(ROLE, "edt");
1063 }
1064 }
1065 } else {
1066 agent.setAttribute(ROLE, role);
1067 }
1068 agentsCount = buildReference(p, agents, REF, agent, "a",
1069 agentsCount);
1070 element.appendChild(agent);
1071 }
1072
1073 if (ag instanceof Team) {
1074 Team team = (Team) ag;
1075 for (int i = 0; i < team.getTeamMembers().size(); i++) {
1076 Person author = team.getTeamMembers().get(i);
1077 ElementImpl agent = new ElementImpl(document, AGENT);
1078 if (author.getMarkers() != null) {
1079 Set<Marker> markers = author.getMarkers();
1080 if (!markers.isEmpty()) {
1081 for (Iterator<Marker> m = markers.iterator(); m
1082 .hasNext();) {
1083 Marker marker = m.next();
1084 if (marker.getMarkerType().getLabel()
1085 .equals("editor")) {
1086 agent.setAttribute(ROLE, "edt");
1087 }
1088 }
1089 } else {
1090 agent.setAttribute(ROLE, role);
1091 }
1092 } else {
1093 agent.setAttribute(ROLE, role);
1094 }
1095 if (author.getSources() != null) {
1096 IdentifiableSource os = (IdentifiableSource) author
1097 .getSources().toArray()[0];
1098 String id = os.getIdInSource();
1099 if (id != null) {
1100 if (!id.equals("")) {
1101 if (!agents.containsValue(id)) {
1102 agent.setAttribute(REF, id);
1103 } else if (!agents.containsValue("a"
1104 + (agentsCount + 1))) {
1105 agent.setAttribute(REF, "a" + (agentsCount + 1));
1106 agentsCount++;
1107 } else {
1108 agent.setAttribute(REF, id + (agentsCount + 1));
1109 agentsCount++;
1110 }
1111 } else {
1112 agent.setAttribute(REF, "a" + (agentsCount + 1));
1113 agentsCount++;
1114 }
1115 } else {
1116 agent.setAttribute(REF, "a" + (agentsCount + 1));
1117 agentsCount++;
1118 }
1119 } else {
1120 agent.setAttribute(REF, "a" + (agentsCount + 1));
1121 agentsCount++;
1122 }
1123 agents.put(author, agent.getAttribute(REF));
1124 element.appendChild(agent);
1125 }
1126 }
1127 }
1128
1129 /**
1130 * Builds Agents associated with the Dataset
1131 */
1132 public void buildAgents(ElementImpl dataset) throws ParseException {
1133
1134 if (cdmSource.getAgents() != null) {
1135 ElementImpl elAgents = new ElementImpl(document, AGENTS);
1136
1137 for (int i = 0; i < cdmSource.getAgents().size(); i++) {
1138 ElementImpl elAgent = new ElementImpl(document, AGENT);
1139 AgentBase personagent = cdmSource.getAgents().get(i);
1140 if (personagent instanceof Person) {
1141 if (personagent.getMarkers() != null) {
1142 Set<Marker> markers = personagent.getMarkers();
1143 for (Iterator<Marker> m = markers.iterator(); m
1144 .hasNext();) {
1145 Marker marker = m.next();
1146 if (marker.getMarkerType().getLabel()
1147 .equals("editor")) {
1148 agentsCount = buildReference(personagent,
1149 agents, ID, elAgent, "a", agentsCount);
1150 }
1151 }
1152 }
1153 agentsCount = buildReference(personagent, agents, ID,
1154 elAgent, "a", agentsCount);
1155 buildRepresentation(elAgent, personagent);
1156 elAgents.appendChild(elAgent);
1157 }
1158 }
1159
1160 dataset.appendChild(elAgents);
1161 }
1162 }
1163
1164 public void buildCharacterTrees(ElementImpl dataset) throws ParseException {
1165
1166 if (cdmSource.getFeatureData() != null) {
1167 ElementImpl elChartrees = new ElementImpl(document, CHARACTER_TREES);
1168
1169 for (int i = 0; i < cdmSource.getFeatureData().size(); i++) {
1170 VersionableEntity featu = cdmSource.getFeatureData().get(i);
1171 if (featu instanceof FeatureTree) {
1172 FeatureTree ft = (FeatureTree) featu;
1173 ElementImpl elChartree = new ElementImpl(document,
1174 CHARACTER_TREE);
1175 chartreeCount = buildReference(featu, featuretrees, ID,
1176 elChartree, "ct", chartreeCount);
1177 buildRepresentation(elChartree, ft);
1178 elChartrees.appendChild(elChartree);
1179 ElementImpl elNodes = new ElementImpl(document, NODES);
1180 elChartree.appendChild(elNodes);
1181 List<FeatureNode> roots = ft.getRootChildren();
1182 for (Iterator<FeatureNode> fn = roots.iterator(); fn
1183 .hasNext();) {
1184 FeatureNode featureNode = fn.next();
1185 buildBranches(featureNode, elNodes, true);
1186 }
1187 }
1188 }
1189 dataset.appendChild(elChartrees);
1190 }
1191 }
1192
1193 public void buildClassifications(ElementImpl dataset) throws ParseException {
1194
1195 if (cdmSource.getTaxa() != null) {
1196 ElementImpl elTaxonHierarchies = new ElementImpl(document,
1197 "TaxonHierarchies");
1198 ElementImpl elTaxonHierarchy = new ElementImpl(document,
1199 "TaxonHierarchy");
1200 for (Iterator<? extends TaxonBase> tb = cdmSource.getTaxa()
1201 .iterator(); tb.hasNext();) {
1202 Taxon taxon = (Taxon) tb.next();
1203 if (taxon.getTaxonNodes() != null) {
1204 for (Iterator<TaxonNode> tn = taxon.getTaxonNodes()
1205 .iterator(); tn.hasNext();) {
1206 TaxonNode taxonnode = tn.next();
1207 if (taxonnode.isTopmostNode()) {
1208 ElementImpl elNode = new ElementImpl(document, "Node");
1209 taxonNodesCount = buildReference(taxonnode,
1210 taxonNodes, ID, elNode, "tn", taxonNodesCount);
1211 ElementImpl elTaxonName = new ElementImpl(document, TAXON_NAME);
1212 taxonNamesCount = buildReference(taxonnode.getTaxon().getName(),
1213 taxonNames, REF, elTaxonName, "t", taxonNamesCount);
1214 elNode.appendChild(elTaxonName);
1215 elTaxonHierarchy.appendChild(elNode);
1216 if (taxonnode.hasChildNodes()) {buildTaxonBranches(
1217 taxonnode.getChildNodes(), taxonnode, elTaxonHierarchy);
1218 }
1219 }
1220 }
1221 }
1222 }
1223 elTaxonHierarchies.appendChild(elTaxonHierarchy);
1224 dataset.appendChild(elTaxonHierarchies);
1225 }
1226 }
1227
1228 private void buildTaxonBranches(List<TaxonNode> children, TaxonNode parent,
1229 ElementImpl elTaxonHierarchy) {
1230 if (children != null) {
1231 for (Iterator<TaxonNode> tn = children.iterator(); tn.hasNext();) {
1232 TaxonNode taxonnode = tn.next();
1233 ElementImpl elNode = new ElementImpl(document, "Node");
1234 ElementImpl elParent = new ElementImpl(document, PARENT);
1235 ElementImpl elTaxonName = new ElementImpl(document, TAXON_NAME);
1236 if (taxonnode.hasChildNodes()) {
1237 buildTaxonBranches(taxonnode.getChildNodes(), taxonnode,
1238 elTaxonHierarchy);
1239 }
1240 taxonNodesCount = buildReference(taxonnode, taxonNodes, ID,
1241 elNode, "tn", taxonNodesCount);
1242 taxonNodesCount = buildReference(parent, taxonNodes, REF,
1243 elParent, "tn", taxonNodesCount);
1244 taxonNamesCount = buildReference(
1245 taxonnode.getTaxon().getName(), taxonNames, REF,
1246 elTaxonName, "t", taxonNamesCount);
1247 elNode.appendChild(elParent);
1248 elNode.appendChild(elTaxonName);
1249 elTaxonHierarchy.appendChild(elNode);
1250 }
1251 }
1252 }
1253
1254 public void buildBranches(FeatureNode parent, ElementImpl element,
1255 boolean isRoot) {
1256 List<FeatureNode> children = parent.getChildNodes();
1257 if (!parent.isLeaf()) {
1258 ElementImpl elCharNode = new ElementImpl(document, NODE);
1259 charnodeCount = buildReference(parent, featuretrees, ID,
1260 elCharNode, "cn", charnodeCount);
1261 FeatureNode grandparent = parent.getParent();
1262 if ((grandparent != null) && (!isRoot)) {
1263 ElementImpl elParent = new ElementImpl(document, PARENT);
1264 charnodeCount = buildReference(grandparent, featuretrees, REF,
1265 elParent, "cn", charnodeCount);
1266 elCharNode.appendChild(elParent);
1267 }
1268 ElementImpl elDescriptiveConcept = new ElementImpl(document,
1269 DESCRIPTIVE_CONCEPT);
1270 Feature fref = parent.getFeature();
1271 descriptiveConceptCount = buildReference(fref, descriptiveConcepts,
1272 REF, elDescriptiveConcept, "dc", descriptiveConceptCount);
1273 elCharNode.appendChild(elDescriptiveConcept);
1274 element.appendChild(elCharNode);
1275 for (Iterator<FeatureNode> ifn = children.iterator(); ifn.hasNext();) {
1276 FeatureNode fn = ifn.next();
1277 buildBranches(fn, element, false);
1278 }
1279 } else {
1280 ElementImpl elCharNode = new ElementImpl(document, CHAR_NODE);
1281 ElementImpl elParent = new ElementImpl(document, PARENT);
1282 FeatureNode grandparent = parent.getParent();
1283 charnodeCount = buildReference(grandparent, featuretrees, REF,
1284 elParent, "cn", charnodeCount);
1285 charnodeCount = buildReference(parent, featuretrees, ID,
1286 elCharNode, "cn", charnodeCount);
1287 ElementImpl elCharacter = new ElementImpl(document, CHARACTER);
1288 Feature fref = parent.getFeature();
1289 boolean dependencies = false;
1290 ElementImpl elDependecyRules = new ElementImpl(document,
1291 "DependecyRules");
1292 if (parent.getInapplicableIf() != null) {
1293 Set<State> innaplicableIf = parent.getInapplicableIf();
1294 ElementImpl elInnaplicableIf = new ElementImpl(document,
1295 "InapplicableIf");
1296 for (State state : innaplicableIf) {
1297 ElementImpl elState = new ElementImpl(document, STATE);
1298 buildReference(state, states, REF, elState, "State",
1299 statesCount);
1300 elInnaplicableIf.appendChild(elState);
1301 }
1302 elDependecyRules.appendChild(elInnaplicableIf);
1303 dependencies = true;
1304 }
1305 if (parent.getOnlyApplicableIf() != null) {
1306 Set<State> onlyApplicableIf = parent.getOnlyApplicableIf();
1307 ElementImpl elOnlyApplicableIf = new ElementImpl(document,
1308 "OnlyApplicableIf");
1309 for (State state : onlyApplicableIf) {
1310 ElementImpl elState = new ElementImpl(document, STATE);
1311 buildReference(state, states, REF, elState, "State",
1312 statesCount);
1313 elOnlyApplicableIf.appendChild(elState);
1314 }
1315 elDependecyRules.appendChild(elOnlyApplicableIf);
1316 dependencies = true;
1317 }
1318 if (dependencies == true)
1319 elCharNode.appendChild(elDependecyRules);
1320 charactersCount = buildReference(fref, characters, REF,
1321 elCharacter, "c", charactersCount);
1322 elCharNode.appendChild(elCharacter);
1323 elCharNode.appendChild(elParent);
1324 element.appendChild(elCharNode);
1325 }
1326 }
1327
1328 public void buildMediaObjects(ElementImpl dataset) throws ParseException {
1329
1330 if (cdmSource.getMedia() != null) {
1331 ElementImpl elMediaObjects = new ElementImpl(document,
1332 MEDIA_OBJECTS);
1333
1334 for (int i = 0; i < cdmSource.getMedia().size(); i++) {
1335 ElementImpl elMediaObject = new ElementImpl(document,
1336 MEDIA_OBJECT);
1337 Media mediobj = (Media) cdmSource.getMedia().get(i);
1338 mediasCount = buildReference(mediobj, medias, ID,
1339 elMediaObject, "t", mediasCount);
1340 buildRepresentation(elMediaObject, mediobj);
1341 Set<MediaRepresentation> smr = mediobj.getRepresentations();
1342 for (Iterator<MediaRepresentation> mr = smr.iterator(); mr
1343 .hasNext();) {
1344 MediaRepresentation mediarep = mr.next();
1345 ElementImpl elType = new ElementImpl(document, "Type");
1346 elType.appendChild(document.createTextNode(mediarep
1347 .getMimeType()));
1348 elMediaObject.appendChild(elType);
1349 List<MediaRepresentationPart> lmrp = mediarep.getParts();
1350 for (Iterator<MediaRepresentationPart> mrp = lmrp
1351 .iterator(); mrp.hasNext();) {
1352 MediaRepresentationPart mediareppart = mrp.next();
1353 ElementImpl elSource = new ElementImpl(document,
1354 "Source");
1355 elSource.setAttribute("href", mediareppart.getUri()
1356 .toString());
1357 elMediaObject.appendChild(elSource);
1358 }
1359 }
1360 elMediaObjects.appendChild(elMediaObject);
1361 }
1362 dataset.appendChild(elMediaObjects);
1363 }
1364 }
1365
1366 public void buildPublications(ElementImpl dataset) throws ParseException {
1367
1368 if (cdmSource.getReferences() != null) {
1369 ElementImpl elPublications = new ElementImpl(document, PUBLICATIONS);
1370 boolean editorial = false;
1371 for (int i = 0; i < cdmSource.getReferences().size(); i++) {
1372 ElementImpl elPublication = new ElementImpl(document,
1373 "Publication");
1374 Reference<?> publication = cdmSource.getReferences().get(i);
1375 Set<Annotation> annotations = publication.getAnnotations();
1376 for (Iterator<Annotation> a = annotations.iterator(); a
1377 .hasNext();) {
1378 Annotation annotation = a.next();
1379 AnnotationType annotationType = annotation
1380 .getAnnotationType();
1381 if (annotationType != null && annotationType.equals(AnnotationType.EDITORIAL())) {
1382 editorial = true;
1383 } else {
1384 editorial = false;
1385 }
1386 }
1387 if (!editorial) {
1388 articlesCount = buildReference(publication, articles, ID,
1389 elPublication, "p", articlesCount);
1390 buildRepresentation(elPublication, (IDatabase) publication);
1391 elPublications.appendChild(elPublication);
1392 }
1393 }
1394 dataset.appendChild(elPublications);
1395 }
1396 }
1397
1398 public int buildReference(VersionableEntity ve, Map references,
1399 String refOrId, ElementImpl element, String prefix, int count)
1400 throws ParseException {
1401 if (references.containsKey(ve)) {
1402 element.setAttribute(refOrId, (String) references.get(ve));
1403 } else {
1404 if (ve instanceof IdentifiableEntity) {
1405 IdentifiableEntity ie = (IdentifiableEntity) ve;
1406 if (ie.getSources().size() > 0) {
1407 IdentifiableSource os = (IdentifiableSource) ie
1408 .getSources().toArray()[0];
1409 String id = os.getIdInSource();
1410 String uri = os.getCitationMicroReference();
1411 if (uri != null) {
1412 element.setAttribute(URI, uri);
1413 }
1414 if (id != null) {
1415 if (!id.equals("")) {
1416 if (!references.containsValue(id)) {
1417 element.setAttribute(refOrId, id);
1418 } else
1419 while (element.getAttribute(refOrId).equals("")) {
1420 if (!references.containsValue(prefix
1421 + (count + 1))) {
1422 element.setAttribute(refOrId, prefix
1423 + (count + 1));
1424 }
1425 count++;
1426 }
1427 } else
1428 while (element.getAttribute(refOrId).equals("")) {
1429 if (!references.containsValue(prefix
1430 + (count + 1))) {
1431 element.setAttribute(refOrId, prefix
1432 + (count + 1));
1433 }
1434 count++;
1435 }
1436 } else
1437 while (element.getAttribute(refOrId).equals("")) {
1438 if (!references.containsValue(prefix + (count + 1))) {
1439 element.setAttribute(refOrId, prefix
1440 + (count + 1));
1441 }
1442 count++;
1443 }
1444 } else
1445 while (element.getAttribute(refOrId).equals("")) {
1446 if (!references.containsValue(prefix + (count + 1))) {
1447 element.setAttribute(refOrId, prefix + (count + 1));
1448 }
1449 count++;
1450 }
1451 } else
1452 while (element.getAttribute(refOrId).equals("")) {
1453 if (!references.containsValue(prefix + (count + 1))) {
1454 element.setAttribute(refOrId, prefix + (count + 1));
1455 }
1456 count++;
1457 }
1458 references.put(ve, element.getAttribute(refOrId));
1459 }
1460 return count;
1461 }
1462
1463 public void buildGeographicAreas(ElementImpl dataset) {
1464 if (cdmSource.getTerms() != null) {
1465 ElementImpl elGeographicAreas = new ElementImpl(document,
1466 "GeographicAreas");
1467
1468 int f = cdmSource.getTerms().size();
1469 for (int i = 0; i < f; i++) {
1470 if (cdmSource.getTerms().get(i) instanceof NamedArea) {
1471 NamedArea na = (NamedArea) cdmSource.getTerms().get(i);
1472 for (Iterator<Marker> mark = na.getMarkers().iterator(); mark
1473 .hasNext();) {
1474 Marker marker = mark.next();
1475 if (marker.getMarkerType().getLabel()
1476 .equals("SDDGeographicArea")) {
1477 ElementImpl elGeographicArea = new ElementImpl(
1478 document, "GeographicArea");
1479 namedAreasCount = buildReference(na, namedAreas,
1480 ID, elGeographicArea, "a", namedAreasCount);
1481 buildRepresentation(elGeographicArea, na);
1482 elGeographicAreas.appendChild(elGeographicArea);
1483 }
1484 }
1485
1486 }
1487 }
1488 dataset.appendChild(elGeographicAreas);
1489 }
1490 }
1491
1492 public void buildSpecimens(ElementImpl dataset) throws ParseException {
1493
1494 if (cdmSource.getOccurrences() != null) {
1495 ElementImpl elSpecimens = new ElementImpl(document, "Specimens");
1496
1497 for (int i = 0; i < cdmSource.getOccurrences().size(); i++) {
1498 ElementImpl elSpecimen = new ElementImpl(document, "Specimen");
1499 SpecimenOrObservationBase<?> sob = cdmSource.getOccurrences().get(i);
1500 if (sob.getRecordBasis().isPreservedSpecimen()) {
1501 specimenCount = buildReference(sob, specimens, ID, elSpecimen, "s", specimenCount);
1502 buildRepresentation(elSpecimen, sob);
1503 elSpecimens.appendChild(elSpecimen);
1504 }
1505 }
1506 dataset.appendChild(elSpecimens);
1507 }
1508
1509 }
1510
1511 }