View Javadoc
1   /*
2    * This file is part of ***  M y C o R e  ***
3    * See http://www.mycore.de/ for details.
4    *
5    * MyCoRe is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * MyCoRe is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with MyCoRe.  If not, see <http://www.gnu.org/licenses/>.
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      /** Constructor initializes the HashMap */
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; //normally not the case
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      * Returns the owner of this derivate link. In most cases this is
104      * the derivate id itself.
105      *
106      * @return the owner of this derivate link.
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      * Returns the URI decoded path of this derivate link. Use {@link #getRawPath()}
118      * if you want the URI encoded path.
119      *
120      * @return path of this derivate link
121      * @throws URISyntaxException the path couldn't be decoded
122      */
123     public String getPath() throws URISyntaxException {
124         return new URI(getRawPath()).getPath();
125     }
126 
127     /**
128      * Returns the raw path of this derivate link. Be aware that
129      * this path is URI encoded. Use {@link #getPath()} if you want
130      * the URI decoded path.
131      *
132      * @return URI encoded path
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      * Returns the {@link MCRPath} to this derivate link.
144      *
145      * @return path to this derivate link
146      * @throws URISyntaxException the path part of this derivate link couldn't be decoded because
147      *           its an invalid URI
148      */
149     public MCRPath getLinkedFile() throws URISyntaxException {
150         return MCRPath.getPath(getOwner(), getPath());
151     }
152 
153     /**
154      * Validates this MCRMetaDerivateLink. This method throws an exception if:
155      * <ul>
156      * <li>the subtag is not null or empty</li>
157      * <li>the lang value was supported</li>
158      * <li>the inherited value is lower than zero</li>
159      * <li>the linked files is null or does not exist</li>
160      * </ul>
161      *
162      * @throws MCRException the MCRMetaDerivateLink is invalid
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 }