1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.mycore.datamodel.metadata;
20
21 import java.net.URI;
22 import java.net.URISyntaxException;
23 import java.nio.file.Files;
24 import java.util.HashMap;
25 import java.util.List;
26
27 import org.apache.logging.log4j.LogManager;
28 import org.apache.logging.log4j.Logger;
29 import org.jdom2.Element;
30 import org.jdom2.Namespace;
31 import org.mycore.common.MCRException;
32 import org.mycore.common.xml.MCRXMLFunctions;
33 import org.mycore.datamodel.niofs.MCRPath;
34
35 public class MCRMetaDerivateLink extends MCRMetaLink {
36
37 private static final String ANNOTATION = "annotation";
38
39 private static final String ATTRIBUTE = "lang";
40
41 private static final Logger LOGGER = LogManager.getLogger();
42
43 private HashMap<String, String> map;
44
45
46 public MCRMetaDerivateLink() {
47 super();
48 map = new HashMap<>();
49 }
50
51 public void setLinkToFile(MCRPath file) {
52 if (!file.isAbsolute()) {
53 throw new IllegalArgumentException("file parameter must be absolute");
54 }
55 String owner = file.getOwner();
56 String path = file.getOwnerRelativePath();
57 if (path.isEmpty()) {
58 throw new IllegalArgumentException("file parameter is empty");
59 }
60 if (path.charAt(0) != '/') {
61 path = '/' + path;
62 }
63 try {
64 path = MCRXMLFunctions.encodeURIPath(path, true);
65 } catch (URISyntaxException uriExc) {
66 LOGGER.warn("Unable to encode URI path {}", path, uriExc);
67 }
68 super.href = owner + path;
69 }
70
71 public void setFromDOM(Element element) throws MCRException {
72 super.setFromDOM(element);
73 List<Element> childrenList = element.getChildren(MCRMetaDerivateLink.ANNOTATION);
74 if (childrenList == null) {
75 return;
76 }
77
78 for (Element anAnnotation : childrenList) {
79 String key = anAnnotation.getAttributeValue(MCRMetaDerivateLink.ATTRIBUTE, Namespace.XML_NAMESPACE);
80 String annotationText = anAnnotation.getText();
81 this.map.put(key, annotationText);
82 }
83 }
84
85 public Element createXML() throws MCRException {
86 Element elm = super.createXML();
87
88 for (String key : map.keySet()) {
89 Element annotationElem = new Element(MCRMetaDerivateLink.ANNOTATION);
90 annotationElem.setAttribute(MCRMetaDerivateLink.ATTRIBUTE, key, Namespace.XML_NAMESPACE);
91 String content = map.get(key);
92 if (content == null || content.length() == 0) {
93 continue;
94 }
95 annotationElem.addContent(content);
96 elm.addContent(annotationElem);
97 }
98
99 return elm;
100 }
101
102
103
104
105
106
107
108 public String getOwner() {
109 int index = super.href.indexOf('/');
110 if (index < 0) {
111 return null;
112 }
113 return super.href.substring(0, index);
114 }
115
116
117
118
119
120
121
122
123 public String getPath() throws URISyntaxException {
124 return new URI(getRawPath()).getPath();
125 }
126
127
128
129
130
131
132
133
134 public String getRawPath() {
135 int index = super.href.indexOf('/');
136 if (index < 0) {
137 return null;
138 }
139 return super.href.substring(index);
140 }
141
142
143
144
145
146
147
148
149 public MCRPath getLinkedFile() throws URISyntaxException {
150 return MCRPath.getPath(getOwner(), getPath());
151 }
152
153
154
155
156
157
158
159
160
161
162
163
164 public void validate() throws MCRException {
165 super.validate();
166 try {
167 MCRPath linkedFile = getLinkedFile();
168
169 if (linkedFile == null) {
170 throw new MCRException(getSubTag() + ": linked file is null");
171 }
172
173 if (!Files.exists(linkedFile)) {
174 LOGGER.warn("{}: File not found: {}", getSubTag(), super.href);
175 }
176
177 } catch (Exception exc) {
178 throw new MCRException(getSubTag() + ": Error while getting linked file " + super.href, exc);
179 }
180 }
181
182 @Override
183 public MCRMetaDerivateLink clone() {
184 MCRMetaDerivateLink clone = (MCRMetaDerivateLink) super.clone();
185
186 clone.map = new HashMap<>(this.map);
187
188 return clone;
189 }
190 }