PageRenderTime 61ms CodeModel.GetById 49ms app.highlight 9ms 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 */
 37package org.purl.sword.base;
 38
 39import java.util.ArrayList;
 40import java.util.Properties;
 41import nu.xom.Attribute;
 42import nu.xom.Element;
 43
 44import org.apache.log4j.Logger;
 45import org.purl.sword.atom.Author;
 46import org.purl.sword.atom.Title;
 47
 48/**
 49 * Extension of the SWORD Entry class, specialized for Error Documents. 
 50 * 
 51 * @author Stuart Lewis (sdl@aber.ac.uk)
 52 * @author Neil Taylor (nst@aber.ac.uk)
 53 */ 
 54public class SWORDErrorDocument extends SWORDEntry
 55{
 56	/**
 57	* Local name for the element. 
 58	*/
 59   @Deprecated
 60   public static final String ELEMENT_NAME = "error";
 61   
 62   /**
 63    * The logger.
 64    */
 65   private static Logger log = Logger.getLogger(SWORDErrorDocument.class);
 66
 67   private static final XmlName XML_NAME =
 68           new XmlName(Namespaces.PREFIX_SWORD, "error", Namespaces.NS_SWORD);
 69
 70   private static final XmlName ATTRIBUTE_HREF_NAME =
 71           new XmlName(Namespaces.PREFIX_SWORD, "href", Namespaces.NS_SWORD);
 72
 73   /**
 74    * The Error URI
 75    */
 76   private String errorURI;
 77
 78   /**
 79    * Create the error document (intended to be used when unmarshalling an error document
 80    * as this will set the errorURI)
 81    */
 82   public SWORDErrorDocument() {
 83	   super(XML_NAME.getPrefix(),
 84             XML_NAME.getLocalName(),
 85             XML_NAME.getNamespace());
 86   }
 87
 88   /**
 89    * Create the error document
 90    * 
 91    * @param errorURI The URI of the error
 92    */
 93   public SWORDErrorDocument(String errorURI) {
 94	   this();
 95	   this.errorURI = errorURI;
 96   }
 97
 98   /**
 99    * Get the element name.
100    * 
101    * @return
102    */
103   public static XmlName elementName()
104   {
105      return XML_NAME; 
106   }
107
108   /**
109    * 
110    */
111   protected void initialise()
112   {
113       super.initialise();
114   }
115   
116   /**
117    * Overrides the marshall method in the parent SWORDEntry. This will 
118    * call the parent marshall method and then add the additional 
119    * elements that have been added in this subclass.  
120    */
121   public Element marshall()
122   {
123	   Element entry = new Element(getQualifiedName(), Namespaces.NS_SWORD);
124	   entry.addNamespaceDeclaration(Namespaces.PREFIX_SWORD, Namespaces.NS_SWORD);
125	   entry.addNamespaceDeclaration(Namespaces.PREFIX_ATOM, Namespaces.NS_ATOM);
126	   Attribute error = new Attribute("href", errorURI);
127       entry.addAttribute(error);
128	   super.marshallElements(entry);
129       return entry;
130   }
131
132   /**
133    * Overrides the unmarshall method in the parent SWORDEntry. This will 
134    * call the parent method to parse the general Atom elements and
135    * attributes. This method will then parse the remaining sword
136    * extensions that exist in the element. 
137    * 
138    * @param entry The entry to parse. 
139    * 
140    * @throws UnmarshallException If the entry is not an atom:entry 
141    *              or if there is an exception extracting the data. 
142    */
143   public void unmarshall(Element entry) throws UnmarshallException
144   {
145       unmarshall(entry, null);
146   }
147
148   /**
149    * 
150    * @param entry
151    * @param validationProperties
152    * @return
153    * @throws org.purl.sword.base.UnmarshallException
154    */
155   public SwordValidationInfo unmarshall(Element entry, Properties validationProperties)
156   throws UnmarshallException
157   {
158      SwordValidationInfo result = super.unmarshall(entry, validationProperties);
159      result.clearValidationItems();
160
161      errorURI = entry.getAttributeValue(ATTRIBUTE_HREF_NAME.getLocalName());
162      
163      if( validationProperties != null )
164      {
165         result = validate(result, validationProperties);
166      }
167      
168      return result;
169   }
170
171   /**
172    * This method overrides the XmlElement definition so that it can allow
173    * the definition of the href attribute. All other attributes are
174    * shown as 'Unknown Attribute' info elements.
175    *
176    * @param element The element that contains the attributes
177    * @param info    The info object that will hold the validation info. 
178    */
179   @Override
180   protected void processUnexpectedAttributes(Element element, SwordValidationInfo info)
181   {
182       int attributeCount = element.getAttributeCount();
183       Attribute attribute = null;
184
185       for( int i = 0; i < attributeCount; i++ )
186       {
187            attribute = element.getAttribute(i);
188            if( ! ATTRIBUTE_HREF_NAME.getLocalName().equals(attribute.getQualifiedName()) )
189            {
190
191               XmlName attributeName = new XmlName(attribute.getNamespacePrefix(),
192                       attribute.getLocalName(),
193                       attribute.getNamespaceURI());
194
195               SwordValidationInfo item = new SwordValidationInfo(xmlName, attributeName,
196                       SwordValidationInfo.UNKNOWN_ATTRIBUTE,
197                       SwordValidationInfoType.INFO);
198               item.setContentDescription(attribute.getValue());
199               info.addUnmarshallAttributeInfo(item);
200            }
201       }
202   }
203
204   /**
205    *
206    * @param elementName
207    * @return
208    */
209   protected boolean isElementChecked(XmlName elementName)
210   {
211       return super.isElementChecked(elementName);
212   }
213
214   /**
215    *
216    * @return
217    */
218   public SwordValidationInfo validate(Properties validationContext)
219   {
220       return validate(null, validationContext);
221   }
222
223   /**
224    * 
225    * @param elements
226    * @param attributes
227    * @return
228    */
229   protected SwordValidationInfo validate(SwordValidationInfo info, Properties validationContext)
230   {
231      
232      if( errorURI == null )
233      {
234         info.addValidationInfo(new SwordValidationInfo(xmlName, ATTRIBUTE_HREF_NAME,
235                 SwordValidationInfo.MISSING_ATTRIBUTE_WARNING,
236                 SwordValidationInfoType.WARNING));
237      }
238      else
239      {
240         boolean validUri = true;
241         if(errorURI.startsWith("http://purl.org/net/sword/error/"))
242         {
243             // check that the list of codes
244             if( ! (errorURI.equals(ErrorCodes.ERROR_CONTENT) ||
245                    errorURI.equals(ErrorCodes.ERROR_CHECKSUM_MISMATCH) ||
246                    errorURI.equals(ErrorCodes.ERROR_BAD_REQUEST) ||
247                    errorURI.equals(ErrorCodes.TARGET_OWNER_UKNOWN) ||
248                    errorURI.equals(ErrorCodes.MEDIATION_NOT_ALLOWED)) )
249             {
250                 info.addValidationInfo(new SwordValidationInfo(xmlName,
251                         ATTRIBUTE_HREF_NAME,
252                         "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.",
253                         SwordValidationInfoType.ERROR));
254                 validUri = false; 
255             }
256         }
257
258         if( validUri )
259         {
260             SwordValidationInfo item = new SwordValidationInfo(xmlName, ATTRIBUTE_HREF_NAME);
261             item.setContentDescription(errorURI);
262             info.addAttributeValidationInfo(item);
263         }
264      }
265      return info; 
266   }
267
268   
269   /**
270    * Get the error URI
271    * 
272    * @return the error URI
273    */
274   public String getErrorURI()
275   {
276       return errorURI;
277   }
278
279   /**
280    * set the error URI
281    * 
282    * @param error the error URI
283    */
284   public void setErrorURI(String error)
285   {
286       errorURI = error;
287   }
288
289   /**
290    * Main method to perform a brief test of the class
291    * 
292    * @param args
293    */
294   /*public static void main(String[] args)
295   {
296	   SWORDErrorDocumentTest sed = new SWORDErrorDocumentTest(ErrorCodes.MEDIATION_NOT_ALLOWED);
297	   sed.setNoOp(true);
298	   sed.setTreatment("Short back and shine");
299	   sed.setId("123456789");
300	   Title t = new Title();
301	   t.setContent("My first book");
302	   sed.setTitle(t);
303	   Author a = new Author();
304	   a.setName("Lewis, Stuart");
305	   a.setEmail("stuart@example.com");
306	   sed.addAuthors(a);
307	   
308	   System.out.println(sed.marshall().toXML());
309   }
310    */
311}