Project

General

Profile

Download (29 KB) Statistics
| Branch: | Tag: | Revision:
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.util.HashMap;
18
import java.util.Iterator;
19
import java.util.List;
20
import java.util.Map;
21
import java.util.Set;
22

    
23
import javax.xml.bind.Marshaller;
24

    
25
import org.apache.log4j.Logger;
26
import org.apache.xerces.dom.DocumentImpl;
27
import org.apache.xerces.dom.ElementImpl;
28
import org.joda.time.DateTime;
29
import org.joda.time.format.DateTimeFormatter;
30
import org.joda.time.format.ISODateTimeFormat;
31
import org.xml.sax.SAXException;
32

    
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 eu.etaxonomy.cdm.io.jaxb.CdmMarshallerListener;
39
import eu.etaxonomy.cdm.model.agent.Person;
40
import eu.etaxonomy.cdm.model.agent.Team;
41
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
42
import eu.etaxonomy.cdm.model.common.Annotation;
43
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
44
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
45
import eu.etaxonomy.cdm.model.common.OriginalSource;
46
import eu.etaxonomy.cdm.model.common.Representation;
47
import eu.etaxonomy.cdm.model.common.TermBase;
48
import eu.etaxonomy.cdm.model.common.TermVocabulary;
49
import eu.etaxonomy.cdm.model.common.VersionableEntity;
50
import eu.etaxonomy.cdm.model.description.Feature;
51
import eu.etaxonomy.cdm.model.description.State;
52
import eu.etaxonomy.cdm.model.media.Media;
53
import eu.etaxonomy.cdm.model.media.Rights;
54
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
55
import eu.etaxonomy.cdm.model.reference.Article;
56
import eu.etaxonomy.cdm.model.reference.Database;
57
import eu.etaxonomy.cdm.model.reference.ReferenceBase;
58

    
59
/**
60
 * Writes the SDD XML file. 
61
 * 
62
 * @author h.fradin
63
 * @created 10.12.2008
64
 * @version 1.0
65
 */
66

    
67
public class SDDDocumentBuilder {
68

    
69
	private DocumentImpl document;
70
	private XMLSerializer xmlserializer;
71
	private Writer writer;
72
	private DOMSerializer domi;
73
	private SDDDataSet cdmSource;
74

    
75
	private Map<Person,String> agents = new HashMap<Person,String>();
76
	private Map<TaxonNameBase,String> taxonNames = new HashMap<TaxonNameBase,String>();
77
	private Map<Feature,String> characters = new HashMap<Feature,String>();
78
	private Map<Media,String> medias = new HashMap<Media,String>();
79
	private Map<State,String> states = new HashMap<State,String>();
80
	private Map<Article, String> articles = new HashMap<Article, String>();
81
	private int agentsCount = 0;
82
	private int taxonNamesCount = 0;
83
	private int charactersCount = 0;
84
	private int mediasCount = 0;
85
	private int statesCount = 0;
86

    
87
	private String AGENT = "Agent";
88
	private String AGENTS = "Agents";
89
	private String CATEGORICAL = "Categorical";
90
	private String CATEGORICAL_CHARACTER = "CategoricalCharacter";
91
	private String CHARACTER = "Character";
92
	private String CHARACTERS = "Characters";
93
	private String CHARACTER_TREE = "CharacterTree";
94
	private String CHARACTER_TREES = "CharacterTrees";
95
	private String CHAR_NODE = "CharNode";
96
	private String CODED_DESCRIPTION = "CodedDescription";
97
	private String CODED_DESCRIPTIONS = "CodedDescriptions";
98
	private String CREATORS = "Creators";
99
	private String DATASET = "Dataset";
100
	private String DATASETS = "Datasets";
101
	private String DATE_CREATED = "DateCreated";
102
	private String DATE_MODIFIED = "DateModified";
103
	private String DEPENDENCY_RULES = "DependencyRules";
104
	private String DESCRIPTIVE_CONCEPT = "DescriptiveConcept";
105
	private String DESCRIPTIVE_CONCEPTS = "DescriptiveConcepts";
106
	private String DETAIL = "Detail";
107
	private String GENERATOR = "Generator";
108
	private String ID = "id";
109
	private String IMAGE = "Image";
110
	private String INAPPLICABLE_IF = "InapplicableIf";
111
	private String IPR_STATEMENT = "IPRStatement";
112
	private String IPR_STATEMENTS = "IPRStatements";
113
	private String LABEL = "Label";
114
	private String MEDIA_OBJECT = "MediaObject";
115
	private String MEDIA_OBJECTS = "MediaObjects";
116
	private String NODE = "Node";
117
	private String NODES = "Nodes";
118
	private String NOTE = "Note";
119
	private String PARENT = "Parent";
120
	private String QUANTITATIVE_CHARACTER = "QuantitativeCharacter";
121
	private String REF = "ref";
122
	private String REPRESENTATION = "Representation";
123
	private String REVISION_DATA = "RevisionData";
124
	private String ROLE = "role";
125
	private String SHOULD_CONTAIN_ALL_CHARACTERS = "ShouldContainAllCharacters";
126
	private String SOURCE = "Source";
127
	private String STATE = "State";
128
	private String STATE_DEFINITION = "StateDefinition";
129
	private String STATES = "States";
130
	private String STATUS = "Status";
131
	private String SUMMARY_DATA = "SummaryData";
132
	private String TAXON_NAME = "TaxonName";
133
	private String TAXON_NAMES = "TaxonNames";
134
	private String TECHNICAL_METADATA = "TechnicalMetadata";
135
	private String TEXT = "Text";
136
	private String TEXT_CHARACTER = "TextCharacter";
137
	private String TYPE = "Type";
138
	private String URI = "uri";
139

    
140
	private static final Logger logger = Logger.getLogger(SDDDocumentBuilder.class);
141

    
142
	// private SDDContext sddContext;
143

    
144
	public SDDDocumentBuilder() throws SAXException, IOException {
145

    
146
		document = new DocumentImpl();
147

    
148
		// sddContext = SDDContext.newInstance(new Class[] {SDDDataSet.class});
149
		// logger.debug(sddContext.toString());
150

    
151
	}
152

    
153
	public void marshal(SDDDataSet cdmSource, File sddDestination) throws IOException {
154

    
155
		this.cdmSource = cdmSource;
156
		Marshaller marshaller;		
157
		CdmMarshallerListener marshallerListener = new CdmMarshallerListener();
158
		logger.info("Start marshalling");
159
		writeCDMtoSDD(sddDestination);
160

    
161
	}
162

    
163
	/**Write the DOM document.
164
	 * @param base
165
	 * @throws IOException
166
	 */
167
	public void writeCDMtoSDD(File sddDestination) throws IOException {
168

    
169
		try {
170
			buildDocument();
171
		} catch (ParseException e) {
172
			System.out.println("Problem with SDD export located in the buildDocument() method ...");
173
			e.printStackTrace();
174
		}
175

    
176
		OutputFormat format = new OutputFormat(document, "UTF-8", true);
177

    
178
		FileOutputStream fos = new FileOutputStream(sddDestination);
179

    
180
		writer = new OutputStreamWriter(fos, "UTF-8");
181

    
182
		xmlserializer = new XMLSerializer(writer, format);
183
		domi = xmlserializer.asDOMSerializer(); // As a DOM Serializer
184

    
185
		domi.serialize(document.getDocumentElement());
186

    
187
		writer.close();
188
	}
189

    
190
	//	#############
191
	//	# BUILD DOM	#
192
	//	#############	
193

    
194
	/**
195
	 * Builds the whole document.
196
	 * @param base the Base
197
	 * @throws ParseException 
198
	 */
199
	public void buildDocument() throws ParseException {
200

    
201
		//create <Datasets> = root node
202
		ElementImpl baselement = new ElementImpl(document, DATASETS);
203

    
204
		baselement.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
205
		baselement.setAttribute("xmlns", "http://rs.tdwg.org/UBIF/2006/");
206
		baselement.setAttribute("xsi:schemaLocation", "http://rs.tdwg.org/UBIF/2006 http://rs.tdwg.org/UBIF/2006/Schema/1.1/SDD.xsd");
207

    
208
		buildTechnicalMetadata(baselement);
209

    
210
		List<ReferenceBase> references = cdmSource.getReferences();
211
		Iterator<ReferenceBase> iterator = references.iterator();
212
		Database d = Database.NewInstance();
213
		while (iterator.hasNext()) {
214
			ReferenceBase reference = (ReferenceBase) iterator.next();
215
			if (reference instanceof Database) {
216
				buildDataset(baselement, (Database) reference);
217
			}
218
		}
219

    
220
		// for datasets with no Database ReferenceBase
221
		// buildDataset(baselement, cdmSource, null);
222

    
223
		//append the root element to the DOM document
224
		document.appendChild(baselement);
225
	}
226

    
227
	//	#############
228
	//	# BUILD DOM	#
229
	//	#############	
230

    
231
	/**
232
	 * Builds TechnicalMetadata associated with the SDD file
233
	 */
234
	public void buildTechnicalMetadata(ElementImpl baselement) throws ParseException {
235
		//create TechnicalMetadata
236
		ElementImpl technicalMetadata = new ElementImpl(document, TECHNICAL_METADATA);
237
		//select different databases associated to different descriptions TODO
238
		List<ReferenceBase> references = cdmSource.getReferences();
239
		Iterator<ReferenceBase> iterator = references.iterator();
240
		boolean database = false;
241
		Database d = Database.NewInstance();
242
		while ((iterator.hasNext()) && (!database)) {
243
			ReferenceBase reference = (ReferenceBase) iterator.next();
244
			if (reference instanceof Database) {
245
				d = (Database) reference;
246
			}
247
		}
248
		DateTime dt = d.getCreated();
249
		String date = dt.toString().substring(0, 19);
250
		technicalMetadata.setAttribute("created", date);
251

    
252
		ElementImpl generator = new ElementImpl(document, GENERATOR);
253
		generator.setAttribute("name", "EDIT CDM");
254
		generator.setAttribute("version", "v1");
255
		generator.setAttribute("notes","This SDD file has been generated by the SDD export functionality of the EDIT platform for Cybertaxonomy - Copyright (c) 2008");
256
		technicalMetadata.appendChild(generator);
257

    
258
		baselement.appendChild(technicalMetadata);
259
	}
260

    
261
	// Builds the information associated with a dataset
262
	public void buildDataset(ElementImpl baselement, Database reference) throws ParseException {
263
		// create Dataset and language
264
		ElementImpl dataset = new ElementImpl(document, DATASET);
265
		// no default language associated with a dataset in the CDM
266
		// dataset.setAttribute("xml:lang", datasetLanguage);
267
		baselement.appendChild(dataset);
268
		buildRepresentation(dataset, reference);
269
		buildRevisionData(dataset, reference);
270
		buildIPRStatements(dataset, reference);
271
		buildTaxonNames(dataset);
272
		buildCharacters(dataset);
273

    
274
	}
275

    
276
	/**
277
	 * Builds a Representation element using a ReferenceBase
278
	 */
279
	public void buildRepresentation(ElementImpl element, ReferenceBase reference) throws ParseException {
280

    
281
		//			create <Representation> element
282
		ElementImpl representation = new ElementImpl(document, REPRESENTATION);
283
		element.appendChild(representation);
284
		buildLabel(representation, reference.getTitleCache());
285

    
286
		Set<Annotation> annotations = reference.getAnnotations();
287
		Iterator iterator = annotations.iterator();
288
		String detailText = null;
289
		if (iterator.hasNext()) {
290
			Annotation annotation = (Annotation) iterator.next();
291
			detailText = annotation.getText();
292
		}
293

    
294
		if (detailText != null && !detailText.equals("")) {
295
			ElementImpl detail = new ElementImpl(document, DETAIL);
296
			detail.appendChild(document.createTextNode(detailText));
297
			representation.appendChild(detail);
298
		}
299

    
300
		Set<Media> rm = reference.getMedia();
301

    
302
		if (rm != null && rm.size() > 0) {
303
			ElementImpl mediaObject;
304

    
305
			for (int i = 0; i < rm.size(); i++) {
306
				mediaObject = new ElementImpl(document, MEDIA_OBJECT);
307
				//mediaObject = org.apache.xerces.dom.ElementImpl(document, MEDIA_OBJECT);
308
				mediasCount = buildReference((Media) rm.toArray()[i], medias, REF, mediaObject, "m", mediasCount);
309
				representation.appendChild(mediaObject);
310
			}
311
		}
312

    
313
	}
314

    
315
	//	################
316
	//	# GENERIC BRICKS       #
317
	//	################
318

    
319
	/**
320
	 * Creates a Label element 
321
	 * @param base
322
	 * @param element
323
	 */
324
	public void buildLabel(ElementImpl element, String text) {
325
		//		create <Label> element
326
		ElementImpl label = new ElementImpl(document, LABEL);
327

    
328
		// if language different from language dataset, indicate it TODO, but need to deal with a database language
329
		label.appendChild(document.createTextNode(text));
330
		element.appendChild(label);
331
	}
332

    
333

    
334
	/**
335
	 * Builds TaxonNames associated with the Dataset
336
	 */
337
	public void buildTaxonNames(ElementImpl dataset) throws ParseException {
338

    
339
		// <TaxonNames>
340
		//  <TaxonName id="t1" uri="urn:lsid:authority:namespace:my-own-id">
341
		//    <Representation>
342
		//      <Label xml:lang="la">Viola hederacea Labill.</Label>
343
		//    </Representation>
344
		//  </TaxonName>
345
		// </TaxonNames>
346

    
347
		if (cdmSource.getTaxonomicNames() != null) {
348
			ElementImpl elTaxonNames = new ElementImpl(document, TAXON_NAMES);
349

    
350
			for (int i = 0; i < cdmSource.getTaxonomicNames().size(); i++) {
351
				ElementImpl elTaxonName = new ElementImpl(document, TAXON_NAME);
352
				TaxonNameBase tnb = cdmSource.getTaxonomicNames().get(i);
353

    
354
				taxonNamesCount = buildReference(tnb, taxonNames, REF, elTaxonName, "t", taxonNamesCount);
355

    
356
				buildRepresentation(elTaxonName, tnb);
357

    
358
				elTaxonNames.appendChild(elTaxonName);
359
			}
360

    
361
			dataset.appendChild(elTaxonNames);
362
		}
363

    
364
	}
365

    
366
	/**
367
	 * Builds an element Agent referring to Agent defined later in the SDD file
368
	 */
369
	public void buildRefAgent(ElementImpl element, TeamOrPersonBase ag, String role) throws ParseException {
370
		if (ag instanceof Person) {
371
			Person p = (Person) ag;
372
			ElementImpl agent = new ElementImpl(document, AGENT);
373
			agent.setAttribute(ROLE, role);
374
			agentsCount = buildReference(p, agents, REF, agent, "a", agentsCount);
375
			element.appendChild(agent);
376
		}
377

    
378
		if (ag instanceof Team) {
379
			Team team = (Team) ag;
380
			for (int i = 0; i < team.getTeamMembers().size(); i++) {
381
				ElementImpl agent = new ElementImpl(document, AGENT);
382
				agent.setAttribute(ROLE, role);
383
				Person author = team.getTeamMembers().get(i);
384
				if (author.getSources() != null) {
385
					OriginalSource os = (OriginalSource) author.getSources().toArray()[0];
386
					String id = os.getIdInSource();
387
					if (id != null) {
388
						if (!id.equals("")) {
389
							if (!agents.containsValue(id)) {
390
								agent.setAttribute(REF, id);
391
							} else if (!agents.containsValue("a" + (agentsCount+1))) {
392
								agent.setAttribute(REF, "a" + (agentsCount+1));
393
								agentsCount++;
394
							} else {
395
								agent.setAttribute(REF, id + (agentsCount+1));
396
								agentsCount++;
397
							}
398
						} else {
399
							agent.setAttribute(REF, "a" + (agentsCount+1));
400
							agentsCount++;
401
						}
402
					} else {
403
						agent.setAttribute(REF, "a" + (agentsCount+1));
404
						agentsCount++;
405
					}
406
				} else {
407
					agent.setAttribute(REF, "a" + (agentsCount+1));
408
					agentsCount++;
409
				}
410
				agents.put(author, agent.getAttribute(REF));
411
				element.appendChild(agent);
412
			}
413
		}
414
	}
415

    
416
	/**
417
	 * Builds ModifiedDate associated with RevisionData
418
	 */
419
	public void buildDateModified(ElementImpl revisionData, Database database) throws ParseException {
420

    
421
		//  <DateModified>2006-04-08T00:00:00</DateModified>
422

    
423
		if (database.getUpdated() != null) {
424
			ElementImpl dateModified = new ElementImpl(document, DATE_MODIFIED);
425

    
426
			DateTime c = database.getUpdated();
427
			DateTimeFormatter fmt = ISODateTimeFormat.dateTime();
428
		
429
			String date = fmt.print(c);
430
			dateModified.appendChild(document.createTextNode(date));
431

    
432
			revisionData.appendChild(dateModified);
433
		}
434

    
435
	}
436

    
437
	/**
438
	 * Builds IPRStatements associated with the Dataset
439
	 */
440
	public void buildIPRStatements(ElementImpl dataset, Database database) throws ParseException {
441

    
442
		// <IPRStatements>
443
		//  <IPRStatement role="Copyright">
444
		//    <Label xml:lang="en-au">(c) 2003-2006 Centre for Occasional Botany.</Label>
445
		//  </IPRStatement>
446
		// </IPRStatements>
447

    
448
		if (database.getRights() != null) {
449
			//			create IPRStatements
450
			ElementImpl iprStatements = new ElementImpl(document, IPR_STATEMENTS);
451
			dataset.appendChild(iprStatements);
452

    
453
			//mapping between IPRStatement Copyright (SDD) and first Right in the list of Rights
454
			ElementImpl iprStatement = new ElementImpl(document, IPR_STATEMENT);
455
			iprStatement.setAttribute("role", "Copyright");
456
			iprStatements.appendChild(iprStatement);
457
			buildLabel(iprStatement, ((Rights) database.getRights().toArray()[0]).getText());
458
		}
459

    
460
	}
461

    
462
	/**
463
	 * Builds RevisionData associated with the Dataset
464
	 */
465
	public void buildRevisionData(ElementImpl dataset, Database database) throws ParseException {
466

    
467
		// <RevisionData>
468
		//  <Creators>
469
		//    <Agent role="aut" ref="a1"/>
470
		//    <Agent role="aut" ref="a2"/>
471
		//    <Agent role="edt" ref="a3"/>
472
		//  </Creators>
473
		//  <DateModified>2006-04-08T00:00:00</DateModified>
474
		// </RevisionData>
475

    
476
		ElementImpl revisionData = new ElementImpl(document, REVISION_DATA);
477

    
478
		// authors
479
		TeamOrPersonBase authors = database.getAuthorTeam();
480
//		TeamOrPersonBase editors = database.getUpdatedBy();
481

    
482
		if ((authors != null)) { // || (editors != null)) {
483
			ElementImpl creators = new ElementImpl(document, CREATORS);
484
			if (authors != null) {
485
				buildRefAgent(creators, authors, "aut");
486
			}
487
//			if (editors != null) {
488
//				buildRefAgent(creators, editors, "edt");
489
//			}
490
			revisionData.appendChild(creators);
491
		}
492

    
493
		buildDateModified(revisionData, database);
494

    
495
		dataset.appendChild(revisionData);
496
	}
497

    
498
	/**
499
	 * Builds a Representation element using an IdentifiableEntity 
500
	 */
501
	public void buildRepresentation(ElementImpl element, IdentifiableEntity ie) throws ParseException {
502

    
503
		//			create <Representation> element
504
		ElementImpl representation = new ElementImpl(document, REPRESENTATION);
505
		element.appendChild(representation);
506
		buildLabel(representation, ie.getTitleCache());
507

    
508
		Set<Annotation> annotations = ie.getAnnotations();
509
		Iterator iterator = annotations.iterator();
510
		String detailText = null;
511
		if (iterator.hasNext()) {
512
			Annotation annotation = (Annotation) iterator.next();
513
			detailText = annotation.getText();
514
		}
515

    
516
		if (detailText != null && !detailText.equals("")) {
517
			ElementImpl detail = new ElementImpl(document, DETAIL);
518
			detail.appendChild(document.createTextNode(detailText));
519
			representation.appendChild(detail);
520
		}
521

    
522
	}
523

    
524
	/**
525
	 * Builds Characters associated with the Dataset
526
	 */
527
	public void buildCharacters(ElementImpl dataset) throws ParseException {
528

    
529
		if (cdmSource.getTerms() != null) {
530
			ElementImpl elCharacters = new ElementImpl(document, CHARACTERS);
531

    
532
			//TODO Boucle infinie
533
			int f = cdmSource.getTerms().size();
534
			for (int i = 0; i < f; i++) {
535
				if (cdmSource.getTerms().get(i) instanceof Feature) {
536
					Feature character = (Feature) cdmSource.getTerms().get(i);
537
					if (character.supportsQuantitativeData()) {
538
						ElementImpl elQuantitativeCharacter = new ElementImpl(document, QUANTITATIVE_CHARACTER);
539
						charactersCount = buildReference(character, characters, ID, elQuantitativeCharacter, "c", charactersCount);
540
						// TODO if the character also supports text, add to the label a short tag to distinguish
541
						// it as the quantitative version and create a unique label
542
						buildRepresentation(elQuantitativeCharacter, character);
543
						// TODO <MeasurementUnit> and <Default>
544
						elCharacters.appendChild(elQuantitativeCharacter);
545
					}
546
					if (character.supportsTextData()) {
547
						Set<TermVocabulary<State>> enumerations = character.getSupportedCategoricalEnumerations();
548
						if (enumerations != null) {
549
							if (enumerations.size()>0) {
550
								ElementImpl elCategoricalCharacter = new ElementImpl(document, CATEGORICAL_CHARACTER);
551
								charactersCount = buildReference(character, characters, ID, elCategoricalCharacter, "c", charactersCount);
552
								buildRepresentation(elCategoricalCharacter, character);
553
								ElementImpl elStates = new ElementImpl(document, STATES);
554
								TermVocabulary tv = (TermVocabulary) enumerations.toArray()[0];
555
								Set<State> stateList = tv.getTerms();
556
								for (int j = 0; j < stateList.size(); j++) {
557
									ElementImpl elStateDefinition = new ElementImpl(document, STATE_DEFINITION);
558
									State state = (State) stateList.toArray()[j];
559
									statesCount = buildReference(state, states, ID, elStateDefinition, "s", statesCount);
560
									buildRepresentation(elStateDefinition, state);
561
									elStates.appendChild(elStateDefinition);
562
								}
563
								elCategoricalCharacter.appendChild(elStates);
564
								elCharacters.appendChild(elCategoricalCharacter);
565
							}
566
						}
567
						if ((enumerations == null) || (enumerations.size() <= 0)) {
568
							ElementImpl elTextCharacter = new ElementImpl(document, TEXT_CHARACTER);
569
							charactersCount = buildReference(character, characters, ID, elTextCharacter, "c", charactersCount);
570
							buildRepresentation(elTextCharacter, character);
571
							// TODO <MeasurementUnit> and <Default>
572
							elCharacters.appendChild(elTextCharacter);
573
						}
574
					}
575
				}
576
			}
577

    
578
			dataset.appendChild(elCharacters);
579
		}
580

    
581
	}
582

    
583
	/**
584
	 * Builds an element Agent referring to Agent defined later in the SDD file
585
	 */
586
	public int buildReference(VersionableEntity ve, Map references, String refOrId, ElementImpl element, String prefix, int count) throws ParseException {
587
		if (ve instanceof IdentifiableEntity) {
588
			IdentifiableEntity ie = (IdentifiableEntity) ve;
589
			if (ie.getSources() != null) {
590
				OriginalSource os = (OriginalSource) ie.getSources().toArray()[0];
591
				String id = os.getIdInSource();
592
				if (id != null) {
593
					if (!id.equals("")) {
594
						if (!references.containsValue(id)) {
595
							element.setAttribute(refOrId, id);
596
						} else while (element.getAttribute(refOrId).equals("")) {
597
							if (!references.containsValue(prefix + (count+1))) {
598
								element.setAttribute(refOrId, prefix + (count+1));
599
							}
600
							count++;
601
						}
602
					} else while (element.getAttribute(refOrId).equals("")) {
603
						if (!references.containsValue(prefix + (count+1))) {
604
							element.setAttribute(refOrId, prefix + (count+1));
605
						}
606
						count++;
607
					}
608
				} else while (element.getAttribute(refOrId).equals("")) {
609
					if (!references.containsValue(prefix + (count+1))) {
610
						element.setAttribute(refOrId, prefix + (count+1));
611
					}
612
					count++;
613
				}
614
			} else while (element.getAttribute(refOrId).equals("")) {
615
				if (!references.containsValue(prefix + (count+1))) {
616
					element.setAttribute(refOrId, prefix + (count+1));
617
				}
618
				count++;
619
			}
620
		} else while (element.getAttribute(refOrId).equals("")) {
621
			if (!references.containsValue(prefix + (count+1))) {
622
				element.setAttribute(refOrId, prefix + (count+1));
623
			}
624
			count++;
625
		}
626
		references.put(ve, element.getAttribute(refOrId));
627
		return count;
628
	}
629

    
630
	/**
631
	 * Builds a Representation element using a Feature
632
	 */
633
	public void buildRepresentation(ElementImpl element, TermBase tb) throws ParseException {
634
	
635
		//			create <Representation> element
636
		ElementImpl representation = new ElementImpl(document, REPRESENTATION);
637
		element.appendChild(representation);
638
		String label = ((Representation) tb.getRepresentations().toArray()[0]).getLabel();
639
		buildLabel(representation, label);
640
	
641
		String detailText = tb.getDescription();
642
	
643
		if (detailText != null && !detailText.equals("")) {
644
			if (!detailText.equals(label)) {
645
				ElementImpl detail = new ElementImpl(document, DETAIL);
646
				detail.appendChild(document.createTextNode(detailText));
647
				representation.appendChild(detail);
648
			}
649
		}
650
	
651
		if (tb instanceof DefinedTermBase) {
652
			DefinedTermBase dtb = (DefinedTermBase) tb;
653
			Set<Media> rm = dtb.getMedia();
654

    
655
			if (rm != null && rm.size() > 0) {
656
				ElementImpl mediaObject;
657

    
658
				for (int i = 0; i < rm.size(); i++) {
659
					mediaObject = new ElementImpl(document, MEDIA_OBJECT);
660
					mediasCount = buildReference((Media) rm.toArray()[i], medias, REF, mediaObject, "m", mediasCount);
661
					representation.appendChild(mediaObject);
662
				}
663
			}
664
		}
665
	
666
	}
667

    
668

    
669
	//	/**
670
	//	 * Build Hashtables with the references for the different elements that build the dataset, 
671
	//	 * and that are then used in the different building elements methods
672
	//	 */
673
	//
674
	//	public void buildReferences() {
675
	//
676
	//		// <TaxonNames> references
677
	//		for (int i = 0; i < cdmSource.getTaxonomicNames().size(); i++) {
678
	//			OriginalSource os = (OriginalSource) cdmSource.getAgents().get(i).getSources().toArray()[i];
679
	//			String id = os.getIdNamespace();
680
	//			if (id != null) {
681
	//				if (!id.equals("")) {
682
	//					if (!references.containsValue(id)) {
683
	//						references.put(cdmSource.getAgents().get(i), id);
684
	//					} else if (!references.containsValue("a" + (i+1))) {
685
	//						references.put(cdmSource.getAgents().get(i), "a" + (i+1));
686
	//					} else {
687
	//						references.put(cdmSource.getAgents().get(i), id + (i+1));
688
	//					}
689
	//				} else {
690
	//					references.put(cdmSource.getAgents().get(i), "a" + (i+1));
691
	//				}
692
	//			} else {
693
	//				references.put(cdmSource.getAgents().get(i), "a" + (i+1));
694
	//			}
695
	//		}
696
	//
697
	//		// <Character> references
698
	//		for (int i = 0; i < cdmSource.getFeatureData().size(); i++) {
699
	//			references.put(cdmSource.getFeatureData().get(i), "c" + (i+1));
700
	//		}
701
	//
702
	//		/* no groups so far in CDM TODO
703
	//		// <DescriptiveConcept> and <Node> references
704
	//		for (int i = 0; i < base.getNbGroups(); i++) {
705
	//			references.put(base.getGroupAt(i), "dc" + (i+1));
706
	//		}
707
	//		 */
708
	//
709
	//		// <State> references
710
	//
711
	//		for (int i = 0; i < cdmSource.get(); i++) {
712
	//			variable = base.getVariableAt(i);
713
	//			for (int j = 0; j < variable.getNbModes(); j++) {
714
	//				references.put(variable.getModeAt(j), "s" + statesCounter);
715
	//				statesCounter++;
716
	//			}
717
	//		}
718
	//
719
	//		// <CodedDescription> references
720
	//		for (int i = 0; i < base.getNbIndividuals(); i++) {
721
	//			references.put(base.getIndividualAt(i), "D" + (i+1));
722
	//		}
723
	//
724
	//		// <MediaObject> references
725
	//		// TODO
726
	//		ArrayList al = base.getAllResources();
727
	//		for (int i = 0; i < al.size(); i++) {
728
	//			BaseObjectResource bor = (BaseObjectResource) al.get(i);
729
	//
730
	//			if (!referencesMediaObjects.containsKey(bor)) {
731
	//				referencesMediaObjects.put(bor, "m" + mediaObjectsCounter);
732
	//				mediaObjectsCounter++;
733
	//			}
734
	//		}
735
	//
736
	//		// base.images
737
	//		// BaseObjectResource bor = base.getResource();
738
	//		//bor.getName();
739
	//		//bor.getDescription();
740
	//		//bor.getFullFilename();
741
	//
742
	//		if (!referencesMediaObjects.containsKey(bor)) {
743
	//			referencesMediaObjects.put(bor, "m" + mediaObjectsCounter);
744
	//			mediaObjectsCounter++;
745
	//		}
746
	//
747
	//		// group.images
748
	//
749
	//		for (int i = 0; i < base.getNbGroups(); i++) {
750
	//			Object[] tab = ((Group) base.getGroupAt(i)).getAllResources();
751
	//
752
	//			for (int j = 0; j < tab.length; j++) {
753
	//				bor = (BaseObjectResource) tab[j];
754
	//
755
	//				if (!referencesMediaObjects.containsKey(bor)) {
756
	//					referencesMediaObjects.put(bor, "m" + mediaObjectsCounter);
757
	//					mediaObjectsCounter++;
758
	//				}
759
	//			}
760
	//		}
761
	//
762
	//		int nbGroups = base.getNbGroups();
763
	//		ArrayList mObjArrayList;
764
	//		if (nbGroups > 0) {
765
	//			for (int i = 0; i < nbGroups; i++) {
766
	//				mObjArrayList = ((Group) base.getGroupAt(i)).getImages();
767
	//				for (int j = 0; j < mObjArrayList.size(); j++) {
768
	//					String temp = (String) mObjArrayList.get(j);
769
	//					if (!referencesMediaObjects.containsKey(temp)) {
770
	//						referencesMediaObjects.put(temp, "m" + mediaObjectsCounter);
771
	//						mediaObjectsCounter++;
772
	//					}
773
	//				}
774
	//			}
775
	//		}
776
	//
777
	//		// individual.images
778
	//		for (int i = 0; i < base.getNbIndividuals(); i++) {
779
	//			Object[] tab = ((Individual) base.getIndividualAt(i)).getAllResources();
780
	//
781
	//			for (int j = 0; j < tab.length; j++) {
782
	//				bor = (BaseObjectResource) tab[j];
783
	//
784
	//				if (!referencesMediaObjects.containsKey(bor)) {
785
	//					referencesMediaObjects.put(bor, "m" + mediaObjectsCounter);
786
	//					mediaObjectsCounter++;
787
	//				}
788
	//			}
789
	//		}
790
	//
791
	//		int nbIndividuals = base.getNbIndividuals();
792
	//		if (nbIndividuals > 0) {
793
	//			for (int i = 0; i < nbIndividuals; i++) {
794
	//				mObjArrayList = ((Individual) base.getIndividualAt(i)).getImages();
795
	//				for (int j = 0; j < mObjArrayList.size(); j++) {
796
	//					String temp = (String) mObjArrayList.get(j);
797
	//					if (!referencesMediaObjects.containsKey(temp)) {
798
	//						referencesMediaObjects.put(temp, "m" + mediaObjectsCounter);
799
	//						mediaObjectsCounter++;
800
	//					}
801
	//				}
802
	//			}
803
	//		}
804
	//
805
	//		// variable.images
806
	//
807
	//		int nbVariables = base.getNbVariables();
808
	//		if (nbVariables > 0) {
809
	//			for (int i = 0; i < nbVariables; i++) {
810
	//				mObjArrayList = ((Variable) base.getVariableAt(i)).getImages();
811
	//				for (int j = 0; j < mObjArrayList.size(); j++) {
812
	//					String temp = (String) mObjArrayList.get(j);
813
	//					if (!referencesMediaObjects.containsKey(temp)) {
814
	//						referencesMediaObjects.put(temp, "m" + mediaObjectsCounter);
815
	//						mediaObjectsCounter++;
816
	//					}
817
	//				}
818
	//			}
819
	//		}
820
	//
821
	//		// mode.images
822
	//
823
	//		int nbModesTotal = base.getNbModes();
824
	//		int nbModes;
825
	//		if (nbModesTotal > 0) {
826
	//			for (int i = 0; i < nbVariables; i++) {
827
	//				variable = (Variable) base.getVariableAt(i);
828
	//				nbModes = variable.getNbModes();
829
	//				for (int j = 0; j < nbModes; j++) {
830
	//					mObjArrayList = variable.getModeAt(j).getImages();
831
	//					for (int k = 0; k < mObjArrayList.size(); k++) {
832
	//						String temp = (String) mObjArrayList.get(k);
833
	//						if (!referencesMediaObjects.containsKey(temp)) {
834
	//							referencesMediaObjects.put(temp, "m" + mediaObjectsCounter);
835
	//							mediaObjectsCounter++;
836
	//						}
837
	//					}
838
	//				}
839
	//			}
840
	//		}
841
	//
842
	//		for (int i = 0; i < base.getLinks().size(); i++) {
843
	//			referencesLinks.put(base.getLinkAt(i), "m" + mediaObjectsCounter);
844
	//			mediaObjectsCounter++;
845
	//		}
846
	//
847
	//	}
848

    
849
}
850

    
851

    
(4-4/7)