aboutsummaryrefslogtreecommitdiff
path: root/headless/src/main/java/com/galois/fiveui/HeadlessAtom.java
blob: 32601f0ecf427932121c2bac939761a33b9fbe35 (plain)
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
/**
 * 
 */
package com.galois.fiveui;

import java.io.File;
import java.io.IOException;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;

import com.galois.fiveui.RuleSet;
import com.galois.fiveui.Utils;

/**
 * A singular url to test with a RuleSet.
 * 
 * @author bjones
 */
public class HeadlessAtom {

	private String _url;
	private RuleSet _ruleSet;
	
	public HeadlessAtom(String url, RuleSet ruleSet) {
		_url = url;
		_ruleSet = ruleSet;
	}
	
	public String getURL() {
		return _url;
	}
	
	public RuleSet getRuleSet() {
		return _ruleSet;
	}
	
	public String toString() {
        Gson gson = new Gson();
        return gson.toJson(this);
    }

    /**
     * Construct a HeadlessAtom from a JsonObject
     * 
     * @param obj JsonObject to convert from
     * @param dir parent directory of the filenames referenced in the ruleSet field
     * @return a HeadlessAtom POJO
     * @throws IOException 
     * @throws JsonParseException
     */
    public static HeadlessAtom fromJsonObject(JsonObject obj, String dir) throws IOException {
		String url = obj.get("url").getAsString();
		String ruleSet = obj.get("ruleSet").getAsString();

		if (url == null || ruleSet == null) {
			throw new JsonParseException("could get either 'url' or 'ruleSet' properties");
		}

		String rsPath = dir + File.separator + ruleSet;
        String ruleSetStr = Utils.readFile(rsPath);
        
        RuleSet parsed = RuleSet.parse(ruleSetStr);
        
        return new HeadlessAtom(url, parsed);
    }
	
	@Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((_url == null) ? 0 : _url.hashCode());
        result = prime * result + ((_ruleSet == null) ? 0 : _ruleSet.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        HeadlessAtom other = (HeadlessAtom) obj;
        if (_url == null) {
            if (other._url != null)
                return false;
        } else if (!_url.equals(other._url))
            return false;
        if (_ruleSet == null) {
            if (other._ruleSet != null)
                return false;
        } else if (!_ruleSet.equals(other._ruleSet))
            return false;
        return true;
    }
	
}