1 package com.trendmicro.grid.acl.metadata;
2
3 import com.trendmicro.grid.acl.commons.XmlSerializer;
4 import com.trendmicro.grid.acl.commons.collections.AbstractMapValueCollection;
5 import org.w3c.dom.Document;
6 import org.w3c.dom.ProcessingInstruction;
7
8 import javax.xml.bind.annotation.*;
9 import java.io.File;
10 import java.io.InputStream;
11 import java.io.OutputStream;
12 import java.util.Collection;
13 import java.util.LinkedHashMap;
14 import java.util.List;
15 import java.util.Map;
16
17
18
19
20
21
22
23
24 @XmlAccessorType(XmlAccessType.PROPERTY)
25 @XmlType(name = "metadata-profiles", namespace = ProfilesDocument.NS)
26 @XmlRootElement(name = "metadata-profiles", namespace = ProfilesDocument.NS)
27 public class ProfilesDocument {
28
29 public static final String NS = "http://grid.trendmicro.com/metadata-profile/1.0";
30
31 public static final String DEFAULT_SCHEMA_BASENAME = "metadata-profile-schema";
32
33 private static XmlSerializer<ProfilesDocument> xmlSerializer =
34 new XmlSerializer<ProfilesDocument>(ProfilesDocument.class, NS, DEFAULT_SCHEMA_BASENAME) {
35 @Override
36 protected boolean isDocumentAdjusted() {
37 return true;
38 }
39
40 @Override
41 protected void adjustDocument(Document document) {
42 ProcessingInstruction pi = document.createProcessingInstruction(
43 "xml-stylesheet", "type=\"text/xsl\" href=\"metadata-profile-to-html.xsl\"");
44 document.insertBefore(pi, document.getDocumentElement());
45 }
46 };
47
48 public static XmlSerializer<ProfilesDocument> getXmlSerializer() {
49 return xmlSerializer;
50 }
51
52 public static List<File> generateSchema(final File outputDirectory, final String basename) throws Exception {
53 return xmlSerializer.generateSchema(outputDirectory, basename);
54 }
55
56 public static ProfilesDocument load(File file) throws Exception {
57 return xmlSerializer.load(file);
58 }
59
60 public static ProfilesDocument load(InputStream in) throws Exception {
61 return xmlSerializer.load(in);
62 }
63
64 public void save(File file) throws Exception {
65 xmlSerializer.save(this, file);
66 }
67
68 public void save(OutputStream out) throws Exception {
69 xmlSerializer.save(this, out);
70 }
71
72 private final Map<String, Profile> profiles = new LinkedHashMap<String, Profile>();
73 private transient Collection<Profile> profilesCollection;
74
75 public ProfilesDocument() {
76 this(null);
77 }
78
79 public ProfilesDocument(Collection<Profile> profiles) {
80 profilesCollection = new AbstractMapValueCollection<String, Profile>(this.profiles) {
81 protected String createKeyForValue(Profile value) {
82 return value.getName();
83 }
84 };
85 setProfiles(profiles);
86 }
87
88
89
90
91
92
93 @XmlElement(name = "profile")
94 public Collection<Profile> getProfiles() {
95 return profilesCollection;
96 }
97
98 public void setProfiles(Collection<Profile> profiles) {
99 if (profiles != profilesCollection) {
100 this.profiles.clear();
101 if (profiles != null)
102 profilesCollection.addAll(profiles);
103 }
104 }
105
106 public Map<String, Profile> getAll() {
107 return profiles;
108 }
109
110 public Profile add(String name) {
111 Profile p = new Profile(name);
112 profilesCollection.add(p);
113 return p;
114 }
115
116 public Profile get(String name) {
117 return profiles.get(name);
118 }
119
120 public Profile remove(String name) {
121 return profiles.remove(name);
122 }
123
124
125
126
127 @Override
128 public boolean equals(Object o) {
129 if (this == o) return true;
130 if (!(o instanceof ProfilesDocument)) return false;
131
132 ProfilesDocument that = (ProfilesDocument) o;
133 return profiles.equals(that.profiles);
134 }
135
136
137
138
139 @Override
140 public int hashCode() {
141 return profiles.hashCode();
142 }
143 }