PageRenderTime 52ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/dspace-sword/dspace-sword-api/src/main/java/org/purl/sword/base/SWORDErrorDocument.java

https://github.com/hedtek/dspace_rest_integration
Java | 311 lines | 134 code | 30 blank | 147 comment | 9 complexity | 70861de42cbe2eb325c4376c67b77ab2 MD5 | raw file
  1. /**
  2. * Copyright (c) 2008-2009, Aberystwyth University
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * - Redistributions of source code must retain the above
  11. * copyright notice, this list of conditions and the
  12. * following disclaimer.
  13. *
  14. * - Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * - Neither the name of the Centre for Advanced Software and
  20. * Intelligent Systems (CASIS) nor the names of its
  21. * contributors may be used to endorse or promote products derived
  22. * from this software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  27. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  28. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  29. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  30. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  31. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  32. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  33. * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  34. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  35. * SUCH DAMAGE.
  36. */
  37. package org.purl.sword.base;
  38. import java.util.ArrayList;
  39. import java.util.Properties;
  40. import nu.xom.Attribute;
  41. import nu.xom.Element;
  42. import org.apache.log4j.Logger;
  43. import org.purl.sword.atom.Author;
  44. import org.purl.sword.atom.Title;
  45. /**
  46. * Extension of the SWORD Entry class, specialized for Error Documents.
  47. *
  48. * @author Stuart Lewis (sdl@aber.ac.uk)
  49. * @author Neil Taylor (nst@aber.ac.uk)
  50. */
  51. public class SWORDErrorDocument extends SWORDEntry
  52. {
  53. /**
  54. * Local name for the element.
  55. */
  56. @Deprecated
  57. public static final String ELEMENT_NAME = "error";
  58. /**
  59. * The logger.
  60. */
  61. private static Logger log = Logger.getLogger(SWORDErrorDocument.class);
  62. private static final XmlName XML_NAME =
  63. new XmlName(Namespaces.PREFIX_SWORD, "error", Namespaces.NS_SWORD);
  64. private static final XmlName ATTRIBUTE_HREF_NAME =
  65. new XmlName(Namespaces.PREFIX_SWORD, "href", Namespaces.NS_SWORD);
  66. /**
  67. * The Error URI
  68. */
  69. private String errorURI;
  70. /**
  71. * Create the error document (intended to be used when unmarshalling an error document
  72. * as this will set the errorURI)
  73. */
  74. public SWORDErrorDocument() {
  75. super(XML_NAME.getPrefix(),
  76. XML_NAME.getLocalName(),
  77. XML_NAME.getNamespace());
  78. }
  79. /**
  80. * Create the error document
  81. *
  82. * @param errorURI The URI of the error
  83. */
  84. public SWORDErrorDocument(String errorURI) {
  85. this();
  86. this.errorURI = errorURI;
  87. }
  88. /**
  89. * Get the element name.
  90. *
  91. * @return
  92. */
  93. public static XmlName elementName()
  94. {
  95. return XML_NAME;
  96. }
  97. /**
  98. *
  99. */
  100. protected void initialise()
  101. {
  102. super.initialise();
  103. }
  104. /**
  105. * Overrides the marshall method in the parent SWORDEntry. This will
  106. * call the parent marshall method and then add the additional
  107. * elements that have been added in this subclass.
  108. */
  109. public Element marshall()
  110. {
  111. Element entry = new Element(getQualifiedName(), Namespaces.NS_SWORD);
  112. entry.addNamespaceDeclaration(Namespaces.PREFIX_SWORD, Namespaces.NS_SWORD);
  113. entry.addNamespaceDeclaration(Namespaces.PREFIX_ATOM, Namespaces.NS_ATOM);
  114. Attribute error = new Attribute("href", errorURI);
  115. entry.addAttribute(error);
  116. super.marshallElements(entry);
  117. return entry;
  118. }
  119. /**
  120. * Overrides the unmarshall method in the parent SWORDEntry. This will
  121. * call the parent method to parse the general Atom elements and
  122. * attributes. This method will then parse the remaining sword
  123. * extensions that exist in the element.
  124. *
  125. * @param entry The entry to parse.
  126. *
  127. * @throws UnmarshallException If the entry is not an atom:entry
  128. * or if there is an exception extracting the data.
  129. */
  130. public void unmarshall(Element entry) throws UnmarshallException
  131. {
  132. unmarshall(entry, null);
  133. }
  134. /**
  135. *
  136. * @param entry
  137. * @param validationProperties
  138. * @return
  139. * @throws org.purl.sword.base.UnmarshallException
  140. */
  141. public SwordValidationInfo unmarshall(Element entry, Properties validationProperties)
  142. throws UnmarshallException
  143. {
  144. SwordValidationInfo result = super.unmarshall(entry, validationProperties);
  145. result.clearValidationItems();
  146. errorURI = entry.getAttributeValue(ATTRIBUTE_HREF_NAME.getLocalName());
  147. if( validationProperties != null )
  148. {
  149. result = validate(result, validationProperties);
  150. }
  151. return result;
  152. }
  153. /**
  154. * This method overrides the XmlElement definition so that it can allow
  155. * the definition of the href attribute. All other attributes are
  156. * shown as 'Unknown Attribute' info elements.
  157. *
  158. * @param element The element that contains the attributes
  159. * @param info The info object that will hold the validation info.
  160. */
  161. @Override
  162. protected void processUnexpectedAttributes(Element element, SwordValidationInfo info)
  163. {
  164. int attributeCount = element.getAttributeCount();
  165. Attribute attribute = null;
  166. for( int i = 0; i < attributeCount; i++ )
  167. {
  168. attribute = element.getAttribute(i);
  169. if( ! ATTRIBUTE_HREF_NAME.getLocalName().equals(attribute.getQualifiedName()) )
  170. {
  171. XmlName attributeName = new XmlName(attribute.getNamespacePrefix(),
  172. attribute.getLocalName(),
  173. attribute.getNamespaceURI());
  174. SwordValidationInfo item = new SwordValidationInfo(xmlName, attributeName,
  175. SwordValidationInfo.UNKNOWN_ATTRIBUTE,
  176. SwordValidationInfoType.INFO);
  177. item.setContentDescription(attribute.getValue());
  178. info.addUnmarshallAttributeInfo(item);
  179. }
  180. }
  181. }
  182. /**
  183. *
  184. * @param elementName
  185. * @return
  186. */
  187. protected boolean isElementChecked(XmlName elementName)
  188. {
  189. return super.isElementChecked(elementName);
  190. }
  191. /**
  192. *
  193. * @return
  194. */
  195. public SwordValidationInfo validate(Properties validationContext)
  196. {
  197. return validate(null, validationContext);
  198. }
  199. /**
  200. *
  201. * @param elements
  202. * @param attributes
  203. * @return
  204. */
  205. protected SwordValidationInfo validate(SwordValidationInfo info, Properties validationContext)
  206. {
  207. if( errorURI == null )
  208. {
  209. info.addValidationInfo(new SwordValidationInfo(xmlName, ATTRIBUTE_HREF_NAME,
  210. SwordValidationInfo.MISSING_ATTRIBUTE_WARNING,
  211. SwordValidationInfoType.WARNING));
  212. }
  213. else
  214. {
  215. boolean validUri = true;
  216. if(errorURI.startsWith("http://purl.org/net/sword/error/"))
  217. {
  218. // check that the list of codes
  219. if( ! (errorURI.equals(ErrorCodes.ERROR_CONTENT) ||
  220. errorURI.equals(ErrorCodes.ERROR_CHECKSUM_MISMATCH) ||
  221. errorURI.equals(ErrorCodes.ERROR_BAD_REQUEST) ||
  222. errorURI.equals(ErrorCodes.TARGET_OWNER_UKNOWN) ||
  223. errorURI.equals(ErrorCodes.MEDIATION_NOT_ALLOWED)) )
  224. {
  225. info.addValidationInfo(new SwordValidationInfo(xmlName,
  226. ATTRIBUTE_HREF_NAME,
  227. "Errors in the SWORD namespace are reserved and legal values are enumerated in the SWORD 1.3 specification. Implementations MAY define their own errors, but MUST use a different namespace to do so.",
  228. SwordValidationInfoType.ERROR));
  229. validUri = false;
  230. }
  231. }
  232. if( validUri )
  233. {
  234. SwordValidationInfo item = new SwordValidationInfo(xmlName, ATTRIBUTE_HREF_NAME);
  235. item.setContentDescription(errorURI);
  236. info.addAttributeValidationInfo(item);
  237. }
  238. }
  239. return info;
  240. }
  241. /**
  242. * Get the error URI
  243. *
  244. * @return the error URI
  245. */
  246. public String getErrorURI()
  247. {
  248. return errorURI;
  249. }
  250. /**
  251. * set the error URI
  252. *
  253. * @param error the error URI
  254. */
  255. public void setErrorURI(String error)
  256. {
  257. errorURI = error;
  258. }
  259. /**
  260. * Main method to perform a brief test of the class
  261. *
  262. * @param args
  263. */
  264. /*public static void main(String[] args)
  265. {
  266. SWORDErrorDocumentTest sed = new SWORDErrorDocumentTest(ErrorCodes.MEDIATION_NOT_ALLOWED);
  267. sed.setNoOp(true);
  268. sed.setTreatment("Short back and shine");
  269. sed.setId("123456789");
  270. Title t = new Title();
  271. t.setContent("My first book");
  272. sed.setTitle(t);
  273. Author a = new Author();
  274. a.setName("Lewis, Stuart");
  275. a.setEmail("stuart@example.com");
  276. sed.addAuthors(a);
  277. System.out.println(sed.marshall().toXML());
  278. }
  279. */
  280. }