-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
DataPackage.java
1249 lines (1060 loc) · 36.7 KB
/
DataPackage.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* '$RCSfile: DataPackage.java,v $'
*
* '$Author: leinfelder $'
* '$Date: 2008-08-12 23:28:18 $'
* '$Revision: 1.6 $'
*
* For Details: http://kepler.ecoinformatics.org
*
* Copyright (c) 2003 The Regents of the University of California.
* All rights reserved.
*
* Permission is hereby granted, without written agreement and without
* license or royalty fees, to use, copy, modify, and distribute this
* software and its documentation for any purpose, provided that the
* above copyright notice and the following two paragraphs appear in
* all copies of this software.
*
* IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
* FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
* ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
* IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
*
* THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
* PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY
* OF CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT,
* UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
*/
package org.ecoinformatics.datamanager.parser;
import java.io.File;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.util.TreeSet;
import java.util.Vector;
import java.util.regex.Pattern;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import org.ecoinformatics.datamanager.quality.EntityReport;
import org.ecoinformatics.datamanager.quality.QualityReport;
import org.ecoinformatics.datamanager.quality.QualityCheck;
import org.ecoinformatics.datamanager.quality.QualityCheck.Status;
import org.ecoinformatics.eml.EMLParser;
import org.ecoinformatics.eml.SAXValidate;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import edu.ucsb.nceas.utilities.XMLUtilities;
/**
* This class represents a metadata package information to describe entity
*
* @author tao
*/
public class DataPackage
{
/*
* Class fields
*/
private static final String IDENTIFIER_WITH_LEADING_ZERO_PATTERN = "^[A-Za-z_0-9\\-]+\\.0\\d+\\.\\d+$";
private static final String REVISION_WITH_LEADING_ZERO_PATTERN = "^[A-Za-z_0-9\\-]+\\.\\d+\\.0\\d+$";
//private static final String LTER_PACKAGE_ID_PATTERN = "^knb-lter-[a-z][a-z][a-z]\\.\\d+\\.\\d+$";
private static final String METACAT_PACKAGE_ID_PATTERN = "^[A-Za-z_0-9\\-]+\\.\\d+\\.\\d+$";
/* A comma-separated list of allowable scope values. This value should be set
* to non-null by applications that use the 'packageIdPattern' quality check.
*/
private static String scopeRegistry = null;
/*
* Instance fields
*/
private String accessXML = null; // <access> element XML string
private String emlNamespace = null; // e.g. "eml://ecoinformatics.org/eml-2.1.0"
private Entity[] entityList = null;
private boolean parserValid = false;
private boolean schemaValid = false;
private int numberOfKeywordElements = 0;
private int numberOfMethodsElements = 0;
private int numberOfCoverageElements = 0;
private int numberOfGeographicCoverageElements = 0;
private int numberOfTaxonomicCoverageElements = 0;
private int numberOfTemporalCoverageElements = 0;
private String packageId = null;
private String pubDate;
private QualityReport qualityReport = null;
private String system = null;
private String title = null;
private List<Party> creators = null;
private String language = null;
private List<String> keywords = null;
private String abstract_str = null;
private Party publisher = null;
/*
* Constructors
*/
/**
* Constructs a DataPackage object.
*
* @param packageId Identifier of this DataPackage
*/
public DataPackage(String packageId)
{
this.packageId = packageId;
this.qualityReport = new QualityReport(this);
this.creators = new ArrayList<Party>();
this.keywords = new ArrayList<String> ();
qualityCheckPackageId(packageId);
}
/*
* Class methods
*/
/**
* Applies the EML dereferencing stylesheet (whose path is set in the
* properties file and stored in the QualityReport class) to transform
* the original EML document to a fully dereferenced EML document.
*
* @param originalEmlString the original EML XML string
* @return emlString the result of the transformation from the
* original EML to the dereferenced EML
* @throws IllegalStateException
* if an error occurs during the transformation process
*/
public static String dereferenceEML(String originalEmlString)
throws IllegalStateException {
String dereferencedEmlString = "";
Result result;
StringWriter stringWriter = new StringWriter();
javax.xml.transform.Transformer transformer;
javax.xml.transform.TransformerFactory transformerFactory;
final String xslPath = QualityReport.getEmlDereferencerXSLTPath();
Source xmlSource;
File xsltFile = new File(xslPath);
Source xsltSource;
StringReader stringReader = new StringReader(originalEmlString);
xmlSource = new javax.xml.transform.stream.StreamSource(stringReader);
xsltSource = new javax.xml.transform.stream.StreamSource(xsltFile);
result = new javax.xml.transform.stream.StreamResult(stringWriter);
String transformerFactoryValue = System.getProperty("javax.xml.transform.TransformerFactory");
System.out.println("javax.xml.transform.TransformerFactory :" + transformerFactoryValue);
transformerFactory = javax.xml.transform.TransformerFactory.newInstance();
try {
transformer = transformerFactory.newTransformer(xsltSource);
transformer.transform(xmlSource, result);
dereferencedEmlString = stringWriter.toString();
}
catch (TransformerConfigurationException e) {
Throwable x = e;
if (e.getException() != null) {
x = e.getException();
}
x.printStackTrace();
throw new IllegalStateException(e);
}
catch (TransformerException e) {
Throwable x = e;
if (e.getException() != null) {
x = e.getException();
}
x.printStackTrace();
throw new IllegalStateException(e);
}
return dereferencedEmlString;
}
/**
* Sets the value of the scope registry, for use by the
* 'packageIdPattern' quality check.
*
* @param scopeRegistry A comma-separated list of allowable
* data package scope values, e.g. "scope1,scope2,scope3".
*/
public static void setScopeRegistry(String scopeRegistry) {
DataPackage.scopeRegistry = scopeRegistry;
}
/*
* Instance methods
*/
/**
* Adds a dataset-level quality check to the data packages's associated
* qualityReport object.
*
* @param qualityCheck the new quality check to add
*/
public void addDatasetQualityCheck(QualityCheck qualityCheck) {
if (qualityReport != null) {
qualityReport.addDatasetQualityCheck(qualityCheck);
}
}
/**
* Getter method for the accessXML field
*
* @return the value of the accessXML field
*/
public String getAccessXML() {
return accessXML;
}
/**
* Getter method for the emlNamespace field
* @return
*/
public String getEmlNamespace() {
return emlNamespace;
}
public Entity[] getEntities(String name)
{
Vector list = new Vector();
if (entityList != null) {
for (int i=0; i < entityList.length; i++) {
if (entityList[i].getName().equals(name)) {
list.add(entityList[i]);
}
}
}
return (Entity[]) list.toArray(new Entity[0]);
}
public Entity getEntity(String name) {
if (getEntities(name).length > 0) {
return getEntities(name)[0];
}
return null;
}
/**
* Get the language used in this eml document
* @return the language
*/
public String getLanguage() {
return language;
}
/**
* Set the language description
* @param language
*/
public void setLanguage(String language) {
this.language = language;
}
/**
* Get the list of keywords in this eml document
* @return
*/
public List<String> getKeywords() {
return keywords;
}
/**
* Set the keywords list
* @param keywords
*/
public void setKeywords(List<String> keywords) {
this.keywords = keywords;
}
/**
* Get the abstract of this eml document
* @return the abstract
*/
public String getAbstract() {
return abstract_str;
}
/**
* Set the abstract
* @param absctrac
*/
public void setAbstract(String abstract_str) {
this.abstract_str = abstract_str;
}
/**
* Determine whether the data package contains
* two entities with the same entityName value.
*
* @return The duplicate name, or null if no
* duplicate entity names were found
*/
public String findDuplicateEntityName() {
String duplicateName = null;
Entity[] entityArray = getEntityList();
if (entityArray != null) {
int len = entityArray.length;
TreeSet<String> treeSet = new TreeSet<String>();
for (int i = 0; i < len; i++) {
Entity entity = entityArray[i];
String entityName = entity.getName();
if (entityName != null) {
if (treeSet.contains(entityName)) {
duplicateName = entityName;
break;
}
else {
treeSet.add(entityName);
}
}
}
}
return duplicateName;
}
/**
* Adds an entity into the DataPackage
*
* @param entity The entity which will be added
*/
public void add(Entity entity)
{
addEntityIntoArray(entity);
}
/**
* Gets the entity array which is in this DataPackage.
*
* @return Entity array in the DataPackage
*/
public Entity[] getEntityList()
{
return entityList;
}
/**
* Gets the number of entities in this DataPackage.
*
* @return an int representing the number of entities
*/
public int getEntityNumber()
{
if (entityList == null) {
return 0;
}
else {
return entityList.length;
}
}
/**
* Gets the qualityReport object associated with this data package.
*
* @return the qualityReport instance variable
*/
public QualityReport getQualityReport()
{
return qualityReport;
}
/**
* Gets the value of the system attribute.
*
* @return a String, e.g. "knb"
*/
public String getSystem() {
return system;
}
/**
* Gets the package identifier for this DataPackage.
*
* @return a string representing the DataPackage identifier
*/
public String getPackageId()
{
return packageId;
}
/*
* Add an entity into the entityList array.
*
* @param entity the entity object to be added to the array
*/
private void addEntityIntoArray(Entity entity) {
if (entityList == null) {
entityList = new Entity[1];
entityList[0] = entity;
}
else {
int size = entityList.length;
Entity[] tmp = new Entity[size + 1];
for (int i = 0; i < size; i++) {
tmp[i] = entityList[i];
}
tmp[size] = entity;
entityList = tmp;
}
}
/**
* Removes ALL previously added Entity objects from the DataPackage
*/
public void clearEntityList() {
entityList = null;
}
/**
* Boolean to determine whether this data package has at
* least one dataset-level quality error. This can be called
* after parsing the metadata. If a data package is found to
* have a dataset-level quality error, an application may want
* to halt processing at that point instead of drilling down
* to the entity-level.
*
* @return true if one or more dataset-level quality errors are
* found, else false
*/
public boolean hasDatasetQualityError() {
boolean hasError = (qualityReport != null &&
qualityReport.hasDatasetQualityError());
return hasError;
}
/**
* Boolean to determine whether this data package has at
* least one entity-level quality error.
*
* @return true if one or more entity-level quality errors are
* found, else false
*/
public boolean hasEntityQualityError() {
boolean hasError = false;
Entity[] entityArray = getEntityList();
for (int i = 0; i < entityArray.length; i++) {
Entity entity = entityArray[i];
EntityReport entityReport = entity.getEntityReport();
if (entityReport != null) {
if (entityReport.hasEntityQualityError()) {
hasError = true;
}
}
}
return hasError;
}
/*
* Gets the value of the numberOfKeywordElements variable.
*
* @return the number of keyword elements detected in this
* data package
*/
public int getNumberOfKeywordElements() {
return numberOfKeywordElements;
}
/*
* Gets the value of the numberOfMethodsElements variable.
*
* @return the number of methods elements detected in this
* data package
*/
public int getNumberOfMethodsElements() {
return numberOfMethodsElements;
}
/**
* Boolean to determine whether this data package has at
* least one dataset-level or entity-level quality error.
*
* @return true if one or more quality errors are
* found, else false
*/
public boolean hasQualityError() {
return hasDatasetQualityError() || hasEntityQualityError();
}
/*
* Boolean to determine whether a given packageId has an
* identifier with a leading zero character. This usually causes problems
* with subsequent processing of the data package and should be
* flagged as an error. A zero by itself is acceptable, however.
*
* Examples:
*
* "datapackage.012.12" // returns true
* "datapackage.0.12" // returns false
* "datapackage.10.12" // returns false
*/
private boolean identifierHasLeadingZero(String packageId) {
boolean hasLeadingZero = false;
String regexPattern = IDENTIFIER_WITH_LEADING_ZERO_PATTERN;
hasLeadingZero = Pattern.matches(regexPattern, packageId);
return hasLeadingZero;
}
/*
* Boolean to determine whether a given packageId has a
* revision with a leading zero character. If true, this may cause problems
* with subsequent processing of the data package and should be
* flagged as an error. A zero by itself is acceptable, however.
*
* Examples:
*
* "datapackage.12.012" // returns true
* "datapackage.12.0" // returns false
* "datapackage.12.11" // returns false
*/
private boolean revisionHasLeadingZero(String packageId) {
boolean hasLeadingZero = false;
String regexPattern = REVISION_WITH_LEADING_ZERO_PATTERN;
hasLeadingZero = Pattern.matches(regexPattern, packageId);
return hasLeadingZero;
}
/**
* Gets the parserValid value
*
* @return the value of the parserValid variable
*/
public boolean isParserValid() {
return parserValid;
}
/**
* Gets the schemaValid value
*
* @return the value of the schemaValid variable
*/
public boolean isSchemaValid() {
return schemaValid;
}
/*
* Boolean to determine whether a given packageId conforms to
* the string pattern of a particular organization such as LTER.
* If the specified system value does not have a regular
* expression pattern declared for its packageId, then the
* packageId is assumed to be valid by default.
*/
private boolean isValidPackageIdForMetacat(String packageId) {
boolean isValid = true;
String regexPattern = METACAT_PACKAGE_ID_PATTERN;
isValid = Pattern.matches(regexPattern, packageId);
return isValid;
}
/*
* Boolean to determine whether a given packageId scope value conforms to
* the allowed set of scopes of a particular organization such as LTER.
*/
private boolean isValidScope(String packageId) {
boolean isValid = false;
if (packageId == null)
return false;
if (scopeRegistry == null)
return true;
StringTokenizer stringTokenizer = new StringTokenizer(scopeRegistry, ",");
final int tokenCount = stringTokenizer.countTokens();
for (int i = 0; i < tokenCount; i++) {
String token = stringTokenizer.nextToken();
if (packageId.startsWith(token + ".")) {
isValid = true;
break;
}
}
return isValid;
}
/**
* Setter method for accessXML field.
*
* @param xmlString the XML string to assign to the
* accessXML field. Should be a block
* of <access> XML.
*/
public void setAccessXML(String xmlString) {
this.accessXML = xmlString;
}
/*
* Executes the "EML packageId check" when applicable
*/
private void qualityCheckPackageId(String packageId) {
// Check the value of the 'packageId' attribute
String packageIdIdentifier = "packageIdPattern";
QualityCheck packageIdTemplate = QualityReport.getQualityCheckTemplate(packageIdIdentifier);
QualityCheck packageIdQualityCheck = new QualityCheck(packageIdIdentifier, packageIdTemplate);
String systemAttribute = packageIdQualityCheck.getSystem();
if (QualityCheck.shouldRunQualityCheck(this, packageIdQualityCheck)) {
if (scopeRegistry != null) {
packageIdQualityCheck.setExpected("'scope.n.m', where 'n' and 'm' are integers and 'scope' is one of an allowed set of values");
}
if (packageId != null) {
packageIdQualityCheck.setFound(packageId);
}
if (!isValidPackageIdForMetacat(packageId)) {
packageIdQualityCheck.setStatus(Status.error);
packageIdQualityCheck.setExplanation("The packageId value should match the pattern 'scope.identifier.revision'.");
}
else if (identifierHasLeadingZero(packageId)) {
packageIdQualityCheck.setStatus(Status.error);
packageIdQualityCheck.setExplanation("A leading zero was found in the identifier. The identifier value must be a whole number.");
packageIdQualityCheck.setSuggestion("Remove leading zeros from the identifier value.");
}
else if (revisionHasLeadingZero(packageId)) {
packageIdQualityCheck.setStatus(Status.error);
packageIdQualityCheck.setExplanation("A leading zero was found in the revision. The revision value must be a whole number.");
packageIdQualityCheck.setSuggestion("Remove leading zeros from the revision value.");
}
else if (isValidScope(packageId)) {
packageIdQualityCheck.setStatus(Status.valid);
packageIdQualityCheck.setSuggestion("");
}
else {
packageIdQualityCheck.setFailedStatus();
if (scopeRegistry != null) {
packageIdQualityCheck.setExplanation(String.format(
"A packageId should start with one of the following scope values: %s",
scopeRegistry));
if (systemAttribute != null && systemAttribute.equals("lter")) {
packageIdQualityCheck.setSuggestion(
"Use a scope value that you are authorized to use for your site or project, " +
"or you may request that a new scope value be added to the list of allowed " +
"values by contacting tech_support@LTERnet.edu.");
}
}
}
this.addDatasetQualityCheck(packageIdQualityCheck);
}
}
/**
* Setter method for emlNamespace field.
*
* @param emlNamespace the emlNamespace value to set,
* e.g. "eml://ecoinformatics.org/eml-2.1.0"
*/
public void setEmlNamespace(String emlNamespace) {
this.emlNamespace = emlNamespace;
// Check the value of the 'xmlns:eml' attribute
String emlVersionIdentifier = "emlVersion";
QualityCheck emlVersionTemplate = QualityReport.getQualityCheckTemplate(emlVersionIdentifier);
QualityCheck emlVersionQualityCheck = new QualityCheck(emlVersionIdentifier, emlVersionTemplate);
if (QualityCheck.shouldRunQualityCheck(this, emlVersionQualityCheck)) {
// Initialize the emlNamespaceQualityCheck
boolean isValidNamespace = false;
if (emlNamespace != null) {
emlVersionQualityCheck.setFound(emlNamespace);
if (emlNamespace.equals("eml://ecoinformatics.org/eml-2.1.0") ||
emlNamespace.equals("eml://ecoinformatics.org/eml-2.1.1")
) {
isValidNamespace = true;
}
}
if (isValidNamespace) {
emlVersionQualityCheck.setStatus(Status.valid);
emlVersionQualityCheck.setSuggestion("");
}
else {
emlVersionQualityCheck.setFailedStatus();
}
this.addDatasetQualityCheck(emlVersionQualityCheck);
}
}
/**
* Performs the 'schemaValid' quality check.
*
* @param doc the XML DOM document object
* @param namespaceInDoc the namespace value specified in the document
*/
public void checkSchemaValid(Document doc, String namespaceInDoc) {
String schemaValidIdentifier = "schemaValid";
QualityCheck schemaValidTemplate =
QualityReport.getQualityCheckTemplate(schemaValidIdentifier);
QualityCheck schemaValidQualityCheck =
new QualityCheck(schemaValidIdentifier, schemaValidTemplate);
if (QualityCheck.shouldRunQualityCheck(this, schemaValidQualityCheck)) {
// Initialize the schemaValidQualityCheck
boolean validateSchema = true;
String found = "";
final String parserName = "DEFAULT";
Node documentElement = doc.getDocumentElement();
String xmlString = XMLUtilities.getDOMTreeAsString(documentElement);
StringReader stringReader = new StringReader(xmlString);
SAXValidate saxValidate = new SAXValidate(validateSchema);
try {
saxValidate.runTest(stringReader, parserName, namespaceInDoc);
found = "Document validated for namespace: '" + namespaceInDoc + "'";
schemaValidQualityCheck.setStatus(Status.valid);
schemaValidQualityCheck.setSuggestion("");
this.schemaValid = true;
}
catch (Exception e) {
found = "Failed to validate for namespace: '" + namespaceInDoc +
"'; " + e.getMessage();
schemaValidQualityCheck.setFailedStatus();
}
schemaValidQualityCheck.setFound(found);
this.addDatasetQualityCheck(schemaValidQualityCheck);
}
}
/**
* Performs the 'parserValid' quality check, using the 'EML IDs
* and References Parser' code in the org.ecoinformatics.eml.EMLParser
* class.
*
* @param doc the XML DOM document object
*/
public void checkParserValid(Document doc) {
String parserValidIdentifier = "parserValid";
QualityCheck parserValidTemplate =
QualityReport.getQualityCheckTemplate(parserValidIdentifier);
QualityCheck parserValidQualityCheck =
new QualityCheck(parserValidIdentifier, parserValidTemplate);
if (QualityCheck.shouldRunQualityCheck(this, parserValidQualityCheck)) {
// Initialize the parserValidQualityCheck
String found = "";
Node documentElement = doc.getDocumentElement();
String xmlString = XMLUtilities.getDOMTreeAsString(documentElement);
try {
EMLParser emlParser = new EMLParser(xmlString);
found = "EML IDs and references parser succeeded";
parserValidQualityCheck.setStatus(Status.valid);
parserValidQualityCheck.setSuggestion("");
this.parserValid = true;
}
catch (Exception e) {
found = "Failed to parse IDs and references: " + e.getMessage();
parserValidQualityCheck.setFailedStatus();
}
parserValidQualityCheck.setFound(found);
this.addDatasetQualityCheck(parserValidQualityCheck);
}
}
/**
* Performs the 'schemaValidDereferenced' quality check.
* Checks schema validity after the original document has
* been dereferenced (i.e. all references to "id" elements
* have been substituted with the actual elements.) An XSLT
* stylesheet is used for performing the dereferencing.
*
* @param doc the XML DOM document object
* @param namespaceInDoc the namespace value specified in the document
*/
public void checkSchemaValidDereferenced(Document doc, String namespaceInDoc) {
String identifier = "schemaValidDereferenced";
QualityCheck qualityCheckTemplate =
QualityReport.getQualityCheckTemplate(identifier);
QualityCheck qualityCheck =
new QualityCheck(identifier, qualityCheckTemplate);
if (QualityCheck.shouldRunQualityCheck(this, qualityCheck)) {
// Initialize the schemaValidQualityCheck
boolean validateSchema = true;
String found = "";
final String parserName = "DEFAULT";
Node documentElement = doc.getDocumentElement();
String xmlString = XMLUtilities.getDOMTreeAsString(documentElement);
String deferencedXmlString = DataPackage.dereferenceEML(xmlString);
StringReader stringReader = new StringReader(deferencedXmlString);
SAXValidate saxValidate = new SAXValidate(validateSchema);
try {
saxValidate.runTest(stringReader, parserName, namespaceInDoc);
found = "Dereferenced document validated for namespace: '" + namespaceInDoc + "'";
qualityCheck.setStatus(Status.valid);
qualityCheck.setSuggestion("");
this.schemaValid = true;
}
catch (Exception e) {
found = "Failed to validate dereferenced document for namespace: '" + namespaceInDoc +
"'; " + e.getMessage();
qualityCheck.setFailedStatus();
}
qualityCheck.setFound(found);
this.addDatasetQualityCheck(qualityCheck);
}
}
/**
* Set the value of the numberOfKeywordElements instance variable
* and run a quality check based on the value.
*
* @param n the number of keyword elements detected in
* this data package by the parser
*/
public void setNumberOfKeywordElements(int n) {
this.numberOfKeywordElements = n;
/*
* Do a quality check for the presence of at least
* one 'keyword' element
*/
String qualityCheckIdentifier = "keywordPresent";
QualityCheck qualityCheckTemplate =
QualityReport.getQualityCheckTemplate(qualityCheckIdentifier);
QualityCheck qualityCheck =
new QualityCheck(qualityCheckIdentifier, qualityCheckTemplate);
if (QualityCheck.shouldRunQualityCheck(this, qualityCheck)) {
String found = numberOfKeywordElements + " 'keyword' element(s) found";
qualityCheck.setFound(found);
if (numberOfKeywordElements > 0) {
qualityCheck.setStatus(Status.valid);
qualityCheck.setExplanation("");
qualityCheck.setSuggestion("");
}
else {
qualityCheck.setFailedStatus();
}
addDatasetQualityCheck(qualityCheck);
}
}
/**
* Set the value of the numberOfMethodsElements instance variable
* and run a quality check based on the value.
*
* @param n the number of methods elements detected in
* this data package by the parser
*/
public void setNumberOfMethodsElements(int n) {
this.numberOfMethodsElements = n;
/*
* Do a quality check for the presence of at least
* one 'methods' element
*/
String methodsElementIdentifier = "methodsElementPresent";
QualityCheck methodsElementTemplate =
QualityReport.getQualityCheckTemplate(methodsElementIdentifier);
QualityCheck methodsElementQualityCheck =
new QualityCheck(methodsElementIdentifier, methodsElementTemplate);
if (QualityCheck.shouldRunQualityCheck(this, methodsElementQualityCheck)) {
String found = numberOfMethodsElements + " 'methods' element(s) found";
methodsElementQualityCheck.setFound(found);
if (numberOfMethodsElements > 0) {
methodsElementQualityCheck.setStatus(Status.valid);
methodsElementQualityCheck.setExplanation("");
methodsElementQualityCheck.setSuggestion("");
}
else {
methodsElementQualityCheck.setFailedStatus();
}
addDatasetQualityCheck(methodsElementQualityCheck);
}
}
/**
* Set the value of the numberOfCoverageElements instance variable
* and run a quality check based on the value.
*
* @param n the number of coverage elements detected in
* this data package by the parser
*/
public void setNumberOfCoverageElements(int n) {
this.numberOfCoverageElements = n;
/*
* Do a quality check for the presence of at least
* one 'coverage' element
*/
String identifier = "coveragePresent";
QualityCheck qualityCheckTemplate =
QualityReport.getQualityCheckTemplate(identifier);
QualityCheck qualityCheck =
new QualityCheck(identifier, qualityCheckTemplate);
if (QualityCheck.shouldRunQualityCheck(this, qualityCheck)) {
String found = numberOfCoverageElements + " 'coverage' element(s) found";
qualityCheck.setFound(found);
if (numberOfCoverageElements > 0) {
qualityCheck.setStatus(Status.valid);
qualityCheck.setExplanation("");
qualityCheck.setSuggestion("");
}
else {
qualityCheck.setFailedStatus();
}
addDatasetQualityCheck(qualityCheck);
}
}
/**
* Set the value of the numberOfGeographicCoverageElements instance
* variable and run a quality check based on the value.
*
* @param n the number of geographicCoverage elements detected in
* this data package by the parser
*/
public void setNumberOfGeographicCoverageElements(int n) {
this.numberOfGeographicCoverageElements = n;
/*
* Do a quality check to report the number of
* 'geographicCoverage' elements detected
*/
String identifier = "geographicCoveragePresent";
QualityCheck qualityCheckTemplate =
QualityReport.getQualityCheckTemplate(identifier);
QualityCheck qualityCheck =
new QualityCheck(identifier, qualityCheckTemplate);
if (QualityCheck.shouldRunQualityCheck(this, qualityCheck)) {