001/* 002 * Copyright 2015 DuraSpace, Inc. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.fcrepo.migration.foxml; 017 018import java.io.InputStream; 019import java.lang.annotation.Annotation; 020import java.lang.reflect.Field; 021import java.util.ArrayList; 022import java.util.Arrays; 023import java.util.List; 024 025import javax.xml.bind.JAXBContext; 026import javax.xml.bind.JAXBElement; 027import javax.xml.bind.JAXBException; 028import javax.xml.bind.Unmarshaller; 029import javax.xml.bind.annotation.XmlElement; 030import javax.xml.transform.stream.StreamSource; 031/** 032 * 033 * @author mdurbin 034 * 035 */ 036public class DC { 037 038 public static final String DC_NS = "http://purl.org/dc/elements/1.1/"; 039 040 @XmlElement(name = "contributor", namespace = DC_NS) 041 public String[] contributor; 042 043 @XmlElement(name = "coverage", namespace = DC_NS) 044 public String[] coverage; 045 046 @XmlElement(name = "creator", namespace = DC_NS) 047 public String[] creator; 048 049 @XmlElement(name = "date", namespace = DC_NS) 050 public String[] date; 051 052 @XmlElement(name = "description", namespace = DC_NS) 053 public String[] description; 054 055 @XmlElement(name = "format", namespace = DC_NS) 056 public String[] format; 057 058 @XmlElement(name = "identifier", namespace = DC_NS) 059 public String[] identifier; 060 061 @XmlElement(name = "language", namespace = DC_NS) 062 public String[] language; 063 064 @XmlElement(name = "publisher", namespace = DC_NS) 065 public String[] publisher; 066 067 @XmlElement(name = "relation", namespace = DC_NS) 068 public String[] relation; 069 070 @XmlElement(name = "rights", namespace = DC_NS) 071 public String[] rights; 072 073 @XmlElement(name = "source", namespace = DC_NS) 074 public String[] source; 075 076 @XmlElement(name = "subject", namespace = DC_NS) 077 public String[] subject; 078 079 @XmlElement(name = "title", namespace = DC_NS) 080 public String[] title; 081 082 @XmlElement(name = "type", namespace = DC_NS) 083 public String[] type; 084 085 /** 086 * get represented element uris 087 * @return the list 088 */ 089 public List<String> getRepresentedElementURIs() { 090 final List<String> result = new ArrayList<String>(); 091 for (final Field f : DC.class.getDeclaredFields()) { 092 for (final Annotation a : f.getAnnotations()) { 093 if (a.annotationType().equals(XmlElement.class)) { 094 final XmlElement e = (XmlElement) a; 095 try { 096 if (f.get(this) != null) { 097 result.add(e.namespace() + e.name()); 098 break; 099 } 100 } catch (final IllegalAccessException ex) { 101 throw new RuntimeException(ex); 102 } 103 } 104 } 105 } 106 return result; 107 } 108 109 /** 110 * get values for uri 111 * @param uri the uri 112 * @return the value list 113 */ 114 public List<String> getValuesForURI(final String uri) { 115 try { 116 final String fieldName = uri.substring(uri.lastIndexOf('/') + 1); 117 final Field field = DC.class.getField(fieldName); 118 final String[] values = (String[]) field.get(this); 119 if (values != null) { 120 return Arrays.asList(values); 121 } else { 122 return null; 123 } 124 } catch (final NoSuchFieldException e) { 125 throw new RuntimeException(uri + " not recognized as a DC element!"); 126 } catch (final IllegalAccessException e) { 127 throw new RuntimeException(e); 128 } 129 } 130 131 132 /** 133 * parse DC 134 * @param is the input stream 135 * @return the DC 136 * @throws JAXBException JAXB exception 137 */ 138 public static DC parseDC(final InputStream is) throws JAXBException { 139 final JAXBContext jc = JAXBContext.newInstance(DC.class); 140 final Unmarshaller unmarshaller = jc.createUnmarshaller(); 141 final JAXBElement<DC> p = unmarshaller.unmarshal(new StreamSource(is), DC.class); 142 return p.getValue(); 143 } 144 145 146}