001/**
002 * The contents of this file are subject to the license and copyright
003 * detailed in the LICENSE and NOTICE files at the root of the source
004 * tree.
005 *
006 */
007package org.fcrepo.migration.foxml;
008
009import java.io.InputStream;
010import java.lang.annotation.Annotation;
011import java.lang.reflect.Field;
012import java.util.ArrayList;
013import java.util.Arrays;
014import java.util.List;
015
016import javax.xml.bind.JAXBContext;
017import javax.xml.bind.JAXBElement;
018import javax.xml.bind.JAXBException;
019import javax.xml.bind.Unmarshaller;
020import javax.xml.bind.annotation.XmlElement;
021import javax.xml.transform.stream.StreamSource;
022/**
023 *
024 * @author mdurbin
025 *
026 */
027public class DC {
028
029    public static final String DC_NS = "http://purl.org/dc/elements/1.1/";
030
031    @XmlElement(name = "contributor", namespace = DC_NS)
032    public String[] contributor;
033
034    @XmlElement(name = "coverage", namespace = DC_NS)
035    public String[] coverage;
036
037    @XmlElement(name = "creator", namespace = DC_NS)
038    public String[] creator;
039
040    @XmlElement(name = "date", namespace = DC_NS)
041    public String[] date;
042
043    @XmlElement(name = "description", namespace = DC_NS)
044    public String[] description;
045
046    @XmlElement(name = "format", namespace = DC_NS)
047    public String[] format;
048
049    @XmlElement(name = "identifier", namespace = DC_NS)
050    public String[] identifier;
051
052    @XmlElement(name = "language", namespace = DC_NS)
053    public String[] language;
054
055    @XmlElement(name = "publisher", namespace = DC_NS)
056    public String[] publisher;
057
058    @XmlElement(name = "relation", namespace = DC_NS)
059    public String[] relation;
060
061    @XmlElement(name = "rights", namespace = DC_NS)
062    public String[] rights;
063
064    @XmlElement(name = "source", namespace = DC_NS)
065    public String[] source;
066
067    @XmlElement(name = "subject", namespace = DC_NS)
068    public String[] subject;
069
070    @XmlElement(name = "title", namespace = DC_NS)
071    public String[] title;
072
073    @XmlElement(name = "type", namespace = DC_NS)
074    public String[] type;
075
076    /**
077     * get represented element uris
078     * @return the list
079     */
080    public List<String> getRepresentedElementURIs() {
081        final List<String> result = new ArrayList<String>();
082        for (final Field f : DC.class.getDeclaredFields()) {
083            for (final Annotation a : f.getAnnotations()) {
084                if (a.annotationType().equals(XmlElement.class)) {
085                    final XmlElement e = (XmlElement) a;
086                    try {
087                        if (f.get(this) != null) {
088                            result.add(e.namespace() + e.name());
089                            break;
090                        }
091                    } catch (final IllegalAccessException ex) {
092                        throw new RuntimeException(ex);
093                    }
094                }
095            }
096        }
097        return result;
098    }
099
100    /**
101     * get values for uri
102     * @param uri the uri
103     * @return the value list
104     */
105    public List<String> getValuesForURI(final String uri) {
106        try {
107            final String fieldName = uri.substring(uri.lastIndexOf('/') + 1);
108            final Field field = DC.class.getField(fieldName);
109            final String[] values = (String[]) field.get(this);
110            if (values != null) {
111                return Arrays.asList(values);
112            } else {
113                return null;
114            }
115        } catch (final NoSuchFieldException e) {
116            throw new RuntimeException(uri + " not recognized as a DC element!");
117        } catch (final IllegalAccessException e) {
118            throw new RuntimeException(e);
119        }
120    }
121
122
123    /**
124     * parse DC
125     * @param is the input stream
126     * @return the DC
127     * @throws JAXBException JAXB exception
128     */
129    public static DC parseDC(final InputStream is) throws JAXBException {
130        final JAXBContext jc = JAXBContext.newInstance(DC.class);
131        final Unmarshaller unmarshaller = jc.createUnmarshaller();
132        final JAXBElement<DC> p = unmarshaller.unmarshal(new StreamSource(is), DC.class);
133        return p.getValue();
134    }
135
136
137}