Committing large number of changes relating to versioning implementation (#108) ...
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / sdd / 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;
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.text.SimpleDateFormat;
18 import java.util.Date;
19 import java.util.HashMap;
20 import java.util.Iterator;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.Set;
24
25 import javax.xml.bind.Marshaller;
26
27 import org.apache.log4j.Logger;
28 import org.joda.time.DateTime;
29 import org.xml.sax.SAXException;
30
31 import org.apache.xerces.dom.DocumentImpl;
32 //import com.sun.org.apache.xerces.internal.dom.ElementImpl;
33 import com.sun.org.apache.xerces.internal.impl.xpath.regex.ParseException;
34 import com.sun.org.apache.xml.internal.serialize.DOMSerializer;
35 import com.sun.org.apache.xml.internal.serialize.OutputFormat;
36 import com.sun.org.apache.xml.internal.serialize.XMLSerializer;
37
38 import org.apache.xerces.dom.ElementImpl;
39
40
41 import eu.etaxonomy.cdm.io.jaxb.CdmMarshallerListener;
42 import eu.etaxonomy.cdm.model.agent.Person;
43 import eu.etaxonomy.cdm.model.agent.Team;
44 import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
45 import eu.etaxonomy.cdm.model.common.Annotation;
46 import eu.etaxonomy.cdm.model.common.DefinedTermBase;
47 import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
48 import eu.etaxonomy.cdm.model.common.OriginalSource;
49 import eu.etaxonomy.cdm.model.common.Representation;
50 import eu.etaxonomy.cdm.model.common.TermBase;
51 import eu.etaxonomy.cdm.model.common.TermVocabulary;
52 import eu.etaxonomy.cdm.model.common.VersionableEntity;
53 import eu.etaxonomy.cdm.model.description.Feature;
54 import eu.etaxonomy.cdm.model.description.State;
55 import eu.etaxonomy.cdm.model.media.Media;
56 import eu.etaxonomy.cdm.model.media.Rights;
57 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
58 import eu.etaxonomy.cdm.model.reference.Database;
59 import eu.etaxonomy.cdm.model.reference.ReferenceBase;
60
61 /**
62 * Writes the SDD XML file.
63 *
64 * @author h.fradin
65 * @created 10.12.2008
66 * @version 1.0
67 */
68
69 public class SDDDocumentBuilder {
70
71 private DocumentImpl document;
72 private XMLSerializer xmlserializer;
73 private Writer writer;
74 private DOMSerializer domi;
75 private SDDDataSet cdmSource;
76
77 private Map<Person,String> agents = new HashMap<Person,String>();
78 private Map<TaxonNameBase,String> taxonNames = new HashMap<TaxonNameBase,String>();
79 private Map<Feature,String> characters = new HashMap<Feature,String>();
80 private Map<Media,String> medias = new HashMap<Media,String>();
81 private Map<State,String> states = new HashMap<State,String>();
82 private int agentsCount = 0;
83 private int taxonNamesCount = 0;
84 private int charactersCount = 0;
85 private int mediasCount = 0;
86 private int statesCount = 0;
87
88 private String AGENT = "Agent";
89 private String AGENTS = "Agents";
90 private String CATEGORICAL = "Categorical";
91 private String CATEGORICAL_CHARACTER = "CategoricalCharacter";
92 private String CHARACTER = "Character";
93 private String CHARACTERS = "Characters";
94 private String CHARACTER_TREE = "CharacterTree";
95 private String CHARACTER_TREES = "CharacterTrees";
96 private String CHAR_NODE = "CharNode";
97 private String CODED_DESCRIPTION = "CodedDescription";
98 private String CODED_DESCRIPTIONS = "CodedDescriptions";
99 private String CREATORS = "Creators";
100 private String DATASET = "Dataset";
101 private String DATASETS = "Datasets";
102 private String DATE_CREATED = "DateCreated";
103 private String DATE_MODIFIED = "DateModified";
104 private String DEPENDENCY_RULES = "DependencyRules";
105 private String DESCRIPTIVE_CONCEPT = "DescriptiveConcept";
106 private String DESCRIPTIVE_CONCEPTS = "DescriptiveConcepts";
107 private String DETAIL = "Detail";
108 private String GENERATOR = "Generator";
109 private String ID = "id";
110 private String IMAGE = "Image";
111 private String INAPPLICABLE_IF = "InapplicableIf";
112 private String IPR_STATEMENT = "IPRStatement";
113 private String IPR_STATEMENTS = "IPRStatements";
114 private String LABEL = "Label";
115 private String MEDIA_OBJECT = "MediaObject";
116 private String MEDIA_OBJECTS = "MediaObjects";
117 private String NODE = "Node";
118 private String NODES = "Nodes";
119 private String NOTE = "Note";
120 private String PARENT = "Parent";
121 private String QUANTITATIVE_CHARACTER = "QuantitativeCharacter";
122 private String REF = "ref";
123 private String REPRESENTATION = "Representation";
124 private String REVISION_DATA = "RevisionData";
125 private String ROLE = "role";
126 private String SHOULD_CONTAIN_ALL_CHARACTERS = "ShouldContainAllCharacters";
127 private String SOURCE = "Source";
128 private String STATE = "State";
129 private String STATE_DEFINITION = "StateDefinition";
130 private String STATES = "States";
131 private String STATUS = "Status";
132 private String SUMMARY_DATA = "SummaryData";
133 private String TAXON_NAME = "TaxonName";
134 private String TAXON_NAMES = "TaxonNames";
135 private String TECHNICAL_METADATA = "TechnicalMetadata";
136 private String TEXT = "Text";
137 private String TEXT_CHARACTER = "TextCharacter";
138 private String TYPE = "Type";
139 private String URI = "uri";
140
141 private static final Logger logger = Logger.getLogger(SDDDocumentBuilder.class);
142
143 // private SDDContext sddContext;
144
145 public SDDDocumentBuilder() throws SAXException, IOException {
146
147 document = new DocumentImpl();
148
149 // sddContext = SDDContext.newInstance(new Class[] {SDDDataSet.class});
150 // logger.debug(sddContext.toString());
151
152 }
153
154 public void marshal(SDDDataSet cdmSource, File sddDestination) throws IOException {
155
156 this.cdmSource = cdmSource;
157 Marshaller marshaller;
158 CdmMarshallerListener marshallerListener = new CdmMarshallerListener();
159 logger.info("Start marshalling");
160 writeCDMtoSDD(sddDestination);
161
162 }
163
164 /**Write the DOM document.
165 * @param base
166 * @throws IOException
167 */
168 public void writeCDMtoSDD(File sddDestination) throws IOException {
169
170 try {
171 buildDocument();
172 } catch (ParseException e) {
173 System.out.println("Problem with SDD export located in the buildDocument() method ...");
174 e.printStackTrace();
175 }
176
177 OutputFormat format = new OutputFormat(document, "UTF-8", true);
178
179 FileOutputStream fos = new FileOutputStream(sddDestination);
180
181 writer = new OutputStreamWriter(fos, "UTF-8");
182
183 xmlserializer = new XMLSerializer(writer, format);
184 domi = xmlserializer.asDOMSerializer(); // As a DOM Serializer
185
186 domi.serialize(document.getDocumentElement());
187
188 writer.close();
189 }
190
191 // #############
192 // # BUILD DOM #
193 // #############
194
195 /**
196 * Builds the whole document.
197 * @param base the Base
198 * @throws ParseException
199 */
200 public void buildDocument() throws ParseException {
201
202 //create <Datasets> = root node
203 ElementImpl baselement = new ElementImpl(document, DATASETS);
204
205 baselement.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
206 baselement.setAttribute("xmlns", "http://rs.tdwg.org/UBIF/2006/");
207 baselement.setAttribute("xsi:schemaLocation", "http://rs.tdwg.org/UBIF/2006 http://rs.tdwg.org/UBIF/2006/Schema/1.1/SDD.xsd");
208
209 buildTechnicalMetadata(baselement);
210
211 List<ReferenceBase> references = cdmSource.getReferences();
212 Iterator<ReferenceBase> iterator = references.iterator();
213 Database d = Database.NewInstance();
214 while (iterator.hasNext()) {
215 ReferenceBase reference = (ReferenceBase) iterator.next();
216 if (reference instanceof Database) {
217 buildDataset(baselement, (Database) reference);
218 }
219 }
220
221 // for datasets with no Database ReferenceBase
222 // buildDataset(baselement, cdmSource, null);
223
224 //append the root element to the DOM document
225 document.appendChild(baselement);
226 }
227
228 // #############
229 // # BUILD DOM #
230 // #############
231
232 /**
233 * Builds TechnicalMetadata associated with the SDD file
234 */
235 public void buildTechnicalMetadata(ElementImpl baselement) throws ParseException {
236 //create TechnicalMetadata
237 ElementImpl technicalMetadata = new ElementImpl(document, TECHNICAL_METADATA);
238 //select different databases associated to different descriptions TODO
239 List<ReferenceBase> references = cdmSource.getReferences();
240 Iterator<ReferenceBase> iterator = references.iterator();
241 boolean database = false;
242 Database d = Database.NewInstance();
243 while ((iterator.hasNext()) && (!database)) {
244 ReferenceBase reference = (ReferenceBase) iterator.next();
245 if (reference instanceof Database) {
246 d = (Database) reference;
247 }
248 }
249 DateTime dt = d.getCreated();
250 String date = dt.toString().substring(0, 19);
251 technicalMetadata.setAttribute("created", date);
252
253 ElementImpl generator = new ElementImpl(document, GENERATOR);
254 generator.setAttribute("name", "EDIT CDM");
255 generator.setAttribute("version", "v1");
256 generator.setAttribute("notes","This SDD file has been generated by the SDD export functionality of the EDIT platform for Cybertaxonomy - Copyright (c) 2008");
257 technicalMetadata.appendChild(generator);
258
259 baselement.appendChild(technicalMetadata);
260 }
261
262 // Builds the information associated with a dataset
263 public void buildDataset(ElementImpl baselement, Database reference) throws ParseException {
264 // create Dataset and language
265 ElementImpl dataset = new ElementImpl(document, DATASET);
266 // no default language associated with a dataset in the CDM
267 // dataset.setAttribute("xml:lang", datasetLanguage);
268 baselement.appendChild(dataset);
269 buildRepresentation(dataset, reference);
270 buildRevisionData(dataset, reference);
271 buildIPRStatements(dataset, reference);
272 buildTaxonNames(dataset);
273 buildCharacters(dataset);
274
275 }
276
277 /**
278 * Builds a Representation element using a ReferenceBase
279 */
280 public void buildRepresentation(ElementImpl element, ReferenceBase reference) throws ParseException {
281
282 // create <Representation> element
283 ElementImpl representation = new ElementImpl(document, REPRESENTATION);
284 element.appendChild(representation);
285 buildLabel(representation, reference.getTitleCache());
286
287 Set<Annotation> annotations = reference.getAnnotations();
288 Iterator iterator = annotations.iterator();
289 String detailText = null;
290 if (iterator.hasNext()) {
291 Annotation annotation = (Annotation) iterator.next();
292 detailText = annotation.getText();
293 }
294
295 if (detailText != null && !detailText.equals("")) {
296 ElementImpl detail = new ElementImpl(document, DETAIL);
297 detail.appendChild(document.createTextNode(detailText));
298 representation.appendChild(detail);
299 }
300
301 Set<Media> rm = reference.getMedia();
302
303 if (rm != null && rm.size() > 0) {
304 ElementImpl mediaObject;
305
306 for (int i = 0; i < rm.size(); i++) {
307 mediaObject = new ElementImpl(document, MEDIA_OBJECT);
308 //mediaObject = org.apache.xerces.dom.ElementImpl(document, MEDIA_OBJECT);
309 mediasCount = buildReference((Media) rm.toArray()[i], medias, REF, mediaObject, "m", mediasCount);
310 representation.appendChild(mediaObject);
311 }
312 }
313
314 }
315
316 // ################
317 // # GENERIC BRICKS #
318 // ################
319
320 /**
321 * Creates a Label element
322 * @param base
323 * @param element
324 */
325 public void buildLabel(ElementImpl element, String text) {
326 // create <Label> element
327 ElementImpl label = new ElementImpl(document, LABEL);
328
329 // if language different from language dataset, indicate it TODO, but need to deal with a database language
330 label.appendChild(document.createTextNode(text));
331 element.appendChild(label);
332 }
333
334
335 /**
336 * Builds TaxonNames associated with the Dataset
337 */
338 public void buildTaxonNames(ElementImpl dataset) throws ParseException {
339
340 // <TaxonNames>
341 // <TaxonName id="t1" uri="urn:lsid:authority:namespace:my-own-id">
342 // <Representation>
343 // <Label xml:lang="la">Viola hederacea Labill.</Label>
344 // </Representation>
345 // </TaxonName>
346 // </TaxonNames>
347
348 if (cdmSource.getTaxonomicNames() != null) {
349 ElementImpl elTaxonNames = new ElementImpl(document, TAXON_NAMES);
350
351 for (int i = 0; i < cdmSource.getTaxonomicNames().size(); i++) {
352 ElementImpl elTaxonName = new ElementImpl(document, TAXON_NAME);
353 TaxonNameBase tnb = cdmSource.getTaxonomicNames().get(i);
354
355 taxonNamesCount = buildReference(tnb, taxonNames, REF, elTaxonName, "t", taxonNamesCount);
356
357 buildRepresentation(elTaxonName, tnb);
358
359 elTaxonNames.appendChild(elTaxonName);
360 }
361
362 dataset.appendChild(elTaxonNames);
363 }
364
365 }
366
367 /**
368 * Builds an element Agent referring to Agent defined later in the SDD file
369 */
370 public void buildRefAgent(ElementImpl element, TeamOrPersonBase ag, String role) throws ParseException {
371 if (ag instanceof Person) {
372 Person p = (Person) ag;
373 ElementImpl agent = new ElementImpl(document, AGENT);
374 agent.setAttribute(ROLE, role);
375 agentsCount = buildReference(p, agents, REF, agent, "a", agentsCount);
376 element.appendChild(agent);
377 }
378
379 if (ag instanceof Team) {
380 Team team = (Team) ag;
381 for (int i = 0; i < team.getTeamMembers().size(); i++) {
382 ElementImpl agent = new ElementImpl(document, AGENT);
383 agent.setAttribute(ROLE, role);
384 Person author = team.getTeamMembers().get(i);
385 if (author.getSources() != null) {
386 OriginalSource os = (OriginalSource) author.getSources().toArray()[0];
387 String id = os.getIdInSource();
388 if (id != null) {
389 if (!id.equals("")) {
390 if (!agents.containsValue(id)) {
391 agent.setAttribute(REF, id);
392 } else if (!agents.containsValue("a" + (agentsCount+1))) {
393 agent.setAttribute(REF, "a" + (agentsCount+1));
394 agentsCount++;
395 } else {
396 agent.setAttribute(REF, id + (agentsCount+1));
397 agentsCount++;
398 }
399 } else {
400 agent.setAttribute(REF, "a" + (agentsCount+1));
401 agentsCount++;
402 }
403 } else {
404 agent.setAttribute(REF, "a" + (agentsCount+1));
405 agentsCount++;
406 }
407 } else {
408 agent.setAttribute(REF, "a" + (agentsCount+1));
409 agentsCount++;
410 }
411 agents.put(author, agent.getAttribute(REF));
412 element.appendChild(agent);
413 }
414 }
415 }
416
417 /**
418 * Builds ModifiedDate associated with RevisionData
419 */
420 public void buildDateModified(ElementImpl revisionData, Database database) throws ParseException {
421
422 // <DateModified>2006-04-08T00:00:00</DateModified>
423
424 if (database.getUpdated() != null) {
425 ElementImpl dateModified = new ElementImpl(document, DATE_MODIFIED);
426
427 java.util.Calendar c = database.getUpdated();
428 Date d = c.getTime();
429 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss");
430 String date = sdf.format(d);
431 dateModified.appendChild(document.createTextNode(date));
432
433 revisionData.appendChild(dateModified);
434 }
435
436 }
437
438 /**
439 * Builds IPRStatements associated with the Dataset
440 */
441 public void buildIPRStatements(ElementImpl dataset, Database database) throws ParseException {
442
443 // <IPRStatements>
444 // <IPRStatement role="Copyright">
445 // <Label xml:lang="en-au">(c) 2003-2006 Centre for Occasional Botany.</Label>
446 // </IPRStatement>
447 // </IPRStatements>
448
449 if (database.getRights() != null) {
450 // create IPRStatements
451 ElementImpl iprStatements = new ElementImpl(document, IPR_STATEMENTS);
452 dataset.appendChild(iprStatements);
453
454 //mapping between IPRStatement Copyright (SDD) and first Right in the list of Rights
455 ElementImpl iprStatement = new ElementImpl(document, IPR_STATEMENT);
456 iprStatement.setAttribute("role", "Copyright");
457 iprStatements.appendChild(iprStatement);
458 buildLabel(iprStatement, ((Rights) database.getRights().toArray()[0]).getText());
459 }
460
461 }
462
463 /**
464 * Builds RevisionData associated with the Dataset
465 */
466 public void buildRevisionData(ElementImpl dataset, Database database) throws ParseException {
467
468 // <RevisionData>
469 // <Creators>
470 // <Agent role="aut" ref="a1"/>
471 // <Agent role="aut" ref="a2"/>
472 // <Agent role="edt" ref="a3"/>
473 // </Creators>
474 // <DateModified>2006-04-08T00:00:00</DateModified>
475 // </RevisionData>
476
477 ElementImpl revisionData = new ElementImpl(document, REVISION_DATA);
478
479 // authors
480 TeamOrPersonBase authors = database.getAuthorTeam();
481 TeamOrPersonBase editors = database.getUpdatedBy();
482
483 if ((authors != null) || (editors != null)) {
484 ElementImpl creators = new ElementImpl(document, CREATORS);
485 if (authors != null) {
486 buildRefAgent(creators, authors, "aut");
487 }
488 if (editors != null) {
489 buildRefAgent(creators, editors, "edt");
490 }
491 revisionData.appendChild(creators);
492 }
493
494 buildDateModified(revisionData, database);
495
496 dataset.appendChild(revisionData);
497 }
498
499 /**
500 * Builds a Representation element using an IdentifiableEntity
501 */
502 public void buildRepresentation(ElementImpl element, IdentifiableEntity ie) throws ParseException {
503
504 // create <Representation> element
505 ElementImpl representation = new ElementImpl(document, REPRESENTATION);
506 element.appendChild(representation);
507 buildLabel(representation, ie.getTitleCache());
508
509 Set<Annotation> annotations = ie.getAnnotations();
510 Iterator iterator = annotations.iterator();
511 String detailText = null;
512 if (iterator.hasNext()) {
513 Annotation annotation = (Annotation) iterator.next();
514 detailText = annotation.getText();
515 }
516
517 if (detailText != null && !detailText.equals("")) {
518 ElementImpl detail = new ElementImpl(document, DETAIL);
519 detail.appendChild(document.createTextNode(detailText));
520 representation.appendChild(detail);
521 }
522
523 }
524
525 /**
526 * Builds Characters associated with the Dataset
527 */
528 public void buildCharacters(ElementImpl dataset) throws ParseException {
529
530 if (cdmSource.getTerms() != null) {
531 ElementImpl elCharacters = new ElementImpl(document, CHARACTERS);
532
533 //TODO Boucle infinie
534 int f = cdmSource.getTerms().size();
535 for (int i = 0; i < f; i++) {
536 if (cdmSource.getTerms().get(i) instanceof Feature) {
537 Feature character = (Feature) cdmSource.getTerms().get(i);
538 if (character.supportsQuantitativeData()) {
539 ElementImpl elQuantitativeCharacter = new ElementImpl(document, QUANTITATIVE_CHARACTER);
540 charactersCount = buildReference(character, characters, ID, elQuantitativeCharacter, "c", charactersCount);
541 // TODO if the character also supports text, add to the label a short tag to distinguish
542 // it as the quantitative version and create a unique label
543 buildRepresentation(elQuantitativeCharacter, character);
544 // TODO <MeasurementUnit> and <Default>
545 elCharacters.appendChild(elQuantitativeCharacter);
546 }
547 if (character.supportsTextData()) {
548 Set<TermVocabulary<State>> enumerations = character.getSupportedCategoricalEnumerations();
549 if (enumerations != null) {
550 if (enumerations.size()>0) {
551 ElementImpl elCategoricalCharacter = new ElementImpl(document, CATEGORICAL_CHARACTER);
552 charactersCount = buildReference(character, characters, ID, elCategoricalCharacter, "c", charactersCount);
553 buildRepresentation(elCategoricalCharacter, character);
554 ElementImpl elStates = new ElementImpl(document, STATES);
555 TermVocabulary tv = (TermVocabulary) enumerations.toArray()[0];
556 Set<State> stateList = tv.getTerms();
557 for (int j = 0; j < stateList.size(); j++) {
558 ElementImpl elStateDefinition = new ElementImpl(document, STATE_DEFINITION);
559 State state = (State) stateList.toArray()[j];
560 statesCount = buildReference(state, states, ID, elStateDefinition, "s", statesCount);
561 buildRepresentation(elStateDefinition, state);
562 elStates.appendChild(elStateDefinition);
563 }
564 elCategoricalCharacter.appendChild(elStates);
565 elCharacters.appendChild(elCategoricalCharacter);
566 }
567 }
568 if ((enumerations == null) || (enumerations.size() <= 0)) {
569 ElementImpl elTextCharacter = new ElementImpl(document, TEXT_CHARACTER);
570 charactersCount = buildReference(character, characters, ID, elTextCharacter, "c", charactersCount);
571 buildRepresentation(elTextCharacter, character);
572 // TODO <MeasurementUnit> and <Default>
573 elCharacters.appendChild(elTextCharacter);
574 }
575 }
576 }
577 }
578
579 dataset.appendChild(elCharacters);
580 }
581
582 }
583
584 /**
585 * Builds an element Agent referring to Agent defined later in the SDD file
586 */
587 public int buildReference(VersionableEntity ve, Map references, String refOrId, ElementImpl element, String prefix, int count) throws ParseException {
588 if (ve instanceof IdentifiableEntity) {
589 IdentifiableEntity ie = (IdentifiableEntity) ve;
590 if (ie.getSources() != null) {
591 OriginalSource os = (OriginalSource) ie.getSources().toArray()[0];
592 String id = os.getIdInSource();
593 if (id != null) {
594 if (!id.equals("")) {
595 if (!references.containsValue(id)) {
596 element.setAttribute(refOrId, id);
597 } else while (element.getAttribute(refOrId).equals("")) {
598 if (!references.containsValue(prefix + (count+1))) {
599 element.setAttribute(refOrId, prefix + (count+1));
600 }
601 count++;
602 }
603 } else while (element.getAttribute(refOrId).equals("")) {
604 if (!references.containsValue(prefix + (count+1))) {
605 element.setAttribute(refOrId, prefix + (count+1));
606 }
607 count++;
608 }
609 } else while (element.getAttribute(refOrId).equals("")) {
610 if (!references.containsValue(prefix + (count+1))) {
611 element.setAttribute(refOrId, prefix + (count+1));
612 }
613 count++;
614 }
615 } else while (element.getAttribute(refOrId).equals("")) {
616 if (!references.containsValue(prefix + (count+1))) {
617 element.setAttribute(refOrId, prefix + (count+1));
618 }
619 count++;
620 }
621 } else while (element.getAttribute(refOrId).equals("")) {
622 if (!references.containsValue(prefix + (count+1))) {
623 element.setAttribute(refOrId, prefix + (count+1));
624 }
625 count++;
626 }
627 references.put(ve, element.getAttribute(refOrId));
628 return count;
629 }
630
631 /**
632 * Builds a Representation element using a Feature
633 */
634 public void buildRepresentation(ElementImpl element, TermBase tb) throws ParseException {
635
636 // create <Representation> element
637 ElementImpl representation = new ElementImpl(document, REPRESENTATION);
638 element.appendChild(representation);
639 String label = ((Representation) tb.getRepresentations().toArray()[0]).getLabel();
640 buildLabel(representation, label);
641
642 String detailText = tb.getDescription();
643
644 if (detailText != null && !detailText.equals("")) {
645 if (!detailText.equals(label)) {
646 ElementImpl detail = new ElementImpl(document, DETAIL);
647 detail.appendChild(document.createTextNode(detailText));
648 representation.appendChild(detail);
649 }
650 }
651
652 if (tb instanceof DefinedTermBase) {
653 DefinedTermBase dtb = (DefinedTermBase) tb;
654 Set<Media> rm = dtb.getMedia();
655
656 if (rm != null && rm.size() > 0) {
657 ElementImpl mediaObject;
658
659 for (int i = 0; i < rm.size(); i++) {
660 mediaObject = new ElementImpl(document, MEDIA_OBJECT);
661 mediasCount = buildReference((Media) rm.toArray()[i], medias, REF, mediaObject, "m", mediasCount);
662 representation.appendChild(mediaObject);
663 }
664 }
665 }
666
667 }
668
669
670 // /**
671 // * Build Hashtables with the references for the different elements that build the dataset,
672 // * and that are then used in the different building elements methods
673 // */
674 //
675 // public void buildReferences() {
676 //
677 // // <TaxonNames> references
678 // for (int i = 0; i < cdmSource.getTaxonomicNames().size(); i++) {
679 // OriginalSource os = (OriginalSource) cdmSource.getAgents().get(i).getSources().toArray()[i];
680 // String id = os.getIdNamespace();
681 // if (id != null) {
682 // if (!id.equals("")) {
683 // if (!references.containsValue(id)) {
684 // references.put(cdmSource.getAgents().get(i), id);
685 // } else if (!references.containsValue("a" + (i+1))) {
686 // references.put(cdmSource.getAgents().get(i), "a" + (i+1));
687 // } else {
688 // references.put(cdmSource.getAgents().get(i), id + (i+1));
689 // }
690 // } else {
691 // references.put(cdmSource.getAgents().get(i), "a" + (i+1));
692 // }
693 // } else {
694 // references.put(cdmSource.getAgents().get(i), "a" + (i+1));
695 // }
696 // }
697 //
698 // // <Character> references
699 // for (int i = 0; i < cdmSource.getFeatureData().size(); i++) {
700 // references.put(cdmSource.getFeatureData().get(i), "c" + (i+1));
701 // }
702 //
703 // /* no groups so far in CDM TODO
704 // // <DescriptiveConcept> and <Node> references
705 // for (int i = 0; i < base.getNbGroups(); i++) {
706 // references.put(base.getGroupAt(i), "dc" + (i+1));
707 // }
708 // */
709 //
710 // // <State> references
711 //
712 // for (int i = 0; i < cdmSource.get(); i++) {
713 // variable = base.getVariableAt(i);
714 // for (int j = 0; j < variable.getNbModes(); j++) {
715 // references.put(variable.getModeAt(j), "s" + statesCounter);
716 // statesCounter++;
717 // }
718 // }
719 //
720 // // <CodedDescription> references
721 // for (int i = 0; i < base.getNbIndividuals(); i++) {
722 // references.put(base.getIndividualAt(i), "D" + (i+1));
723 // }
724 //
725 // // <MediaObject> references
726 // // TODO
727 // ArrayList al = base.getAllResources();
728 // for (int i = 0; i < al.size(); i++) {
729 // BaseObjectResource bor = (BaseObjectResource) al.get(i);
730 //
731 // if (!referencesMediaObjects.containsKey(bor)) {
732 // referencesMediaObjects.put(bor, "m" + mediaObjectsCounter);
733 // mediaObjectsCounter++;
734 // }
735 // }
736 //
737 // // base.images
738 // // BaseObjectResource bor = base.getResource();
739 // //bor.getName();
740 // //bor.getDescription();
741 // //bor.getFullFilename();
742 //
743 // if (!referencesMediaObjects.containsKey(bor)) {
744 // referencesMediaObjects.put(bor, "m" + mediaObjectsCounter);
745 // mediaObjectsCounter++;
746 // }
747 //
748 // // group.images
749 //
750 // for (int i = 0; i < base.getNbGroups(); i++) {
751 // Object[] tab = ((Group) base.getGroupAt(i)).getAllResources();
752 //
753 // for (int j = 0; j < tab.length; j++) {
754 // bor = (BaseObjectResource) tab[j];
755 //
756 // if (!referencesMediaObjects.containsKey(bor)) {
757 // referencesMediaObjects.put(bor, "m" + mediaObjectsCounter);
758 // mediaObjectsCounter++;
759 // }
760 // }
761 // }
762 //
763 // int nbGroups = base.getNbGroups();
764 // ArrayList mObjArrayList;
765 // if (nbGroups > 0) {
766 // for (int i = 0; i < nbGroups; i++) {
767 // mObjArrayList = ((Group) base.getGroupAt(i)).getImages();
768 // for (int j = 0; j < mObjArrayList.size(); j++) {
769 // String temp = (String) mObjArrayList.get(j);
770 // if (!referencesMediaObjects.containsKey(temp)) {
771 // referencesMediaObjects.put(temp, "m" + mediaObjectsCounter);
772 // mediaObjectsCounter++;
773 // }
774 // }
775 // }
776 // }
777 //
778 // // individual.images
779 // for (int i = 0; i < base.getNbIndividuals(); i++) {
780 // Object[] tab = ((Individual) base.getIndividualAt(i)).getAllResources();
781 //
782 // for (int j = 0; j < tab.length; j++) {
783 // bor = (BaseObjectResource) tab[j];
784 //
785 // if (!referencesMediaObjects.containsKey(bor)) {
786 // referencesMediaObjects.put(bor, "m" + mediaObjectsCounter);
787 // mediaObjectsCounter++;
788 // }
789 // }
790 // }
791 //
792 // int nbIndividuals = base.getNbIndividuals();
793 // if (nbIndividuals > 0) {
794 // for (int i = 0; i < nbIndividuals; i++) {
795 // mObjArrayList = ((Individual) base.getIndividualAt(i)).getImages();
796 // for (int j = 0; j < mObjArrayList.size(); j++) {
797 // String temp = (String) mObjArrayList.get(j);
798 // if (!referencesMediaObjects.containsKey(temp)) {
799 // referencesMediaObjects.put(temp, "m" + mediaObjectsCounter);
800 // mediaObjectsCounter++;
801 // }
802 // }
803 // }
804 // }
805 //
806 // // variable.images
807 //
808 // int nbVariables = base.getNbVariables();
809 // if (nbVariables > 0) {
810 // for (int i = 0; i < nbVariables; i++) {
811 // mObjArrayList = ((Variable) base.getVariableAt(i)).getImages();
812 // for (int j = 0; j < mObjArrayList.size(); j++) {
813 // String temp = (String) mObjArrayList.get(j);
814 // if (!referencesMediaObjects.containsKey(temp)) {
815 // referencesMediaObjects.put(temp, "m" + mediaObjectsCounter);
816 // mediaObjectsCounter++;
817 // }
818 // }
819 // }
820 // }
821 //
822 // // mode.images
823 //
824 // int nbModesTotal = base.getNbModes();
825 // int nbModes;
826 // if (nbModesTotal > 0) {
827 // for (int i = 0; i < nbVariables; i++) {
828 // variable = (Variable) base.getVariableAt(i);
829 // nbModes = variable.getNbModes();
830 // for (int j = 0; j < nbModes; j++) {
831 // mObjArrayList = variable.getModeAt(j).getImages();
832 // for (int k = 0; k < mObjArrayList.size(); k++) {
833 // String temp = (String) mObjArrayList.get(k);
834 // if (!referencesMediaObjects.containsKey(temp)) {
835 // referencesMediaObjects.put(temp, "m" + mediaObjectsCounter);
836 // mediaObjectsCounter++;
837 // }
838 // }
839 // }
840 // }
841 // }
842 //
843 // for (int i = 0; i < base.getLinks().size(); i++) {
844 // referencesLinks.put(base.getLinkAt(i), "m" + mediaObjectsCounter);
845 // mediaObjectsCounter++;
846 // }
847 //
848 // }
849
850 }
851
852