Changeset 1163 for OAIHarvester


Ignore:
Timestamp:
03/11/11 09:52:27 (13 years ago)
Author:
oschonef
Message:
  • convert ealiestDatestamp to Date in Identify
  • add pluggable parser architecture for additional descriptions in Identify
  • add description parsers for OAI-identifier and OAI Dublin Core descriptions
Location:
OAIHarvester/trunk/OAIHarvester/src
Files:
7 added
7 edited

Legend:

Unmodified
Added
Removed
  • OAIHarvester/trunk/OAIHarvester/src/main/java/eu/clarin/cmdi/oai/harvester/Harvester.java

    r1148 r1163  
    33import java.net.URI;
    44
     5import eu.clarin.cmdi.oai.harvester.ext.DescriptionParser;
     6
    57public interface Harvester {
     8
     9    public void registerDescriptionParser(DescriptionParser parser)
     10            throws HarvesterException;
    611
    712    public HarvestJob createJob(URI repositoryURI, HarvestHandler handler)
  • OAIHarvester/trunk/OAIHarvester/src/main/java/eu/clarin/cmdi/oai/harvester/Repository.java

    r1128 r1163  
    11package eu.clarin.cmdi.oai.harvester;
     2import java.util.Date;
    23import java.util.List;
    34
     
    2526    public List<String> getAdminEmail();
    2627
    27     public String getEarliestTimestamp();
     28    public Date getEarliestTimestamp();
    2829
    2930    public DeletedNotion getDeletedNotion();
     
    3334    public int getCompressionMask();
    3435
     36    public List<Description> getDescriptions();
     37
    3538} // interface Repository
  • OAIHarvester/trunk/OAIHarvester/src/main/java/eu/clarin/cmdi/oai/harvester/impl/AbstractHarvester.java

    r1149 r1163  
    44import java.net.URI;
    55import java.net.URISyntaxException;
     6import java.util.ArrayList;
     7import java.util.List;
    68
    79import javax.xml.stream.XMLInputFactory;
     
    1618import eu.clarin.cmdi.oai.harvester.Harvester;
    1719import eu.clarin.cmdi.oai.harvester.HarvesterException;
     20import eu.clarin.cmdi.oai.harvester.ext.DescriptionParser;
    1821
    1922abstract class AbstractHarvester implements Harvester {
     
    2225    private final XMLInputFactory factory;
    2326//    private final XMLValidationSchema schema;
     27    private List<DescriptionParser> descriptionParsers =
     28        new ArrayList<DescriptionParser>();
    2429
    2530    protected AbstractHarvester() {
     
    4146    }
    4247
     48    public final void registerDescriptionParser(DescriptionParser parser)
     49            throws HarvesterException {
     50        if (parser == null) {
     51            throw new NullPointerException("parser == null");
     52        }
     53        if (findDescriptionParser(parser.getNamespaceURI(),
     54                parser.getLocalName()) != null) {
     55            throw new HarvesterException("description parser for '{" +
     56                    parser.getNamespaceURI() + "}" + parser.getLocalName() +
     57                    "' was already registered");
     58        }
     59        if (descriptionParsers == null) {
     60            descriptionParsers = new ArrayList<DescriptionParser>();
     61        }
     62        descriptionParsers.add(parser);
     63    }
     64   
    4365    public abstract HarvestJob createJob(URI repositoryURI,
    4466            HarvestHandler handler) throws HarvesterException;
     
    94116    }
    95117
     118    DescriptionParser findDescriptionParser(String namespaceURI,
     119            String localName) {
     120        if (descriptionParsers != null) {
     121            for (DescriptionParser parser : descriptionParsers) {
     122                if (namespaceURI.equals(parser.getNamespaceURI()) &&
     123                        localName.equals(parser.getLocalName())) {
     124                    return parser;
     125                }
     126            }
     127        }
     128        return null;
     129    }
     130
    96131    abstract void doRunJob(HarvestJobImpl job) throws HarvesterException;
    97132
  • OAIHarvester/trunk/OAIHarvester/src/main/java/eu/clarin/cmdi/oai/harvester/impl/HarvestJobImpl.java

    r1145 r1163  
    1313import javax.xml.stream.XMLStreamReader;
    1414
     15import eu.clarin.cmdi.oai.harvester.Description;
    1516import eu.clarin.cmdi.oai.harvester.HarvestHandler;
    1617import eu.clarin.cmdi.oai.harvester.HarvestJob;
     
    109110    private String protocolVersion;
    110111    private List<String> adminEmail = Collections.emptyList();
    111     private String earliestTimestamp;
     112    private Date earliestTimestamp;
    112113    private DeletedNotion deletedNotion;
    113114    private Granularity granularity;
    114115    private int compressionMask;
     116    private List<Description> descriptions;
    115117    private List<MetadataFormat> metadataFormats;
    116118    private long totalRecordCount = 0;
     
    277279            return Collections.unmodifiableList(statistics);
    278280        } else {
    279             return null;
     281            return Collections.emptyList();
    280282        }
    281283    }
     
    355357    }
    356358
    357     void setEarliestTimestamp(String earliestTimestamp) {
     359    void setEarliestTimestamp(Date earliestTimestamp) {
    358360        this.earliestTimestamp = earliestTimestamp;
    359361    }
     
    377379    int getCompressionMask() {
    378380        return compressionMask;
     381    }
     382
     383    void setDescriptions(List<Description> descriptions) {
     384        this.descriptions = descriptions;
    379385    }
    380386
     
    539545               
    540546                @Override
    541                 public String getEarliestTimestamp() {
     547                public Date getEarliestTimestamp() {
    542548                    return earliestTimestamp;
    543549                }
     
    560566                @Override
    561567                public List<String> getAdminEmail() {
    562                     return adminEmail;
     568                    return (adminEmail != null) ?
     569                            Collections.unmodifiableList(adminEmail) : null;
     570                }
     571
     572                @Override
     573                public List<Description> getDescriptions() {
     574                    return (descriptions != null) ?
     575                            Collections.unmodifiableList(descriptions) : null;
    563576                }
    564577            };
     
    570583        this.metadataFormats = metadataFormats;
    571584        if (handler != null) {
    572             handler.onListMetadataFormats(this.metadataFormats);
     585            handler.onListMetadataFormats(
     586                    Collections.unmodifiableList(this.metadataFormats));
    573587        }
    574588    }
  • OAIHarvester/trunk/OAIHarvester/src/main/java/eu/clarin/cmdi/oai/harvester/impl/HarvestWorker.java

    r1150 r1163  
    2525import org.joda.time.format.DateTimeFormatter;
    2626
     27import eu.clarin.cmdi.oai.harvester.Description;
    2728import eu.clarin.cmdi.oai.harvester.HarvestJob.State;
    2829import eu.clarin.cmdi.oai.harvester.HarvesterException;
     
    3132import eu.clarin.cmdi.oai.harvester.Repository.DeletedNotion;
    3233import eu.clarin.cmdi.oai.harvester.Repository.Granularity;
     34import eu.clarin.cmdi.oai.harvester.ext.DescriptionParser;
    3335import eu.clarin.cmdi.oai.harvester.impl.HarvestJobImpl.Task;
    3436
     
    187189            job.setAdminEmail(adminEmail);
    188190
    189             s = response.readContent(OAI_NS, "earliestDatestamp", true);
    190             job.setEarliestTimestamp(s);
     191            /*
     192             *  defer setting of earliestDatestamp in job until we know
     193             *  the datestamp granularity of the repository
     194             */
     195            final String earliestDatestamp =
     196                response.readContent(OAI_NS, "earliestDatestamp", true);
    191197
    192198            s = response.readContent(OAI_NS, "deletedRecord", true);
     
    203209
    204210            s = response.readContent(OAI_NS, "granularity", true);
     211            Granularity granularity = null;
    205212            if (GRANULARITY_DAYS.equals(s)) {
    206                 job.setGranularity(Granularity.DAYS);
     213                granularity = Granularity.DAYS;
    207214            } else if (GRANULARITY_SECONDS.equals(s)) {
    208                 job.setGranularity(Granularity.SECONDS);
     215                granularity = Granularity.SECONDS;
    209216            } else {
    210217                throw new HarvesterException(
    211218                        "invalid value for 'granularity': " + s);
    212219            }
     220            job.setGranularity(granularity);
     221            job.setEarliestTimestamp(parseDate(granularity, earliestDatestamp));
    213222
    214223            int mask = 0;
     
    225234            job.setCompressionMask(mask);
    226235
     236            List<Description> descriptions = null;
    227237            while (response.readStart(OAI_NS, "description", false)) {
    228238                response.consumeWhitespace();
    229239                final String namespaceURI = response.readNamespaceURI();
    230                 System.err.println("DESCRIPTION: URI = " + namespaceURI);
     240                final String localName = response.peekElementLocalName();
     241                DescriptionParser parser = harvester.findDescriptionParser(
     242                        namespaceURI, localName);
     243                if (parser != null) {
     244                    Description desc =
     245                        parser.parseDescription(response.getXMLStreamReader());
     246                    if (desc != null) {
     247                        if (descriptions == null) {
     248                            descriptions = new ArrayList<Description>();
     249                        }
     250                        descriptions.add(desc);
     251                    }
     252                } else {
     253                    System.err.println("skipping DESC " + namespaceURI + ", " +
     254                            localName);
     255                }
    231256                response.readEnd(OAI_NS, "description", true);
     257                job.setDescriptions(descriptions);
    232258            }
    233259            response.readEnd(OAI_NS, "Identify");
  • OAIHarvester/trunk/OAIHarvester/src/main/java/eu/clarin/cmdi/oai/harvester/impl/Response.java

    r1137 r1163  
    286286        }
    287287        return reader.getNamespaceURI();
     288    }
     289
     290    public String peekElementLocalName() throws XMLStreamException {
     291        if (!reader.isStartElement()) {
     292            throw new XMLStreamException("not at a start elment event",
     293                    reader.getLocation());
     294        }
     295        return reader.getLocalName();
    288296    }
    289297
  • OAIHarvester/trunk/OAIHarvester/src/test/java/eu/clarin/cmdi/oai/harvester/HarvesterTest.java

    r1147 r1163  
    8989            }
    9090            System.err.println("        compression: " + sb.toString());
     91            final List<Description> descs = repository.getDescriptions();
     92            if (descs != null) {
     93                for (Description desc : descs) {
     94                    System.err.println("        description: " + desc);
     95                }
     96            }
    9197        }
    9298       
     
    189195
    190196        Harvester harvester = SimpleHarvester.newInstance();
    191         harvester.setMaxNetworkRetryCount(16);
    192197        try {
     198            // configure harvester
     199            harvester.setMaxNetworkRetryCount(16);
     200            harvester.registerDescriptionParser(
     201                    new OAIIdentifierDescriptionParser());
     202            harvester.registerDescriptionParser(
     203                    new OAIDublinCoreDescriptionParser());
     204
    193205            HarvestJob job = harvester.createJob(repos, handler);
    194206//            Calendar cal = Calendar.getInstance(TimeZone.getDefault());
Note: See TracChangeset for help on using the changeset viewer.