Project

General

Profile

1 21163 george.ath
/*
2
 * To change this license header, choose License Headers in Project Properties.
3
 * To change this template file, choose Tools | Templates
4
 * and open the template in the editor.
5
 */
6 21189 george.ath
package eu.dnetlib.espas.util;
7 21163 george.ath
8 21656 stefania.m
import java.io.StringReader;
9
10 21651 george.ath
import javax.xml.parsers.DocumentBuilderFactory;
11 21163 george.ath
import org.apache.log4j.Logger;
12
import org.apache.log4j.Priority;
13
import org.w3c.dom.Node;
14 21656 stefania.m
import org.xml.sax.InputSource;
15 21651 george.ath
16 21163 george.ath
/**
17
 *
18
 * @author George Athanasopoulos <george.athanasopoulos, at UoA>
19
 */
20 21651 george.ath
21
22 21163 george.ath
public class MetadataValidator {
23 21651 george.ath
        public enum ValidityStatus {Valid, Invalid, Unsupported};
24
25
        private static DocumentBuilderFactory docFactory;
26
        private static final Logger _logger = Logger.getLogger(MetadataValidator.class);
27 21163 george.ath
28 21651 george.ath
29
        static{
30
            docFactory= DocumentBuilderFactory.newInstance();
31
            docFactory.setNamespaceAware(true);
32
        }
33
34
35
        /** Returns true if the provided serialized form of the gml:element is either an accepted point or polygon instance.
36
         */
37
        public static ValidityStatus isValidGMLLocation(String geomLocationChild){
38
            try {
39 21656 stefania.m
40
            	InputSource is = new InputSource();
41
        		is.setCharacterStream(new StringReader(geomLocationChild));
42
                Node locationDoc = docFactory.newDocumentBuilder().parse(is);
43 21651 george.ath
                Node elementNode = locationDoc.getFirstChild();
44
45 21656 stefania.m
                return isValidGMLLocation(elementNode);
46 21651 george.ath
47
            } catch (Exception ex) {
48
                _logger.log(Priority.ERROR, null, ex);
49
                return ValidityStatus.Invalid;
50
            }
51
        }
52
53 21163 george.ath
        /** Returns true if the provided gml:element is either an accepted point or polygon instance.
54
         */
55 21651 george.ath
        public static ValidityStatus isValidGMLLocation(Node geomLocationChild){
56 21163 george.ath
                _logger.log(Priority.INFO, "Validating location gml element");
57
            try {
58
                String output = GeometryMetadataHandler.getGeoLocation(geomLocationChild);
59 21651 george.ath
60
                if(output.isEmpty())
61
                    return ValidityStatus.Unsupported;
62
                else if (output==null)
63
                    return ValidityStatus.Invalid;
64
65
                return ValidityStatus.Valid;
66 21647 george.ath
            } catch (Exception ex) {
67 21651 george.ath
                _logger.log(Priority.ERROR, null, ex);
68
                return ValidityStatus.Invalid;
69 21163 george.ath
            }
70
        }
71
72 21651 george.ath
73 21163 george.ath
74 21651 george.ath
        public static ValidityStatus isValidExtent(Node extentNode){
75 21163 george.ath
                _logger.log(Priority.INFO, "Validating gmd:extent element");
76
            try {
77 21627 george.ath
//                @todo: to update this implementation; an extent element may contain a list of geoextent, verticalextent and timeextent objects
78 21163 george.ath
                if(GeometryMetadataHandler.hasGeoExtent(extentNode) && GeometryMetadataHandler.getGeoExtent(extentNode).isEmpty())
79 21651 george.ath
                    return ValidityStatus.Unsupported;
80 21163 george.ath
                if(GeometryMetadataHandler.hasVerticalExtent(extentNode) &&  GeometryMetadataHandler.getVerticalExtentMax(extentNode).isEmpty() && GeometryMetadataHandler.getVerticalExtentMin(extentNode).isEmpty())
81 21651 george.ath
                    return ValidityStatus.Unsupported;
82 21163 george.ath
                if(TemporalMetadataHandler.hasTemporalExtent(extentNode) && TemporalMetadataHandler.getTemporalExtentStart(extentNode).isEmpty() && TemporalMetadataHandler.getTemporalExtentEnd(extentNode).isEmpty())
83 21651 george.ath
                    return ValidityStatus.Unsupported;
84
                return ValidityStatus.Valid;
85 21163 george.ath
            } catch (Exception ex) {
86
                _logger.log(Priority.ERROR, null, ex);
87 21651 george.ath
                return ValidityStatus.Invalid;
88 21163 george.ath
            }
89
        }
90 21627 george.ath
91 21651 george.ath
92 21627 george.ath
93 21651 george.ath
        public static ValidityStatus isValidGeoExtent(String geoExtentNode){
94
            try {
95 21656 stefania.m
96
            	InputSource is = new InputSource();
97
        		is.setCharacterStream(new StringReader(geoExtentNode));
98
                Node locationDoc = docFactory.newDocumentBuilder().parse(is);
99 21651 george.ath
                Node elementNode = locationDoc.getFirstChild();
100 21656 stefania.m
101 21651 george.ath
                return isValidGeoExtent(elementNode);
102
            } catch (Exception ex) {
103
                _logger.log(Priority.ERROR, null, ex);
104
                return ValidityStatus.Invalid;
105
            }
106
107
        }
108
109
        public static ValidityStatus isValidGeoExtent(Node geoExtentNode){
110 21627 george.ath
                _logger.log(Priority.INFO, "Validating gmd:extent element");
111
            try {
112
                if(GeometryMetadataHandler.isGeoExtent(geoExtentNode) && GeometryMetadataHandler.getGeoExtent(geoExtentNode).isEmpty())
113 21651 george.ath
                    return ValidityStatus.Unsupported;
114
                return ValidityStatus.Valid;
115 21627 george.ath
            } catch (Exception ex) {
116
                _logger.log(Priority.ERROR, null, ex);
117 21651 george.ath
                return ValidityStatus.Invalid;
118 21627 george.ath
            }
119
120
        }
121 21163 george.ath
}