lenya-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From andr...@apache.org
Subject cvs commit: cocoon-lenya/src/java/org/apache/lenya/cms/publication DublinCore.java
Date Thu, 14 Aug 2003 13:49:21 GMT
andreas     2003/08/14 06:49:21

  Modified:    src/java/org/apache/lenya/cms/publication DublinCore.java
  Log:
  added terms handling
  
  Revision  Changes    Path
  1.14      +194 -56   cocoon-lenya/src/java/org/apache/lenya/cms/publication/DublinCore.java
  
  Index: DublinCore.java
  ===================================================================
  RCS file: /home/cvs/cocoon-lenya/src/java/org/apache/lenya/cms/publication/DublinCore.java,v
  retrieving revision 1.13
  retrieving revision 1.14
  diff -u -r1.13 -r1.14
  --- DublinCore.java	14 Aug 2003 10:45:34 -0000	1.13
  +++ DublinCore.java	14 Aug 2003 13:49:21 -0000	1.14
  @@ -56,21 +56,25 @@
   package org.apache.lenya.cms.publication;
   
   import org.apache.lenya.xml.DocumentHelper;
  +import org.apache.lenya.xml.NamespaceHelper;
   
   import java.io.File;
   import java.io.IOException;
   import java.util.Date;
   import java.util.HashMap;
  +import java.util.Map;
   
   import javax.xml.parsers.ParserConfigurationException;
   import javax.xml.transform.TransformerConfigurationException;
   import javax.xml.transform.TransformerException;
   
  +import org.w3c.dom.Element;
   import org.w3c.dom.NodeList;
   import org.xml.sax.SAXException;
   
   /**
  - * A publication.
  + * Access dublin core meta data in documents.
  + * This class uses the dublin core specification from 2003-03-04.
    *
    * @author <a href="mailto:gregor@apache.org">Gregor J. Rothfuss</a>
    */
  @@ -79,32 +83,123 @@
       private File infofile;
       private Date timestamp = new Date(0);
   
  -    private HashMap map = new HashMap();
  +    private Map elements = new HashMap();
  +    private Map terms = new HashMap();
  +    private static final String META = "meta";
  +
  +    // Dublin Core Elements
  +
  +    private static final String DC_NAMESPACE = "http://purl.org/dc/elements/1.1/";
  +    private static final String DC_PREFIX = "dc";
  +
  +    private static final String ELEMENT_TITLE = "title";
  +    private static final String ELEMENT_CREATOR = "creator";
  +    private static final String ELEMENT_SUBJECT = "subject";
  +    private static final String ELEMENT_DESCRIPTION = "description";
  +    private static final String ELEMENT_PUBLISHER = "publisher";
  +    private static final String ELEMENT_CONTRIBUTOR = "contributor";
  +    private static final String ELEMENT_DATE = "date";
  +    private static final String ELEMENT_TYPE = "type";
  +    private static final String ELEMENT_FORMAT = "format";
  +    private static final String ELEMENT_IDENTIFIER = "identifier";
  +    private static final String ELEMENT_SOURCE = "source";
  +    private static final String ELEMENT_LANGUAGE = "language";
  +    private static final String ELEMENT_RELATION = "relation";
  +    private static final String ELEMENT_COVERAGE = "coverage";
  +    private static final String ELEMENT_RIGHTS = "rights";
   
  -    private static final String DC_NAMESPACE =
  -        "http://purl.org/dc/elements/1.1/";
  -    
  -    private static final String CREATOR = "creator";
  -    private static final String TITLE = "title";
  -    private static final String DESCRIPTION = "description";
  -    private static final String IDENTIFIER = "identifier";
  -    private static final String SUBJECT = "subject";
  -    private static final String PUBLISHER = "publisher";
  -    private static final String DATEISSUED = "dateissued";
  -    private static final String DATEPUBLISHED = "datepublished";
  -    private static final String RIGHTS = "rights";
  +    private static final String[] ELEMENTS =
  +        {
  +            ELEMENT_TITLE,
  +            ELEMENT_CREATOR,
  +            ELEMENT_SUBJECT,
  +            ELEMENT_DESCRIPTION,
  +            ELEMENT_PUBLISHER,
  +            ELEMENT_CONTRIBUTOR,
  +            ELEMENT_DATE,
  +            ELEMENT_TYPE,
  +            ELEMENT_FORMAT,
  +            ELEMENT_IDENTIFIER,
  +            ELEMENT_SOURCE,
  +            ELEMENT_LANGUAGE,
  +            ELEMENT_RELATION,
  +            ELEMENT_COVERAGE,
  +            ELEMENT_RIGHTS };
  +
  +    // Dublin Core Terms
  +
  +    private static final String DCTERMS_NAMESPACE = "http://purl.org/dc/terms/";
  +    private static final String DCTERMS_PREFIX = "dcterms";
  +
  +    private static final String TERM_AUDIENCE = "audience";
  +    private static final String TERM_ALTERNATIVE = "alternative";
  +    private static final String TERM_TABLEOFCONTENTS = "tableOfContents";
  +    private static final String TERM_ABSTRACT = "abstract";
  +    private static final String TERM_CREATED = "created";
  +    private static final String TERM_VALID = "valid";
  +    private static final String TERM_AVAILABLE = "available";
  +    private static final String TERM_ISSUED = "issued";
  +    private static final String TERM_MODIFIED = "modified";
  +    private static final String TERM_EXTENT = "extent";
  +    private static final String TERM_MEDIUM = "medium";
  +    private static final String TERM_ISVERSIONOF = "isVersionOf";
  +    private static final String TERM_HASVERSION = "hasVersion";
  +    private static final String TERM_ISREPLACEDBY = "isReplacedBy";
  +    private static final String TERM_REPLACES = "replaces";
  +    private static final String TERM_ISREQUIREDBY = "isRequiredBy";
  +    private static final String TERM_REQUIRES = "requires";
  +    private static final String TERM_ISPARTOF = "isPartOf";
  +    private static final String TERM_HASPART = "hasPart";
  +    private static final String TERM_ISREFERENCEDBY = "isReferencedBy";
  +    private static final String TERM_REFERENCES = "references";
  +    private static final String TERM_ISFORMATOF = "isFormatOf";
  +    private static final String TERM_HASFORMAT = "hasFormat";
  +    private static final String TERM_CONFORMSTO = "conformsTo";
  +    private static final String TERM_SPATIAL = "spatial";
  +    private static final String TERM_TEMPORAL = "temporal";
  +    private static final String TERM_MEDIATOR = "mediator";
  +    private static final String TERM_DATEACCEPTED = "dateAccepted";
  +    private static final String TERM_DATECOPYRIGHTED = "dateCopyrighted";
  +    private static final String TERM_DATESUBMITTED = "dateSubmitted";
  +    private static final String TERM_EDUCATIONLEVEL = "educationLevel";
  +    private static final String TERM_ACCESSRIGHTS = "accessRights";
  +    private static final String TERM_BIBLIOGRAPHICCITATION = "bibliographicCitation";
   
  -    private static final String[] fields =
  +    private static final String[] TERMS =
           {
  -            CREATOR,
  -            TITLE,
  -            DESCRIPTION,
  -            IDENTIFIER,
  -            SUBJECT,
  -            PUBLISHER,
  -            DATEISSUED,
  -            DATEPUBLISHED,
  -            RIGHTS };
  +            TERM_AUDIENCE,
  +            TERM_ALTERNATIVE,
  +            TERM_TABLEOFCONTENTS,
  +            TERM_ABSTRACT,
  +            TERM_CREATED,
  +            TERM_EXTENT,
  +            TERM_AVAILABLE,
  +            TERM_ISSUED,
  +            TERM_MODIFIED,
  +            TERM_EXTENT,
  +            TERM_MEDIUM,
  +            TERM_ISVERSIONOF,
  +            TERM_HASVERSION,
  +            TERM_ISREPLACEDBY,
  +            TERM_REPLACES,
  +            TERM_ISREQUIREDBY,
  +            TERM_REQUIRES,
  +            TERM_ISPARTOF,
  +            TERM_HASPART,
  +            TERM_ISREFERENCEDBY,
  +            TERM_REFERENCES,
  +            TERM_ISFORMATOF,
  +            TERM_HASFORMAT,
  +            TERM_CONFORMSTO,
  +            TERM_SPATIAL,
  +            TERM_TEMPORAL,
  +            TERM_MEDIATOR,
  +            TERM_DATEACCEPTED,
  +            TERM_DATECOPYRIGHTED,
  +            TERM_DATESUBMITTED,
  +            TERM_EDUCATIONLEVEL,
  +            TERM_ACCESSRIGHTS,
  +            TERM_BIBLIOGRAPHICCITATION };
   
       /** 
        * Creates a new instance of Dublin Core
  @@ -142,14 +237,26 @@
               } catch (IOException e) {
                   throw new DocumentException(e);
               }
  -            NodeList nodelist = null;
  -            String value = null;
               
  -            for (int i = 0; i < fields.length; i++) {
  -                    nodelist = doc.getElementsByTagNameNS(DC_NAMESPACE, fields[i]);
  -                    value = nodelist.item(0).getFirstChild().getNodeValue();
  -                    map.put(fields[i], value);
  +            Element metaElement = getMetaElement(doc);
  +
  +            String[] namespaces = { DC_NAMESPACE, DCTERMS_NAMESPACE };
  +            String[] prefixes = { DC_PREFIX, DCTERMS_PREFIX };
  +            String[][] arrays = { ELEMENTS, TERMS };
  +            Map[] maps = { elements, terms };
  +
  +            for (int type = 0; type < 2; type++) {
  +                NamespaceHelper helper = new NamespaceHelper(namespaces[type], prefixes[type],
doc);
  +                String[] elementNames = arrays[type];
  +                for (int i = 0; i < elementNames.length; i++) {
  +                    Element child = helper.getFirstChild(metaElement, elementNames[i]);
  +                    if (child != null) {
  +                        String value = DocumentHelper.getSimpleElementText(child);
  +                        maps[type].put(elementNames[i], value);
  +                    }
  +                }
               }
  +
           }
       }
   
  @@ -170,13 +277,30 @@
               throw new DocumentException(e);
           }
           NodeList nodelist = null;
  -        String value = null;
   
  -        for (int i = 0; i < fields.length; i++) {
  -            nodelist = doc.getElementsByTagNameNS(DC_NAMESPACE, fields[i]);
  -            nodelist.item(0).getFirstChild().setNodeValue(
  -                (String)map.get(fields[i]));
  +        Element metaElement = getMetaElement(doc);
  +
  +        String[] namespaces = { DC_NAMESPACE, DCTERMS_NAMESPACE };
  +        String[] prefixes = { DC_PREFIX, DCTERMS_PREFIX };
  +        String[][] arrays = { ELEMENTS, TERMS };
  +        Map[] maps = { elements, terms };
  +
  +        for (int type = 0; type < 2; type++) {
  +            NamespaceHelper helper = new NamespaceHelper(namespaces[type], prefixes[type],
doc);
  +            String[] elementNames = arrays[type];
  +            for (int i = 0; i < elementNames.length; i++) {
  +                Element element = helper.getFirstChild(metaElement, elementNames[i]);
  +                if (element != null) {
  +                    metaElement.removeChild(element);
  +                }
  +                String value = (String) maps[type].get(elementNames[i]);
  +                if (value != null) {
  +                    element = helper.createElement(elementNames[i], value);
  +                    metaElement.appendChild(element);
  +                }
  +            }
           }
  +
           try {
               DocumentHelper.writeDocument(doc, infofile);
           } catch (TransformerConfigurationException e) {
  @@ -188,7 +312,20 @@
           }
   
       }
  -    
  +
  +    /**
  +     * Returns the Lenya meta data element.
  +     * @param doc The XML document.
  +     * @return A DOM element.
  +     */
  +    protected Element getMetaElement(org.w3c.dom.Document doc) {
  +        NamespaceHelper namespaceHelper =
  +            new NamespaceHelper(PageEnvelope.NAMESPACE, PageEnvelope.DEFAULT_PREFIX, doc);
  +        Element documentElement = doc.getDocumentElement();
  +        Element metaElement = namespaceHelper.getFirstChild(documentElement, META);
  +        return metaElement;
  +    }
  +
       /**
        * Get the creator
        * 
  @@ -198,7 +335,7 @@
        */
       public String getCreator() throws DocumentException {
           checkValidity();
  -        return (String)map.get(CREATOR);
  +        return (String) elements.get(ELEMENT_CREATOR);
       }
   
       /**
  @@ -207,7 +344,7 @@
        * @param creator the Creator
        */
       public void setCreator(String creator) {
  -        map.put(CREATOR, creator);
  +        elements.put(ELEMENT_CREATOR, creator);
       }
   
       /**
  @@ -219,7 +356,7 @@
        */
       public String getTitle() throws DocumentException {
           checkValidity();
  -        return (String)map.get(TITLE);
  +        return (String) elements.get(ELEMENT_TITLE);
       }
   
       /**
  @@ -228,7 +365,7 @@
        * @param title the title
        */
       public void setTitle(String title) {
  -        map.put(TITLE, title);
  +        elements.put(ELEMENT_TITLE, title);
       }
   
       /**
  @@ -240,7 +377,7 @@
        */
       public String getDescription() throws DocumentException {
           checkValidity();
  -        return (String)map.get(DESCRIPTION);
  +        return (String) elements.get(ELEMENT_DESCRIPTION);
       }
   
       /**
  @@ -249,7 +386,7 @@
        * @param description the description
        */
       public void setDescription(String description) {
  -        map.put(DESCRIPTION, description);
  +        elements.put(ELEMENT_DESCRIPTION, description);
       }
   
       /**
  @@ -261,7 +398,7 @@
        */
       public String getIdentifier() throws DocumentException {
           checkValidity();
  -        return (String)map.get(IDENTIFIER);
  +        return (String) elements.get(ELEMENT_IDENTIFIER);
       }
   
       /**
  @@ -270,7 +407,7 @@
        * @param identifier the identifier
        */
       public void setIdentifier(String identifier) {
  -        map.put(IDENTIFIER, identifier);
  +        elements.put(ELEMENT_IDENTIFIER, identifier);
       }
   
       /**
  @@ -282,7 +419,7 @@
        */
       public String getSubject() throws DocumentException {
           checkValidity();
  -        return (String)map.get(SUBJECT);
  +        return (String) elements.get(ELEMENT_SUBJECT);
       }
   
       /**
  @@ -291,7 +428,7 @@
        * @param subject the subject
        */
       public void setSubject(String subject) {
  -        map.put(SUBJECT, subject);
  +        elements.put(ELEMENT_SUBJECT, subject);
       }
   
       /**
  @@ -303,7 +440,7 @@
        */
       public String getPublisher() throws DocumentException {
           checkValidity();
  -        return (String)map.get(PUBLISHER);
  +        return (String) elements.get(ELEMENT_PUBLISHER);
       }
   
       /**
  @@ -312,7 +449,7 @@
        * @param publisher the publisher
        */
       public void setPublisher(String publisher) {
  -        map.put(PUBLISHER, publisher);
  +        elements.put(ELEMENT_PUBLISHER, publisher);
       }
   
       /**
  @@ -324,7 +461,7 @@
        */
       public String getDateIssued() throws DocumentException {
           checkValidity();
  -        return (String)map.get(DATEISSUED);
  +        return (String) terms.get(TERM_ISSUED);
       }
   
       /**
  @@ -333,7 +470,7 @@
        * @param dateIssued the date of issue
        */
       public void setDateIssued(String dateIssued) {
  -        map.put(DATEISSUED, dateIssued);
  +        terms.put(TERM_ISSUED, dateIssued);
       }
   
       /**
  @@ -342,19 +479,20 @@
        * @return the date of publication
        * 
        * @throws DocumentException if an error occurs
  +     * @deprecated Use {@link #getDateIssued()} instead!
        */
       public String getDatePublished() throws DocumentException {
  -        checkValidity();
  -        return (String)map.get(DATEPUBLISHED);
  +        return getDateIssued();
       }
   
       /**
        * Set the publication date
        * 
        * @param datePublished the date of publication
  +     * @deprecated Use {@link #setDateIssued(String)} instead!
        */
       public void setDatePublished(String datePublished) {
  -        map.put(DATEPUBLISHED, datePublished);
  +        setDateIssued(datePublished);
       }
   
       /**
  @@ -366,7 +504,7 @@
        */
       public String getRights() throws DocumentException {
           checkValidity();
  -        return (String)map.get(RIGHTS);
  +        return (String) elements.get(ELEMENT_RIGHTS);
       }
   
       /**
  @@ -375,7 +513,7 @@
        * @param rights the rights
        */
       public void setRights(String rights) {
  -        map.put(RIGHTS, rights);
  +        elements.put(ELEMENT_RIGHTS, rights);
       }
   
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: lenya-cvs-unsubscribe@cocoon.apache.org
For additional commands, e-mail: lenya-cvs-help@cocoon.apache.org


Mime
View raw message