com.strider.datadefender.requirement.file.Loader   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 25
dl 0
loc 50
ccs 0
cts 12
cp 0
rs 10
c 1
b 0
f 0
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A Loader() 0 4 1
A load(String) 0 22 1
1
/*
2
 *
3
 * Copyright 2014, Armenak Grigoryan, Matthew Eaton, and individual contributors as indicated
4
 * by the @authors tag. See the copyright.txt in the distribution for a
5
 * full listing of individual contributors.
6
 *
7
 * This is free software; you can redistribute it and/or modify it
8
 * under the terms of the GNU Lesser General Public License as
9
 * published by the Free Software Foundation; either version 2.1 of
10
 * the License, or (at your option) any later version.
11
 *
12
 * This software is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 */
18
package com.strider.datadefender.requirement.file;
19
20
import java.io.File;
21
import java.io.FileInputStream;
22
import java.io.FileNotFoundException;
23
24
import javax.xml.bind.JAXBContext;
25
import javax.xml.bind.JAXBException;
26
import javax.xml.bind.Unmarshaller;
27
import static javax.xml.bind.JAXBContext.newInstance;
28
29
import com.strider.datadefender.requirement.Requirement;
30
import com.strider.datadefender.requirement.registry.ClassAndFunctionRegistry;
31
import java.lang.reflect.InvocationTargetException;
32
import javax.xml.XMLConstants;
33
import javax.xml.validation.Schema;
34
import javax.xml.validation.SchemaFactory;
35
36
import lombok.RequiredArgsConstructor;
37
import lombok.extern.log4j.Log4j2;
38
import org.xml.sax.SAXException;
39
40
/**
41
 * Utility class to help handling requirement objects
42
 * @author Matthew Eaton
43
 */
44
@Log4j2
45
@RequiredArgsConstructor
46
public class Loader {
47
48
    private final JAXBContext jaxbContext;
49
    private final Unmarshaller unmarshaller;
50
51
    public Loader() throws JAXBException {
52
53
        jaxbContext = newInstance(Requirement.class);
54
        unmarshaller = jaxbContext.createUnmarshaller();
55
    }
56
57
    /**
58
     * Load requirement XML file and return a Requirement object representing
59
     * it.
60
     *
61
     * @param requirementFile Requirement filename and path
62
     * @param version required version
63
     * @return Requirement object loaded based on file
64
     * @throws javax.xml.bind.JAXBException
65
     * @throws java.io.FileNotFoundException
66
     * @throws java.lang.NoSuchMethodException
67
     * @throws java.lang.InstantiationException
68
     * @throws java.lang.IllegalAccessException
69
     * @throws java.lang.reflect.InvocationTargetException
70
     * @throws org.xml.sax.SAXException
71
     */
72
    public Requirement load(final String requirementFile) throws
73
        FileNotFoundException,
74
        JAXBException,
75
        NoSuchMethodException,
76
        InstantiationException,
77
        IllegalAccessException,
78
        IllegalArgumentException,
79
        InvocationTargetException,
80
        SAXException {
81
82
        Requirement requirement = null;
83
        log.info("Loading requirement file: {}", requirementFile);
84
85
        SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
86
        Schema schema = schemaFactory.newSchema(Loader.class.getResource("requirement.xsd"));
87
88
        unmarshaller.setSchema(schema);
89
        requirement = (Requirement) unmarshaller.unmarshal(
90
            new FileInputStream(new File(requirementFile))
91
        );
92
        ClassAndFunctionRegistry.singleton().registerFunctions(requirement);
93
        return requirement;
94
    }
95
}
96