Project

General

Profile

1
/*
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
package eu.dnetlib.espas.util;
7

    
8
import java.io.StringReader;
9

    
10
import javax.xml.parsers.DocumentBuilderFactory;
11
import org.apache.log4j.Logger;
12
import org.apache.log4j.Priority;
13
import org.w3c.dom.Node;
14
import org.xml.sax.InputSource;
15

    
16
/**
17
 *
18
 * @author George Athanasopoulos <george.athanasopoulos, at UoA>
19
 */
20

    
21

    
22
public class MetadataValidator {
23
        public enum ValidityStatus {Valid, Invalid, Unsupported};
24
        
25
        private static DocumentBuilderFactory docFactory;
26
        private static final Logger _logger = Logger.getLogger(MetadataValidator.class);
27

    
28
        
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
            	
40
            	InputSource is = new InputSource();
41
        		is.setCharacterStream(new StringReader(geomLocationChild));
42
                Node locationDoc = docFactory.newDocumentBuilder().parse(is);
43
                Node elementNode = locationDoc.getFirstChild();
44

    
45
                return isValidGMLLocation(elementNode);
46

    
47
            } catch (Exception ex) {
48
                _logger.log(Priority.ERROR, null, ex);
49
                return ValidityStatus.Invalid;
50
            }
51
        }
52
        
53
        /** Returns true if the provided gml:element is either an accepted point or polygon instance.
54
         */
55
        public static ValidityStatus isValidGMLLocation(Node geomLocationChild){
56
                _logger.log(Priority.INFO, "Validating location gml element");
57
            try {
58
                String output = GeometryMetadataHandler.getGeoLocation(geomLocationChild);
59
                
60
                if(output.isEmpty())
61
                    return ValidityStatus.Unsupported;
62
                else if (output==null)
63
                    return ValidityStatus.Invalid;
64

    
65
                return ValidityStatus.Valid;
66
            } catch (Exception ex) {
67
                _logger.log(Priority.ERROR, null, ex);
68
                return ValidityStatus.Invalid;
69
            }
70
        }
71
        
72

    
73
        
74
        public static ValidityStatus isValidExtent(Node extentNode){
75
                _logger.log(Priority.INFO, "Validating gmd:extent element");
76
            try {
77
//                @todo: to update this implementation; an extent element may contain a list of geoextent, verticalextent and timeextent objects
78
                if(GeometryMetadataHandler.hasGeoExtent(extentNode) && GeometryMetadataHandler.getGeoExtent(extentNode).isEmpty())
79
                    return ValidityStatus.Unsupported;
80
                if(GeometryMetadataHandler.hasVerticalExtent(extentNode) &&  GeometryMetadataHandler.getVerticalExtentMax(extentNode).isEmpty() && GeometryMetadataHandler.getVerticalExtentMin(extentNode).isEmpty())
81
                    return ValidityStatus.Unsupported;
82
                if(TemporalMetadataHandler.hasTemporalExtent(extentNode) && TemporalMetadataHandler.getTemporalExtentStart(extentNode).isEmpty() && TemporalMetadataHandler.getTemporalExtentEnd(extentNode).isEmpty())
83
                    return ValidityStatus.Unsupported;
84
                return ValidityStatus.Valid;
85
            } catch (Exception ex) {
86
                _logger.log(Priority.ERROR, null, ex);
87
                return ValidityStatus.Invalid;
88
            }
89
        }
90
        
91

    
92
        
93
        public static ValidityStatus isValidGeoExtent(String geoExtentNode){
94
            try {
95
            	
96
            	InputSource is = new InputSource();
97
        		is.setCharacterStream(new StringReader(geoExtentNode));
98
                Node locationDoc = docFactory.newDocumentBuilder().parse(is);
99
                Node elementNode = locationDoc.getFirstChild();
100
                
101
                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
                _logger.log(Priority.INFO, "Validating gmd:extent element");
111
            try {
112
                if(GeometryMetadataHandler.isGeoExtent(geoExtentNode) && GeometryMetadataHandler.getGeoExtent(geoExtentNode).isEmpty())
113
                    return ValidityStatus.Unsupported;
114
                return ValidityStatus.Valid;
115
            } catch (Exception ex) {
116
                _logger.log(Priority.ERROR, null, ex);
117
                return ValidityStatus.Invalid;
118
            }
119

    
120
        }
121
}
(8-8/9)